encryption: Do not proactively fetch version (#40118)

This commit is contained in:
Eric Fritz 2022-08-09 06:39:17 -05:00 committed by GitHub
parent 33a741166c
commit 4b534d77f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 12 deletions

View File

@ -384,7 +384,7 @@ func TestExternalAccounts_Encryption(t *testing.T) {
}
// values encrypted should not be readable without the encrypting key
noopStore := store.WithEncryptionKey(&encryption.NoopKey{})
noopStore := store.WithEncryptionKey(&encryption.NoopKey{FailDecrypt: true})
if _, err := noopStore.List(ctx, ExternalAccountsListOptions{}); err == nil {
t.Fatalf("expected error decrypting with a different key")
}

View File

@ -13,11 +13,12 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/keegancsmith/sqlf"
"github.com/lib/pq"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/log/logtest"
"github.com/sourcegraph/sourcegraph/cmd/frontend/envvar"
@ -1163,7 +1164,7 @@ func TestExternalServicesStore_GetByID_Encrypted(t *testing.T) {
}
// values encrypted should not be readable without the encrypting key
noopStore := store.WithEncryptionKey(&encryption.NoopKey{})
noopStore := store.WithEncryptionKey(&encryption.NoopKey{FailDecrypt: true})
if _, err := noopStore.GetByID(ctx, es.ID); err == nil {
t.Fatalf("expected error decrypting with a different key")
}
@ -2004,7 +2005,7 @@ func TestExternalServicesStore_Upsert(t *testing.T) {
}
// values encrypted should not be readable without the encrypting key
noopStore := ExternalServicesWith(logger, tx).WithEncryptionKey(&encryption.NoopKey{})
noopStore := ExternalServicesWith(logger, tx).WithEncryptionKey(&encryption.NoopKey{FailDecrypt: true})
for _, e := range want {
if _, err := noopStore.GetByID(ctx, e.ID); err == nil {

View File

@ -27,7 +27,9 @@ func MaybeEncrypt(ctx context.Context, key Key, data string) (_, keyIdent string
return "", "", err
}
span, ctx = ot.StartSpanFromContext(ctx, "key.Version")
version, err := key.Version(ctx)
span.Finish()
if err != nil {
return "", "", errors.Wrap(err, "failed to get encryption key version")
}
@ -47,18 +49,18 @@ func MaybeDecrypt(ctx context.Context, key Key, data, keyIdent string) (string,
if key == nil {
return data, errors.Errorf("key mismatch: value is encrypted but no encryption key available in site-config")
}
version, err := key.Version(ctx)
if err != nil {
return "", errors.Wrap(err, "failed to get encryption key version")
}
if keyIdent != version.JSON() {
return "", errors.New("key mismatch: value is encrypted with an encryption key distinct from the one available in site-config")
}
span, ctx := ot.StartSpanFromContext(ctx, "key.Decrypt")
decrypted, err := key.Decrypt(ctx, []byte(data))
span.Finish()
if err != nil {
span, ctx = ot.StartSpanFromContext(ctx, "key.Version")
version, versionErr := key.Version(ctx)
span.Finish()
if versionErr == nil && keyIdent != version.JSON() {
return "", errors.New("key mismatch: value is encrypted with an encryption key distinct from the one available in site-config")
}
return data, err
}

View File

@ -2,11 +2,15 @@ package encryption
import (
"context"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
var _ Key = &NoopKey{}
type NoopKey struct{}
type NoopKey struct {
FailDecrypt bool
}
func (k *NoopKey) Version(ctx context.Context) (KeyVersion, error) {
return KeyVersion{
@ -21,6 +25,10 @@ func (k *NoopKey) Encrypt(ctx context.Context, plaintext []byte) ([]byte, error)
}
func (k *NoopKey) Decrypt(ctx context.Context, ciphertext []byte) (*Secret, error) {
if k.FailDecrypt {
return nil, errors.New("unsupported decrypt")
}
s := NewSecret(string(ciphertext))
return &s, nil
}