service

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 29, 2026 License: AGPL-3.0 Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultGeneratedSecretBytes = 32

DefaultGeneratedSecretBytes is the default randomness for GenerateSecret. 32 bytes (→ 64 hex chars) is comfortably above the 256-bit threshold for HMAC-SHA256 keys and gives plenty of margin for future schemes.

View Source
const MaxGeneratedSecretBytes = 128

MaxGeneratedSecretBytes is the cap accepted by GenerateSecret. Beyond this the secret is almost certainly being used as a payload, not a key.

View Source
const MinGeneratedSecretBytes = 16

MinGeneratedSecretBytes is the floor. 16 bytes is the smallest size where brute-force is computationally infeasible for current/near-future attackers.

Variables

This section is empty.

Functions

This section is empty.

Types

type Resolver

type Resolver interface {
	GetDecryptedValueByName(ctx context.Context, name, tenantID string) (string, error)
}

Resolver is the narrow read-only contract consumers depend on when they only need to look up a decrypted secret by name. Prefer this over *Service in consumer signatures — it keeps the API surface minimal (no write/admin methods exposed), makes tests trivial to stub, and lets callers layer behavior (tenant→platform fallback, caching, audit) by composing a wrapper that still satisfies Resolver.

The concrete *Service satisfies this interface, so any caller that already holds a *Service can pass it directly where a Resolver is expected.

type Secret

type Secret struct {
	// Name is a human-readable identifier for this secret, unique per tenant.
	Name string

	// Value is the plaintext secret. The service encrypts it using AES-256-GCM
	// before persisting. Never logged or returned via the admin API.
	Value string

	// ConnectorType identifies the connector this secret belongs to,
	// e.g. "smtp", "slack", "stripe", "aws".
	ConnectorType string

	// Description is an optional human-readable note about the secret.
	Description string

	// TenantID scopes the secret to a tenant (optional).
	TenantID string

	// CreatedBy is the user ID that created this secret (optional).
	CreatedBy string
}

Secret is the caller-facing type for storing a connector secret. Callers pass the plaintext Value — the service encrypts it internally.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides secure secret storage with AES-256-GCM encryption at rest. The encryption key is managed via crypto.KeyStore — callers pass plaintext, and the service handles all cryptographic operations.

func NewService

func NewService(store *db.Store, keyStore *crypto.KeyStore) *Service

NewService creates a Service. The KeyStore must contain at least the current encryption key version. Panics if keyStore is nil — failing open is not acceptable for a secret management service.

func (*Service) CreateSecret

func (s *Service) CreateSecret(ctx context.Context, sec Secret) (repository.SecrSecret, error)

CreateSecret encrypts + persists a new secret and returns the stored row so HTTP handlers can build a response DTO without re-querying. Keeps the encrypt-then-insert dance inside this package (the store API takes pre-encrypted bytes; the handler must not touch plaintext beyond passing it here).

func (*Service) GenerateSecret

func (s *Service) GenerateSecret(ctx context.Context, sec Secret, lengthBytes int) (repository.SecrSecret, string, error)

GenerateSecret mints a strong random value (hex-encoded) and stores it as a new Secret, returning both the persisted row and the plaintext so the caller can show it to the user exactly once.

Use this when the caller has no pre-existing value (HMAC keys, internal signing keys). The plaintext is never logged and never recoverable after this call — the database only sees the encrypted form.

lengthBytes is clamped to [MinGeneratedSecretBytes, MaxGeneratedSecretBytes]. 0 means "use DefaultGeneratedSecretBytes."

func (*Service) GetDecryptedValue

func (s *Service) GetDecryptedValue(ctx context.Context, id uuid.UUID) (string, error)

GetDecryptedValue retrieves a secret by ID and decrypts its value. This is the only way to obtain the plaintext — the admin API never exposes it.

func (*Service) GetDecryptedValueByName

func (s *Service) GetDecryptedValueByName(ctx context.Context, name, tenantID string) (string, error)

GetDecryptedValueByName retrieves an active secret by name and tenant, and decrypts it. Connectors should use this to look up their secrets at runtime.

tenantID selects the scope exactly, never approximately:

  • "<tid>" → that tenant's row only.
  • "" → the PLATFORM row (tenant_id IS NULL) only.

An empty tenantID does not mean "any tenant". Callers wanting the tenant-first, platform-fallback behaviour must ask for the two scopes in turn; this method never falls back on its own.

func (*Service) RevokeSecretByName

func (s *Service) RevokeSecretByName(ctx context.Context, name, tenantID string) error

RevokeSecretByName revokes the active secret of that name in that scope (tenantID "" is the platform scope, as in GetDecryptedValueByName). No active secret is not an error, so a retried revoke succeeds.

func (*Service) RotateSecret

func (s *Service) RotateSecret(ctx context.Context, sec Secret, lengthBytes int) (repository.SecrSecret, string, error)

RotateSecret revokes the active secret named sec.Name in sec.TenantID's scope (if there is one) and generates a new value under the same name, in one transaction: the name never resolves to nothing, and never to both. lengthBytes is clamped as in GenerateSecret.

func (*Service) StoreSecret

func (s *Service) StoreSecret(ctx context.Context, sec Secret) error

StoreSecret encrypts the plaintext value and persists it.

func (*Service) StoreSecretTx

func (s *Service) StoreSecretTx(ctx context.Context, tx pgx.Tx, sec Secret) error

StoreSecretTx encrypts the plaintext value and persists it inside an existing pgx.Tx.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL