Skip to content

Commit 128ad09

Browse files
committed
handle sentinel mismatch with a specific message
1 parent ad44e1e commit 128ad09

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

enterprise/coderd/coderd.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
6767
cryptDB, err := dbcrypt.New(ctx, options.Database, options.ExternalTokenEncryption)
6868
if err != nil {
6969
cancelFunc()
70+
if xerrors.Is(err, dbcrypt.ErrSentinelMismatch) {
71+
panic(`Coder has shut down to prevent data corruption: your configured database is encrypted with an unknown external token encryption key. Please check your configuration and try again.`)
72+
}
7073
return nil, xerrors.Errorf("init dbcrypt: %w", err)
7174
}
7275
options.Database = cryptDB

enterprise/dbcrypt/dbcrypt.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ const MagicPrefix = "dbcrypt-"
5252
const sentinelValue = "coder"
5353

5454
var (
55-
ErrNotEnabled = xerrors.New("encryption is not enabled")
56-
b64encode = base64.StdEncoding.EncodeToString
57-
b64decode = base64.StdEncoding.DecodeString
55+
ErrNotEnabled = xerrors.New("encryption is not enabled")
56+
ErrSentinelMismatch = xerrors.New("database is already encrypted under a different key")
57+
b64encode = base64.StdEncoding.EncodeToString
58+
b64decode = base64.StdEncoding.DecodeString
5859
)
5960

6061
// DecryptFailedError is returned when decryption fails.
@@ -266,7 +267,7 @@ func ensureEncrypted(ctx context.Context, dbc *dbCrypt) error {
266267
}
267268

268269
if val != "" && val != sentinelValue {
269-
return xerrors.Errorf("database is already encrypted with a different key")
270+
return ErrSentinelMismatch
270271
}
271272

272273
// Mark the database as officially having been touched by the new cipher.

enterprise/dbcrypt/dbcrypt_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ func TestNew(t *testing.T) {
208208
// When: we init the crypt db with no access to the old cipher
209209
cipher2 := initCipher(t)
210210
_, err = dbcrypt.New(ctx, rawDB, dbcrypt.NewCiphers(cipher2))
211-
// Then: an error is returned
212-
require.ErrorContains(t, err, "database is already encrypted with a different key")
211+
// Then: a special error is returned
212+
require.ErrorIs(t, err, dbcrypt.ErrSentinelMismatch)
213213

214214
// And the sentinel value should remain unchanged. For now.
215215
rawVal, err := rawDB.GetDBCryptSentinelValue(ctx)

0 commit comments

Comments
 (0)