Documentation
¶
Overview ¶
Package secrets provides a unified interface for retrieving secrets from various sources.
This package follows the standard service pattern used throughout the services layer:
Interface Definition (provider.go): Defines the Provider interface for retrieving secrets.
Environment Implementation (env.go): Reads secrets from environment variables. Use this for local development and testing.
AWS Implementation (aws.go): Reads secrets from AWS Secrets Manager. Use this for Lambda/production deployments.
Mock Implementation (mock.go): Provides a configurable mock for testing.
Usage ¶
In local/test environments:
provider := secrets.NewEnvProvider() password, err := provider.GetSecret(ctx, "db_write_password")
In Lambda/production:
provider, err := secrets.NewAWSProvider(ctx, secretARN) allSecrets, err := provider.GetAllSecrets(ctx)
Index ¶
- Constants
- Variables
- type AWSProvider
- type EnvProvider
- type MockProvider
- func (m *MockProvider) GetAllSecrets(ctx context.Context) (map[string]string, error)
- func (m *MockProvider) GetAllSecretsCallCount() int64
- func (m *MockProvider) GetSecret(ctx context.Context, key string) (string, error)
- func (m *MockProvider) GetSecretCallCount() int64
- func (m *MockProvider) ResetCallCounts()
- func (m *MockProvider) SetSecret(key, value string)
- type Provider
Constants ¶
const ( // KeyDBWritePassword is the key for the write database password. KeyDBWritePassword = "db_write_password" // KeyDBReadPassword is the key for the read database password. KeyDBReadPassword = "db_read_password" )
Secret key constants - these are the keys used in the JSON secret stored in Secrets Manager. They map to the environment variable suffixes (without the project-specific prefix).
Variables ¶
var ( // ErrSecretNotFound is returned when a requested secret key does not exist. ErrSecretNotFound = errors.New("secret not found") // ErrProviderNotInitialized is returned when the provider is used before initialization. ErrProviderNotInitialized = errors.New("secrets provider not initialized") // ErrInvalidSecretFormat is returned when the secret data cannot be parsed. ErrInvalidSecretFormat = errors.New("invalid secret format") // ErrAWSSecretsManager is returned when AWS Secrets Manager operations fail. ErrAWSSecretsManager = errors.New("AWS Secrets Manager error") )
Sentinel errors for secrets operations.
Functions ¶
This section is empty.
Types ¶
type AWSProvider ¶
type AWSProvider struct {
// contains filtered or unexported fields
}
AWSProvider reads secrets from AWS Secrets Manager. It fetches a single JSON secret and caches the parsed values in memory. Use this for Lambda/production deployments.
func NewAWSProvider ¶
func NewAWSProvider(ctx context.Context, secretARN string) (*AWSProvider, error)
NewAWSProvider creates a new AWS Secrets Manager provider. The secretARN should point to a JSON secret containing key-value pairs.
Example secret format in Secrets Manager:
{
"db_write_password": "secret123",
"db_read_password": "secret456",
"api_key": "apikey789"
}
func NewAWSProviderWithClient ¶
func NewAWSProviderWithClient(client *secretsmanager.Client, secretARN string) *AWSProvider
NewAWSProviderWithClient creates an AWSProvider with a custom Secrets Manager client. This is useful for testing with mock clients.
func (*AWSProvider) GetAllSecrets ¶
GetAllSecrets retrieves all secrets from AWS Secrets Manager. Results are cached in memory after the first call.
func (*AWSProvider) GetSecret ¶
GetSecret retrieves a single secret value by key. On first call, fetches and caches all secrets from Secrets Manager.
func (*AWSProvider) Refresh ¶
func (p *AWSProvider) Refresh()
Refresh clears the cache and forces a refresh on the next GetSecret call. This can be useful for long-running processes that need to pick up rotated secrets.
type EnvProvider ¶
type EnvProvider struct {
// contains filtered or unexported fields
}
EnvProvider reads secrets from environment variables. Use this for local development and testing where secrets are set directly in environment variables rather than fetched from AWS Secrets Manager.
func NewEnvProvider ¶
func NewEnvProvider() *EnvProvider
NewEnvProvider creates a new environment-based secrets provider.
func NewEnvProviderWithMapping ¶
func NewEnvProviderWithMapping(mapping map[string]string) *EnvProvider
NewEnvProviderWithMapping creates an EnvProvider with custom key-to-envvar mapping. This is useful for testing with custom environment variable names.
func (*EnvProvider) GetAllSecrets ¶
GetAllSecrets retrieves all mapped secrets from environment variables. Only returns secrets that have non-empty values.
type MockProvider ¶
type MockProvider struct {
// GetSecretFunc is called when GetSecret is invoked.
// If nil, returns values from the Secrets map.
GetSecretFunc func(ctx context.Context, key string) (string, error)
// GetAllSecretsFunc is called when GetAllSecrets is invoked.
// If nil, returns a copy of the Secrets map.
GetAllSecretsFunc func(ctx context.Context) (map[string]string, error)
// Secrets holds the mock secret values for default behavior.
Secrets map[string]string
// contains filtered or unexported fields
}
MockProvider is a configurable mock for testing secrets retrieval. It allows customizing the behavior of GetSecret and GetAllSecrets methods.
func NewMockProvider ¶
func NewMockProvider() *MockProvider
NewMockProvider creates a new mock provider with empty secrets.
func NewMockProviderWithSecrets ¶
func NewMockProviderWithSecrets(secrets map[string]string) *MockProvider
NewMockProviderWithSecrets creates a mock provider with predefined secrets.
func NewTestSecretsProvider ¶
func NewTestSecretsProvider() *MockProvider
NewTestSecretsProvider creates a mock provider with typical test values. Use this in unit tests that need database credentials.
func (*MockProvider) GetAllSecrets ¶
GetAllSecrets retrieves all secrets from the mock.
func (*MockProvider) GetAllSecretsCallCount ¶
func (m *MockProvider) GetAllSecretsCallCount() int64
GetAllSecretsCallCount returns the number of times GetAllSecrets was called.
func (*MockProvider) GetSecretCallCount ¶
func (m *MockProvider) GetSecretCallCount() int64
GetSecretCallCount returns the number of times GetSecret was called.
func (*MockProvider) ResetCallCounts ¶
func (m *MockProvider) ResetCallCounts()
ResetCallCounts resets all call counters to zero.
func (*MockProvider) SetSecret ¶
func (m *MockProvider) SetSecret(key, value string)
SetSecret sets a single secret value in the mock.
type Provider ¶
type Provider interface {
// GetSecret retrieves a single secret value by key.
// Returns ErrSecretNotFound if the key does not exist.
GetSecret(ctx context.Context, key string) (string, error)
// GetAllSecrets retrieves all secrets as a key-value map.
// This is more efficient when multiple secrets are needed.
GetAllSecrets(ctx context.Context) (map[string]string, error)
}
Provider defines the interface for retrieving secrets. Implementations can read from environment variables, AWS Secrets Manager, or other sources.