|
| 1 | +/* |
| 2 | +Copyright 2022 The cert-manager Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package integration |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "crypto" |
| 22 | + "crypto/x509" |
| 23 | + "errors" |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + "reflect" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 31 | + "google.golang.org/grpc/codes" |
| 32 | + "google.golang.org/grpc/status" |
| 33 | + "k8s.io/apimachinery/pkg/util/wait" |
| 34 | + fakeclock "k8s.io/utils/clock/testing" |
| 35 | + |
| 36 | + "github.com/cert-manager/csi-lib/manager" |
| 37 | + "github.com/cert-manager/csi-lib/metadata" |
| 38 | + "github.com/cert-manager/csi-lib/storage" |
| 39 | + testutil "github.com/cert-manager/csi-lib/test/util" |
| 40 | +) |
| 41 | + |
| 42 | +func Test_CompletesIfNotReadyToRequest_ContinueOnNotReadyEnabled(t *testing.T) { |
| 43 | + store := storage.NewMemoryFS() |
| 44 | + clock := fakeclock.NewFakeClock(time.Now()) |
| 45 | + |
| 46 | + calls := 0 |
| 47 | + opts, cl, stop := testutil.RunTestDriver(t, testutil.DriverOptions{ |
| 48 | + Store: store, |
| 49 | + Clock: clock, |
| 50 | + ContinueOnNotReady: true, |
| 51 | + ReadyToRequest: func(meta metadata.Metadata) (bool, string) { |
| 52 | + if calls < 1 { |
| 53 | + calls++ |
| 54 | + return false, "calls < 1" |
| 55 | + } |
| 56 | + // only indicate we are ready after issuance has been attempted 1 time |
| 57 | + return calls == 1, "calls == 1" |
| 58 | + }, |
| 59 | + GeneratePrivateKey: func(meta metadata.Metadata) (crypto.PrivateKey, error) { |
| 60 | + return nil, nil |
| 61 | + }, |
| 62 | + GenerateRequest: func(meta metadata.Metadata) (*manager.CertificateRequestBundle, error) { |
| 63 | + return &manager.CertificateRequestBundle{ |
| 64 | + Namespace: "certificaterequest-namespace", |
| 65 | + }, nil |
| 66 | + }, |
| 67 | + SignRequest: func(meta metadata.Metadata, key crypto.PrivateKey, request *x509.CertificateRequest) (csr []byte, err error) { |
| 68 | + return []byte{}, nil |
| 69 | + }, |
| 70 | + WriteKeypair: func(meta metadata.Metadata, key crypto.PrivateKey, chain []byte, ca []byte) error { |
| 71 | + store.WriteFiles(meta, map[string][]byte{ |
| 72 | + "ca": ca, |
| 73 | + "cert": chain, |
| 74 | + }) |
| 75 | + nextIssuanceTime := clock.Now().Add(time.Hour) |
| 76 | + meta.NextIssuanceTime = &nextIssuanceTime |
| 77 | + return store.WriteMetadata(meta.VolumeID, meta) |
| 78 | + }, |
| 79 | + }) |
| 80 | + defer stop() |
| 81 | + |
| 82 | + // Setup a routine to issue/sign the request IF it is created |
| 83 | + stopCh := make(chan struct{}) |
| 84 | + go testutil.IssueAllRequests(t, opts.Client, "certificaterequest-namespace", stopCh, []byte("certificate bytes"), []byte("ca bytes")) |
| 85 | + defer close(stopCh) |
| 86 | + |
| 87 | + tmpDir, err := os.MkdirTemp("", "*") |
| 88 | + if err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + defer os.RemoveAll(tmpDir) |
| 92 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
| 93 | + defer cancel() |
| 94 | + _, err = cl.NodePublishVolume(ctx, &csi.NodePublishVolumeRequest{ |
| 95 | + VolumeId: "test-vol", |
| 96 | + VolumeContext: map[string]string{ |
| 97 | + "csi.storage.k8s.io/ephemeral": "true", |
| 98 | + "csi.storage.k8s.io/pod.name": "the-pod-name", |
| 99 | + "csi.storage.k8s.io/pod.namespace": "the-pod-namespace", |
| 100 | + }, |
| 101 | + TargetPath: tmpDir, |
| 102 | + Readonly: true, |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + t.Fatal(err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := wait.PollUntil(time.Second, func() (done bool, err error) { |
| 109 | + files, err := store.ReadFiles("test-vol") |
| 110 | + if errors.Is(err, storage.ErrNotFound) || len(files) <= 1 { |
| 111 | + return false, nil |
| 112 | + } |
| 113 | + if err != nil { |
| 114 | + return false, err |
| 115 | + } |
| 116 | + if !reflect.DeepEqual(files["ca"], []byte("ca bytes")) { |
| 117 | + return false, fmt.Errorf("unexpected CA data: %v", files["ca"]) |
| 118 | + } |
| 119 | + if !reflect.DeepEqual(files["cert"], []byte("certificate bytes")) { |
| 120 | + return false, fmt.Errorf("unexpected certificate data: %v", files["cert"]) |
| 121 | + } |
| 122 | + return true, nil |
| 123 | + }, ctx.Done()); err != nil { |
| 124 | + t.Error(err) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestFailsIfNotReadyToRequest_ContinueOnNotReadyDisabled(t *testing.T) { |
| 129 | + store := storage.NewMemoryFS() |
| 130 | + clock := fakeclock.NewFakeClock(time.Now()) |
| 131 | + |
| 132 | + opts, cl, stop := testutil.RunTestDriver(t, testutil.DriverOptions{ |
| 133 | + Store: store, |
| 134 | + Clock: clock, |
| 135 | + ContinueOnNotReady: false, |
| 136 | + ReadyToRequest: func(meta metadata.Metadata) (bool, string) { |
| 137 | + return false, "never ready" |
| 138 | + }, |
| 139 | + GeneratePrivateKey: func(meta metadata.Metadata) (crypto.PrivateKey, error) { |
| 140 | + return nil, nil |
| 141 | + }, |
| 142 | + GenerateRequest: func(meta metadata.Metadata) (*manager.CertificateRequestBundle, error) { |
| 143 | + return &manager.CertificateRequestBundle{ |
| 144 | + Namespace: "certificaterequest-namespace", |
| 145 | + }, nil |
| 146 | + }, |
| 147 | + SignRequest: func(meta metadata.Metadata, key crypto.PrivateKey, request *x509.CertificateRequest) (csr []byte, err error) { |
| 148 | + return []byte{}, nil |
| 149 | + }, |
| 150 | + WriteKeypair: func(meta metadata.Metadata, key crypto.PrivateKey, chain []byte, ca []byte) error { |
| 151 | + store.WriteFiles(meta, map[string][]byte{ |
| 152 | + "ca": ca, |
| 153 | + "cert": chain, |
| 154 | + }) |
| 155 | + nextIssuanceTime := clock.Now().Add(time.Hour) |
| 156 | + meta.NextIssuanceTime = &nextIssuanceTime |
| 157 | + return store.WriteMetadata(meta.VolumeID, meta) |
| 158 | + }, |
| 159 | + }) |
| 160 | + defer stop() |
| 161 | + |
| 162 | + // Setup a routine to issue/sign the request IF it is created |
| 163 | + stopCh := make(chan struct{}) |
| 164 | + go testutil.IssueAllRequests(t, opts.Client, "certificaterequest-namespace", stopCh, []byte("certificate bytes"), []byte("ca bytes")) |
| 165 | + defer close(stopCh) |
| 166 | + tmpDir, err := os.MkdirTemp("", "*") |
| 167 | + if err != nil { |
| 168 | + t.Fatal(err) |
| 169 | + } |
| 170 | + defer os.RemoveAll(tmpDir) |
| 171 | + ctx, cancel := context.WithTimeout(context.Background(), time.Second) |
| 172 | + defer cancel() |
| 173 | + _, err = cl.NodePublishVolume(ctx, &csi.NodePublishVolumeRequest{ |
| 174 | + VolumeId: "test-vol", |
| 175 | + VolumeContext: map[string]string{ |
| 176 | + "csi.storage.k8s.io/ephemeral": "true", |
| 177 | + "csi.storage.k8s.io/pod.name": "the-pod-name", |
| 178 | + "csi.storage.k8s.io/pod.namespace": "the-pod-namespace", |
| 179 | + }, |
| 180 | + TargetPath: tmpDir, |
| 181 | + Readonly: true, |
| 182 | + }) |
| 183 | + if status.Code(err) != codes.DeadlineExceeded { |
| 184 | + t.Errorf("Expected timeout to be returned from NodePublishVolume but got: %v", err) |
| 185 | + } |
| 186 | + |
| 187 | + // allow 1s for the cleanup functions in NodePublishVolume to be run |
| 188 | + // without this pause, the test can flake due to the storage backend not |
| 189 | + // being cleaned up of the persisted metadata file. |
| 190 | + ctx, cancel2 := context.WithTimeout(context.Background(), time.Second) |
| 191 | + defer cancel2() |
| 192 | + if err := wait.PollUntil(time.Millisecond*100, func() (bool, error) { |
| 193 | + _, err := store.ReadFiles("test-vol") |
| 194 | + if err != storage.ErrNotFound { |
| 195 | + return false, nil |
| 196 | + } |
| 197 | + return true, nil |
| 198 | + }, ctx.Done()); err != nil { |
| 199 | + t.Errorf("failed to wait for storage backend to return NotFound: %v", err) |
| 200 | + } |
| 201 | +} |
0 commit comments