Skip to content

Commit 3b9d617

Browse files
Add tests to use rotation config.
1 parent b482f3a commit 3b9d617

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

pkg/secrets-store/nodeserver.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,6 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
7676
errorReason := internalerrors.FailedToMount
7777
rotationEnabled := ns.rotationConfig.enabled
7878

79-
if ns.rotationConfig.enabled {
80-
if ns.rotationConfig.nextRotationTime.After(startTime) {
81-
klog.InfoS("Too soon !!!!, will rotate secret after", ns.rotationConfig.nextRotationTime)
82-
return &csi.NodePublishVolumeResponse{}, nil
83-
}
84-
ns.rotationConfig.nextRotationTime = ns.rotationConfig.nextRotationTime.Add(ns.rotationConfig.interval)
85-
}
86-
8779
defer func() {
8880
if err != nil {
8981
// if there is an error at any stage during node publish volume and if the path
@@ -101,6 +93,15 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
10193
ns.reporter.ReportNodePublishCtMetric(ctx, providerName)
10294
}()
10395

96+
if ns.rotationConfig.enabled {
97+
// Node server retries nodepublish volume continuously, so to rotate secret after every `nextRotationTime`,
98+
// nodeserver should skip secret mount till the next `nextRotationTime`
99+
if ns.rotationConfig.nextRotationTime.After(startTime) {
100+
return &csi.NodePublishVolumeResponse{}, nil
101+
}
102+
ns.rotationConfig.nextRotationTime = ns.rotationConfig.nextRotationTime.Add(ns.rotationConfig.interval)
103+
}
104+
104105
// Check arguments
105106
if req.GetVolumeCapability() == nil {
106107
return nil, status.Error(codes.InvalidArgument, "Volume capability missing in request")
@@ -140,6 +141,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis
140141
return nil, status.Errorf(codes.Internal, "failed to check if target path %s is mount point, err: %v", targetPath, err)
141142
}
142143
}
144+
// If rotation is not enabled, don't remount the already mounted secrets.
143145
if !rotationEnabled && mounted {
144146
klog.InfoS("target path is already mounted", "targetPath", targetPath, "pod", klog.ObjectRef{Namespace: podNamespace, Name: podName})
145147
return &csi.NodePublishVolumeResponse{}, nil

pkg/secrets-store/nodeserver_test.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"os"
2222
"path/filepath"
2323
"testing"
24+
"time"
2425

2526
secretsstorev1 "sigs.k8s.io/secrets-store-csi-driver/apis/v1"
2627
"sigs.k8s.io/secrets-store-csi-driver/pkg/secrets-store/mocks"
@@ -267,6 +268,7 @@ func TestNodePublishVolume(t *testing.T) {
267268
name string
268269
nodePublishVolReq *csi.NodePublishVolumeRequest
269270
initObjects []client.Object
271+
rotationConfig *RotationConfig
270272
}{
271273
{
272274
name: "volume mount",
@@ -294,9 +296,14 @@ func TestNodePublishVolume(t *testing.T) {
294296
},
295297
},
296298
},
299+
rotationConfig: &RotationConfig{
300+
enabled: false,
301+
nextRotationTime: time.Now(),
302+
interval: time.Minute,
303+
},
297304
},
298305
{
299-
name: "volume mount with refresh token",
306+
name: "volume mount with refresh token ",
300307
nodePublishVolReq: &csi.NodePublishVolumeRequest{
301308
VolumeCapability: &csi.VolumeCapability{},
302309
VolumeId: "testvolid1",
@@ -324,6 +331,43 @@ func TestNodePublishVolume(t *testing.T) {
324331
},
325332
},
326333
},
334+
rotationConfig: &RotationConfig{
335+
enabled: true,
336+
nextRotationTime: time.Now().Add(-3 * time.Minute), // so that rotation period is passed and secret will be mounted.
337+
interval: time.Minute,
338+
},
339+
},
340+
{
341+
name: "volume mount with rotation but skipped",
342+
nodePublishVolReq: &csi.NodePublishVolumeRequest{
343+
VolumeCapability: &csi.VolumeCapability{},
344+
VolumeId: "testvolid1",
345+
TargetPath: targetPath(t),
346+
VolumeContext: map[string]string{
347+
"secretProviderClass": "provider1",
348+
CSIPodName: "pod1",
349+
CSIPodNamespace: "default",
350+
CSIPodUID: "poduid1",
351+
},
352+
Readonly: true,
353+
},
354+
initObjects: []client.Object{
355+
&secretsstorev1.SecretProviderClass{
356+
ObjectMeta: metav1.ObjectMeta{
357+
Name: "provider1",
358+
Namespace: "default",
359+
},
360+
Spec: secretsstorev1.SecretProviderClassSpec{
361+
Provider: "provider1",
362+
Parameters: map[string]string{"parameter1": "value1"},
363+
},
364+
},
365+
},
366+
rotationConfig: &RotationConfig{
367+
enabled: true,
368+
nextRotationTime: time.Now().Add(2 * time.Minute),
369+
interval: time.Minute,
370+
},
327371
},
328372
}
329373

@@ -338,7 +382,7 @@ func TestNodePublishVolume(t *testing.T) {
338382
t.Run(test.name, func(t *testing.T) {
339383
r := mocks.NewFakeReporter()
340384

341-
ns, err := testNodeServer(t, fake.NewClientBuilder().WithScheme(s).WithObjects(test.initObjects...).Build(), r, &RotationConfig{})
385+
ns, err := testNodeServer(t, fake.NewClientBuilder().WithScheme(s).WithObjects(test.initObjects...).Build(), r, test.rotationConfig)
342386
if err != nil {
343387
t.Fatalf("expected error to be nil, got: %+v", err)
344388
}
@@ -365,8 +409,13 @@ func TestNodePublishVolume(t *testing.T) {
365409
if err != nil {
366410
t.Fatalf("expected err to be nil, got: %v", err)
367411
}
368-
if len(mnts) == 0 {
369-
t.Errorf("expected mounts...: %v", mnts)
412+
expectedMounts := 1
413+
if ns.rotationConfig.enabled && ns.rotationConfig.nextRotationTime.After(time.Now()) {
414+
// If rotation time is not reached, there should not be any mounts.
415+
expectedMounts = 0
416+
}
417+
if len(mnts) != expectedMounts {
418+
t.Errorf("[Number of mounts] want : %d, got mount: %d", expectedMounts, len(mnts))
370419
}
371420
}
372421
})

0 commit comments

Comments
 (0)