mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 15:31:48 +00:00
encryption: Do not proactively fetch version (#40118)
This commit is contained in:
parent
33a741166c
commit
4b534d77f3
@ -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")
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user