41 lines
1.3 KiB
Go
41 lines
1.3 KiB
Go
package statestore
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher"
|
|
"github.com/samcharles93/archie-core/internal/secret"
|
|
)
|
|
|
|
// bindingCipherFromConfig resolves the binding at-rest encryption key from
|
|
// config through the secret registry and returns the EDA store's cipher. An
|
|
// unset encryption_key returns a nil cipher, keeping the legacy plaintext path
|
|
// (the store's default).
|
|
func bindingCipherFromConfig(cfg config.Config, secrets *secret.Registry) (bindingcipher.BindingCipher, error) {
|
|
if cfg.Bindings.EncryptionKey == (secret.SecretRef{}) {
|
|
return nil, nil
|
|
}
|
|
active, err := secrets.Resolve(cfg.Bindings.EncryptionKey)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve bindings encryption_key: %w", err)
|
|
}
|
|
active = strings.TrimSpace(active)
|
|
if active == "" {
|
|
return nil, fmt.Errorf("bindings encryption_key resolved empty")
|
|
}
|
|
|
|
previous := make([]string, 0, len(cfg.Bindings.PreviousEncryptionKeys))
|
|
for _, ref := range cfg.Bindings.PreviousEncryptionKeys {
|
|
v, err := secrets.Resolve(ref)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolve bindings previous_encryption_key: %w", err)
|
|
}
|
|
if v = strings.TrimSpace(v); v != "" {
|
|
previous = append(previous, v)
|
|
}
|
|
}
|
|
return bindingcipher.NewBindingCipher(active, previous)
|
|
}
|