@@ -20,6 +20,7 @@ import (
2020 "path/filepath"
2121 "runtime"
2222 "testing"
23+ "time"
2324
2425 "github.com/stretchr/testify/assert"
2526 "github.com/stretchr/testify/require"
@@ -101,21 +102,27 @@ func mustReadSigner(t *testing.T, path string) crypto.Signer {
101102 return signer
102103}
103104
104- func mustCertificate (t * testing.T , path string ) []* x509.Certificate {
105+ func mustCertificate (t * testing.T , path string , opts ... createFuncOption ) []* x509.Certificate {
105106 t .Helper ()
106107
108+ o := new (createOptions )
109+ o .templateModifier = func (c * x509.Certificate ) * x509.Certificate { return c }
110+ for _ , fn := range opts {
111+ fn (o )
112+ }
113+
107114 ca , err := minica .New ()
108115 require .NoError (t , err )
109116
110117 signer , err := keyutil .GenerateDefaultSigner ()
111118 require .NoError (t , err )
112119
113- cert , err := ca .Sign (& x509.Certificate {
120+ cert , err := ca .Sign (o . templateModifier ( & x509.Certificate {
114121 KeyUsage : x509 .KeyUsageDigitalSignature ,
115122 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
116123 PublicKey : signer .Public (),
117124 DNSNames : []string {"example.com" },
118- })
125+ }))
119126 require .NoError (t , err )
120127
121128 if path != "" {
@@ -186,6 +193,7 @@ type createOptions struct {
186193 name string
187194 noCleanup bool
188195 noCleanupCertificate bool
196+ templateModifier func (* x509.Certificate ) * x509.Certificate
189197}
190198
191199type createFuncOption func (* createOptions )
@@ -209,6 +217,12 @@ func withNoCleanupCertificate() createFuncOption {
209217 }
210218}
211219
220+ func withTemplateModifier (fn func (* x509.Certificate ) * x509.Certificate ) createFuncOption {
221+ return func (co * createOptions ) {
222+ co .templateModifier = fn
223+ }
224+ }
225+
212226func mustCreatePlatformKey (t * testing.T , km * KMS , opts ... createFuncOption ) * apiv1.CreateKeyResponse {
213227 t .Helper ()
214228
@@ -242,6 +256,7 @@ func mustCreatePlatformCertificate(t *testing.T, km *KMS, opts ...createFuncOpti
242256 t .Helper ()
243257
244258 o := new (createOptions )
259+ o .templateModifier = func (c * x509.Certificate ) * x509.Certificate { return c }
245260 o .name = platformCertName
246261 for _ , fn := range opts {
247262 fn (o )
@@ -257,12 +272,12 @@ func mustCreatePlatformCertificate(t *testing.T, km *KMS, opts ...createFuncOpti
257272 }
258273
259274 key := mustCreatePlatformKey (t , km , opts ... )
260- cert , err := ca .Sign (& x509.Certificate {
275+ cert , err := ca .Sign (o . templateModifier ( & x509.Certificate {
261276 KeyUsage : x509 .KeyUsageDigitalSignature ,
262277 ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageServerAuth },
263278 PublicKey : key .PublicKey ,
264279 DNSNames : []string {"example.com" },
265- })
280+ }))
266281 require .NoError (t , err )
267282
268283 require .NoError (t , km .StoreCertificateChain (& apiv1.StoreCertificateChainRequest {
@@ -1289,6 +1304,39 @@ func TestKMS_SearchKeys(t *testing.T) {
12891304 }
12901305}
12911306
1307+ func TestKMS_CleanupCredentials (t * testing.T ) {
1308+ dir := t .TempDir ()
1309+ softKMS := mustKMS (t , "kms:backend=softkms" )
1310+ // Create an expired certificate
1311+ chain := mustCertificate (t , filepath .Join (dir , "chain.crt" ), withTemplateModifier (func (c * x509.Certificate ) * x509.Certificate {
1312+ c .NotBefore = time .Now ().Add (- time .Minute ).Truncate (time .Second )
1313+ c .NotAfter = time .Now ().Add (- time .Second ).Truncate (time .Second )
1314+ return c
1315+ }))
1316+
1317+ type args struct {
1318+ req * apiv1.CleanupCredentialsRequest
1319+ }
1320+ tests := []struct {
1321+ name string
1322+ kms * KMS
1323+ args args
1324+ assertion assert.ErrorAssertionFunc
1325+ }{
1326+ {"not implemented" , softKMS , args {& apiv1.CleanupCredentialsRequest {
1327+ Issuer : chain [0 ].Issuer .CommonName ,
1328+ RawSubject : chain [0 ].RawSubject ,
1329+ }}, func (tt assert.TestingT , err error , i ... interface {}) bool {
1330+ return assert .ErrorIs (tt , err , apiv1.NotImplementedError {})
1331+ }},
1332+ }
1333+ for _ , tt := range tests {
1334+ t .Run (tt .name , func (t * testing.T ) {
1335+ tt .assertion (t , tt .kms .CleanupCredentials (tt .args .req ))
1336+ })
1337+ }
1338+ }
1339+
12921340func Test_getBackend (t * testing.T ) {
12931341 type args struct {
12941342 opts apiv1.Options
0 commit comments