Skip to content

Commit d0f7c21

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

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
@@ -85,6 +85,7 @@ const (
8585
storageSPNClientIDField = "azurestoragespnclientid"
8686
storageSPNTenantIDField = "azurestoragespntenantid"
8787
storageAuthTypeField = "azurestorageauthtype"
88+
storageAuthTypeMSI = "msi"
8889
storageIdentityClientIDField = "azurestorageidentityclientid"
8990
storageIdentityObjectIDField = "azurestorageidentityobjectid"
9091
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
@@ -590,6 +590,54 @@ func TestGetAuthEnv(t *testing.T) {
590590
}
591591
},
592592
},
593+
{
594+
name: "valid request with MSIAuthTypeAddsIdentityEnv",
595+
testFunc: func(t *testing.T) {
596+
d := NewFakeDriver()
597+
d.cloud = &storage.AccountRepo{}
598+
d.cloud.Config.AzureAuthConfig = azclient.AzureAuthConfig{
599+
UserAssignedIdentityID: "unit-test-identity-id",
600+
}
601+
602+
attrib := map[string]string{
603+
subscriptionIDField: "subID",
604+
resourceGroupField: "rg",
605+
storageAccountField: "accountname",
606+
storageAccountNameField: "accountname",
607+
secretNameField: "secretName",
608+
secretNamespaceField: "sNS",
609+
containerNameField: "containername",
610+
mountWithWITokenField: "false",
611+
pvcNamespaceKey: "pvcNSKey",
612+
getAccountKeyFromSecretField: "false",
613+
storageAuthTypeField: storageAuthTypeMSI,
614+
msiEndpointField: "msiEndpoint",
615+
getLatestAccountKeyField: "true",
616+
}
617+
secret := make(map[string]string)
618+
volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
619+
ctrl := gomock.NewController(t)
620+
defer ctrl.Finish()
621+
mockStorageAccountsClient := mock_accountclient.NewMockInterface(ctrl)
622+
d.cloud.ComputeClientFactory = mock_azclient.NewMockClientFactory(ctrl)
623+
d.cloud.ComputeClientFactory.(*mock_azclient.MockClientFactory).EXPECT().GetAccountClient().Return(mockStorageAccountsClient).AnyTimes()
624+
s := "unit-test"
625+
accountkey := armstorage.AccountKey{Value: &s}
626+
list := []*armstorage.AccountKey{&accountkey}
627+
mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), gomock.Any(), gomock.Any()).Return(list, nil).AnyTimes()
628+
d.cloud.ComputeClientFactory.(*mock_azclient.MockClientFactory).EXPECT().GetAccountClientForSub(gomock.Any()).Return(mockStorageAccountsClient, nil).AnyTimes()
629+
_, _, _, _, authEnv, err := d.GetAuthEnv(context.TODO(), volumeID, "", attrib, secret)
630+
assert.NoError(t, err)
631+
found := false
632+
for _, env := range authEnv {
633+
if env == "AZURE_STORAGE_IDENTITY_CLIENT_ID=unit-test-identity-id" {
634+
found = true
635+
break
636+
}
637+
}
638+
assert.True(t, found, "AZURE_STORAGE_IDENTITY_CLIENT_ID should be present in authEnv")
639+
},
640+
},
593641
{
594642
name: "invalid getLatestAccountKey value",
595643
testFunc: func(t *testing.T) {

0 commit comments

Comments
 (0)