Skip to content

Commit 0864993

Browse files
committed
fix: Workload Identity token refresh issue
fix fix fix fix fix
1 parent 1ec31c1 commit 0864993

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
lines changed
33 Bytes
Binary file not shown.

charts/latest/blob-csi-driver/templates/csi-blob-driver.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ spec:
1212
volumeLifecycleModes:
1313
- Persistent
1414
- Ephemeral
15+
requiresRepublish: {{ .Values.feature.requiresRepublish }}
1516
tokenRequests:
1617
- audience: api://AzureADTokenExchange
18+
expirationSeconds: 3600

charts/latest/blob-csi-driver/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ node:
165165

166166
feature:
167167
fsGroupPolicy: ReadWriteOnceWithFSType
168+
requiresRepublish: true
168169
enableGetVolumeStats: false
169170

170171
driver:

deploy/csi-blob-driver.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ spec:
1010
volumeLifecycleModes:
1111
- Persistent
1212
- Ephemeral
13+
requiresRepublish: true
1314
tokenRequests:
1415
- audience: api://AzureADTokenExchange
16+
expirationSeconds: 3600

pkg/blob/blob.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"flag"
2424
"fmt"
2525
"os"
26+
"path/filepath"
2627
"strconv"
2728
"strings"
2829
"sync"
@@ -160,7 +161,6 @@ const (
160161
tagValueDelimiterField = "tagvaluedelimiter"
161162

162163
DefaultTokenAudience = "api://AzureADTokenExchange" //nolint:gosec // G101 ignore this!
163-
164164
)
165165

166166
var (
@@ -170,7 +170,8 @@ var (
170170

171171
// azcopyCloneVolumeOptions used in volume cloning between different storage account and --check-length to false because volume data may be in changing state, copy volume is not same as current source volume,
172172
// set --s2s-preserve-access-tier=false to avoid BlobAccessTierNotSupportedForAccountType error in azcopy
173-
azcopyCloneVolumeOptions = []string{"--recursive", "--check-length=false", "--s2s-preserve-access-tier=false", "--log-level=ERROR"}
173+
azcopyCloneVolumeOptions = []string{"--recursive", "--check-length=false", "--s2s-preserve-access-tier=false", "--log-level=ERROR"}
174+
defaultAzureOAuthTokenDir = "/var/lib/kubelet/plugins/" + DefaultDriverName
174175
)
175176

176177
// DriverOptions defines driver parameters specified in driver deployment
@@ -586,13 +587,17 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
586587
if err != nil {
587588
return rgName, accountName, accountKey, containerName, authEnv, err
588589
}
590+
azureOAuthTokenFile := filepath.Join(defaultAzureOAuthTokenDir, clientID+accountName)
591+
if err := os.WriteFile(azureOAuthTokenFile, []byte(workloadIdentityToken), 0600); err != nil {
592+
return rgName, accountName, accountKey, containerName, authEnv, fmt.Errorf("failed to write workload identity token file %s: %v", azureOAuthTokenFile, err)
593+
}
589594

590595
authEnv = append(authEnv, "AZURE_STORAGE_SPN_CLIENT_ID="+clientID)
591596
if tenantID != "" {
592597
authEnv = append(authEnv, "AZURE_STORAGE_SPN_TENANT_ID="+tenantID)
593598
}
594-
authEnv = append(authEnv, "WORKLOAD_IDENTITY_TOKEN="+workloadIdentityToken)
595-
599+
authEnv = append(authEnv, "AZURE_OAUTH_TOKEN_FILE="+azureOAuthTokenFile)
600+
klog.V(2).Infof("workload identity auth: %v", authEnv)
596601
return rgName, accountName, accountKey, containerName, authEnv, err
597602
}
598603
klog.V(2).Infof("clientID(%s) is specified, use service account token to get account key", clientID)

pkg/blob/nodeserver.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,17 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe
327327
if err != nil {
328328
return nil, status.Errorf(codes.Internal, "Could not mount target %q: %v", targetPath, err)
329329
}
330-
if mnt {
331-
klog.V(2).Infof("NodeStageVolume: volume %s is already mounted on %s", volumeID, targetPath)
332-
return &csi.NodeStageVolumeResponse{}, nil
333-
}
334330

335331
_, accountName, _, containerName, authEnv, err := d.GetAuthEnv(ctx, volumeID, protocol, attrib, secrets)
336-
if err != nil {
332+
if err != nil && !mnt {
337333
return nil, status.Errorf(codes.Internal, "%v", err)
338334
}
339335

336+
if mnt {
337+
klog.V(2).Infof("NodeStageVolume: volume %s is already mounted on %s", volumeID, targetPath)
338+
return &csi.NodeStageVolumeResponse{}, nil
339+
}
340+
340341
// replace pv/pvc name namespace metadata in subDir
341342
containerName = replaceWithMap(containerName, containerNameReplaceMap)
342343

pkg/blob/nodeserver_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ func TestNodePublishVolume(t *testing.T) {
243243
// Mock NodeStageVolume to return success
244244
d.cloud.ResourceGroup = "rg"
245245
d.enableBlobMockMount = true
246+
// Create the directory for token file
247+
defaultAzureOAuthTokenDir = "./blob.csi.azure.com/"
248+
_ = makeDir(defaultAzureOAuthTokenDir)
249+
},
250+
cleanup: func(_ *Driver) {
251+
_ = os.RemoveAll(defaultAzureOAuthTokenDir)
246252
},
247253
req: &csi.NodePublishVolumeRequest{
248254
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},
@@ -253,6 +259,7 @@ func TestNodePublishVolume(t *testing.T) {
253259
serviceAccountTokenField: `{"api://AzureADTokenExchange":{"token":"test-token","expirationTimestamp":"2023-01-01T00:00:00Z"}}`,
254260
mountWithWITokenField: "true",
255261
clientIDField: "client-id-value",
262+
storageAccountNameField: "test-account",
256263
},
257264
},
258265
expectedErr: nil,

0 commit comments

Comments
 (0)