Repository navigation
fix(coderd): ensure that clearing invalid oauth refresh tokens works with dbcrypt - #15721
Merged
Merged
Conversation
johnstcn
commented
Dec 2, 2024
Comment on lines
+57
to
+58
| -- Required for sqlc to generate a parameter for the oauth_refresh_token_key_id | ||
| @oauth_refresh_token_key_id :: text = @oauth_refresh_token_key_id :: text; |
Member
Author
There was a problem hiding this comment.
self-review: this is yuck. We don't actually need this parameter in the query but we need in the params for dbcrypt. This is the 'best' way I could find to set it.
Contributor
There was a problem hiding this comment.
I feel pretty numb now to these kinda sqlc hacks now it doesn't phase me 🚶
johnstcn
commented
Dec 2, 2024
Comment on lines
+95
to
+125
| // If you're looking here, you're probably in trouble. | ||
| // Here's what you need to do: | ||
| // 1. Get the current CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS environment variable. | ||
| // 2. Run the following command: | ||
| // ENCRYPT_ME="<value to encrypt>" CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS="<secret keys here>" go test -v -count=1 ./enterprise/dbcrypt -test.run='^TestHelpMeEncryptSomeValue$' | ||
| // 3. Copy the value from the test output and do what you need with it. | ||
| func TestHelpMeEncryptSomeValue(t *testing.T) { | ||
| t.Parallel() | ||
| t.Skip("this only exists if you need to encrypt a value with dbcrypt, it does not actually test anything") | ||
|
|
||
| valueToEncrypt := os.Getenv("ENCRYPT_ME") | ||
| t.Logf("valueToEncrypt: %q", valueToEncrypt) | ||
| keys := os.Getenv("CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS") | ||
| require.NotEmpty(t, keys, "Set the CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS environment variable to use this") | ||
|
|
||
| base64Keys := strings.Split(keys, ",") | ||
| activeKey := base64Keys[0] | ||
|
|
||
| decodedKey, err := base64.StdEncoding.DecodeString(activeKey) | ||
| require.NoError(t, err, "the active key should be valid base64") | ||
|
|
||
| cipher, err := cipherAES256(decodedKey) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Logf("cipher digest: %+v", cipher.HexDigest()) | ||
|
|
||
| encryptedEmptyString, err := cipher.Encrypt([]byte(valueToEncrypt)) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Logf("encrypted and base64-encoded: %q", base64.StdEncoding.EncodeToString(encryptedEmptyString)) | ||
| } |
Member
Author
There was a problem hiding this comment.
self-review: we could potentially make this a proper CLI function for use in a pinch.
Contributor
There was a problem hiding this comment.
Seems pretty reasonable to leave as a test for now
coadler
approved these changes
Dec 3, 2024
Comment on lines
+95
to
+125
| // If you're looking here, you're probably in trouble. | ||
| // Here's what you need to do: | ||
| // 1. Get the current CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS environment variable. | ||
| // 2. Run the following command: | ||
| // ENCRYPT_ME="<value to encrypt>" CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS="<secret keys here>" go test -v -count=1 ./enterprise/dbcrypt -test.run='^TestHelpMeEncryptSomeValue$' | ||
| // 3. Copy the value from the test output and do what you need with it. | ||
| func TestHelpMeEncryptSomeValue(t *testing.T) { | ||
| t.Parallel() | ||
| t.Skip("this only exists if you need to encrypt a value with dbcrypt, it does not actually test anything") | ||
|
|
||
| valueToEncrypt := os.Getenv("ENCRYPT_ME") | ||
| t.Logf("valueToEncrypt: %q", valueToEncrypt) | ||
| keys := os.Getenv("CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS") | ||
| require.NotEmpty(t, keys, "Set the CODER_EXTERNAL_TOKEN_ENCRYPTION_KEYS environment variable to use this") | ||
|
|
||
| base64Keys := strings.Split(keys, ",") | ||
| activeKey := base64Keys[0] | ||
|
|
||
| decodedKey, err := base64.StdEncoding.DecodeString(activeKey) | ||
| require.NoError(t, err, "the active key should be valid base64") | ||
|
|
||
| cipher, err := cipherAES256(decodedKey) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Logf("cipher digest: %+v", cipher.HexDigest()) | ||
|
|
||
| encryptedEmptyString, err := cipher.Encrypt([]byte(valueToEncrypt)) | ||
| require.NoError(t, err) | ||
|
|
||
| t.Logf("encrypted and base64-encoded: %q", base64.StdEncoding.EncodeToString(encryptedEmptyString)) | ||
| } |
Contributor
There was a problem hiding this comment.
Seems pretty reasonable to leave as a test for now
Comment on lines
+57
to
+58
| -- Required for sqlc to generate a parameter for the oauth_refresh_token_key_id | ||
| @oauth_refresh_token_key_id :: text = @oauth_refresh_token_key_id :: text; |
Contributor
There was a problem hiding this comment.
I feel pretty numb now to these kinda sqlc hacks now it doesn't phase me 🚶
stirby
pushed a commit
that referenced
this pull request
Dec 3, 2024
…with dbcrypt (#15721) #15608 introduced a buggy behaviour with dbcrypt enabled. When clearing an oauth refresh token, we had been setting the value to the empty string. The database encryption package considers decrypting an empty string to be an error, as an empty encrypted string value will still have a nonce associated with it and thus not actually be empty when stored at rest. Instead of 'deleting' the refresh token, 'update' it to be the empty string. This plays nicely with dbcrypt. It also adds a 'utility test' in the dbcrypt package to help encrypt a value. This was useful when manually fixing users affected by this bug on our dogfood instance. (cherry picked from commit e744cde)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#15608 introduced a buggy behaviour with dbcrypt enabled.
When clearing an oauth refresh token, we had been setting the value to the empty string.
The database encryption package considers decrypting an empty string to be an error, as an empty encrypted string value will still have a nonce associated with it and thus not actually be empty when stored at rest.
Instead of 'deleting' the refresh token, 'update' it to be the empty string.
This plays nicely with dbcrypt.
It also adds a 'utility test' in the dbcrypt package to help encrypt a value. This was useful when manually fixing users affected by this bug on our dogfood instance.
cc @bpmct @stirby