Skip to content

Commit 014cc40

Browse files
authored
Merge pull request #1993 from andyzhangx/add-mountWithWIToken-1.25
[release-1.25] fix: add mountWithWorkloadIdentityToken parameter
2 parents aba0fdb + 998e2ab commit 014cc40

File tree

5 files changed

+60
-12
lines changed

5 files changed

+60
-12
lines changed

docs/workload-identity-static-pv-mount.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export IDENTITY_TENANT=$(az aks show --name $CLUSTER_NAME --resource-group $RESO
3333
export ACCOUNT_SCOPE=$(az storage account show --name $ACCOUNT --query id -o tsv)
3434
3535
# please retry if you meet `Cannot find user or service principal in graph database` error, it may take a while for the identity to propagate
36-
az role assignment create --role "Storage Blob Data Contributor" --assignee $USER_ASSIGNED_CLIENT_ID --scope $ACCOUNT_SCOPE
36+
az role assignment create --role "Storage Account Contributor" --assignee $USER_ASSIGNED_CLIENT_ID --scope $ACCOUNT_SCOPE
3737
```
3838

3939
### 4. Create service account on AKS

pkg/blob/blob.go

+24-10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const (
101101
podNamespaceField = "csi.storage.k8s.io/pod.namespace"
102102
serviceAccountTokenField = "csi.storage.k8s.io/serviceAccount.tokens"
103103
clientIDField = "clientid"
104+
mountWithWITokenField = "mountwithworkloadidentitytoken"
104105
tenantIDField = "tenantid"
105106
mountOptionsField = "mountoptions"
106107
falseValue = "false"
@@ -490,6 +491,7 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
490491
getAccountKeyFromSecret bool
491492
getLatestAccountKey bool
492493
clientID string
494+
mountWithWIToken bool
493495
tenantID string
494496
serviceAccountToken string
495497
)
@@ -543,6 +545,10 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
543545
}
544546
case clientIDField:
545547
clientID = v
548+
case mountWithWITokenField:
549+
if mountWithWIToken, err = strconv.ParseBool(v); err != nil {
550+
return rgName, accountName, accountKey, containerName, authEnv, fmt.Errorf("invalid %s: %s in volume context", mountWithWITokenField, v)
551+
}
546552
case tenantIDField:
547553
tenantID = v
548554
case strings.ToLower(serviceAccountTokenField):
@@ -572,21 +578,29 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
572578
tenantID = d.cloud.TenantID
573579
}
574580

575-
// if client id is specified, we only use workload identity for blobfuse auth
576581
if clientID != "" {
577-
klog.V(2).Infof("clientID(%s) is specified, use workload identity for blobfuse auth", clientID)
582+
if mountWithWIToken {
583+
klog.V(2).Infof("clientID(%s) is specified, use workload identity for blobfuse auth", clientID)
584+
585+
workloadIdentityToken, err := parseServiceAccountToken(serviceAccountToken)
586+
if err != nil {
587+
return rgName, accountName, accountKey, containerName, authEnv, err
588+
}
589+
590+
authEnv = append(authEnv, "AZURE_STORAGE_SPN_CLIENT_ID="+clientID)
591+
if tenantID != "" {
592+
authEnv = append(authEnv, "AZURE_STORAGE_SPN_TENANT_ID="+tenantID)
593+
}
594+
authEnv = append(authEnv, "WORKLOAD_IDENTITY_TOKEN="+workloadIdentityToken)
578595

579-
workloadIdentityToken, err := parseServiceAccountToken(serviceAccountToken)
580-
if err != nil {
581596
return rgName, accountName, accountKey, containerName, authEnv, err
582597
}
583-
584-
authEnv = append(authEnv, "AZURE_STORAGE_SPN_CLIENT_ID="+clientID)
585-
if tenantID != "" {
586-
authEnv = append(authEnv, "AZURE_STORAGE_SPN_TENANT_ID="+tenantID)
598+
klog.V(2).Infof("clientID(%s) is specified, use service account token to get account key", clientID)
599+
if subsID == "" {
600+
subsID = d.cloud.SubscriptionID
587601
}
588-
authEnv = append(authEnv, "WORKLOAD_IDENTITY_TOKEN="+workloadIdentityToken)
589-
602+
accountKey, err := d.cloud.GetStorageAccesskeyFromServiceAccountToken(ctx, subsID, accountName, rgName, clientID, tenantID, serviceAccountToken)
603+
authEnv = append(authEnv, "AZURE_STORAGE_ACCESS_KEY="+accountKey)
590604
return rgName, accountName, accountKey, containerName, authEnv, err
591605
}
592606

pkg/blob/blob_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,23 @@ func TestGetAuthEnv(t *testing.T) {
549549
name: "valid request",
550550
testFunc: func(t *testing.T) {
551551
d := NewFakeDriver()
552-
attrib := make(map[string]string)
552+
attrib := map[string]string{
553+
subscriptionIDField: "subID",
554+
resourceGroupField: "rg",
555+
storageAccountField: "accountname",
556+
storageAccountNameField: "accountname",
557+
secretNameField: "secretName",
558+
secretNamespaceField: "sNS",
559+
containerNameField: "containername",
560+
mountWithWITokenField: "false",
561+
pvcNamespaceKey: "pvcNSKey",
562+
getAccountKeyFromSecretField: "false",
563+
storageAuthTypeField: "key",
564+
msiEndpointField: "msiEndpoint",
565+
getLatestAccountKeyField: "true",
566+
tenantIDField: "tenantID",
567+
serviceAccountTokenField: "serviceAccountToken",
568+
}
553569
secret := make(map[string]string)
554570
volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
555571
d.cloud = &azure.Cloud{}
@@ -605,6 +621,22 @@ func TestGetAuthEnv(t *testing.T) {
605621
}
606622
},
607623
},
624+
{
625+
name: "invalid mountWithWIToken value",
626+
testFunc: func(t *testing.T) {
627+
d := NewFakeDriver()
628+
attrib := map[string]string{
629+
mountWithWITokenField: "invalid",
630+
}
631+
secret := make(map[string]string)
632+
volumeID := "rg#f5713de20cde511e8ba4900#pvc-fuse-dynamic-17e43f84-f474-11e8-acd0-000d3a00df41"
633+
_, _, _, _, _, err := d.GetAuthEnv(context.TODO(), volumeID, "", attrib, secret)
634+
expectedErr := fmt.Errorf("invalid %s: %s in volume context", mountWithWITokenField, "invalid")
635+
if !reflect.DeepEqual(err, expectedErr) {
636+
t.Errorf("actualErr: (%v), expectedErr: (%v)", err, expectedErr)
637+
}
638+
},
639+
},
608640
{
609641
name: "secret not empty",
610642
testFunc: func(t *testing.T) {

pkg/blob/controllerserver.go

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
188188
case storageIdentityObjectIDField:
189189
case storageIdentityResourceIDField:
190190
case clientIDField:
191+
case mountWithWITokenField:
191192
case tenantIDField:
192193
case msiEndpointField:
193194
case storageAADEndpointField:

pkg/blob/controllerserver_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ func TestCreateVolume(t *testing.T) {
450450
mp[storageAuthTypeField] = "msi"
451451
mp[storageIdentityClientIDField] = "msi"
452452
mp[clientIDField] = "clientID"
453+
mp[mountWithWITokenField] = "true"
453454
mp[tenantIDField] = "tenantID"
454455
mp[storageIdentityObjectIDField] = "msi"
455456
mp[storageIdentityResourceIDField] = "msi"

0 commit comments

Comments
 (0)