Skip to content

Commit d735e97

Browse files
committed
feat: use kubelet identity by default in msi auth
test: add unit test fix
1 parent d84a738 commit d735e97

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

pkg/blob/blob.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const (
8383
storageSPNClientIDField = "azurestoragespnclientid"
8484
storageSPNTenantIDField = "azurestoragespntenantid"
8585
storageAuthTypeField = "azurestorageauthtype"
86+
storageAuthTypeMSI = "msi"
8687
storageIdentityClientIDField = "azurestorageidentityclientid"
8788
storageIdentityObjectIDField = "azurestorageidentityobjectid"
8889
storageIdentityResourceIDField = "azurestorageidentityresourceid"
@@ -635,7 +636,7 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
635636
if spnTenantID != "" {
636637
storageSPNTenantID = spnTenantID
637638
}
638-
if err != nil && strings.EqualFold(azureStorageAuthType, "msi") {
639+
if err != nil && strings.EqualFold(azureStorageAuthType, storageAuthTypeMSI) {
639640
klog.V(2).Infof("ignore error(%v) since secret is optional for auth type(%s)", err, azureStorageAuthType)
640641
err = nil
641642
}
@@ -708,6 +709,23 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
708709
authEnv = append(authEnv, "AZURE_STORAGE_SPN_TENANT_ID="+storageSPNTenantID)
709710
}
710711

712+
if azureStorageAuthType == storageAuthTypeMSI {
713+
// check whether authEnv contains AZURE_STORAGE_IDENTITY_ prefix
714+
containsIdentityEnv := false
715+
for _, env := range authEnv {
716+
if strings.HasPrefix(env, "AZURE_STORAGE_IDENTITY_") {
717+
klog.V(2).Infof("AZURE_STORAGE_IDENTITY_ is already set in authEnv, skip setting it again")
718+
containsIdentityEnv = true
719+
break
720+
}
721+
}
722+
if !containsIdentityEnv && d.cloud != nil && d.cloud.Config.AzureAuthConfig.UserAssignedIdentityID != "" {
723+
klog.V(2).Infof("azureStorageAuthType is set to %s, add AZURE_STORAGE_IDENTITY_CLIENT_ID(%s) into authEnv",
724+
azureStorageAuthType, d.cloud.Config.AzureAuthConfig.UserAssignedIdentityID)
725+
authEnv = append(authEnv, "AZURE_STORAGE_IDENTITY_CLIENT_ID="+d.cloud.Config.AzureAuthConfig.UserAssignedIdentityID)
726+
}
727+
}
728+
711729
return rgName, accountName, accountKey, containerName, authEnv, err
712730
}
713731

pkg/blob/blob_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,54 @@ func TestGetAuthEnv(t *testing.T) {
588588
}
589589
},
590590
},
591+
{
592+
name: "valid request with MSIAuthTypeAddsIdentityEnv",
593+
testFunc: func(t *testing.T) {
594+
d := NewFakeDriver()
595+
d.cloud = &storage.AccountRepo{}
596+
d.cloud.Config.AzureAuthConfig = azclient.AzureAuthConfig{
597+
UserAssignedIdentityID: "unit-test-identity-id",
598+
}
599+
600+
attrib := map[string]string{
601+
subscriptionIDField: "subID",
602+
resourceGroupField: "rg",
603+
storageAccountField: "accountname",
604+
storageAccountNameField: "accountname",
605+
secretNameField: "secretName",
606+
secretNamespaceField: "sNS",
607+
containerNameField: "containername",
608+
mountWithWITokenField: "false",
609+
pvcNamespaceKey: "pvcNSKey",
610+
getAccountKeyFromSecretField: "false",
611+
storageAuthTypeField: storageAuthTypeMSI,
612+
msiEndpointField: "msiEndpoint",
613+
getLatestAccountKeyField: "true",
614+
}
615+
secret := make(map[string]string)
616+
volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
617+
ctrl := gomock.NewController(t)
618+
defer ctrl.Finish()
619+
mockStorageAccountsClient := mock_accountclient.NewMockInterface(ctrl)
620+
d.cloud.ComputeClientFactory = mock_azclient.NewMockClientFactory(ctrl)
621+
d.cloud.ComputeClientFactory.(*mock_azclient.MockClientFactory).EXPECT().GetAccountClient().Return(mockStorageAccountsClient).AnyTimes()
622+
s := "unit-test"
623+
accountkey := armstorage.AccountKey{Value: &s}
624+
list := []*armstorage.AccountKey{&accountkey}
625+
mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any()).Return(list, nil).AnyTimes()
626+
d.cloud.ComputeClientFactory.(*mock_azclient.MockClientFactory).EXPECT().GetAccountClientForSub(gomock.Any()).Return(mockStorageAccountsClient, nil).AnyTimes()
627+
_, _, _, _, authEnv, err := d.GetAuthEnv(context.TODO(), volumeID, "", attrib, secret)
628+
assert.NoError(t, err)
629+
found := false
630+
for _, env := range authEnv {
631+
if env == "AZURE_STORAGE_IDENTITY_CLIENT_ID=unit-test-identity-id" {
632+
found = true
633+
break
634+
}
635+
}
636+
assert.True(t, found, "AZURE_STORAGE_IDENTITY_CLIENT_ID should be present in authEnv")
637+
},
638+
},
591639
{
592640
name: "invalid getLatestAccountKey value",
593641
testFunc: func(t *testing.T) {

0 commit comments

Comments
 (0)