Skip to content

Commit 3dde49f

Browse files
committed
fix: only read secret for inline volume
1 parent 3717a7e commit 3dde49f

File tree

3 files changed

+61
-43
lines changed

3 files changed

+61
-43
lines changed

pkg/blob/blob.go

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,37 @@ import (
4141

4242
const (
4343
// DriverName holds the name of the csi-driver
44-
DriverName = "blob.csi.azure.com"
45-
blobCSIDriverName = "blob_csi_driver"
46-
separator = "#"
47-
volumeIDTemplate = "%s#%s#%s"
48-
secretNameTemplate = "azure-storage-account-%s-secret"
49-
serverNameField = "server"
50-
storageEndpointSuffixField = "storageendpointsuffix"
51-
tagsField = "tags"
52-
protocolField = "protocol"
53-
accountNameField = "accountname"
54-
accountKeyField = "accountkey"
55-
storageAccountField = "storageaccount"
56-
storageAccountTypeField = "storageaccounttype"
57-
skuNameField = "skuname"
58-
resourceGroupField = "resourcegroup"
59-
locationField = "location"
60-
secretNameField = "secretname"
61-
secretNamespaceField = "secretnamespace"
62-
containerNameField = "containername"
63-
storeAccountKeyField = "storeaccountkey"
64-
keyVaultURLField = "keyvaulturl"
65-
keyVaultSecretNameField = "keyvaultsecretname"
66-
keyVaultSecretVersionField = "keyvaultsecretversion"
67-
storageAccountNameField = "storageaccountname"
68-
storeAccountKeyFalse = "false"
69-
defaultSecretAccountName = "azurestorageaccountname"
70-
defaultSecretAccountKey = "azurestorageaccountkey"
71-
fuse = "fuse"
72-
nfs = "nfs"
44+
DriverName = "blob.csi.azure.com"
45+
blobCSIDriverName = "blob_csi_driver"
46+
separator = "#"
47+
volumeIDTemplate = "%s#%s#%s"
48+
secretNameTemplate = "azure-storage-account-%s-secret"
49+
serverNameField = "server"
50+
storageEndpointSuffixField = "storageendpointsuffix"
51+
tagsField = "tags"
52+
protocolField = "protocol"
53+
accountNameField = "accountname"
54+
accountKeyField = "accountkey"
55+
storageAccountField = "storageaccount"
56+
storageAccountTypeField = "storageaccounttype"
57+
skuNameField = "skuname"
58+
resourceGroupField = "resourcegroup"
59+
locationField = "location"
60+
secretNameField = "secretname"
61+
secretNamespaceField = "secretnamespace"
62+
containerNameField = "containername"
63+
storeAccountKeyField = "storeaccountkey"
64+
getAccountKeyFromSecretField = "getaccountkeyfromsecret"
65+
keyVaultURLField = "keyvaulturl"
66+
keyVaultSecretNameField = "keyvaultsecretname"
67+
keyVaultSecretVersionField = "keyvaultsecretversion"
68+
storageAccountNameField = "storageaccountname"
69+
falseValue = "false"
70+
trueValue = "true"
71+
defaultSecretAccountName = "azurestorageaccountname"
72+
defaultSecretAccountKey = "azurestorageaccountkey"
73+
fuse = "fuse"
74+
nfs = "nfs"
7375

7476
// See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata#container-names
7577
containerNameMinLength = 3
@@ -238,14 +240,15 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
238240
}
239241

240242
var (
241-
accountKey string
242-
accountSasToken string
243-
secretName string
244-
secretNamespace string
245-
keyVaultURL string
246-
keyVaultSecretName string
247-
keyVaultSecretVersion string
248-
authEnv []string
243+
accountKey string
244+
accountSasToken string
245+
secretName string
246+
secretNamespace string
247+
keyVaultURL string
248+
keyVaultSecretName string
249+
keyVaultSecretVersion string
250+
authEnv []string
251+
getAccountKeyFromSecret bool
249252
)
250253

251254
for k, v := range attrib {
@@ -266,6 +269,10 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
266269
secretName = v
267270
case secretNamespaceField:
268271
secretNamespace = v
272+
case getAccountKeyFromSecretField:
273+
if v == trueValue {
274+
getAccountKeyFromSecret = true
275+
}
269276
case "azurestorageauthtype":
270277
authEnv = append(authEnv, "AZURE_STORAGE_AUTH_TYPE="+v)
271278
case "azurestorageidentityclientid":
@@ -314,7 +321,10 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
314321
// read from k8s secret first
315322
var name string
316323
name, accountKey, err = d.GetStorageAccountFromSecret(secretName, secretNamespace)
317-
if err != nil {
324+
if name != "" {
325+
accountName = name
326+
}
327+
if err != nil && !getAccountKeyFromSecret {
318328
klog.V(2).Infof("could not get account(%s) key from secret, error: %v, use cluster identity to get account key instead", accountName, err)
319329
if rgName == "" {
320330
rgName = d.cloud.ResourceGroup

pkg/blob/controllerserver.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
6565
if parameters == nil {
6666
parameters = make(map[string]string)
6767
}
68-
var storageAccountType, resourceGroup, location, account, containerName, protocol, customTags, storeAccountKey, secretNamespace string
68+
var storageAccountType, resourceGroup, location, account, containerName, protocol, customTags, secretNamespace string
69+
70+
// store account key to k8s secret by default
71+
storeAccountKey := true
6972

7073
// Apply ProvisionerParameters (case-insensitive). We leave validation of
7174
// the values to the cloud provider.
@@ -90,7 +93,9 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
9093
case secretNamespaceField:
9194
secretNamespace = v
9295
case storeAccountKeyField:
93-
storeAccountKey = v
96+
if v == falseValue {
97+
storeAccountKey = false
98+
}
9499
case pvcNamespaceKey:
95100
if secretNamespace == "" {
96101
// respect `secretNamespace` field as first priority
@@ -136,7 +141,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
136141
return nil, status.Errorf(codes.Internal, "update service endpoints failed with error: %v", err)
137142
}
138143
// NFS protocol does not need account key
139-
storeAccountKey = storeAccountKeyFalse
144+
storeAccountKey = false
140145
}
141146

142147
if strings.HasPrefix(strings.ToLower(storageAccountType), "premium") {
@@ -218,7 +223,7 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest)
218223
return nil, fmt.Errorf("failed to create container(%s) on account(%s) type(%s) rg(%s) location(%s) size(%d), error: %v", validContainerName, accountName, storageAccountType, resourceGroup, location, requestGiB, err)
219224
}
220225

221-
if storeAccountKey != storeAccountKeyFalse && len(req.GetSecrets()) == 0 {
226+
if storeAccountKey && len(req.GetSecrets()) == 0 {
222227
secretName, err := setAzureCredentials(d.cloud.KubeClient, accountName, accountKey, secretNamespace)
223228
if err != nil {
224229
return nil, status.Errorf(codes.Internal, "failed to store storage account key: %v", err)

pkg/blob/nodeserver.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
6969
}
7070

7171
context := req.GetVolumeContext()
72-
if context != nil && context["csi.storage.k8s.io/ephemeral"] == "true" {
72+
if context != nil && context["csi.storage.k8s.io/ephemeral"] == trueValue {
7373
context[secretNamespaceField] = context["csi.storage.k8s.io/pod.namespace"]
74+
// only get storage account from secret
75+
context[getAccountKeyFromSecretField] = trueValue
76+
context[storageAccountField] = ""
7477
klog.V(2).Infof("NodePublishVolume: ephemeral volume(%s) mount on %s, VolumeContext: %v", volumeID, target, context)
7578
_, err := d.NodeStageVolume(ctx, &csi.NodeStageVolumeRequest{
7679
StagingTargetPath: target,

0 commit comments

Comments
 (0)