Skip to content

Commit 34a77d5

Browse files
committed
Refactor key cache initialization and retrieval
- Simplify error handling by removing redundant checks in `newCache`. - Ensure `Latest` returns appropriate errors for key cache state. - Update tests to align with new error handling logic.
1 parent a8e7140 commit 34a77d5

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

coderd/cryptokeys/dbkeycache.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func NewDBKeyCache(ctx context.Context, logger slog.Logger, db database.Store, f
3939
for _, opt := range opts {
4040
opt(d)
4141
}
42+
4243
err := d.newCache(ctx)
4344
if err != nil {
4445
return nil, xerrors.Errorf("new cache: %w", err)
@@ -115,6 +116,14 @@ func (d *DBKeyCache) Latest(ctx context.Context) (database.CryptoKey, error) {
115116
return database.CryptoKey{}, xerrors.Errorf("new cache: %w", err)
116117
}
117118

119+
if len(d.cache) == 0 {
120+
return database.CryptoKey{}, ErrKeyNotFound
121+
}
122+
123+
if !d.latestKey.IsActive(now) {
124+
return database.CryptoKey{}, ErrKeyInvalid
125+
}
126+
118127
return d.latestKey, nil
119128
}
120129

@@ -135,12 +144,7 @@ func (d *DBKeyCache) newCache(ctx context.Context) error {
135144
if err != nil {
136145
return xerrors.Errorf("get crypto keys by feature: %w", err)
137146
}
138-
if len(keys) == 0 {
139-
return ErrKeyNotFound
140-
}
141-
142147
cache := toMap(keys)
143-
144148
var latest database.CryptoKey
145149
for _, key := range keys {
146150
if !key.IsActive(now) {
@@ -150,10 +154,6 @@ func (d *DBKeyCache) newCache(ctx context.Context) error {
150154
break
151155
}
152156

153-
if latest.IsInvalid(now) {
154-
return ErrKeyInvalid
155-
}
156-
157157
d.cache = cache
158158
d.latestKey = latest
159159
return nil

coderd/cryptokeys/dbkeycache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDBKeyCache(t *testing.T) {
3131
)
3232

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

3737
t.Run("Version", func(t *testing.T) {

0 commit comments

Comments
 (0)