Skip to content

Commit ca58440

Browse files
authored
Merge pull request #409 from jacobwolfaws/master
Handle empty and misconfigured extraTags in controller
2 parents 73ba6f9 + 180a995 commit ca58440

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

pkg/driver/controller.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,12 @@ func (d *controllerService) CreateVolume(ctx context.Context, req *csi.CreateVol
249249
tagArray = strings.Split(optionsTags, ",")
250250
}
251251

252-
if val, ok := volumeParams[volumeParamsExtraTags]; ok {
252+
if val, ok := volumeParams[volumeParamsExtraTags]; ok && len(val) > 0 {
253253
extraTags := strings.Split(val, ",")
254+
err := validateExtraTags(extraTags)
255+
if err != nil {
256+
return nil, status.Error(codes.InvalidArgument, err.Error())
257+
}
254258
tagArray = append(tagArray, extraTags...)
255259
}
256260
fsOptions.ExtraTags = tagArray
@@ -474,3 +478,13 @@ func newCreateVolumeResponse(fs *cloud.FileSystem) *csi.CreateVolumeResponse {
474478
},
475479
}
476480
}
481+
482+
func validateExtraTags(tags []string) error {
483+
for _, tag := range tags {
484+
tagSplit := strings.Split(tag, "=")
485+
if len(tagSplit) != 2 {
486+
return fmt.Errorf("invalid extraTag %s was provided", tag)
487+
}
488+
}
489+
return nil
490+
}

pkg/driver/controller_test.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ func TestCreateVolume(t *testing.T) {
5656
Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
5757
},
5858
}
59-
extraTags = "key1=value1,key2=value2"
59+
extraTags = "key1=value1,key2=value2"
60+
emptyExtraTags = ""
61+
invalidExtraTags = "key1=value1,value2"
6062
)
6163
testCases := []struct {
6264
name string
@@ -374,6 +376,111 @@ func TestCreateVolume(t *testing.T) {
374376
mockCtl.Finish()
375377
},
376378
},
379+
{
380+
name: "success: empty extraTags",
381+
testFunc: func(t *testing.T) {
382+
mockCtl := gomock.NewController(t)
383+
mockCloud := mocks.NewMockCloud(mockCtl)
384+
385+
driver := controllerService{
386+
cloud: mockCloud,
387+
inFlight: internal.NewInFlight(),
388+
driverOptions: &DriverOptions{},
389+
}
390+
391+
req := &csi.CreateVolumeRequest{
392+
Name: volumeName,
393+
VolumeCapabilities: []*csi.VolumeCapability{
394+
stdVolCap,
395+
},
396+
Parameters: map[string]string{
397+
volumeParamsSubnetId: subnetId,
398+
volumeParamsSecurityGroupIds: securityGroupIds,
399+
volumeParamsExtraTags: emptyExtraTags,
400+
},
401+
}
402+
403+
ctx := context.Background()
404+
fs := &cloud.FileSystem{
405+
FileSystemId: fileSystemId,
406+
CapacityGiB: volumeSizeGiB,
407+
DnsName: dnsName,
408+
MountName: mountName,
409+
}
410+
mockCloud.EXPECT().CreateFileSystem(gomock.Eq(ctx), gomock.Eq(volumeName), gomock.Any()).Return(fs, nil)
411+
mockCloud.EXPECT().WaitForFileSystemAvailable(gomock.Eq(ctx), gomock.Eq(fileSystemId)).Return(nil)
412+
413+
resp, err := driver.CreateVolume(ctx, req)
414+
if err != nil {
415+
t.Fatalf("CreateVolume is failed: %v", err)
416+
}
417+
418+
if resp.Volume == nil {
419+
t.Fatal("resp.Volume is nil")
420+
}
421+
422+
if resp.Volume.VolumeId != fileSystemId {
423+
t.Fatalf("VolumeId mismatches. actual: %v expected: %v", resp.Volume.VolumeId, fileSystemId)
424+
}
425+
426+
if resp.Volume.CapacityBytes == 0 {
427+
t.Fatalf("resp.Volume.CapacityGiB is zero")
428+
}
429+
430+
dnsname, exists := resp.Volume.VolumeContext[volumeContextDnsName]
431+
if !exists {
432+
t.Fatal("dnsname is missing")
433+
}
434+
435+
if dnsname != dnsName {
436+
t.Fatalf("dnsname mismatches. actual: %v expected: %v", dnsname, dnsName)
437+
}
438+
439+
mountname, exists := resp.Volume.VolumeContext[volumeContextMountName]
440+
if !exists {
441+
t.Fatal("mountname is missing")
442+
}
443+
444+
if mountname != mountName {
445+
t.Fatalf("mountname mismatches. actual: %v expected: %v", mountname, mountName)
446+
}
447+
448+
mockCtl.Finish()
449+
},
450+
},
451+
{
452+
name: "fail: invalid extraTags",
453+
testFunc: func(t *testing.T) {
454+
mockCtl := gomock.NewController(t)
455+
mockCloud := mocks.NewMockCloud(mockCtl)
456+
457+
driver := controllerService{
458+
cloud: mockCloud,
459+
inFlight: internal.NewInFlight(),
460+
driverOptions: &DriverOptions{},
461+
}
462+
463+
req := &csi.CreateVolumeRequest{
464+
Name: volumeName,
465+
VolumeCapabilities: []*csi.VolumeCapability{
466+
stdVolCap,
467+
},
468+
Parameters: map[string]string{
469+
volumeParamsSubnetId: subnetId,
470+
volumeParamsSecurityGroupIds: securityGroupIds,
471+
volumeParamsExtraTags: invalidExtraTags,
472+
},
473+
}
474+
475+
ctx := context.Background()
476+
_, err := driver.CreateVolume(ctx, req)
477+
if err == nil {
478+
t.Fatal("CreateVolume is not failed")
479+
}
480+
481+
mockCtl.Finish()
482+
},
483+
},
377484
{
378485
name: "fail: volume name missing",
379486
testFunc: func(t *testing.T) {

0 commit comments

Comments
 (0)