Encryption: Cache new DEKs (only) after a caution period (#60664)
* Encryption: Cache new DEKs (only) after commit * Fix typo * Update secrets manager tests with new failing case * Update secrets manager tests with new clarifications (comments) * Correct broken method calls * Unify methods * Cache data keys only after a caution period * Caution period for data keys caching only for encrypt ops
This commit is contained in:
@@ -2,6 +2,7 @@ package manager
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -17,12 +18,14 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/kmsproviders/osskmsproviders"
|
||||
"github.com/grafana/grafana/pkg/services/secrets"
|
||||
"github.com/grafana/grafana/pkg/services/secrets/database"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func TestSecretsService_EnvelopeEncryption(t *testing.T) {
|
||||
store := database.ProvideSecretsStore(db.InitTestDB(t))
|
||||
testDB := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(testDB)
|
||||
svc := SetupTestService(t, store)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -82,7 +85,8 @@ func TestSecretsService_EnvelopeEncryption(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSecretsService_DataKeys(t *testing.T) {
|
||||
store := database.ProvideSecretsStore(db.InitTestDB(t))
|
||||
testDB := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(testDB)
|
||||
ctx := context.Background()
|
||||
|
||||
dataKey := &secrets.DataKey{
|
||||
@@ -160,7 +164,8 @@ func TestSecretsService_DataKeys(t *testing.T) {
|
||||
|
||||
func TestSecretsService_UseCurrentProvider(t *testing.T) {
|
||||
t.Run("When encryption_provider is not specified explicitly, should use 'secretKey' as a current provider", func(t *testing.T) {
|
||||
svc := SetupTestService(t, database.ProvideSecretsStore(db.InitTestDB(t)))
|
||||
testDB := db.InitTestDB(t)
|
||||
svc := SetupTestService(t, database.ProvideSecretsStore(testDB))
|
||||
assert.Equal(t, secrets.ProviderID("secretKey.v1"), svc.currentProviderID)
|
||||
})
|
||||
|
||||
@@ -187,7 +192,8 @@ func TestSecretsService_UseCurrentProvider(t *testing.T) {
|
||||
|
||||
features := featuremgmt.WithFeatures()
|
||||
kms := newFakeKMS(osskmsproviders.ProvideService(encryptionService, settings, features))
|
||||
secretStore := database.ProvideSecretsStore(db.InitTestDB(t))
|
||||
testDB := db.InitTestDB(t)
|
||||
secretStore := database.ProvideSecretsStore(testDB)
|
||||
|
||||
secretsService, err := ProvideSecretsService(
|
||||
secretStore,
|
||||
@@ -261,8 +267,8 @@ func (f *fakeKMS) Provide() (map[secrets.ProviderID]secrets.Provider, error) {
|
||||
|
||||
func TestSecretsService_Run(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sql := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(sql)
|
||||
testDB := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(testDB)
|
||||
svc := SetupTestService(t, store)
|
||||
|
||||
t.Run("should stop with no error once the context's finished", func(t *testing.T) {
|
||||
@@ -274,16 +280,26 @@ func TestSecretsService_Run(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("should trigger cache clean up", func(t *testing.T) {
|
||||
// Encrypt to ensure there's a data encryption key generated
|
||||
_, err := svc.Encrypt(ctx, []byte("grafana"), secrets.WithoutScope())
|
||||
restoreTimeNowAfterTestExec(t)
|
||||
|
||||
// Encrypt to force data encryption key generation
|
||||
encrypted, err := svc.Encrypt(ctx, []byte("grafana"), secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// Ten minutes later (after caution period)
|
||||
// Look SecretsService.cacheDataKey for more details.
|
||||
now = func() time.Time { return time.Now().Add(10 * time.Minute) }
|
||||
|
||||
// Decrypt to ensure data encryption key is cached
|
||||
_, err = svc.Decrypt(ctx, encrypted)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Data encryption key cache should contain one element
|
||||
require.Len(t, svc.dataKeyCache.byId, 1)
|
||||
require.Len(t, svc.dataKeyCache.byLabel, 1)
|
||||
|
||||
t.Cleanup(func() { now = time.Now })
|
||||
now = func() time.Time { return time.Now().Add(10 * time.Minute) }
|
||||
// Twenty minutes later (after caution period + cache ttl)
|
||||
now = func() time.Time { return time.Now().Add(20 * time.Minute) }
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
|
||||
defer cancel()
|
||||
@@ -301,8 +317,8 @@ func TestSecretsService_Run(t *testing.T) {
|
||||
|
||||
func TestSecretsService_ReEncryptDataKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sql := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(sql)
|
||||
testDB := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(testDB)
|
||||
svc := SetupTestService(t, store)
|
||||
|
||||
// Encrypt to generate data encryption key
|
||||
@@ -326,6 +342,12 @@ func TestSecretsService_ReEncryptDataKeys(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("data keys cache should be invalidated", func(t *testing.T) {
|
||||
restoreTimeNowAfterTestExec(t)
|
||||
|
||||
// Ten minutes later (after caution period)
|
||||
// Look SecretsService.cacheDataKey for more details.
|
||||
now = func() time.Time { return time.Now().Add(10 * time.Minute) }
|
||||
|
||||
// Decrypt to ensure data key is cached
|
||||
_, err := svc.Decrypt(ctx, ciphertext)
|
||||
require.NoError(t, err)
|
||||
@@ -342,7 +364,8 @@ func TestSecretsService_ReEncryptDataKeys(t *testing.T) {
|
||||
|
||||
func TestSecretsService_Decrypt(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
store := database.ProvideSecretsStore(db.InitTestDB(t))
|
||||
testDB := db.InitTestDB(t)
|
||||
store := database.ProvideSecretsStore(testDB)
|
||||
|
||||
t.Run("empty payload should fail", func(t *testing.T) {
|
||||
svc := SetupTestService(t, store)
|
||||
@@ -401,3 +424,137 @@ func TestSecretsService_Decrypt(t *testing.T) {
|
||||
assert.Equal(t, []byte("grafana"), decrypted)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegration_SecretsService(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
someData := []byte(`some-data`)
|
||||
|
||||
tcs := map[string]func(*testing.T, *sqlstore.SQLStore, *SecretsService){
|
||||
"regular": func(t *testing.T, _ *sqlstore.SQLStore, svc *SecretsService) {
|
||||
// We encrypt some data normally, no transactions implied.
|
||||
_, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
},
|
||||
"within successful InTransaction": func(t *testing.T, store *sqlstore.SQLStore, svc *SecretsService) {
|
||||
require.NoError(t, store.InTransaction(ctx, func(ctx context.Context) error {
|
||||
// We encrypt some data within a transaction that shares the db session.
|
||||
_, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// And the transition succeeds.
|
||||
return nil
|
||||
}))
|
||||
},
|
||||
"within unsuccessful InTransaction": func(t *testing.T, store *sqlstore.SQLStore, svc *SecretsService) {
|
||||
require.NotNil(t, store.InTransaction(ctx, func(ctx context.Context) error {
|
||||
// We encrypt some data within a transaction that shares the db session.
|
||||
_, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// But the transaction fails.
|
||||
return errors.New("error")
|
||||
}))
|
||||
},
|
||||
"within unsuccessful InTransaction (plus forced db fetch)": func(t *testing.T, store *sqlstore.SQLStore, svc *SecretsService) {
|
||||
require.NotNil(t, store.InTransaction(ctx, func(ctx context.Context) error {
|
||||
// We encrypt some data within a transaction that shares the db session.
|
||||
encrypted, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// At this point the data key is not cached yet because
|
||||
// the transaction haven't been committed yet,
|
||||
// and won't, so we do a decrypt operation within the
|
||||
// transaction to force the data key to be
|
||||
// (potentially) cached (it shouldn't to prevent issues).
|
||||
decrypted, err := svc.Decrypt(ctx, encrypted)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, someData, decrypted)
|
||||
|
||||
// But the transaction fails.
|
||||
return errors.New("error")
|
||||
}))
|
||||
},
|
||||
"within successful WithTransactionalDbSession": func(t *testing.T, store *sqlstore.SQLStore, svc *SecretsService) {
|
||||
require.NoError(t, store.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
// We encrypt some data within a transaction that does not share the db session.
|
||||
_, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// And the transition succeeds.
|
||||
return nil
|
||||
}))
|
||||
},
|
||||
"within unsuccessful WithTransactionalDbSession": func(t *testing.T, store *sqlstore.SQLStore, svc *SecretsService) {
|
||||
require.NotNil(t, store.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
// We encrypt some data within a transaction that does not share the db session.
|
||||
_, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// But the transaction fails.
|
||||
return errors.New("error")
|
||||
}))
|
||||
},
|
||||
"within unsuccessful WithTransactionalDbSession (plus forced db fetch)": func(t *testing.T, store *sqlstore.SQLStore, svc *SecretsService) {
|
||||
require.NotNil(t, store.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
// We encrypt some data within a transaction that does not share the db session.
|
||||
encrypted, err := svc.Encrypt(ctx, someData, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// At this point the data key is not cached yet because
|
||||
// the transaction haven't been committed yet,
|
||||
// and won't, so we do a decrypt operation within the
|
||||
// transaction to force the data key to be
|
||||
// (potentially) cached (it shouldn't to prevent issues).
|
||||
decrypted, err := svc.Decrypt(ctx, encrypted)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, someData, decrypted)
|
||||
|
||||
// But the transaction fails.
|
||||
return errors.New("error")
|
||||
}))
|
||||
},
|
||||
}
|
||||
|
||||
for name, tc := range tcs {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
testDB := db.InitTestDB(t)
|
||||
svc := SetupTestService(t, database.ProvideSecretsStore(testDB))
|
||||
|
||||
// Here's what actually matters and varies on each test: look at the test case name.
|
||||
//
|
||||
// For historical reasons, and in an old implementation, when a successful encryption
|
||||
// operation happened within an unsuccessful transaction, the data key was used to be
|
||||
// cached in memory for the next encryption operations, which caused some data to be
|
||||
// encrypted with a data key that haven't actually been persisted into the database.
|
||||
tc(t, testDB, svc)
|
||||
// Therefore, the data encrypted after this point, become unrecoverable after a restart.
|
||||
// So, the different test cases here are there to prevent that from happening again
|
||||
// in the future, whatever it is what happens.
|
||||
|
||||
// So, we proceed with an encryption operation:
|
||||
toEncrypt := []byte(`data-to-encrypt`)
|
||||
encrypted, err := svc.Encrypt(ctx, toEncrypt, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
// We simulate an instance restart. So, there's no data in the in-memory cache.
|
||||
svc.dataKeyCache.flush()
|
||||
|
||||
// And then, we MUST still be able to decrypt the previously encrypted data:
|
||||
decrypted, err := svc.Decrypt(ctx, encrypted)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, toEncrypt, decrypted)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Use this function at the beginning of those tests
|
||||
// that manipulates 'now', so it'll leave it in a
|
||||
// correct state once test execution finishes.
|
||||
func restoreTimeNowAfterTestExec(t *testing.T) {
|
||||
t.Helper()
|
||||
t.Cleanup(func() { now = time.Now })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user