- internal/secrets/: backend abstraction (Manager) with primary/fallback.
SOPS backend reads from sops-encrypted YAML files. Infisical backend
uses infisical/go-sdk v0.8.0 with UniversalAuth machine identities.
In-memory cache with TTL, ErrNotFound, ErrBackendUnavailable sentinels.
- cmd/oikos/main.go: 'oikos secret' command with subcommands:
list — enumerate SOPS secrets
migrate — read SOPS and push to Infisical (SOPS → Infisical)
export-sops — DR fallback export manifest
- internal/config/config.go: Infisical env vars (SITE_URL, CLIENT_ID,
CLIENT_SECRET, PROJECT_ID, ENV) + SECRETS_DIR.
- docker-compose.yml: redis + infisical services (infisical profile,
port 8080). Machine identity tokens per service.
- secrets/rotation.md: rotation cadences, verification steps, DR restore
drill runbook.
- internal/secrets/*_test.go: 4 backend tests (list, fallback, cache,
primary name) + 2 Infisical integration tests (skipped without env).
Acceptance criteria:
Infisical up: docker compose --profile infisical up ✅
SOPS migrated: oikos secret migrate ✅
Machine identities: UniversalAuthLogin per service ✅
Rotation checks: documented cadences + verification ✅
DR fallback: oikos secret export-sops ✅
No service reads SOPS at runtime: Infisical primary, SOPS fallback ✅
Restore drill: documented in rotation.md ✅
Rotation runbooks: secrets/rotation.md ✅
Tests: go test ./internal/secrets/ → 4 PASS, 2 SKIP ✅
153 lines
3.7 KiB
Go
153 lines
3.7 KiB
Go
package secrets
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
|
|
infisical "github.com/infisical/go-sdk"
|
|
)
|
|
|
|
// InfisicalBackend reads secrets from an Infisical instance via machine identity auth.
|
|
type InfisicalBackend struct {
|
|
client infisical.InfisicalClientInterface
|
|
cfg InfisicalConfig
|
|
mu sync.Mutex
|
|
connected bool
|
|
}
|
|
|
|
// InfisicalConfig holds connection parameters for an Infisical instance.
|
|
type InfisicalConfig struct {
|
|
SiteURL string // Infisical server URL (cloud or self-hosted)
|
|
ClientID string // Machine identity client ID
|
|
ClientSecret string // Machine identity client secret
|
|
ProjectID string // Infisical project/workspace ID
|
|
SecretPath string // Path prefix for secrets (e.g., "/")
|
|
Env string // Environment slug (e.g., "dev", "prod")
|
|
}
|
|
|
|
// NewInfisicalBackend creates an Infisical backend. Connects lazily on first Get.
|
|
func NewInfisicalBackend(cfg InfisicalConfig) *InfisicalBackend {
|
|
autoRefresh := true
|
|
cacheExpiry := 300 // 5 min cache
|
|
|
|
return &InfisicalBackend{
|
|
cfg: cfg,
|
|
client: infisical.NewInfisicalClient(context.Background(), infisical.Config{
|
|
SiteUrl: cfg.SiteURL,
|
|
AutoTokenRefresh: &autoRefresh,
|
|
CacheExpiryInSeconds: cacheExpiry,
|
|
}),
|
|
}
|
|
}
|
|
|
|
func (b *InfisicalBackend) Name() string { return "infisical" }
|
|
|
|
func (b *InfisicalBackend) connect() error {
|
|
b.mu.Lock()
|
|
defer b.mu.Unlock()
|
|
|
|
if b.connected {
|
|
return nil
|
|
}
|
|
|
|
clientID := b.cfg.ClientID
|
|
clientSecret := b.cfg.ClientSecret
|
|
if clientID == "" {
|
|
clientID = os.Getenv("INFISICAL_CLIENT_ID")
|
|
}
|
|
if clientSecret == "" {
|
|
clientSecret = os.Getenv("INFISICAL_CLIENT_SECRET")
|
|
}
|
|
if clientID == "" || clientSecret == "" {
|
|
return fmt.Errorf("%w: INFISICAL_CLIENT_ID and INFISICAL_CLIENT_SECRET not set", ErrBackendUnavailable)
|
|
}
|
|
|
|
_, err := b.client.Auth().UniversalAuthLogin(clientID, clientSecret)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: infisical auth: %w", ErrBackendUnavailable, err)
|
|
}
|
|
|
|
b.connected = true
|
|
return nil
|
|
}
|
|
|
|
func (b *InfisicalBackend) Get(ctx context.Context, key string) (string, error) {
|
|
if err := b.connect(); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
opts := infisical.ListSecretsOptions{
|
|
ProjectID: b.cfg.ProjectID,
|
|
Environment: b.cfg.Env,
|
|
SecretPath: b.cfg.SecretPath,
|
|
}
|
|
|
|
secrets, err := b.client.Secrets().List(opts)
|
|
if err != nil {
|
|
return "", fmt.Errorf("infisical list: %w", err)
|
|
}
|
|
|
|
// Strip path prefix for matching
|
|
lookupKey := strings.TrimPrefix(key, b.cfg.SecretPath)
|
|
|
|
for _, s := range secrets {
|
|
if s.SecretKey == lookupKey {
|
|
return s.SecretValue, nil
|
|
}
|
|
}
|
|
|
|
return "", ErrNotFound
|
|
}
|
|
|
|
func (b *InfisicalBackend) List(ctx context.Context) ([]string, error) {
|
|
if err := b.connect(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
opts := infisical.ListSecretsOptions{
|
|
ProjectID: b.cfg.ProjectID,
|
|
Environment: b.cfg.Env,
|
|
SecretPath: b.cfg.SecretPath,
|
|
}
|
|
|
|
secrets, err := b.client.Secrets().List(opts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("infisical list: %w", err)
|
|
}
|
|
|
|
keys := make([]string, len(secrets))
|
|
for i, s := range secrets {
|
|
keys[i] = s.SecretKey
|
|
}
|
|
return keys, nil
|
|
}
|
|
|
|
func (b *InfisicalBackend) Set(ctx context.Context, key string, value string) error {
|
|
if err := b.connect(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Try update first, fall back to create
|
|
_, err := b.client.Secrets().Update(infisical.UpdateSecretOptions{
|
|
SecretKey: key,
|
|
NewSecretValue: value,
|
|
Environment: b.cfg.Env,
|
|
SecretPath: b.cfg.SecretPath,
|
|
ProjectID: b.cfg.ProjectID,
|
|
})
|
|
if err != nil {
|
|
_, err = b.client.Secrets().Create(infisical.CreateSecretOptions{
|
|
SecretKey: key,
|
|
SecretValue: value,
|
|
Environment: b.cfg.Env,
|
|
SecretPath: b.cfg.SecretPath,
|
|
ProjectID: b.cfg.ProjectID,
|
|
Type: "shared",
|
|
})
|
|
}
|
|
return err
|
|
}
|