Skip to content

Commit 386c443

Browse files
committed
Rename Keychain to KeyCache for consistency
1 parent f0f32d1 commit 386c443

File tree

6 files changed

+55
-46
lines changed

6 files changed

+55
-46
lines changed

coderd/cryptokeys/dbkeychain.go renamed to coderd/cryptokeys/dbkeycache.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,32 @@ import (
1313
"github.com/coder/quartz"
1414
)
1515

16-
// DBKeychain implements Keychain for callers with access to the database.
17-
type DBKeychain struct {
16+
// DBKeyCache implements KeyCache for callers with access to the database.
17+
type DBKeyCache struct {
18+
Clock quartz.Clock
1819
db database.Store
1920
feature database.CryptoKeyFeature
20-
clock quartz.Clock
2121
logger slog.Logger
2222

23-
// The following are initialized by NewDBKeychain.
23+
// The following are initialized by NewDBKeyCache.
2424
cacheMu sync.RWMutex
2525
cache map[int32]database.CryptoKey
2626
latestKey database.CryptoKey
2727
}
2828

29-
// NewDBKeychain creates a new DBKeychain. It starts a background
29+
// NewDBKeyCache creates a new DBKeyCache. It starts a background
3030
// process that periodically refreshes the cache. The context should
3131
// be canceled to stop the background process.
32-
func NewDBKeychain(ctx context.Context, logger slog.Logger, db database.Store, feature database.CryptoKeyFeature, clock quartz.Clock) (*DBKeychain, error) {
33-
d := &DBKeychain{
32+
func NewDBKeyCache(ctx context.Context, logger slog.Logger, db database.Store, feature database.CryptoKeyFeature, opts ...func(*DBKeyCache)) (*DBKeyCache, error) {
33+
d := &DBKeyCache{
3434
db: db,
3535
feature: feature,
36-
clock: clock,
36+
Clock: quartz.NewReal(),
3737
logger: logger,
3838
}
39+
for _, opt := range opts {
40+
opt(d)
41+
}
3942
err := d.newCache(ctx)
4043
if err != nil {
4144
return nil, xerrors.Errorf("new cache: %w", err)
@@ -47,8 +50,8 @@ func NewDBKeychain(ctx context.Context, logger slog.Logger, db database.Store, f
4750

4851
// Version returns the CryptoKey with the given sequence number, provided that
4952
// it is not deleted or has breached its deletion date.
50-
func (d *DBKeychain) Version(ctx context.Context, sequence int32) (database.CryptoKey, error) {
51-
now := d.clock.Now().UTC()
53+
func (d *DBKeyCache) Version(ctx context.Context, sequence int32) (database.CryptoKey, error) {
54+
now := d.Clock.Now().UTC()
5255
d.cacheMu.RLock()
5356
key, ok := d.cache[sequence]
5457
d.cacheMu.RUnlock()
@@ -91,9 +94,9 @@ func (d *DBKeychain) Version(ctx context.Context, sequence int32) (database.Cryp
9194
return key, nil
9295
}
9396

94-
func (d *DBKeychain) Latest(ctx context.Context) (database.CryptoKey, error) {
97+
func (d *DBKeyCache) Latest(ctx context.Context) (database.CryptoKey, error) {
9598
d.cacheMu.RLock()
96-
now := d.clock.Now().UTC()
99+
now := d.Clock.Now().UTC()
97100
if d.latestKey.IsActive(now) {
98101
d.cacheMu.RUnlock()
99102
return d.latestKey, nil
@@ -115,8 +118,8 @@ func (d *DBKeychain) Latest(ctx context.Context) (database.CryptoKey, error) {
115118
return d.latestKey, nil
116119
}
117120

118-
func (d *DBKeychain) refreshCache(ctx context.Context) {
119-
d.clock.TickerFunc(ctx, time.Minute*10, func() error {
121+
func (d *DBKeyCache) refreshCache(ctx context.Context) {
122+
d.Clock.TickerFunc(ctx, time.Minute*10, func() error {
120123
d.cacheMu.Lock()
121124
defer d.cacheMu.Unlock()
122125
if err := d.newCache(ctx); err != nil {
@@ -126,8 +129,8 @@ func (d *DBKeychain) refreshCache(ctx context.Context) {
126129
})
127130
}
128131

129-
func (d *DBKeychain) newCache(ctx context.Context) error {
130-
now := d.clock.Now().UTC()
132+
func (d *DBKeyCache) newCache(ctx context.Context) error {
133+
now := d.Clock.Now().UTC()
131134
keys, err := d.db.GetCryptoKeysByFeature(ctx, d.feature)
132135
if err != nil {
133136
return xerrors.Errorf("get crypto keys by feature: %w", err)

coderd/cryptokeys/dbkeychain_internal_test.go renamed to coderd/cryptokeys/dbkeycache_internal_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ func Test_Version(t *testing.T) {
4040
32: expectedKey,
4141
}
4242

43-
k := &DBKeychain{
43+
k := &DBKeyCache{
4444
db: mockDB,
4545
feature: database.CryptoKeyFeatureWorkspaceApps,
4646
cache: cache,
47-
clock: clock,
47+
Clock: clock,
4848
}
4949

5050
got, err := k.Version(ctx, 32)
@@ -77,11 +77,11 @@ func Test_Version(t *testing.T) {
7777
Sequence: 33,
7878
}).Return(expectedKey, nil)
7979

80-
k := &DBKeychain{
80+
k := &DBKeyCache{
8181
db: mockDB,
8282
feature: database.CryptoKeyFeatureWorkspaceApps,
8383
cache: map[int32]database.CryptoKey{},
84-
clock: clock,
84+
Clock: clock,
8585
}
8686

8787
got, err := k.Version(ctx, 33)
@@ -115,11 +115,11 @@ func Test_Version(t *testing.T) {
115115
},
116116
}
117117

118-
k := &DBKeychain{
118+
k := &DBKeyCache{
119119
db: mockDB,
120120
feature: database.CryptoKeyFeatureWorkspaceApps,
121121
cache: cache,
122-
clock: clock,
122+
Clock: clock,
123123
}
124124

125125
_, err := k.Version(ctx, 32)
@@ -153,11 +153,11 @@ func Test_Version(t *testing.T) {
153153
Sequence: 32,
154154
}).Return(invalidKey, nil)
155155

156-
k := &DBKeychain{
156+
k := &DBKeyCache{
157157
db: mockDB,
158158
feature: database.CryptoKeyFeatureWorkspaceApps,
159159
cache: map[int32]database.CryptoKey{},
160-
clock: clock,
160+
Clock: clock,
161161
}
162162

163163
_, err := k.Version(ctx, 32)
@@ -187,10 +187,10 @@ func Test_Latest(t *testing.T) {
187187
},
188188
StartsAt: clock.Now().UTC(),
189189
}
190-
k := &DBKeychain{
190+
k := &DBKeyCache{
191191
db: mockDB,
192192
feature: database.CryptoKeyFeatureWorkspaceApps,
193-
clock: clock,
193+
Clock: clock,
194194
latestKey: latestKey,
195195
}
196196

@@ -221,10 +221,10 @@ func Test_Latest(t *testing.T) {
221221

222222
mockDB.EXPECT().GetCryptoKeysByFeature(ctx, database.CryptoKeyFeatureWorkspaceApps).Return([]database.CryptoKey{latestKey}, nil)
223223

224-
k := &DBKeychain{
224+
k := &DBKeyCache{
225225
db: mockDB,
226226
feature: database.CryptoKeyFeatureWorkspaceApps,
227-
clock: clock,
227+
Clock: clock,
228228
latestKey: database.CryptoKey{
229229
Feature: database.CryptoKeyFeatureWorkspaceApps,
230230
Sequence: 32,
@@ -277,10 +277,10 @@ func Test_Latest(t *testing.T) {
277277

278278
mockDB.EXPECT().GetCryptoKeysByFeature(ctx, database.CryptoKeyFeatureWorkspaceApps).Return([]database.CryptoKey{inactiveKey, activeKey}, nil)
279279

280-
k := &DBKeychain{
280+
k := &DBKeyCache{
281281
db: mockDB,
282282
feature: database.CryptoKeyFeatureWorkspaceApps,
283-
clock: clock,
283+
Clock: clock,
284284
cache: map[int32]database.CryptoKey{},
285285
}
286286

@@ -325,10 +325,10 @@ func Test_Latest(t *testing.T) {
325325

326326
mockDB.EXPECT().GetCryptoKeysByFeature(ctx, database.CryptoKeyFeatureWorkspaceApps).Return([]database.CryptoKey{inactiveKey, invalidKey}, nil)
327327

328-
k := &DBKeychain{
328+
k := &DBKeyCache{
329329
db: mockDB,
330330
feature: database.CryptoKeyFeatureWorkspaceApps,
331-
clock: clock,
331+
Clock: clock,
332332
cache: map[int32]database.CryptoKey{},
333333
}
334334

coderd/cryptokeys/dbkeychain_test.go renamed to coderd/cryptokeys/dbkeycache_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import (
99

1010
"cdr.dev/slog/sloggers/slogtest"
1111

12+
"github.com/coder/coder/v2/coderd/cryptokeys"
1213
"github.com/coder/coder/v2/coderd/database"
1314
"github.com/coder/coder/v2/coderd/database/dbgen"
1415
"github.com/coder/coder/v2/coderd/database/dbtestutil"
15-
"github.com/coder/coder/v2/coderd/cryptokeys"
1616
"github.com/coder/coder/v2/testutil"
1717
"github.com/coder/quartz"
1818
)
1919

20-
func TestDBKeychain(t *testing.T) {
20+
func TestDBKeyCache(t *testing.T) {
2121
t.Parallel()
2222

2323
t.Run("NoKeys", func(t *testing.T) {
@@ -30,7 +30,7 @@ func TestDBKeychain(t *testing.T) {
3030
logger = slogtest.Make(t, nil)
3131
)
3232

33-
_, err := cryptokeys.NewDBKeychain(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, clock)
33+
_, err := cryptokeys.NewDBKeyCache(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, withClock(clock))
3434
require.ErrorIs(t, err, cryptokeys.ErrKeyNotFound)
3535
})
3636

@@ -57,7 +57,7 @@ func TestDBKeychain(t *testing.T) {
5757
StartsAt: clock.Now().UTC(),
5858
})
5959

60-
k, err := cryptokeys.NewDBKeychain(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, clock)
60+
k, err := cryptokeys.NewDBKeyCache(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, withClock(clock))
6161
require.NoError(t, err)
6262

6363
got, err := k.Version(ctx, key.Sequence)
@@ -85,7 +85,7 @@ func TestDBKeychain(t *testing.T) {
8585
StartsAt: clock.Now().UTC(),
8686
})
8787

88-
k, err := cryptokeys.NewDBKeychain(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, clock)
88+
k, err := cryptokeys.NewDBKeyCache(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, withClock(clock))
8989
require.NoError(t, err)
9090

9191
key := dbgen.CryptoKey(t, db, database.CryptoKey{
@@ -132,7 +132,7 @@ func TestDBKeychain(t *testing.T) {
132132
StartsAt: clock.Now().UTC(),
133133
})
134134

135-
k, err := cryptokeys.NewDBKeychain(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, clock)
135+
k, err := cryptokeys.NewDBKeyCache(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, withClock(clock))
136136
require.NoError(t, err)
137137

138138
got, err := k.Latest(ctx)
@@ -169,7 +169,7 @@ func TestDBKeychain(t *testing.T) {
169169
},
170170
})
171171
trap := clock.Trap().TickerFunc()
172-
k, err := cryptokeys.NewDBKeychain(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, clock)
172+
k, err := cryptokeys.NewDBKeyCache(ctx, logger, db, database.CryptoKeyFeatureWorkspaceApps, withClock(clock))
173173
require.NoError(t, err)
174174

175175
// Should be able to fetch the expiring key since it's still valid.
@@ -208,3 +208,9 @@ func TestDBKeychain(t *testing.T) {
208208
require.ErrorIs(t, err, cryptokeys.ErrKeyNotFound)
209209
})
210210
}
211+
212+
func withClock(clock quartz.Clock) func(*cryptokeys.DBKeyCache) {
213+
return func(d *cryptokeys.DBKeyCache) {
214+
d.Clock = clock
215+
}
216+
}

coderd/cryptokeys/keychain.go renamed to coderd/cryptokeys/keycache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var ErrKeyNotFound = xerrors.New("key not found")
1010

1111
var ErrKeyInvalid = xerrors.New("key is invalid for use")
1212

13-
// Keychain provides an abstraction for fetching signing keys.
14-
type Keychain interface {
13+
// Keycache provides an abstraction for fetching signing keys.
14+
type Keycache interface {
1515
Latest(ctx context.Context) ([]byte, error)
1616
Version(ctx context.Context, sequence int32) ([]byte, error)
1717
}

coderd/cryptokeys/rotate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ type rotator struct {
3636
features []database.CryptoKeyFeature
3737
}
3838

39-
type Option func(*rotator)
39+
type RotatorOption func(*rotator)
4040

41-
func WithClock(clock quartz.Clock) Option {
41+
func WithClock(clock quartz.Clock) RotatorOption {
4242
return func(r *rotator) {
4343
r.clock = clock
4444
}
4545
}
4646

47-
func WithKeyDuration(keyDuration time.Duration) Option {
47+
func WithKeyDuration(keyDuration time.Duration) RotatorOption {
4848
return func(r *rotator) {
4949
r.keyDuration = keyDuration
5050
}
@@ -53,7 +53,7 @@ func WithKeyDuration(keyDuration time.Duration) Option {
5353
// StartRotator starts a background process that rotates keys in the database.
5454
// It ensures there's at least one valid key per feature prior to returning.
5555
// Canceling the provided context will stop the background process.
56-
func StartRotator(ctx context.Context, logger slog.Logger, db database.Store, opts ...Option) error {
56+
func StartRotator(ctx context.Context, logger slog.Logger, db database.Store, opts ...RotatorOption) error {
5757
kr := &rotator{
5858
db: db,
5959
logger: logger,

coderd/cryptokeys/rotate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"cdr.dev/slog"
1010
"cdr.dev/slog/sloggers/slogtest"
1111

12+
"github.com/coder/coder/v2/coderd/cryptokeys"
1213
"github.com/coder/coder/v2/coderd/database"
1314
"github.com/coder/coder/v2/coderd/database/dbgen"
1415
"github.com/coder/coder/v2/coderd/database/dbtestutil"
15-
"github.com/coder/coder/v2/coderd/cryptokeys"
1616
"github.com/coder/coder/v2/testutil"
1717
"github.com/coder/quartz"
1818
)

0 commit comments

Comments
 (0)