@@ -825,46 +825,40 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
825825// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
826826func (d * Driver ) getAzcopyAuth (ctx context.Context , accountName , accountKey , storageEndpointSuffix string , accountOptions * azure.AccountOptions , secrets map [string ]string , secretName , secretNamespace string ) (string , []string , error ) {
827827 var authAzcopyEnv []string
828+ var err error
828829 useSasToken := false
829830 if ! d .useDataPlaneAPI ("" , accountName ) && len (secrets ) == 0 && len (secretName ) == 0 {
830- var err error
831+ // search in cache first
832+ if cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault ); err == nil && cache != nil {
833+ klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
834+ return cache .(string ), nil , nil
835+ }
836+
831837 authAzcopyEnv , err = d .authorizeAzcopyWithIdentity ()
832838 if err != nil {
833839 klog .Warningf ("failed to authorize azcopy with identity, error: %v" , err )
834840 } else {
835841 if len (authAzcopyEnv ) > 0 {
836- // search in cache first
837- cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault )
838- if err != nil {
839- return "" , nil , fmt .Errorf ("get(%s) from azcopySasTokenCache failed with error: %v" , accountName , err )
842+ out , testErr := d .azcopy .TestListJobs (accountName , storageEndpointSuffix , authAzcopyEnv )
843+ if testErr != nil {
844+ return "" , nil , fmt .Errorf ("azcopy list command failed with error(%v): %v" , testErr , out )
840845 }
841- if cache != nil {
842- klog .V ( 2 ). Infof ( "use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
846+ if strings . Contains ( out , authorizationPermissionMismatch ) {
847+ klog .Warningf ( "azcopy list failed with AuthorizationPermissionMismatch error, should assign \" Storage Blob Data Contributor \" role to controller identity, fall back to use sas token, original output: %v" , out )
843848 useSasToken = true
844- } else {
845- out , testErr := d .azcopy .TestListJobs (accountName , storageEndpointSuffix , authAzcopyEnv )
846- if testErr != nil {
847- return "" , nil , fmt .Errorf ("azcopy list command failed with error(%v): %v" , testErr , out )
848- }
849- if strings .Contains (out , authorizationPermissionMismatch ) {
850- klog .Warningf ("azcopy list failed with AuthorizationPermissionMismatch error, should assign \" Storage Blob Data Contributor\" role to controller identity, fall back to use sas token, original output: %v" , out )
851- d .azcopySasTokenCache .Set (accountName , "" )
852- useSasToken = true
853- }
854849 }
855850 }
856851 }
857852 }
858853
859854 if len (secrets ) > 0 || len (secretName ) > 0 || len (authAzcopyEnv ) == 0 || useSasToken {
860- var err error
861855 if accountKey == "" {
862856 if _ , accountKey , err = d .GetStorageAccesskey (ctx , accountOptions , secrets , secretName , secretNamespace ); err != nil {
863857 return "" , nil , err
864858 }
865859 }
866860 klog .V (2 ).Infof ("generate sas token for account(%s)" , accountName )
867- sasToken , err := generateSASToken (accountName , accountKey , storageEndpointSuffix , d .sasTokenExpirationMinutes )
861+ sasToken , err := d . generateSASToken (accountName , accountKey , storageEndpointSuffix , d .sasTokenExpirationMinutes )
868862 return sasToken , nil , err
869863 }
870864 return "" , authAzcopyEnv , nil
@@ -896,7 +890,17 @@ func parseDays(dayStr string) (int32, error) {
896890}
897891
898892// generateSASToken generate a sas token for storage account
899- func generateSASToken (accountName , accountKey , storageEndpointSuffix string , expiryTime int ) (string , error ) {
893+ func (d * Driver ) generateSASToken (accountName , accountKey , storageEndpointSuffix string , expiryTime int ) (string , error ) {
894+ // search in cache first
895+ cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault )
896+ if err != nil {
897+ return "" , fmt .Errorf ("get(%s) from azcopySasTokenCache failed with error: %v" , accountName , err )
898+ }
899+ if cache != nil {
900+ klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
901+ return cache .(string ), nil
902+ }
903+
900904 credential , err := azblob .NewSharedKeyCredential (accountName , accountKey )
901905 if err != nil {
902906 return "" , status .Errorf (codes .Internal , fmt .Sprintf ("failed to generate sas token in creating new shared key credential, accountName: %s, err: %s" , accountName , err .Error ()))
@@ -918,5 +922,7 @@ func generateSASToken(accountName, accountKey, storageEndpointSuffix string, exp
918922 if err != nil {
919923 return "" , err
920924 }
921- return "?" + u .RawQuery , nil
925+ sasToken := "?" + u .RawQuery
926+ d .azcopySasTokenCache .Set (accountName , sasToken )
927+ return sasToken , nil
922928}
0 commit comments