@@ -843,46 +843,40 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
843
843
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
844
844
func (d * Driver ) getAzcopyAuth (ctx context.Context , accountName , accountKey , storageEndpointSuffix string , accountOptions * azure.AccountOptions , secrets map [string ]string , secretName , secretNamespace string ) (string , []string , error ) {
845
845
var authAzcopyEnv []string
846
+ var err error
846
847
useSasToken := false
847
848
if ! d .useDataPlaneAPI ("" , accountName ) && len (secrets ) == 0 && len (secretName ) == 0 {
848
- var err error
849
+ // search in cache first
850
+ if cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault ); err == nil && cache != nil {
851
+ klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
852
+ return cache .(string ), nil , nil
853
+ }
854
+
849
855
authAzcopyEnv , err = d .authorizeAzcopyWithIdentity ()
850
856
if err != nil {
851
857
klog .Warningf ("failed to authorize azcopy with identity, error: %v" , err )
852
858
} else {
853
859
if len (authAzcopyEnv ) > 0 {
854
- // search in cache first
855
- cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault )
856
- if err != nil {
857
- return "" , nil , fmt .Errorf ("get(%s) from azcopySasTokenCache failed with error: %v" , accountName , err )
860
+ out , testErr := d .azcopy .TestListJobs (accountName , storageEndpointSuffix , authAzcopyEnv )
861
+ if testErr != nil {
862
+ return "" , nil , fmt .Errorf ("azcopy list command failed with error(%v): %v" , testErr , out )
858
863
}
859
- if cache != nil {
860
- klog .V ( 2 ). Infof ( "use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
864
+ if strings . Contains ( out , authorizationPermissionMismatch ) {
865
+ 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 )
861
866
useSasToken = true
862
- } else {
863
- out , testErr := d .azcopy .TestListJobs (accountName , storageEndpointSuffix , authAzcopyEnv )
864
- if testErr != nil {
865
- return "" , nil , fmt .Errorf ("azcopy list command failed with error(%v): %v" , testErr , out )
866
- }
867
- if strings .Contains (out , authorizationPermissionMismatch ) {
868
- 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 )
869
- d .azcopySasTokenCache .Set (accountName , "" )
870
- useSasToken = true
871
- }
872
867
}
873
868
}
874
869
}
875
870
}
876
871
877
872
if len (secrets ) > 0 || len (secretName ) > 0 || len (authAzcopyEnv ) == 0 || useSasToken {
878
- var err error
879
873
if accountKey == "" {
880
874
if _ , accountKey , err = d .GetStorageAccesskey (ctx , accountOptions , secrets , secretName , secretNamespace ); err != nil {
881
875
return "" , nil , err
882
876
}
883
877
}
884
878
klog .V (2 ).Infof ("generate sas token for account(%s)" , accountName )
885
- sasToken , err := generateSASToken (accountName , accountKey , storageEndpointSuffix , d .sasTokenExpirationMinutes )
879
+ sasToken , err := d . generateSASToken (accountName , accountKey , storageEndpointSuffix , d .sasTokenExpirationMinutes )
886
880
return sasToken , nil , err
887
881
}
888
882
return "" , authAzcopyEnv , nil
@@ -914,7 +908,17 @@ func parseDays(dayStr string) (int32, error) {
914
908
}
915
909
916
910
// generateSASToken generate a sas token for storage account
917
- func generateSASToken (accountName , accountKey , storageEndpointSuffix string , expiryTime int ) (string , error ) {
911
+ func (d * Driver ) generateSASToken (accountName , accountKey , storageEndpointSuffix string , expiryTime int ) (string , error ) {
912
+ // search in cache first
913
+ cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault )
914
+ if err != nil {
915
+ return "" , fmt .Errorf ("get(%s) from azcopySasTokenCache failed with error: %v" , accountName , err )
916
+ }
917
+ if cache != nil {
918
+ klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
919
+ return cache .(string ), nil
920
+ }
921
+
918
922
credential , err := azblob .NewSharedKeyCredential (accountName , accountKey )
919
923
if err != nil {
920
924
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 ()))
@@ -936,5 +940,7 @@ func generateSASToken(accountName, accountKey, storageEndpointSuffix string, exp
936
940
if err != nil {
937
941
return "" , err
938
942
}
939
- return "?" + u .RawQuery , nil
943
+ sasToken := "?" + u .RawQuery
944
+ d .azcopySasTokenCache .Set (accountName , sasToken )
945
+ return sasToken , nil
940
946
}
0 commit comments