Skip to content

Commit 9ba87e5

Browse files
dhananjay-ngGouthamML
authored andcommitted
Adding validation for Lustre post mount parameters
1 parent ce0383d commit 9ba87e5

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

pkg/csi-util/lustre_lnet_helper.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,48 @@ func (ls *LnetService) ApplyLustreParameters(logger *zap.SugaredLogger, lustrePa
364364
logger.Infof("Successfully applied lustre parameters.")
365365
return nil
366366
}
367+
368+
func isValidLustreParam(param string) bool {
369+
// Check for no spaces
370+
if strings.Contains(param, " ") {
371+
return false
372+
}
373+
// List of forbidden characters
374+
forbiddenChars := []string{";", "&", "|", "<", ">", "(", ")", "`", "'", "\""}
375+
for _, char := range forbiddenChars {
376+
if strings.Contains(param, char) {
377+
return false
378+
}
379+
}
380+
return true
381+
}
382+
func ValidateLustreParameters(logger *zap.SugaredLogger, lustreParamsJson string) error {
383+
if lustreParamsJson == "" {
384+
logger.Debug("No lustre parameters specified.")
385+
return nil
386+
}
387+
var lustreParams []Parameter
388+
389+
err := json.Unmarshal([]byte(lustreParamsJson), &lustreParams)
390+
391+
if err != nil {
392+
return err
393+
}
394+
395+
var invalidParams []string
396+
397+
for _, param := range lustreParams {
398+
for key, value := range param {
399+
logger.Infof("Validating lustre param %s=%s", key, fmt.Sprintf("%v", value))
400+
if !isValidLustreParam(key) || !isValidLustreParam(fmt.Sprintf("%v", value)) {
401+
invalidParams = append(invalidParams, fmt.Sprintf("%v=%v",key, value))
402+
}
403+
}
404+
}
405+
if len(invalidParams) > 0 {
406+
return fmt.Errorf("%v", strings.Join(invalidParams, ","))
407+
}
408+
logger.Infof("Successfully validated lustre parameters.")
409+
return nil
410+
}
411+

pkg/csi-util/lustre_lnet_helper_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package csi_util
22

33
import (
4+
"fmt"
45
"strings"
56
"testing"
67

@@ -415,3 +416,49 @@ func TestLnetService_ApplyLustreParameters(t *testing.T) {
415416
})
416417
}
417418
}
419+
420+
421+
func TestLnetService_ValidateLustreParameters(t *testing.T) {
422+
logger := zap.NewExample().Sugar()
423+
tests := []struct {
424+
name string
425+
lustreParamsJSON string
426+
expectedErr error
427+
}{
428+
{
429+
name: "Valid Lustre Parameters Json Provided",
430+
lustreParamsJSON: `[{"failover.recovery_mode":"quorum","lnet.debug":"0x200"}]`,
431+
expectedErr: nil,
432+
},
433+
{
434+
name: "No Lustre Parameters Provided",
435+
lustreParamsJSON: "",
436+
expectedErr: nil,
437+
},
438+
{
439+
name: "Invalid Lustre Parameters Json Provided",
440+
lustreParamsJSON: `invalid-json`,
441+
expectedErr: fmt.Errorf("%s","invalid character 'i' looking for beginning of value"),
442+
},
443+
{
444+
name: "Valid and Invalid Lustre Parameters Provided",
445+
lustreParamsJSON: `[{"failover.recovery_mode":"quorum","lnet.debug":"0x200","lnet.debug && ls -l | wc -l":"0x200 I am Invalid"}]`,
446+
expectedErr: fmt.Errorf("%v","lnet.debug && ls -l | wc -l=0x200 I am Invalid"),
447+
},
448+
{
449+
name: "Invalid Lustre Parameters Provided",
450+
lustreParamsJSON: `[{"failover.recovery_mode;cat /var/log/cloud-init.log":"quorum; echo Hello"}]`,
451+
expectedErr: fmt.Errorf("%v","failover.recovery_mode;cat /var/log/cloud-init.log=quorum; echo Hello"),
452+
},
453+
}
454+
455+
for _, tc := range tests {
456+
t.Run(tc.name, func(t *testing.T) {
457+
err := ValidateLustreParameters(logger, tc.lustreParamsJSON)
458+
if err != nil && !strings.EqualFold(tc.expectedErr.Error(), err.Error()) {
459+
t.Errorf("ValidateLustreParameters() got = %v, want %v", err, tc.expectedErr)
460+
}
461+
462+
})
463+
}
464+
}

pkg/csi/driver/lustre_node.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ func (d LustreNodeDriver) NodeStageVolume(ctx context.Context, req *csi.NodeStag
4242
return nil, status.Error(codes.InvalidArgument, "Invalid Volume Handle provided.")
4343
}
4444

45+
46+
d.loadCSIConfig()
47+
48+
if lustrePostMountParameters, exists := req.GetVolumeContext()["lustrePostMountParameters"]; exists && !isSkipLustreParams(d.csiConfig) {
49+
if err := csi_util.ValidateLustreParameters(d.logger, lustrePostMountParameters); err != nil {
50+
return nil, status.Errorf(codes.InvalidArgument, "Invalid lustre parameters provided : %v", err.Error())
51+
}
52+
}
53+
4554
logger := d.logger.With("volumeID", req.VolumeId)
4655

4756
logger.Debugf("volume context: %v", req.VolumeContext)

0 commit comments

Comments
 (0)