Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions pkg/azurefile/nodeserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
context := req.GetVolumeContext()
if context != nil {
// token request
if context[serviceAccountTokenField] != "" && getClientID(context) != "" {
klog.V(2).Infof("NodePublishVolume: volume(%s) mount on %s with service account token, clientID: %s", volumeID, target, getClientID(context))
if context[serviceAccountTokenField] != "" && getValueInMap(context, clientIDField) != "" {
klog.V(2).Infof("NodePublishVolume: volume(%s) mount on %s with service account token, clientID: %s", volumeID, target, getValueInMap(context, clientIDField))
_, err := d.NodeStageVolume(ctx, &csi.NodeStageVolumeRequest{
StagingTargetPath: target,
VolumeContext: context,
Expand Down Expand Up @@ -89,7 +89,7 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
return &csi.NodePublishVolumeResponse{}, err
}

if perm := context[mountPermissionsField]; perm != "" {
if perm := getValueInMap(context, mountPermissionsField); perm != "" {
var err error
if mountPermissions, err = strconv.ParseUint(perm, 8, 32); err != nil {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("invalid mountPermissions %s", perm))
Expand Down Expand Up @@ -169,8 +169,8 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
volumeID := req.GetVolumeId()
context := req.GetVolumeContext()

if getClientID(context) != "" && context[serviceAccountTokenField] == "" {
klog.V(2).Infof("Skip NodeStageVolume for volume(%s) since clientID %s is provided but service account token is empty", volumeID, getClientID(context))
if getValueInMap(context, clientIDField) != "" && context[serviceAccountTokenField] == "" {
klog.V(2).Infof("Skip NodeStageVolume for volume(%s) since clientID %s is provided but service account token is empty", volumeID, getValueInMap(context, clientIDField))
return &csi.NodeStageVolumeResponse{}, nil
}

Expand Down Expand Up @@ -614,12 +614,3 @@ func checkGidPresentInMountFlags(mountFlags []string) bool {
}
return false
}

func getClientID(context map[string]string) string {
for k, v := range context {
if strings.EqualFold(k, clientIDField) && v != "" {
return v
}
}
return ""
}
53 changes: 0 additions & 53 deletions pkg/azurefile/nodeserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1067,56 +1067,3 @@ func makeFakeOutput(output string, err error) testingexec.FakeAction {
return []byte(o), nil, err
}
}

func Test_getClientID(t *testing.T) {
type args struct {
context map[string]string
}
tests := []struct {
name string
args args
want string
}{
{
name: "get client id",
args: args{
context: map[string]string{
clientIDField: "test-client-id",
},
},
want: "test-client-id",
},
{
name: "case not sensitive client id",
args: args{
context: map[string]string{
"ClientId": "test-client-id",
},
},
want: "test-client-id",
},
{
name: "no client id",
args: args{
context: map[string]string{},
},
want: "",
},
{
name: "client id empty",
args: args{
context: map[string]string{
clientIDField: "",
},
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getClientID(tt.args.context); got != tt.want {
t.Errorf("getClientID() = %v, want %v", got, tt.want)
}
})
}
}
14 changes: 14 additions & 0 deletions pkg/azurefile/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@ func setKeyValueInMap(m map[string]string, key, value string) {
m[key] = value
}

// getValueInMap get value from map by key
// key in the map is case insensitive
func getValueInMap(m map[string]string, key string) string {
if m == nil {
return ""
}
for k, v := range m {
if strings.EqualFold(k, key) {
return v
}
}
return ""
}

// replaceWithMap replace key with value for str
func replaceWithMap(str string, m map[string]string) string {
for k, v := range m {
Expand Down
46 changes: 46 additions & 0 deletions pkg/azurefile/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,52 @@ func TestSetKeyValueInMap(t *testing.T) {
}
}

func TestGetValueInMap(t *testing.T) {
tests := []struct {
desc string
m map[string]string
key string
expected string
}{
{
desc: "nil map",
key: "key",
expected: "",
},
{
desc: "empty map",
m: map[string]string{},
key: "key",
expected: "",
},
{
desc: "non-empty map",
m: map[string]string{"k": "v"},
key: "key",
expected: "",
},
{
desc: "same key already exists",
m: map[string]string{"subDir": "value2"},
key: "subDir",
expected: "value2",
},
{
desc: "case insensitive key already exists",
m: map[string]string{"subDir": "value2"},
key: "subdir",
expected: "value2",
},
}

for _, test := range tests {
result := getValueInMap(test.m, test.key)
if result != test.expected {
t.Errorf("test[%s]: unexpected output: %v, expected result: %v", test.desc, result, test.expected)
}
}
}

func TestReplaceWithMap(t *testing.T) {
tests := []struct {
desc string
Expand Down