@@ -1230,42 +1230,44 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
1230
1230
// 4. parameter useSasToken is true
1231
1231
func (d * Driver ) getAzcopyAuth (ctx context.Context , accountName , accountKey , storageEndpointSuffix string , accountOptions * azure.AccountOptions , secrets map [string ]string , secretName , secretNamespace string , useSasToken bool ) (string , []string , error ) {
1232
1232
var authAzcopyEnv []string
1233
+ var err error
1233
1234
if ! useSasToken && len (secrets ) == 0 && len (secretName ) == 0 {
1234
- var err error
1235
+ // search in cache first
1236
+ if cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault ); err == nil && cache != nil {
1237
+ klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
1238
+ return cache .(string ), nil , nil
1239
+ }
1235
1240
authAzcopyEnv , err = d .authorizeAzcopyWithIdentity ()
1236
1241
if err != nil {
1237
1242
klog .Warningf ("failed to authorize azcopy with identity, error: %v" , err )
1238
- } else {
1239
- if len (authAzcopyEnv ) > 0 {
1240
- // search in cache first
1241
- cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault )
1242
- if err != nil {
1243
- return "" , nil , fmt .Errorf ("get(%s) from azcopySasTokenCache failed with error: %v" , accountName , err )
1244
- }
1245
- if cache != nil {
1246
- klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
1247
- useSasToken = true
1248
- }
1249
- }
1250
1243
}
1251
1244
}
1252
1245
1253
1246
if len (secrets ) > 0 || len (secretName ) > 0 || len (authAzcopyEnv ) == 0 || useSasToken {
1254
- var err error
1255
1247
if accountKey == "" {
1256
1248
if accountKey , err = d .GetStorageAccesskey (ctx , accountOptions , secrets , secretName , secretNamespace ); err != nil {
1257
1249
return "" , nil , err
1258
1250
}
1259
1251
}
1260
1252
klog .V (2 ).Infof ("generate sas token for account(%s)" , accountName )
1261
- sasToken , err := generateSASToken (accountName , accountKey , storageEndpointSuffix , d .sasTokenExpirationMinutes )
1253
+ sasToken , err := d . generateSASToken (accountName , accountKey , storageEndpointSuffix , d .sasTokenExpirationMinutes )
1262
1254
return sasToken , nil , err
1263
1255
}
1264
1256
return "" , authAzcopyEnv , nil
1265
1257
}
1266
1258
1267
1259
// generateSASToken generate a sas token for storage account
1268
- func generateSASToken (accountName , accountKey , storageEndpointSuffix string , expiryTime int ) (string , error ) {
1260
+ func (d * Driver ) generateSASToken (accountName , accountKey , storageEndpointSuffix string , expiryTime int ) (string , error ) {
1261
+ // search in cache first
1262
+ cache , err := d .azcopySasTokenCache .Get (accountName , azcache .CacheReadTypeDefault )
1263
+ if err != nil {
1264
+ return "" , fmt .Errorf ("get(%s) from azcopySasTokenCache failed with error: %v" , accountName , err )
1265
+ }
1266
+ if cache != nil {
1267
+ klog .V (2 ).Infof ("use sas token for account(%s) since this account is found in azcopySasTokenCache" , accountName )
1268
+ return cache .(string ), nil
1269
+ }
1270
+
1269
1271
credential , err := service .NewSharedKeyCredential (accountName , accountKey )
1270
1272
if err != nil {
1271
1273
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 ()))
@@ -1286,5 +1288,7 @@ func generateSASToken(accountName, accountKey, storageEndpointSuffix string, exp
1286
1288
if err != nil {
1287
1289
return "" , err
1288
1290
}
1289
- return "?" + u .RawQuery , nil
1291
+ sasToken := "?" + u .RawQuery
1292
+ d .azcopySasTokenCache .Set (accountName , sasToken )
1293
+ return sasToken , nil
1290
1294
}
0 commit comments