Skip to content

Commit 600391f

Browse files
committed
fixup! refactor dbcrypt: add Ciphers to wrap multiple AES256
1 parent 7a64a4e commit 600391f

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

enterprise/cli/server.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,20 @@ func (r *RootCmd) server() *clibase.Cmd {
7272
}
7373

7474
if encKeys := options.DeploymentValues.ExternalTokenEncryptionKeys.Value(); len(encKeys) != 0 {
75-
if len(encKeys) > 2 {
76-
return nil, nil, xerrors.Errorf("at most 2 external-token-encryption-keys may be specified")
77-
}
78-
k1, err := base64.StdEncoding.DecodeString(encKeys[0])
79-
if err != nil {
80-
return nil, nil, xerrors.Errorf("decode external-token-encryption-key: %w", err)
81-
}
82-
o.PrimaryExternalTokenEncryption, err = dbcrypt.CipherAES256(k1)
83-
if err != nil {
84-
return nil, nil, xerrors.Errorf("create external-token-encryption-key cipher: %w", err)
85-
}
86-
if len(encKeys) > 1 {
87-
k2, err := base64.StdEncoding.DecodeString(encKeys[0])
75+
cs := make([]dbcrypt.Cipher, 0, len(encKeys))
76+
for idx, ek := range encKeys {
77+
dk, err := base64.StdEncoding.DecodeString(ek)
8878
if err != nil {
89-
return nil, nil, xerrors.Errorf("decode external-token-encryption-key: %w", err)
79+
return nil, nil, xerrors.Errorf("decode external-token-encryption-key %d: %w", idx, err)
9080
}
91-
o.SecondaryExternalTokenEncryption, err = dbcrypt.CipherAES256(k2)
81+
c, err := dbcrypt.CipherAES256(dk)
9282
if err != nil {
93-
return nil, nil, xerrors.Errorf("create external-token-encryption-key cipher: %w", err)
83+
return nil, nil, xerrors.Errorf("create external-token-encryption-key cipher %d: %w", idx, err)
84+
9485
}
86+
cs = append(cs, c)
9587
}
88+
o.ExternalTokenEncryption = dbcrypt.NewCiphers(cs...)
9689
}
9790

9891
api, err := coderd.New(ctx, o)

enterprise/coderd/coderd.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,8 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
6363

6464
ctx, cancelFunc := context.WithCancel(ctx)
6565

66-
if options.PrimaryExternalTokenEncryption != nil {
67-
cs := make([]dbcrypt.Cipher, 0)
68-
cs = append(cs, options.PrimaryExternalTokenEncryption)
69-
if options.SecondaryExternalTokenEncryption != nil {
70-
cs = append(cs, options.SecondaryExternalTokenEncryption)
71-
}
72-
cryptDB, err := dbcrypt.New(ctx, options.Database, dbcrypt.NewCiphers(cs...))
66+
if options.ExternalTokenEncryption != nil {
67+
cryptDB, err := dbcrypt.New(ctx, options.Database, options.ExternalTokenEncryption)
7368
if err != nil {
7469
cancelFunc()
7570
return nil, xerrors.Errorf("init dbcrypt: %w", err)
@@ -379,9 +374,7 @@ type Options struct {
379374
BrowserOnly bool
380375
SCIMAPIKey []byte
381376

382-
// TODO: wire these up properly
383-
PrimaryExternalTokenEncryption dbcrypt.Cipher
384-
SecondaryExternalTokenEncryption dbcrypt.Cipher
377+
ExternalTokenEncryption *dbcrypt.Ciphers
385378

386379
// Used for high availability.
387380
ReplicaSyncUpdateInterval time.Duration
@@ -449,7 +442,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
449442
codersdk.FeatureHighAvailability: api.DERPServerRelayAddress != "",
450443
codersdk.FeatureMultipleGitAuth: len(api.GitAuthConfigs) > 1,
451444
codersdk.FeatureTemplateRBAC: api.RBAC,
452-
codersdk.FeatureExternalTokenEncryption: api.PrimaryExternalTokenEncryption != nil,
445+
codersdk.FeatureExternalTokenEncryption: api.ExternalTokenEncryption != nil,
453446
codersdk.FeatureExternalProvisionerDaemons: true,
454447
codersdk.FeatureAdvancedTemplateScheduling: true,
455448
// FeatureTemplateRestartRequirement depends on

0 commit comments

Comments
 (0)