Secrets: Refactor data_key_id out of the encoded secure value payload (#111852)
* everything compiles * tests pass * remove file included by accident * add entry to gitignore * some scaffolding for the migration executor * remove file * implement and test the migration * use xkube.Namespace in our interfaces * add todo * update wire deps * add some logs * fix wire dependency ordering * create tests to validate error conditions during migrations
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
package manager
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
@@ -20,13 +18,10 @@ import (
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/contracts"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/encryption"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/encryption/cipher"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/xkube"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
const (
|
||||
keyIdDelimiter = '#'
|
||||
)
|
||||
|
||||
type EncryptionManager struct {
|
||||
tracer trace.Tracer
|
||||
store contracts.DataKeyStorage
|
||||
@@ -99,12 +94,9 @@ func (s *EncryptionManager) registerUsageMetrics() {
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: Why do we need to use a global variable for this?
|
||||
var b64 = base64.RawStdEncoding
|
||||
|
||||
func (s *EncryptionManager) Encrypt(ctx context.Context, namespace string, payload []byte) ([]byte, error) {
|
||||
func (s *EncryptionManager) Encrypt(ctx context.Context, namespace xkube.Namespace, payload []byte) (contracts.EncryptedPayload, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "EnvelopeEncryptionManager.Encrypt", trace.WithAttributes(
|
||||
attribute.String("namespace", namespace),
|
||||
attribute.String("namespace", namespace.String()),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
@@ -128,34 +120,30 @@ func (s *EncryptionManager) Encrypt(ctx context.Context, namespace string, paylo
|
||||
id, dataKey, err = s.currentDataKey(ctx, namespace, label)
|
||||
if err != nil {
|
||||
s.log.Error("Failed to get current data key", "error", err, "label", label)
|
||||
return nil, err
|
||||
return contracts.EncryptedPayload{}, err
|
||||
}
|
||||
|
||||
var encrypted []byte
|
||||
encrypted, err = s.cipher.Encrypt(ctx, payload, string(dataKey))
|
||||
if err != nil {
|
||||
s.log.Error("Failed to encrypt secret", "error", err)
|
||||
return nil, err
|
||||
return contracts.EncryptedPayload{}, err
|
||||
}
|
||||
|
||||
prefix := make([]byte, b64.EncodedLen(len(id))+2)
|
||||
b64.Encode(prefix[1:], []byte(id))
|
||||
prefix[0] = keyIdDelimiter
|
||||
prefix[len(prefix)-1] = keyIdDelimiter
|
||||
encryptedPayload := contracts.EncryptedPayload{
|
||||
DataKeyID: id,
|
||||
EncryptedData: encrypted,
|
||||
}
|
||||
|
||||
blob := make([]byte, len(prefix)+len(encrypted))
|
||||
copy(blob, prefix)
|
||||
copy(blob[len(prefix):], encrypted)
|
||||
|
||||
return blob, nil
|
||||
return encryptedPayload, nil
|
||||
}
|
||||
|
||||
// currentDataKey looks up for current data key in cache or database by name, and decrypts it.
|
||||
// If there's no current data key in cache nor in database it generates a new random data key,
|
||||
// and stores it into both the in-memory cache and database (encrypted by the encryption provider).
|
||||
func (s *EncryptionManager) currentDataKey(ctx context.Context, namespace string, label string) (string, []byte, error) {
|
||||
func (s *EncryptionManager) currentDataKey(ctx context.Context, namespace xkube.Namespace, label string) (string, []byte, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "EnvelopeEncryptionManager.CurrentDataKey", trace.WithAttributes(
|
||||
attribute.String("namespace", namespace),
|
||||
attribute.String("namespace", namespace.String()),
|
||||
attribute.String("label", label),
|
||||
))
|
||||
defer span.End()
|
||||
@@ -166,14 +154,14 @@ func (s *EncryptionManager) currentDataKey(ctx context.Context, namespace string
|
||||
defer s.mtx.Unlock()
|
||||
|
||||
// We try to fetch the data key, either from cache or database
|
||||
id, dataKey, err := s.dataKeyByLabel(ctx, namespace, label)
|
||||
id, dataKey, err := s.dataKeyByLabel(ctx, namespace.String(), label)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// If no existing data key was found, create a new one
|
||||
if dataKey == nil {
|
||||
id, dataKey, err = s.newDataKey(ctx, namespace, label)
|
||||
id, dataKey, err = s.newDataKey(ctx, namespace.String(), label)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
@@ -264,9 +252,9 @@ func newRandomDataKey() ([]byte, error) {
|
||||
return rawDataKey, nil
|
||||
}
|
||||
|
||||
func (s *EncryptionManager) Decrypt(ctx context.Context, namespace string, payload []byte) ([]byte, error) {
|
||||
func (s *EncryptionManager) Decrypt(ctx context.Context, namespace xkube.Namespace, payload contracts.EncryptedPayload) ([]byte, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "EnvelopeEncryptionManager.Decrypt", trace.WithAttributes(
|
||||
attribute.String("namespace", namespace),
|
||||
attribute.String("namespace", namespace.String()),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
@@ -285,50 +273,28 @@ func (s *EncryptionManager) Decrypt(ctx context.Context, namespace string, paylo
|
||||
}
|
||||
}()
|
||||
|
||||
if len(payload) == 0 {
|
||||
if len(payload.EncryptedData) == 0 {
|
||||
err = fmt.Errorf("unable to decrypt empty payload")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload = payload[1:]
|
||||
endOfKey := bytes.Index(payload, []byte{keyIdDelimiter})
|
||||
if endOfKey == -1 {
|
||||
err = fmt.Errorf("could not find valid key id in encrypted payload")
|
||||
return nil, err
|
||||
}
|
||||
b64Key := payload[:endOfKey]
|
||||
payload = payload[endOfKey+1:]
|
||||
keyId := make([]byte, b64.DecodedLen(len(b64Key)))
|
||||
_, err = b64.Decode(keyId, b64Key)
|
||||
if err != nil {
|
||||
if payload.DataKeyID == "" {
|
||||
err = fmt.Errorf("unable to decrypt empty data key id")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dataKey, err := s.dataKeyById(ctx, namespace, string(keyId))
|
||||
dataKey, err := s.dataKeyById(ctx, namespace.String(), payload.DataKeyID)
|
||||
if err != nil {
|
||||
s.log.FromContext(ctx).Error("Failed to lookup data key by id", "id", string(keyId), "error", err)
|
||||
s.log.FromContext(ctx).Error("Failed to lookup data key by id", "id", payload.DataKeyID, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var decrypted []byte
|
||||
decrypted, err = s.cipher.Decrypt(ctx, payload, string(dataKey))
|
||||
decrypted, err = s.cipher.Decrypt(ctx, payload.EncryptedData, string(dataKey))
|
||||
|
||||
return decrypted, err
|
||||
}
|
||||
|
||||
func (s *EncryptionManager) GetDecryptedValue(ctx context.Context, namespace string, sjd map[string][]byte, key, fallback string) string {
|
||||
if value, ok := sjd[key]; ok {
|
||||
decryptedData, err := s.Decrypt(ctx, namespace, value)
|
||||
if err != nil {
|
||||
return fallback
|
||||
}
|
||||
|
||||
return string(decryptedData)
|
||||
}
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
// dataKeyById looks up for data key in the database and returns it decrypted.
|
||||
func (s *EncryptionManager) dataKeyById(ctx context.Context, namespace, id string) ([]byte, error) {
|
||||
ctx, span := s.tracer.Start(ctx, "EnvelopeEncryptionManager.GetDataKey", trace.WithAttributes(
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/encryption"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/encryption/cipher/service"
|
||||
osskmsproviders "github.com/grafana/grafana/pkg/registry/apis/secret/encryption/kmsproviders"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/secret/xkube"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/storage/secret/database"
|
||||
@@ -34,7 +35,7 @@ func TestMain(m *testing.M) {
|
||||
func TestEncryptionService_EnvelopeEncryption(t *testing.T) {
|
||||
svc := setupTestService(t)
|
||||
ctx := context.Background()
|
||||
namespace := "test-namespace"
|
||||
namespace := xkube.Namespace("test-namespace")
|
||||
|
||||
t.Run("encrypting should create DEK", func(t *testing.T) {
|
||||
plaintext := []byte("very secret string")
|
||||
@@ -46,7 +47,7 @@ func TestEncryptionService_EnvelopeEncryption(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, plaintext, decrypted)
|
||||
|
||||
keys, err := svc.store.ListDataKeys(ctx, namespace)
|
||||
keys, err := svc.store.ListDataKeys(ctx, namespace.String())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(keys), 1)
|
||||
})
|
||||
@@ -61,7 +62,7 @@ func TestEncryptionService_EnvelopeEncryption(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, plaintext, decrypted)
|
||||
|
||||
keys, err := svc.store.ListDataKeys(ctx, namespace)
|
||||
keys, err := svc.store.ListDataKeys(ctx, namespace.String())
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, len(keys), 1)
|
||||
})
|
||||
@@ -212,7 +213,7 @@ func TestEncryptionService_UseCurrentProvider(t *testing.T) {
|
||||
}
|
||||
encryptionManager.providerConfig.CurrentProvider = encryption.ProviderID("fakeProvider.v1")
|
||||
|
||||
namespace := "test-namespace"
|
||||
namespace := xkube.Namespace("test-namespace")
|
||||
encrypted, _ := encryptionManager.Encrypt(context.Background(), namespace, []byte{})
|
||||
assert.True(t, fake.encryptCalled)
|
||||
assert.False(t, fake.decryptCalled)
|
||||
@@ -241,7 +242,7 @@ func TestEncryptionService_UseCurrentProvider(t *testing.T) {
|
||||
|
||||
func TestEncryptionService_SecretKeyVersionUpgrade(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
namespace := "test-namespace"
|
||||
namespace := xkube.Namespace("test-namespace")
|
||||
|
||||
// Generate random keys for testing
|
||||
oldKey := util.GenerateShortUID() + util.GenerateShortUID() // 32 chars
|
||||
@@ -416,16 +417,30 @@ func (p *fakeProvider) Decrypt(_ context.Context, _ []byte) ([]byte, error) {
|
||||
|
||||
func TestEncryptionService_Decrypt(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
namespace := "test-namespace"
|
||||
namespace := xkube.Namespace("test-namespace")
|
||||
|
||||
t.Run("empty payload should fail", func(t *testing.T) {
|
||||
svc := setupTestService(t)
|
||||
_, err := svc.Decrypt(context.Background(), namespace, []byte(""))
|
||||
_, err := svc.Decrypt(context.Background(), namespace, contracts.EncryptedPayload{
|
||||
DataKeyID: "test-data-key-id",
|
||||
EncryptedData: []byte(""),
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
assert.Equal(t, "unable to decrypt empty payload", err.Error())
|
||||
})
|
||||
|
||||
t.Run("empty data key id should fail", func(t *testing.T) {
|
||||
svc := setupTestService(t)
|
||||
_, err := svc.Decrypt(context.Background(), namespace, contracts.EncryptedPayload{
|
||||
DataKeyID: "",
|
||||
EncryptedData: []byte("some payload"),
|
||||
})
|
||||
require.Error(t, err)
|
||||
|
||||
assert.Equal(t, "unable to decrypt empty data key id", err.Error())
|
||||
})
|
||||
|
||||
t.Run("ee encrypted payload with ee enabled should work", func(t *testing.T) {
|
||||
svc := setupTestService(t)
|
||||
ciphertext, err := svc.Encrypt(ctx, namespace, []byte("grafana"))
|
||||
@@ -442,7 +457,7 @@ func TestIntegration_SecretsService(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
someData := []byte(`some-data`)
|
||||
namespace := "test-namespace"
|
||||
namespace := xkube.Namespace("test-namespace")
|
||||
|
||||
tcs := map[string]func(*testing.T, db.DB, contracts.EncryptionManager){
|
||||
"regular": func(t *testing.T, _ db.DB, svc contracts.EncryptionManager) {
|
||||
@@ -562,7 +577,7 @@ func TestIntegration_SecretsService(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
namespace := "test-namespace"
|
||||
namespace := xkube.Namespace("test-namespace")
|
||||
|
||||
// Here's what actually matters and varies on each test: look at the test case name.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user