Documentation
¶
Overview ¶
Package encryption provides data encryption at rest for the EventSourcing framework.
This package implements SEC-103 (Data Encryption at Rest) from the security roadmap, providing comprehensive encryption support for events, snapshots, and sensitive data.
Example usage:
// Create encryption service with master key
masterKey := []byte("my-32-byte-master-key-here!!!")
encryptor, err := encryption.NewService(masterKey)
// Encrypt data
ciphertext, err := encryptor.Encrypt([]byte("sensitive data"))
// Decrypt data
plaintext, err := encryptor.Decrypt(ciphertext)
Index ¶
- Variables
- func DecodeKey(encodedKey string) ([]byte, error)
- func DeriveKey(password string, salt []byte, config *Config) ([]byte, error)
- func EncodeKey(key []byte) string
- func GenerateAndEncodeKey(size int) (string, error)
- func GenerateKey(size int) ([]byte, error)
- func GenerateSalt() ([]byte, error)
- type Algorithm
- type Cipher
- func (c *Cipher) Decrypt(ciphertext []byte) ([]byte, error)
- func (c *Cipher) DecryptString(ciphertext string) (string, error)
- func (c *Cipher) Encrypt(plaintext []byte) ([]byte, error)
- func (c *Cipher) EncryptString(plaintext string) (string, error)
- func (c *Cipher) KeyID() string
- func (c *Cipher) SetKeyID(keyID string)
- type Config
- type EventEncryptionConfig
- type EventEncryptor
- func (ee *EventEncryptor) DecryptEvent(event *eventsourcing.Event) (*eventsourcing.Event, error)
- func (ee *EventEncryptor) DecryptEvents(events []*eventsourcing.Event) ([]*eventsourcing.Event, error)
- func (ee *EventEncryptor) EncryptEvent(event *eventsourcing.Event) (*eventsourcing.Event, error)
- func (ee *EventEncryptor) EncryptEvents(events []*eventsourcing.Event) ([]*eventsourcing.Event, error)
- type Key
- type KeyDerivation
- type KeyHelper
- type KeyInfo
- type KeyManager
- func (km *KeyManager) AddKey(id string, key []byte, active bool) error
- func (km *KeyManager) AddKeyWithPassword(id, password string, salt []byte, active bool) error
- func (km *KeyManager) ExportKeys() (string, error)
- func (km *KeyManager) GetActiveKey() (*Key, error)
- func (km *KeyManager) GetKey(id string) (*Key, error)
- func (km *KeyManager) ListKeys() []KeyInfo
- func (km *KeyManager) RemoveKey(id string) error
- func (km *KeyManager) RotateKey() (string, error)
- func (km *KeyManager) SetActiveKey(id string) error
- type Service
- func NewService(masterKey []byte) (*Service, error)
- func NewServiceFromCredentialProvider(ctx context.Context, provider credentials.Provider) (*Service, error)
- func NewServiceFromCredentialProviderWithConfig(ctx context.Context, provider credentials.Provider, config *Config) (*Service, error)
- func NewServiceWithConfig(masterKey []byte, config *Config) (*Service, error)
- func NewServiceWithPassword(password string, salt []byte) (*Service, error)
- func NewServiceWithPasswordAndConfig(password string, salt []byte, config *Config) (*Service, error)
- func (s *Service) Decrypt(ciphertext string) ([]byte, error)
- func (s *Service) DecryptString(ciphertext string) (string, error)
- func (s *Service) Encrypt(plaintext []byte) (string, error)
- func (s *Service) EncryptString(plaintext string) (string, error)
- func (s *Service) KeyManager() *KeyManager
- func (s *Service) ReEncrypt(oldCiphertext string) (string, error)
- func (s *Service) RotateKey() (string, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidCiphertext is returned when ciphertext is invalid or corrupted ErrInvalidCiphertext = errors.New("invalid ciphertext") // ErrInvalidKey is returned when encryption key is invalid ErrInvalidKey = errors.New("invalid encryption key") // ErrDecryptionFailed is returned when decryption fails ErrDecryptionFailed = errors.New("decryption failed") // ErrEncryptionFailed is returned when encryption fails ErrEncryptionFailed = errors.New("encryption failed") )
var ( // ErrKeyNotFound is returned when a key is not found ErrKeyNotFound = errors.New("encryption key not found") // ErrNoActiveKey is returned when no active key is configured ErrNoActiveKey = errors.New("no active encryption key") )
Functions ¶
func GenerateAndEncodeKey ¶
GenerateAndEncodeKey generates a new key and returns it base64-encoded
func GenerateKey ¶
GenerateKey generates a random encryption key of the specified size
func GenerateSalt ¶
GenerateSalt generates a random salt for key derivation
Types ¶
type Cipher ¶
type Cipher struct {
// contains filtered or unexported fields
}
Cipher provides encryption and decryption operations
func NewCipher ¶
NewCipher creates a new cipher with the given key The key should be properly derived using DeriveKey if it's a password
func NewCipherWithPassword ¶
NewCipherWithPassword creates a new cipher by deriving a key from a password
func (*Cipher) DecryptString ¶
DecryptString decrypts a base64-encoded ciphertext string
func (*Cipher) Encrypt ¶
Encrypt encrypts plaintext and returns ciphertext Format: nonce || ciphertext || tag
func (*Cipher) EncryptString ¶
EncryptString encrypts a string and returns base64-encoded ciphertext
type Config ¶
type Config struct {
// Algorithm to use for encryption
Algorithm Algorithm
// KeyDerivation function to use
KeyDerivation KeyDerivation
// KeySize in bytes (16 for AES-128, 32 for AES-256)
KeySize int
// Argon2 parameters (used if KeyDerivation is argon2id)
Argon2Time uint32 // Number of iterations
Argon2Memory uint32 // Memory in KiB
Argon2Threads uint8 // Number of threads
// PBKDF2 parameters (used if KeyDerivation is pbkdf2)
PBKDF2Iterations int
}
Config represents encryption configuration
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns secure default encryption configuration
func FastConfig ¶
func FastConfig() *Config
FastConfig returns a faster but less secure configuration Suitable for development/testing only
type EventEncryptionConfig ¶
type EventEncryptionConfig struct {
// EncryptData encrypts the event data payload
EncryptData bool
// FieldEncryption enables field-level encryption
// Only specified fields are encrypted, others remain plaintext
FieldEncryption bool
// EncryptedFields lists which fields to encrypt (if FieldEncryption is true)
// Example: []string{"password", "ssn", "credit_card"}
EncryptedFields []string
}
EventEncryptionConfig configures event encryption
func DefaultEventEncryptionConfig ¶
func DefaultEventEncryptionConfig() *EventEncryptionConfig
DefaultEventEncryptionConfig returns default event encryption configuration
type EventEncryptor ¶
type EventEncryptor struct {
// contains filtered or unexported fields
}
EventEncryptor provides encryption for event data
func NewEventEncryptor ¶
func NewEventEncryptor(service *Service, config *EventEncryptionConfig) *EventEncryptor
NewEventEncryptor creates a new event encryptor
func (*EventEncryptor) DecryptEvent ¶
func (ee *EventEncryptor) DecryptEvent(event *eventsourcing.Event) (*eventsourcing.Event, error)
DecryptEvent decrypts event data Returns a new event with decrypted data
func (*EventEncryptor) DecryptEvents ¶
func (ee *EventEncryptor) DecryptEvents(events []*eventsourcing.Event) ([]*eventsourcing.Event, error)
DecryptEvents decrypts multiple events
func (*EventEncryptor) EncryptEvent ¶
func (ee *EventEncryptor) EncryptEvent(event *eventsourcing.Event) (*eventsourcing.Event, error)
EncryptEvent encrypts event data based on configuration Returns a new event with encrypted data
func (*EventEncryptor) EncryptEvents ¶
func (ee *EventEncryptor) EncryptEvents(events []*eventsourcing.Event) ([]*eventsourcing.Event, error)
EncryptEvents encrypts multiple events
type KeyDerivation ¶
type KeyDerivation string
KeyDerivation represents the key derivation function
const ( // KeyDerivationArgon2 uses Argon2id (recommended for new applications) KeyDerivationArgon2 KeyDerivation = "argon2id" // KeyDerivationPBKDF2 uses PBKDF2-SHA256 (compatible with legacy systems) KeyDerivationPBKDF2 KeyDerivation = "pbkdf2-sha256" // KeyDerivationNone uses the key directly (only if key is already derived) KeyDerivationNone KeyDerivation = "none" )
type KeyHelper ¶
type KeyHelper struct {
// contains filtered or unexported fields
}
KeyHelper provides utilities for key management with credential providers
func NewKeyHelper ¶
func NewKeyHelper(provider credentials.Provider, config *Config) *KeyHelper
NewKeyHelper creates a new key helper
type KeyInfo ¶
type KeyInfo struct {
// ID is a unique identifier for the key
ID string `json:"id"`
// Version of the key (increments on rotation)
Version int `json:"version"`
// CreatedAt is when the key was created
CreatedAt time.Time `json:"created_at"`
// RotatedAt is when the key was last rotated
RotatedAt time.Time `json:"rotated_at,omitempty"`
// ExpiresAt is when the key expires (optional)
ExpiresAt time.Time `json:"expires_at,omitempty"`
// Active indicates if this is the current encryption key
Active bool `json:"active"`
// Algorithm used with this key
Algorithm Algorithm `json:"algorithm"`
// Purpose describes what this key is used for
Purpose string `json:"purpose,omitempty"`
}
KeyInfo contains metadata about an encryption key
type KeyManager ¶
type KeyManager struct {
// contains filtered or unexported fields
}
KeyManager manages encryption keys with support for key rotation
func NewKeyManager ¶
func NewKeyManager(config *Config) *KeyManager
NewKeyManager creates a new key manager
func (*KeyManager) AddKey ¶
func (km *KeyManager) AddKey(id string, key []byte, active bool) error
AddKey adds a new encryption key
func (*KeyManager) AddKeyWithPassword ¶
func (km *KeyManager) AddKeyWithPassword(id, password string, salt []byte, active bool) error
AddKeyWithPassword adds a new encryption key derived from a password
func (*KeyManager) ExportKeys ¶
func (km *KeyManager) ExportKeys() (string, error)
ExportKeys exports key metadata (not the actual keys!) as JSON This is useful for backup and disaster recovery documentation
func (*KeyManager) GetActiveKey ¶
func (km *KeyManager) GetActiveKey() (*Key, error)
GetActiveKey returns the current active encryption key
func (*KeyManager) GetKey ¶
func (km *KeyManager) GetKey(id string) (*Key, error)
GetKey retrieves a key by ID
func (*KeyManager) RemoveKey ¶
func (km *KeyManager) RemoveKey(id string) error
RemoveKey removes a key (cannot remove active key)
func (*KeyManager) RotateKey ¶
func (km *KeyManager) RotateKey() (string, error)
RotateKey generates a new key and sets it as active Old keys are kept for decryption of existing data
func (*KeyManager) SetActiveKey ¶
func (km *KeyManager) SetActiveKey(id string) error
SetActiveKey sets the active encryption key
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service provides high-level encryption/decryption operations with key management
func NewService ¶
NewService creates a new encryption service with a master key
func NewServiceFromCredentialProvider ¶
func NewServiceFromCredentialProvider(ctx context.Context, provider credentials.Provider) (*Service, error)
NewServiceFromCredentialProvider creates an encryption service using a credential provider The credential's Token field should contain a base64-encoded encryption key
Example:
// AWS Secrets Manager
provider, _ := credentials.NewSecretProvider(ctx,
"awsparamstore:///prod/encryption/master-key")
service, _ := encryption.NewServiceFromCredentialProvider(ctx, provider)
// Environment variable
provider := credentials.NewEnvProvider("ENCRYPTION_KEY", nil)
service, _ := encryption.NewServiceFromCredentialProvider(ctx, provider)
func NewServiceFromCredentialProviderWithConfig ¶
func NewServiceFromCredentialProviderWithConfig(ctx context.Context, provider credentials.Provider, config *Config) (*Service, error)
NewServiceFromCredentialProviderWithConfig creates an encryption service with custom config
func NewServiceWithConfig ¶
NewServiceWithConfig creates a new encryption service with custom configuration
func NewServiceWithPassword ¶
NewServiceWithPassword creates a new encryption service with a password
func NewServiceWithPasswordAndConfig ¶
func NewServiceWithPasswordAndConfig(password string, salt []byte, config *Config) (*Service, error)
NewServiceWithPasswordAndConfig creates a new encryption service with password and config
func (*Service) Decrypt ¶
Decrypt decrypts data encrypted by this service Automatically determines which key to use based on the key ID in the ciphertext
func (*Service) DecryptString ¶
DecryptString decrypts a string
func (*Service) Encrypt ¶
Encrypt encrypts data using the active encryption key Returns: base64(keyID:ciphertext)
func (*Service) EncryptString ¶
EncryptString encrypts a string
func (*Service) KeyManager ¶
func (s *Service) KeyManager() *KeyManager
KeyManager returns the underlying key manager