@@ -590,27 +590,45 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
590590 tenantID = d .cloud .TenantID
591591 }
592592
593- if clientID != "" {
594- if mountWithWIToken {
595- klog .V (2 ).Infof ("clientID(%s) is specified, use workload identity for blobfuse auth" , clientID )
596-
597- workloadIdentityToken , err := parseServiceAccountToken (serviceAccountToken )
598- if err != nil {
599- return rgName , accountName , accountKey , containerName , authEnv , err
593+ if mountWithWIToken {
594+ if clientID == "" {
595+ clientID = d .cloud .Config .AzureAuthConfig .UserAssignedIdentityID
596+ if clientID == "" {
597+ return rgName , accountName , accountKey , containerName , authEnv , fmt .Errorf ("mountWithWorkloadIdentityToken is true but clientID is not specified" )
600598 }
601- azureOAuthTokenFile := filepath .Join (defaultAzureOAuthTokenDir , clientID + accountName )
599+ }
600+ klog .V (2 ).Infof ("mountWithWorkloadIdentityToken is specified, use workload identity auth for mount, clientID: %s, tenantID: %s" , clientID , tenantID )
601+
602+ workloadIdentityToken , err := parseServiceAccountToken (serviceAccountToken )
603+ if err != nil {
604+ return rgName , accountName , accountKey , containerName , authEnv , err
605+ }
606+ tokenFileName := clientID + "-" + accountName
607+ if ! isValidTokenFileName (tokenFileName ) {
608+ return rgName , accountName , accountKey , containerName , authEnv , fmt .Errorf ("the generated token file name %s is invalid" , tokenFileName )
609+ }
610+ azureOAuthTokenFile := filepath .Join (defaultAzureOAuthTokenDir , tokenFileName )
611+ // check whether token value is the same as the one in the token file
612+ existingToken , readErr := os .ReadFile (azureOAuthTokenFile )
613+ if readErr == nil && string (existingToken ) == workloadIdentityToken {
614+ klog .V (6 ).Infof ("the existing workload identity token file %s is up-to-date, no need to rewrite" , azureOAuthTokenFile )
615+ } else {
616+ // write the token to a file
602617 if err := os .WriteFile (azureOAuthTokenFile , []byte (workloadIdentityToken ), 0600 ); err != nil {
603618 return rgName , accountName , accountKey , containerName , authEnv , fmt .Errorf ("failed to write workload identity token file %s: %v" , azureOAuthTokenFile , err )
604619 }
620+ }
605621
606- authEnv = append (authEnv , "AZURE_STORAGE_SPN_CLIENT_ID=" + clientID )
607- if tenantID != "" {
608- authEnv = append (authEnv , "AZURE_STORAGE_SPN_TENANT_ID=" + tenantID )
609- }
610- authEnv = append (authEnv , "AZURE_OAUTH_TOKEN_FILE=" + azureOAuthTokenFile )
611- klog .V (2 ).Infof ("workload identity auth: %v" , authEnv )
612- return rgName , accountName , accountKey , containerName , authEnv , err
622+ authEnv = append (authEnv , "AZURE_STORAGE_SPN_CLIENT_ID=" + clientID )
623+ if tenantID != "" {
624+ authEnv = append (authEnv , "AZURE_STORAGE_SPN_TENANT_ID=" + tenantID )
613625 }
626+ authEnv = append (authEnv , "AZURE_OAUTH_TOKEN_FILE=" + azureOAuthTokenFile )
627+ klog .V (2 ).Infof ("workload identity auth: %v" , authEnv )
628+ return rgName , accountName , accountKey , containerName , authEnv , err
629+ }
630+
631+ if clientID != "" {
614632 klog .V (2 ).Infof ("clientID(%s) is specified, use service account token to get account key" , clientID )
615633 if subsID == "" {
616634 subsID = d .cloud .SubscriptionID
@@ -1244,3 +1262,20 @@ func parseServiceAccountToken(tokenStr string) (string, error) {
12441262 }
12451263 return token .APIAzureADTokenExchange .Token , nil
12461264}
1265+
1266+ // isValidTokenFileName checks if the token file name is valid
1267+ // fileName should only contain alphanumeric characters, hyphens
1268+ func isValidTokenFileName (fileName string ) bool {
1269+ if fileName == "" {
1270+ return false
1271+ }
1272+ for _ , c := range fileName {
1273+ if ! (('a' <= c && c <= 'z' ) ||
1274+ ('A' <= c && c <= 'Z' ) ||
1275+ ('0' <= c && c <= '9' ) ||
1276+ (c == '-' )) {
1277+ return false
1278+ }
1279+ }
1280+ return true
1281+ }
0 commit comments