@@ -825,46 +825,40 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
825
825
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
826
826
func (d * Driver ) getAzcopyAuth (ctx context.Context , accountName , accountKey , storageEndpointSuffix string , accountOptions * azure.AccountOptions , secrets map [string ]string , secretName , secretNamespace string ) (string , []string , error ) {
827
827
var authAzcopyEnv []string
828
+ var err error
828
829
useSasToken := false
829
830
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
+
831
837
authAzcopyEnv , err = d .authorizeAzcopyWithIdentity ()
832
838
if err != nil {
833
839
klog .Warningf ("failed to authorize azcopy with identity, error: %v" , err )
834
840
} else {
835
841
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 )
840
845
}
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 )
843
848
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
- }
854
849
}
855
850
}
856
851
}
857
852
}
858
853
859
854
if len (secrets ) > 0 || len (secretName ) > 0 || len (authAzcopyEnv ) == 0 || useSasToken {
860
- var err error
861
855
if accountKey == "" {
862
856
if _ , accountKey , err = d .GetStorageAccesskey (ctx , accountOptions , secrets , secretName , secretNamespace ); err != nil {
863
857
return "" , nil , err
864
858
}
865
859
}
866
860
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 )
868
862
return sasToken , nil , err
869
863
}
870
864
return "" , authAzcopyEnv , nil
@@ -896,7 +890,17 @@ func parseDays(dayStr string) (int32, error) {
896
890
}
897
891
898
892
// 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
+
900
904
credential , err := azblob .NewSharedKeyCredential (accountName , accountKey )
901
905
if err != nil {
902
906
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
918
922
if err != nil {
919
923
return "" , err
920
924
}
921
- return "?" + u .RawQuery , nil
925
+ sasToken := "?" + u .RawQuery
926
+ d .azcopySasTokenCache .Set (accountName , sasToken )
927
+ return sasToken , nil
922
928
}
0 commit comments