Skip to content

Commit 97a01f9

Browse files
authored
Merge pull request #1304 from k8s-infra-cherrypick-robot/cherry-pick-1303-to-release-1.24
[release-1.24] chore: refine sastoken cache
2 parents 361d128 + 25beacc commit 97a01f9

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

pkg/blob/controllerserver.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -843,46 +843,40 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
843843
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
844844
func (d *Driver) getAzcopyAuth(ctx context.Context, accountName, accountKey, storageEndpointSuffix string, accountOptions *azure.AccountOptions, secrets map[string]string, secretName, secretNamespace string) (string, []string, error) {
845845
var authAzcopyEnv []string
846+
var err error
846847
useSasToken := false
847848
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+
849855
authAzcopyEnv, err = d.authorizeAzcopyWithIdentity()
850856
if err != nil {
851857
klog.Warningf("failed to authorize azcopy with identity, error: %v", err)
852858
} else {
853859
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)
858863
}
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)
861866
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-
}
872867
}
873868
}
874869
}
875870
}
876871

877872
if len(secrets) > 0 || len(secretName) > 0 || len(authAzcopyEnv) == 0 || useSasToken {
878-
var err error
879873
if accountKey == "" {
880874
if _, accountKey, err = d.GetStorageAccesskey(ctx, accountOptions, secrets, secretName, secretNamespace); err != nil {
881875
return "", nil, err
882876
}
883877
}
884878
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)
886880
return sasToken, nil, err
887881
}
888882
return "", authAzcopyEnv, nil
@@ -914,7 +908,17 @@ func parseDays(dayStr string) (int32, error) {
914908
}
915909

916910
// 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+
918922
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
919923
if err != nil {
920924
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
936940
if err != nil {
937941
return "", err
938942
}
939-
return "?" + u.RawQuery, nil
943+
sasToken := "?" + u.RawQuery
944+
d.azcopySasTokenCache.Set(accountName, sasToken)
945+
return sasToken, nil
940946
}

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)