@@ -13,7 +13,6 @@ import (
13
13
14
14
"cdr.dev/slog"
15
15
"github.com/coder/coder/v2/coderd/database"
16
- "github.com/coder/coder/v2/coderd/database/db2sdk"
17
16
"github.com/coder/coder/v2/coderd/database/dbauthz"
18
17
"github.com/coder/coder/v2/codersdk"
19
18
"github.com/coder/quartz"
@@ -73,7 +72,7 @@ func (d *DBFetcher) Fetch(ctx context.Context, feature codersdk.CryptoKeyFeature
73
72
return nil , xerrors .Errorf ("get crypto keys by feature: %w" , err )
74
73
}
75
74
76
- return db2sdk . CryptoKeys (keys ), nil
75
+ return toSDKKeys (keys ), nil
77
76
}
78
77
79
78
// cache implements the caching functionality for both signing and encryption keys.
@@ -378,3 +377,54 @@ func (c *cache) Close() error {
378
377
379
378
return nil
380
379
}
380
+
381
+ // StaticKey fulfills the SigningKeycache and EncryptionKeycache interfaces. Useful for testing.
382
+ type StaticKey struct {
383
+ ID string
384
+ Key interface {}
385
+ }
386
+
387
+ func (s StaticKey ) SigningKey (_ context.Context ) (string , interface {}, error ) {
388
+ return s .ID , s .Key , nil
389
+ }
390
+
391
+ func (s StaticKey ) VerifyingKey (_ context.Context , id string ) (interface {}, error ) {
392
+ if id != s .ID {
393
+ return nil , xerrors .Errorf ("invalid id %q" , id )
394
+ }
395
+ return s .Key , nil
396
+ }
397
+
398
+ func (s StaticKey ) EncryptingKey (_ context.Context ) (string , interface {}, error ) {
399
+ return s .ID , s .Key , nil
400
+ }
401
+
402
+ func (s StaticKey ) DecryptingKey (_ context.Context , id string ) (interface {}, error ) {
403
+ if id != s .ID {
404
+ return nil , xerrors .Errorf ("invalid id %q" , id )
405
+ }
406
+ return s .Key , nil
407
+ }
408
+
409
+ func (s StaticKey ) Close () error {
410
+ return nil
411
+ }
412
+
413
+ // We have to do this to avoid a circular dependency on db2sdk (cryptokeys -> db2sdk -> tailnet -> cryptokeys)
414
+ func toSDKKeys (keys []database.CryptoKey ) []codersdk.CryptoKey {
415
+ into := make ([]codersdk.CryptoKey , 0 , len (keys ))
416
+ for _ , key := range keys {
417
+ into = append (into , toSDK (key ))
418
+ }
419
+ return into
420
+ }
421
+
422
+ func toSDK (key database.CryptoKey ) codersdk.CryptoKey {
423
+ return codersdk.CryptoKey {
424
+ Feature : codersdk .CryptoKeyFeature (key .Feature ),
425
+ Sequence : key .Sequence ,
426
+ StartsAt : key .StartsAt ,
427
+ DeletesAt : key .DeletesAt .Time ,
428
+ Secret : key .Secret .String ,
429
+ }
430
+ }
0 commit comments