mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-21 03:43:41 +01:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63c218c399 | ||
|
|
3b4690d1b7 | ||
|
|
9a7f033d7f |
No files matched your search
@@ -34,6 +34,14 @@ connection_max_lifetime_seconds = 3600
|
||||
[credential.memory]
|
||||
enabled = false
|
||||
|
||||
# Store secret keys KMS-encrypted instead of plaintext.
|
||||
# Requires a KMS provider configured in the [kms] section (filer.toml or
|
||||
# security.toml): local, aws, azure, gcp, or openbao for Vault/OpenBao.
|
||||
# Existing plaintext secrets keep working and are encrypted on their next write.
|
||||
[credential.encryption]
|
||||
enabled = false
|
||||
#key_id = "seaweedfs-credentials"
|
||||
|
||||
# Environment variable overrides:
|
||||
# Any configuration value can be overridden by environment variables
|
||||
# Rules:
|
||||
|
||||
@@ -101,6 +101,25 @@ connection_max_lifetime_seconds = 3600
|
||||
enabled = true
|
||||
```
|
||||
|
||||
### Encrypted Secret Keys
|
||||
|
||||
Any store can persist secret keys encrypted instead of plaintext:
|
||||
|
||||
```toml
|
||||
[credential.filer_etc]
|
||||
enabled = true
|
||||
|
||||
[credential.encryption]
|
||||
enabled = true
|
||||
#key_id = "seaweedfs-credentials" # optional KMS key id
|
||||
```
|
||||
|
||||
Secrets are wrapped with a data key from the configured KMS provider
|
||||
(`[kms]` section — local, aws, azure, gcp, or openbao for Vault/OpenBao),
|
||||
so the master key material can live in an external KMS or Vault. Secrets
|
||||
already stored in plaintext keep working and are encrypted on their next
|
||||
write.
|
||||
|
||||
## Environment Variables
|
||||
|
||||
All credential configuration can be overridden with environment variables:
|
||||
|
||||
@@ -58,6 +58,11 @@ func NewCredentialManager(storeName CredentialStoreTypeName, configuration util.
|
||||
return nil, fmt.Errorf("failed to initialize credential store '%s': %v", storeName, err)
|
||||
}
|
||||
|
||||
if configuration != nil && configuration.GetBool("credential.encryption.enabled") {
|
||||
store = NewEncryptedCredentialStore(store, configuration.GetString("credential.encryption.key_id"))
|
||||
glog.V(0).Infof("Credential store '%s' persists secret keys KMS-encrypted", storeName)
|
||||
}
|
||||
|
||||
return &CredentialManager{
|
||||
Store: store,
|
||||
}, nil
|
||||
|
||||
@@ -0,0 +1,415 @@
|
||||
package credential
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/kms"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/s3api/policy_engine"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// CredentialEncryptionKeyID is the default KMS key used to wrap secret keys
|
||||
// when credential.toml enables encryption without naming one.
|
||||
const CredentialEncryptionKeyID = "seaweedfs-credentials"
|
||||
|
||||
// encryptedDataKeyField carries the KMS-wrapped data key inside the envelope.
|
||||
const encryptedDataKeyField = "encrypted_data_key"
|
||||
|
||||
// credentialEncryptionContext authenticates the data key wrapping so a blob
|
||||
// cannot be replayed from a different subsystem.
|
||||
var credentialEncryptionContext = map[string]string{"seaweedfs": "credential-store"}
|
||||
|
||||
// EncryptedCredentialStore wraps another credential store and encrypts secret
|
||||
// keys before they are persisted, decrypting them on reads. Secrets stored
|
||||
// without an envelope pass through unchanged, so existing plaintext
|
||||
// configurations keep working while new writes are encrypted.
|
||||
type EncryptedCredentialStore struct {
|
||||
CredentialStore
|
||||
keyID string
|
||||
}
|
||||
|
||||
var _ CredentialStore = &EncryptedCredentialStore{}
|
||||
|
||||
// NewEncryptedCredentialStore wraps an upstream store with secret encryption
|
||||
// under the named KMS key. An empty keyID uses CredentialEncryptionKeyID.
|
||||
func NewEncryptedCredentialStore(upstream CredentialStore, keyID string) CredentialStore {
|
||||
if keyID == "" {
|
||||
keyID = CredentialEncryptionKeyID
|
||||
}
|
||||
wrapped := &EncryptedCredentialStore{CredentialStore: upstream, keyID: keyID}
|
||||
if _, ok := upstream.(UserRenamer); ok {
|
||||
return &encryptedRenamerStore{wrapped}
|
||||
}
|
||||
return wrapped
|
||||
}
|
||||
|
||||
// encryptedRenamerStore adds RenameUser to the wrapper only when the upstream
|
||||
// store supports it, so wrapped stores keep their atomic rename capability.
|
||||
type encryptedRenamerStore struct {
|
||||
*EncryptedCredentialStore
|
||||
}
|
||||
|
||||
func (s *encryptedRenamerStore) RenameUser(ctx context.Context, oldName, newName string) error {
|
||||
return s.CredentialStore.(UserRenamer).RenameUser(ctx, oldName, newName)
|
||||
}
|
||||
|
||||
// SetFilerAddressFunc forwards a dynamic filer address to stores that need it.
|
||||
func (s *EncryptedCredentialStore) SetFilerAddressFunc(getFiler func() pb.ServerAddress, grpcDialOption grpc.DialOption) {
|
||||
if setter, ok := s.CredentialStore.(FilerAddressSetter); ok {
|
||||
setter.SetFilerAddressFunc(getFiler, grpcDialOption)
|
||||
}
|
||||
}
|
||||
|
||||
// The optional interface forwarders below return the same neutral results the
|
||||
// CredentialManager produces for stores without the capability, so wrapping a
|
||||
// store never changes which calls succeed.
|
||||
|
||||
func (s *EncryptedCredentialStore) PutUserInlinePolicy(ctx context.Context, userName, policyName string, document policy_engine.PolicyDocument) error {
|
||||
if store, ok := s.CredentialStore.(InlinePolicyStore); ok {
|
||||
return store.PutUserInlinePolicy(ctx, userName, policyName, document)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) GetUserInlinePolicy(ctx context.Context, userName, policyName string) (*policy_engine.PolicyDocument, error) {
|
||||
if store, ok := s.CredentialStore.(InlinePolicyStore); ok {
|
||||
return store.GetUserInlinePolicy(ctx, userName, policyName)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) DeleteUserInlinePolicy(ctx context.Context, userName, policyName string) error {
|
||||
if store, ok := s.CredentialStore.(InlinePolicyStore); ok {
|
||||
return store.DeleteUserInlinePolicy(ctx, userName, policyName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) ListUserInlinePolicies(ctx context.Context, userName string) ([]string, error) {
|
||||
if store, ok := s.CredentialStore.(InlinePolicyStore); ok {
|
||||
return store.ListUserInlinePolicies(ctx, userName)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) LoadInlinePolicies(ctx context.Context) (map[string]map[string]policy_engine.PolicyDocument, error) {
|
||||
if loader, ok := s.CredentialStore.(InlinePoliciesLoader); ok {
|
||||
return loader.LoadInlinePolicies(ctx)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) PutGroupInlinePolicy(ctx context.Context, groupName, policyName string, document policy_engine.PolicyDocument) error {
|
||||
if store, ok := s.CredentialStore.(GroupInlinePolicyStore); ok {
|
||||
return store.PutGroupInlinePolicy(ctx, groupName, policyName, document)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) GetGroupInlinePolicy(ctx context.Context, groupName, policyName string) (*policy_engine.PolicyDocument, error) {
|
||||
if store, ok := s.CredentialStore.(GroupInlinePolicyStore); ok {
|
||||
return store.GetGroupInlinePolicy(ctx, groupName, policyName)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) DeleteGroupInlinePolicy(ctx context.Context, groupName, policyName string) error {
|
||||
if store, ok := s.CredentialStore.(GroupInlinePolicyStore); ok {
|
||||
return store.DeleteGroupInlinePolicy(ctx, groupName, policyName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) ListGroupInlinePolicies(ctx context.Context, groupName string) ([]string, error) {
|
||||
if store, ok := s.CredentialStore.(GroupInlinePolicyStore); ok {
|
||||
return store.ListGroupInlinePolicies(ctx, groupName)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) LoadGroupInlinePolicies(ctx context.Context) (map[string]map[string]policy_engine.PolicyDocument, error) {
|
||||
if loader, ok := s.CredentialStore.(GroupInlinePoliciesLoader); ok {
|
||||
return loader.LoadGroupInlinePolicies(ctx)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) provider() (kms.KMSProvider, error) {
|
||||
provider := kms.GetGlobalKMS()
|
||||
if provider == nil {
|
||||
return nil, fmt.Errorf("credential encryption is enabled but no KMS provider is configured (see the [kms] section)")
|
||||
}
|
||||
return provider, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) encryptSecret(ctx context.Context, plaintext string) (string, error) {
|
||||
if plaintext == "" {
|
||||
return plaintext, nil
|
||||
}
|
||||
if envelope, err := kms.ParseEnvelope([]byte(plaintext)); err == nil && envelope.ProviderSpecific[encryptedDataKeyField] != nil {
|
||||
return plaintext, nil
|
||||
}
|
||||
provider, err := s.provider()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
keyID, err := provider.GetKeyID(ctx, s.keyID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve credential encryption key: %w", err)
|
||||
}
|
||||
dataKey, err := provider.GenerateDataKey(ctx, &kms.GenerateDataKeyRequest{
|
||||
KeyID: keyID,
|
||||
KeySpec: kms.KeySpecAES256,
|
||||
EncryptionContext: credentialEncryptionContext,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("generate credential data key: %w", err)
|
||||
}
|
||||
defer kms.ClearSensitiveData(dataKey.Plaintext)
|
||||
|
||||
aesgcm, err := newGCM(dataKey.Plaintext)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonce := make([]byte, aesgcm.NonceSize())
|
||||
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
|
||||
return "", err
|
||||
}
|
||||
blob := aesgcm.Seal(nonce, nonce, []byte(plaintext), []byte(keyID))
|
||||
|
||||
envelope, err := kms.CreateEnvelope("kms", keyID, base64.StdEncoding.EncodeToString(blob), map[string]interface{}{
|
||||
encryptedDataKeyField: base64.StdEncoding.EncodeToString(dataKey.CiphertextBlob),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(envelope), nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) decryptSecret(ctx context.Context, stored string) (string, error) {
|
||||
envelope, err := kms.ParseEnvelope([]byte(stored))
|
||||
if err != nil {
|
||||
return stored, nil
|
||||
}
|
||||
blob, err := base64.StdEncoding.DecodeString(envelope.Ciphertext)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decode credential ciphertext: %w", err)
|
||||
}
|
||||
encDataKey, ok := envelope.ProviderSpecific[encryptedDataKeyField].(string)
|
||||
if !ok || encDataKey == "" {
|
||||
return "", fmt.Errorf("credential envelope missing %s", encryptedDataKeyField)
|
||||
}
|
||||
encDataKeyBytes, err := base64.StdEncoding.DecodeString(encDataKey)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decode credential data key: %w", err)
|
||||
}
|
||||
|
||||
provider, err := s.provider()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
dataKey, err := provider.Decrypt(ctx, &kms.DecryptRequest{
|
||||
CiphertextBlob: encDataKeyBytes,
|
||||
EncryptionContext: credentialEncryptionContext,
|
||||
})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decrypt credential data key: %w", err)
|
||||
}
|
||||
defer kms.ClearSensitiveData(dataKey.Plaintext)
|
||||
|
||||
aesgcm, err := newGCM(dataKey.Plaintext)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(blob) < aesgcm.NonceSize() {
|
||||
return "", fmt.Errorf("credential ciphertext too short")
|
||||
}
|
||||
plain, err := aesgcm.Open(nil, blob[:aesgcm.NonceSize()], blob[aesgcm.NonceSize():], []byte(envelope.KeyID))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decrypt credential secret: %w", err)
|
||||
}
|
||||
return string(plain), nil
|
||||
}
|
||||
|
||||
func newGCM(dataKey []byte) (cipher.AEAD, error) {
|
||||
block, err := aes.NewCipher(dataKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cipher.NewGCM(block)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) encryptIdentity(ctx context.Context, identity *iam_pb.Identity) (*iam_pb.Identity, error) {
|
||||
cloned := proto.Clone(identity).(*iam_pb.Identity)
|
||||
for _, cred := range cloned.Credentials {
|
||||
encrypted, err := s.encryptSecret(ctx, cred.SecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cred.SecretKey = encrypted
|
||||
}
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) decryptIdentity(ctx context.Context, identity *iam_pb.Identity) (*iam_pb.Identity, error) {
|
||||
if identity == nil {
|
||||
return nil, nil
|
||||
}
|
||||
for _, cred := range identity.Credentials {
|
||||
decrypted, err := s.decryptSecret(ctx, cred.SecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cred.SecretKey = decrypted
|
||||
}
|
||||
return identity, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) encryptServiceAccount(ctx context.Context, sa *iam_pb.ServiceAccount) (*iam_pb.ServiceAccount, error) {
|
||||
cloned := proto.Clone(sa).(*iam_pb.ServiceAccount)
|
||||
if cloned.Credential != nil {
|
||||
encrypted, err := s.encryptSecret(ctx, cloned.Credential.SecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cloned.Credential.SecretKey = encrypted
|
||||
}
|
||||
return cloned, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) decryptServiceAccount(ctx context.Context, sa *iam_pb.ServiceAccount) (*iam_pb.ServiceAccount, error) {
|
||||
if sa == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if sa.Credential != nil {
|
||||
decrypted, err := s.decryptSecret(ctx, sa.Credential.SecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sa.Credential.SecretKey = decrypted
|
||||
}
|
||||
return sa, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) LoadConfiguration(ctx context.Context) (*iam_pb.S3ApiConfiguration, error) {
|
||||
config, err := s.CredentialStore.LoadConfiguration(ctx)
|
||||
if err != nil || config == nil {
|
||||
return config, err
|
||||
}
|
||||
for _, identity := range config.Identities {
|
||||
if _, err := s.decryptIdentity(ctx, identity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) SaveConfiguration(ctx context.Context, config *iam_pb.S3ApiConfiguration) error {
|
||||
cloned := proto.Clone(config).(*iam_pb.S3ApiConfiguration)
|
||||
for _, identity := range cloned.Identities {
|
||||
for _, cred := range identity.Credentials {
|
||||
encrypted, err := s.encryptSecret(ctx, cred.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cred.SecretKey = encrypted
|
||||
}
|
||||
}
|
||||
return s.CredentialStore.SaveConfiguration(ctx, cloned)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) CreateUser(ctx context.Context, identity *iam_pb.Identity) error {
|
||||
encrypted, err := s.encryptIdentity(ctx, identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.CredentialStore.CreateUser(ctx, encrypted)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) GetUser(ctx context.Context, username string) (*iam_pb.Identity, error) {
|
||||
identity, err := s.CredentialStore.GetUser(ctx, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.decryptIdentity(ctx, identity)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) UpdateUser(ctx context.Context, username string, identity *iam_pb.Identity) error {
|
||||
encrypted, err := s.encryptIdentity(ctx, identity)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.CredentialStore.UpdateUser(ctx, username, encrypted)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) GetUserByAccessKey(ctx context.Context, accessKey string) (*iam_pb.Identity, error) {
|
||||
identity, err := s.CredentialStore.GetUserByAccessKey(ctx, accessKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.decryptIdentity(ctx, identity)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) CreateAccessKey(ctx context.Context, username string, cred *iam_pb.Credential) error {
|
||||
cloned := proto.Clone(cred).(*iam_pb.Credential)
|
||||
encrypted, err := s.encryptSecret(ctx, cloned.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cloned.SecretKey = encrypted
|
||||
return s.CredentialStore.CreateAccessKey(ctx, username, cloned)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) CreateServiceAccount(ctx context.Context, sa *iam_pb.ServiceAccount) error {
|
||||
encrypted, err := s.encryptServiceAccount(ctx, sa)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.CredentialStore.CreateServiceAccount(ctx, encrypted)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) UpdateServiceAccount(ctx context.Context, id string, sa *iam_pb.ServiceAccount) error {
|
||||
encrypted, err := s.encryptServiceAccount(ctx, sa)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.CredentialStore.UpdateServiceAccount(ctx, id, encrypted)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) GetServiceAccount(ctx context.Context, id string) (*iam_pb.ServiceAccount, error) {
|
||||
sa, err := s.CredentialStore.GetServiceAccount(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.decryptServiceAccount(ctx, sa)
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) ListServiceAccounts(ctx context.Context) ([]*iam_pb.ServiceAccount, error) {
|
||||
accounts, err := s.CredentialStore.ListServiceAccounts(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, sa := range accounts {
|
||||
if _, err := s.decryptServiceAccount(ctx, sa); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return accounts, nil
|
||||
}
|
||||
|
||||
func (s *EncryptedCredentialStore) GetServiceAccountByAccessKey(ctx context.Context, accessKey string) (*iam_pb.ServiceAccount, error) {
|
||||
sa, err := s.CredentialStore.GetServiceAccountByAccessKey(ctx, accessKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.decryptServiceAccount(ctx, sa)
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package credential
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/seaweedfs/seaweedfs/weed/kms"
|
||||
"github.com/seaweedfs/seaweedfs/weed/kms/local"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// fakeStore is a minimal in-memory CredentialStore for exercising the
|
||||
// encryption wrapper; unneeded methods panic via the embedded nil interface.
|
||||
type fakeStore struct {
|
||||
CredentialStore
|
||||
config *iam_pb.S3ApiConfiguration
|
||||
users map[string]*iam_pb.Identity
|
||||
sas map[string]*iam_pb.ServiceAccount
|
||||
}
|
||||
|
||||
func newFakeStore() *fakeStore {
|
||||
return &fakeStore{users: map[string]*iam_pb.Identity{}, sas: map[string]*iam_pb.ServiceAccount{}}
|
||||
}
|
||||
|
||||
func (f *fakeStore) GetName() CredentialStoreTypeName { return "fake" }
|
||||
|
||||
func (f *fakeStore) CreateUser(ctx context.Context, identity *iam_pb.Identity) error {
|
||||
f.users[identity.Name] = proto.Clone(identity).(*iam_pb.Identity)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) GetUser(ctx context.Context, username string) (*iam_pb.Identity, error) {
|
||||
if u, ok := f.users[username]; ok {
|
||||
return proto.Clone(u).(*iam_pb.Identity), nil
|
||||
}
|
||||
return nil, ErrUserNotFound
|
||||
}
|
||||
|
||||
func (f *fakeStore) UpdateUser(ctx context.Context, username string, identity *iam_pb.Identity) error {
|
||||
return f.CreateUser(ctx, identity)
|
||||
}
|
||||
|
||||
func (f *fakeStore) LoadConfiguration(ctx context.Context) (*iam_pb.S3ApiConfiguration, error) {
|
||||
return proto.Clone(f.config).(*iam_pb.S3ApiConfiguration), nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) SaveConfiguration(ctx context.Context, config *iam_pb.S3ApiConfiguration) error {
|
||||
f.config = proto.Clone(config).(*iam_pb.S3ApiConfiguration)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) CreateAccessKey(ctx context.Context, username string, cred *iam_pb.Credential) error {
|
||||
f.users[username].Credentials = append(f.users[username].Credentials, proto.Clone(cred).(*iam_pb.Credential))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) CreateServiceAccount(ctx context.Context, sa *iam_pb.ServiceAccount) error {
|
||||
f.sas[sa.Id] = proto.Clone(sa).(*iam_pb.ServiceAccount)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) GetServiceAccount(ctx context.Context, id string) (*iam_pb.ServiceAccount, error) {
|
||||
return proto.Clone(f.sas[id]).(*iam_pb.ServiceAccount), nil
|
||||
}
|
||||
|
||||
func setupKMS(t *testing.T) {
|
||||
t.Helper()
|
||||
provider, err := local.NewLocalKMSProvider(nil)
|
||||
require.NoError(t, err)
|
||||
kms.SetGlobalKMSProvider(provider)
|
||||
t.Cleanup(func() { kms.SetGlobalKMSProvider(nil) })
|
||||
}
|
||||
|
||||
func testIdentity() *iam_pb.Identity {
|
||||
return &iam_pb.Identity{
|
||||
Name: "alice",
|
||||
Credentials: []*iam_pb.Credential{
|
||||
{AccessKey: "AKIA_TEST", SecretKey: "secret-one"},
|
||||
},
|
||||
Actions: []string{"Read"},
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptedStoreRoundTrip(t *testing.T) {
|
||||
setupKMS(t)
|
||||
fake := newFakeStore()
|
||||
store := NewEncryptedCredentialStore(fake, "")
|
||||
ctx := context.Background()
|
||||
|
||||
require.NoError(t, store.CreateUser(ctx, testIdentity()))
|
||||
|
||||
stored := fake.users["alice"].Credentials[0].SecretKey
|
||||
assert.NotEqual(t, "secret-one", stored)
|
||||
assert.True(t, strings.HasPrefix(stored, "{"), "stored secret should be an envelope: %s", stored)
|
||||
envelope, err := kms.ParseEnvelope([]byte(stored))
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, envelope.Ciphertext)
|
||||
assert.NotEmpty(t, envelope.ProviderSpecific[encryptedDataKeyField])
|
||||
|
||||
got, err := store.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "secret-one", got.Credentials[0].SecretKey)
|
||||
assert.Equal(t, "AKIA_TEST", got.Credentials[0].AccessKey)
|
||||
}
|
||||
|
||||
func TestEncryptedStorePlaintextPassthrough(t *testing.T) {
|
||||
fake := newFakeStore()
|
||||
fake.users["bob"] = testIdentity()
|
||||
fake.users["bob"].Name = "bob"
|
||||
store := NewEncryptedCredentialStore(fake, "")
|
||||
|
||||
got, err := store.GetUser(context.Background(), "bob")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "secret-one", got.Credentials[0].SecretKey)
|
||||
}
|
||||
|
||||
func TestEncryptedStoreNoKMSFailsWrite(t *testing.T) {
|
||||
kms.SetGlobalKMSProvider(nil)
|
||||
fake := newFakeStore()
|
||||
store := NewEncryptedCredentialStore(fake, "")
|
||||
|
||||
err := store.CreateUser(context.Background(), testIdentity())
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "KMS provider")
|
||||
assert.Empty(t, fake.users, "failed writes must not persist plaintext secrets")
|
||||
}
|
||||
|
||||
func TestEncryptedStoreSaveLoadConfiguration(t *testing.T) {
|
||||
setupKMS(t)
|
||||
fake := newFakeStore()
|
||||
store := NewEncryptedCredentialStore(fake, "")
|
||||
ctx := context.Background()
|
||||
|
||||
config := &iam_pb.S3ApiConfiguration{Identities: []*iam_pb.Identity{testIdentity()}}
|
||||
require.NoError(t, store.SaveConfiguration(ctx, config))
|
||||
assert.Equal(t, "secret-one", config.Identities[0].Credentials[0].SecretKey,
|
||||
"the caller's config object must not be mutated")
|
||||
|
||||
loaded, err := store.LoadConfiguration(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "secret-one", loaded.Identities[0].Credentials[0].SecretKey)
|
||||
|
||||
stored := fake.config.Identities[0].Credentials[0].SecretKey
|
||||
assert.NotEqual(t, "secret-one", stored)
|
||||
}
|
||||
|
||||
func TestEncryptedStoreServiceAccount(t *testing.T) {
|
||||
setupKMS(t)
|
||||
fake := newFakeStore()
|
||||
store := NewEncryptedCredentialStore(fake, "")
|
||||
ctx := context.Background()
|
||||
|
||||
sa := &iam_pb.ServiceAccount{
|
||||
Id: "sa-1",
|
||||
ParentUser: "alice",
|
||||
Credential: &iam_pb.Credential{AccessKey: "SA_KEY", SecretKey: "sa-secret"},
|
||||
}
|
||||
require.NoError(t, store.CreateServiceAccount(ctx, sa))
|
||||
|
||||
assert.NotEqual(t, "sa-secret", fake.sas["sa-1"].Credential.SecretKey)
|
||||
|
||||
got, err := store.GetServiceAccount(ctx, "sa-1")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "sa-secret", got.Credential.SecretKey)
|
||||
}
|
||||
|
||||
func TestEncryptedStoreNoDoubleWrap(t *testing.T) {
|
||||
setupKMS(t)
|
||||
fake := newFakeStore()
|
||||
store := NewEncryptedCredentialStore(fake, "")
|
||||
ctx := context.Background()
|
||||
|
||||
require.NoError(t, store.CreateUser(ctx, testIdentity()))
|
||||
storedOnce := fake.users["alice"].Credentials[0].SecretKey
|
||||
|
||||
// Saving an already-enveloped value must not wrap it again.
|
||||
identity := testIdentity()
|
||||
identity.Credentials[0].SecretKey = storedOnce
|
||||
require.NoError(t, store.UpdateUser(ctx, "alice", identity))
|
||||
assert.Equal(t, storedOnce, fake.users["alice"].Credentials[0].SecretKey)
|
||||
|
||||
got, err := store.GetUser(ctx, "alice")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "secret-one", got.Credentials[0].SecretKey)
|
||||
}
|
||||
Reference in new issue
Block a user