Skip to content

Commit a3a2f15

Browse files
committed
fix: only generate sas token when secret is used in volume cloning
1 parent c2a1d9b commit a3a2f15

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

pkg/blob/controllerserver.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ func (d *Driver) copyVolume(ctx context.Context, req *csi.CreateVolumeRequest, a
800800
}
801801
}
802802

803+
// authorizeAzcopyWithIdentity returns auth env for azcopy using cluster identity
803804
func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
804805
azureAuthConfig := d.cloud.Config.AzureAuthConfig
805806
var authAzcopyEnv []string
@@ -834,30 +835,39 @@ func (d *Driver) authorizeAzcopyWithIdentity() ([]string, error) {
834835
// 2. driver is not using managed identity and service principal
835836
// 3. azcopy returns AuthorizationPermissionMismatch error when using service principal or managed identity
836837
func (d *Driver) getSASToken(ctx context.Context, accountName, accountKey, storageEndpointSuffix string, accountOptions *azure.AccountOptions, secrets map[string]string, secretName, secretNamespace string) (string, error) {
837-
authAzcopyEnv, _ := d.authorizeAzcopyWithIdentity()
838+
var authAzcopyEnv []string
838839
useSasToken := false
839-
if len(authAzcopyEnv) > 0 {
840-
// search in cache first
841-
cache, err := d.azcopySasTokenCache.Get(accountName, azcache.CacheReadTypeDefault)
840+
if len(secrets) == 0 && len(secretName) == 0 {
841+
var err error
842+
authAzcopyEnv, err = d.authorizeAzcopyWithIdentity()
842843
if err != nil {
843-
return "", fmt.Errorf("get(%s) from azcopySasTokenCache failed with error: %v", accountName, err)
844-
}
845-
if cache != nil {
846-
klog.V(2).Infof("use sas token for account(%s) since this account is found in azcopySasTokenCache", accountName)
847-
useSasToken = true
844+
klog.Warningf("failed to authorize azcopy with identity, error: %v", err)
848845
} else {
849-
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
850-
if testErr != nil {
851-
return "", fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
852-
}
853-
if strings.Contains(out, authorizationPermissionMismatch) {
854-
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)
855-
d.azcopySasTokenCache.Set(accountName, "")
856-
useSasToken = true
846+
if len(authAzcopyEnv) > 0 {
847+
// search in cache first
848+
cache, err := d.azcopySasTokenCache.Get(accountName, azcache.CacheReadTypeDefault)
849+
if err != nil {
850+
return "", fmt.Errorf("get(%s) from azcopySasTokenCache failed with error: %v", accountName, err)
851+
}
852+
if cache != nil {
853+
klog.V(2).Infof("use sas token for account(%s) since this account is found in azcopySasTokenCache", accountName)
854+
useSasToken = true
855+
} else {
856+
out, testErr := d.azcopy.TestListJobs(accountName, storageEndpointSuffix, authAzcopyEnv)
857+
if testErr != nil {
858+
return "", fmt.Errorf("azcopy list command failed with error(%v): %v", testErr, out)
859+
}
860+
if strings.Contains(out, authorizationPermissionMismatch) {
861+
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)
862+
d.azcopySasTokenCache.Set(accountName, "")
863+
useSasToken = true
864+
}
865+
}
857866
}
858867
}
859868
}
860-
if len(secrets) > 0 || len(authAzcopyEnv) == 0 || useSasToken {
869+
870+
if len(secrets) > 0 || len(secretName) > 0 || len(authAzcopyEnv) == 0 || useSasToken {
861871
var err error
862872
if accountKey == "" {
863873
if _, accountKey, err = d.GetStorageAccesskey(ctx, accountOptions, secrets, secretName, secretNamespace); err != nil {

pkg/blob/controllerserver_test.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,7 @@ func TestGetSASToken(t *testing.T) {
20152015
},
20162016
},
20172017
{
2018-
name: "failed to test azcopy list command",
2018+
name: "generate sas token using account key",
20192019
testFunc: func(t *testing.T) {
20202020
d := NewFakeDriver()
20212021
d.cloud = &azure.Cloud{
@@ -2029,21 +2029,10 @@ func TestGetSASToken(t *testing.T) {
20292029
}
20302030
secrets := map[string]string{
20312031
defaultSecretAccountName: "accountName",
2032+
defaultSecretAccountKey: "YWNjb3VudGtleQo=",
20322033
}
2033-
ctrl := gomock.NewController(t)
2034-
defer ctrl.Finish()
2035-
2036-
m := util.NewMockEXEC(ctrl)
2037-
listStr := "error"
2038-
m.EXPECT().RunCommand(gomock.Any(), gomock.Any()).Return(listStr, fmt.Errorf("error"))
2039-
2040-
d.azcopy.ExecCmd = m
2041-
2042-
ctx := context.Background()
2043-
expectedAccountSASToken := ""
2044-
expectedErr := fmt.Errorf("azcopy list command failed with error(%v): %v", fmt.Errorf("error"), "error")
2045-
accountSASToken, err := d.getSASToken(ctx, "accountName", "", "core.windows.net", &azure.AccountOptions{}, secrets, "secretsName", "secretsNamespace")
2046-
if !reflect.DeepEqual(err, expectedErr) || !reflect.DeepEqual(accountSASToken, expectedAccountSASToken) {
2034+
accountSASToken, err := d.getSASToken(context.Background(), "accountName", "", "core.windows.net", &azure.AccountOptions{}, secrets, "secretsName", "secretsNamespace")
2035+
if !reflect.DeepEqual(err, nil) || !strings.Contains(accountSASToken, "?se=") {
20472036
t.Errorf("Unexpected accountSASToken: %s, Unexpected error: %v", accountSASToken, err)
20482037
}
20492038
},
@@ -2065,19 +2054,10 @@ func TestGetSASToken(t *testing.T) {
20652054
defaultSecretAccountName: "accountName",
20662055
defaultSecretAccountKey: "fakeValue",
20672056
}
2068-
ctrl := gomock.NewController(t)
2069-
defer ctrl.Finish()
2070-
2071-
m := util.NewMockEXEC(ctrl)
2072-
listStr := "RESPONSE 403: 403 This request is not authorized to perform this operation using this permission.\nERROR CODE: AuthorizationPermissionMismatch"
2073-
m.EXPECT().RunCommand(gomock.Any(), gomock.Any()).Return(listStr, nil)
2074-
2075-
d.azcopy.ExecCmd = m
20762057

2077-
ctx := context.Background()
20782058
expectedAccountSASToken := ""
20792059
expectedErr := status.Errorf(codes.Internal, fmt.Sprintf("failed to generate sas token in creating new shared key credential, accountName: %s, err: %s", "accountName", "decode account key: illegal base64 data at input byte 8"))
2080-
accountSASToken, err := d.getSASToken(ctx, "accountName", "", "core.windows.net", &azure.AccountOptions{}, secrets, "secretsName", "secretsNamespace")
2060+
accountSASToken, err := d.getSASToken(context.Background(), "accountName", "", "core.windows.net", &azure.AccountOptions{}, secrets, "secretsName", "secretsNamespace")
20812061
if !reflect.DeepEqual(err, expectedErr) || !reflect.DeepEqual(accountSASToken, expectedAccountSASToken) {
20822062
t.Errorf("Unexpected accountSASToken: %s, Unexpected error: %v", accountSASToken, err)
20832063
}

0 commit comments

Comments
 (0)