Skip to content

Commit 345de75

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

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

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: 8 additions & 1 deletion
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"
@@ -161,6 +162,7 @@ const (
161162

162163
DefaultTokenAudience = "api://AzureADTokenExchange" //nolint:gosec // G101 ignore this!
163164

165+
defaultAzureFederatedTokenDir = "/var/lib/kubelet/" + DefaultDriverName
164166
)
165167

166168
var (
@@ -586,12 +588,17 @@ func (d *Driver) GetAuthEnv(ctx context.Context, volumeID, protocol string, attr
586588
if err != nil {
587589
return rgName, accountName, accountKey, containerName, authEnv, err
588590
}
591+
azureFederatedTokenFile := filepath.Join(defaultAzureFederatedTokenDir, clientID)
592+
klog.V(2).Infof("write workload identity token to %s", azureFederatedTokenFile)
593+
if err := os.WriteFile(azureFederatedTokenFile, []byte(workloadIdentityToken), 0644); err != nil {
594+
return rgName, accountName, accountKey, containerName, authEnv, fmt.Errorf("failed to write azure federated token file %s: %v", azureFederatedTokenFile, err)
595+
}
589596

590597
authEnv = append(authEnv, "AZURE_STORAGE_SPN_CLIENT_ID="+clientID)
591598
if tenantID != "" {
592599
authEnv = append(authEnv, "AZURE_STORAGE_SPN_TENANT_ID="+tenantID)
593600
}
594-
authEnv = append(authEnv, "WORKLOAD_IDENTITY_TOKEN="+workloadIdentityToken)
601+
authEnv = append(authEnv, "AZURE_FEDERATED_TOKEN_FILE="+azureFederatedTokenFile)
595602

596603
return rgName, accountName, accountKey, containerName, authEnv, err
597604
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ 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+
_ = makeDir("/var/lib/kubelet/blob.csi.azure.com/")
246248
},
247249
req: &csi.NodePublishVolumeRequest{
248250
VolumeCapability: &csi.VolumeCapability{AccessMode: &volumeCap},

0 commit comments

Comments
 (0)