Skip to content

Commit 56c0fc1

Browse files
andyzhangxk8s-infra-cherrypick-robot
authored andcommitted
feat: use kubelet identity by default in msi auth
test: add unit test fix
1 parent 9b3f9ba commit 56c0fc1

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
@@ -84,6 +84,7 @@ const (
8484
storageSPNClientIDField = "azurestoragespnclientid"
8585
storageSPNTenantIDField = "azurestoragespntenantid"
8686
storageAuthTypeField = "azurestorageauthtype"
87+
storageAuthTypeMSI = "msi"
8788
storageIdentityClientIDField = "azurestorageidentityclientid"
8889
storageIdentityObjectIDField = "azurestorageidentityobjectid"
8990
storageIdentityResourceIDField = "azurestorageidentityresourceid"
@@ -600,7 +601,7 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
600601
if spnTenantID != "" {
601602
storageSPNTenantID = spnTenantID
602603
}
603-
if err != nil && strings.EqualFold(azureStorageAuthType, "msi") {
604+
if err != nil && strings.EqualFold(azureStorageAuthType, storageAuthTypeMSI) {
604605
klog.V(2).Infof("ignore error(%v) since secret is optional for auth type(%s)", err, azureStorageAuthType)
605606
err = nil
606607
}
@@ -673,6 +674,23 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
673674
authEnv = append(authEnv, "AZURE_STORAGE_SPN_TENANT_ID="+storageSPNTenantID)
674675
}
675676

677+
if azureStorageAuthType == storageAuthTypeMSI {
678+
// check whether authEnv contains AZURE_STORAGE_IDENTITY_ prefix
679+
containsIdentityEnv := false
680+
for _, env := range authEnv {
681+
if strings.HasPrefix(env, "AZURE_STORAGE_IDENTITY_") {
682+
klog.V(2).Infof("AZURE_STORAGE_IDENTITY_ is already set in authEnv, skip setting it again")
683+
containsIdentityEnv = true
684+
break
685+
}
686+
}
687+
if !containsIdentityEnv && d.cloud != nil && d.cloud.Config.AzureAuthConfig.UserAssignedIdentityID != "" {
688+
klog.V(2).Infof("azureStorageAuthType is set to %s, add AZURE_STORAGE_IDENTITY_CLIENT_ID(%s) into authEnv",
689+
azureStorageAuthType, d.cloud.Config.AzureAuthConfig.UserAssignedIdentityID)
690+
authEnv = append(authEnv, "AZURE_STORAGE_IDENTITY_CLIENT_ID="+d.cloud.Config.AzureAuthConfig.UserAssignedIdentityID)
691+
}
692+
}
693+
676694
return rgName, accountName, accountKey, containerName, authEnv, err
677695
}
678696

pkg/blob/blob_test.go

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

0 commit comments

Comments
 (0)