Skip to content

Commit 4f34ae5

Browse files
authored
Merge pull request #1305 from k8s-infra-cherrypick-robot/cherry-pick-1303-to-release-1.23
[release-1.23] chore: refine sastoken cache
2 parents 5e3ede3 + 0fd5a78 commit 4f34ae5

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
@@ -825,46 +825,40 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
825825
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
826826
func (d *Driver) getAzcopyAuth(ctx context.Context, accountName, accountKey, storageEndpointSuffix string, accountOptions *azure.AccountOptions, secrets map[string]string, secretName, secretNamespace string) (string, []string, error) {
827827
var authAzcopyEnv []string
828+
var err error
828829
useSasToken := false
829830
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+
831837
authAzcopyEnv, err = d.authorizeAzcopyWithIdentity()
832838
if err != nil {
833839
klog.Warningf("failed to authorize azcopy with identity, error: %v", err)
834840
} else {
835841
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)
840845
}
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)
843848
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-
}
854849
}
855850
}
856851
}
857852
}
858853

859854
if len(secrets) > 0 || len(secretName) > 0 || len(authAzcopyEnv) == 0 || useSasToken {
860-
var err error
861855
if accountKey == "" {
862856
if _, accountKey, err = d.GetStorageAccesskey(ctx, accountOptions, secrets, secretName, secretNamespace); err != nil {
863857
return "", nil, err
864858
}
865859
}
866860
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)
868862
return sasToken, nil, err
869863
}
870864
return "", authAzcopyEnv, nil
@@ -896,7 +890,17 @@ func parseDays(dayStr string) (int32, error) {
896890
}
897891

898892
// 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+
900904
credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
901905
if err != nil {
902906
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
918922
if err != nil {
919923
return "", err
920924
}
921-
return "?" + u.RawQuery, nil
925+
sasToken := "?" + u.RawQuery
926+
d.azcopySasTokenCache.Set(accountName, sasToken)
927+
return sasToken, nil
922928
}

pkg/blob/controllerserver_test.go

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

17801780
func TestGenerateSASToken(t *testing.T) {
1781+
d := NewFakeDriver()
17811782
storageEndpointSuffix := "core.windows.net"
17821783
tests := []struct {
17831784
name string
@@ -1803,7 +1804,7 @@ func TestGenerateSASToken(t *testing.T) {
18031804
}
18041805
for _, tt := range tests {
18051806
t.Run(tt.name, func(t *testing.T) {
1806-
sas, err := generateSASToken(tt.accountName, tt.accountKey, storageEndpointSuffix, 30)
1807+
sas, err := d.generateSASToken(tt.accountName, tt.accountKey, storageEndpointSuffix, 30)
18071808
if !reflect.DeepEqual(err, tt.expectedErr) {
18081809
t.Errorf("generateSASToken error = %v, expectedErr %v, sas token = %v, want %v", err, tt.expectedErr, sas, tt.want)
18091810
return

0 commit comments

Comments
 (0)