Skip to content

Commit a36d3f0

Browse files
committed
chore: refine sastoken cache
1 parent a44b977 commit a36d3f0

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

pkg/blob/controllerserver.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -851,24 +851,13 @@ func (d *Driver) getAzcopyAuth(ctx context.Context, accountName, accountKey, sto
851851
klog.Warningf("failed to authorize azcopy with identity, error: %v", err)
852852
} else {
853853
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)
854+
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
855+
if testErr != nil {
856+
return "", nil, fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
858857
}
859-
if cache != nil {
860-
klog.V(2).Infof("use sas token for account(%s) since this account is found in azcopySasTokenCache", accountName)
858+
if strings.Contains(out, authorizationPermissionMismatch) {
859+
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)
861860
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-
}
872861
}
873862
}
874863
}
@@ -882,7 +871,7 @@ func (d *Driver) getAzcopyAuth(ctx context.Context, accountName, accountKey, sto
882871
}
883872
}
884873
klog.V(2).Infof("generate sas token for account(%s)", accountName)
885-
sasToken, err := generateSASToken(accountName, accountKey, storageEndpointSuffix, d.sasTokenExpirationMinutes)
874+
sasToken, err := d.generateSASToken(accountName, accountKey, storageEndpointSuffix, d.sasTokenExpirationMinutes)
886875
return sasToken, nil, err
887876
}
888877
return "", authAzcopyEnv, nil
@@ -914,7 +903,17 @@ func parseDays(dayStr string) (int32, error) {
914903
}
915904

916905
// generateSASToken generate a sas token for storage account
917-
func generateSASToken(accountName, accountKey, storageEndpointSuffix string, expiryTime int) (string, error) {
906+
func (d *Driver) generateSASToken(accountName, accountKey, storageEndpointSuffix string, expiryTime int) (string, error) {
907+
// search in cache first
908+
cache, err := d.azcopySasTokenCache.Get(accountName, azcache.CacheReadTypeDefault)
909+
if err != nil {
910+
return "", fmt.Errorf("get(%s) from azcopySasTokenCache failed with error: %v", accountName, err)
911+
}
912+
if cache != nil {
913+
klog.V(2).Infof("use sas token for account(%s) since this account is found in azcopySasTokenCache", accountName)
914+
return cache.(string), nil
915+
}
916+
918917
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
919918
if err != nil {
920919
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 +935,7 @@ func generateSASToken(accountName, accountKey, storageEndpointSuffix string, exp
936935
if err != nil {
937936
return "", err
938937
}
939-
return "?" + u.RawQuery, nil
938+
sasToken := "?" + u.RawQuery
939+
d.azcopySasTokenCache.Set(accountName, sasToken)
940+
return sasToken, nil
940941
}

pkg/blob/controllerserver_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,7 @@ func TestParseDays(t *testing.T) {
18081808
}
18091809

18101810
func TestGenerateSASToken(t *testing.T) {
1811+
d := NewFakeDriver()
18111812
storageEndpointSuffix := "core.windows.net"
18121813
tests := []struct {
18131814
name string
@@ -1833,7 +1834,7 @@ func TestGenerateSASToken(t *testing.T) {
18331834
}
18341835
for _, tt := range tests {
18351836
t.Run(tt.name, func(t *testing.T) {
1836-
sas, err := generateSASToken(tt.accountName, tt.accountKey, storageEndpointSuffix, 30)
1837+
sas, err := d.generateSASToken(tt.accountName, tt.accountKey, storageEndpointSuffix, 30)
18371838
if !reflect.DeepEqual(err, tt.expectedErr) {
18381839
t.Errorf("generateSASToken error = %v, expectedErr %v, sas token = %v, want %v", err, tt.expectedErr, sas, tt.want)
18391840
return

0 commit comments

Comments
 (0)