postgres

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSchema is the default PostgreSQL schema for encryption keys.
	DefaultSchema = "infrastructure"
	// DefaultTable is the default PostgreSQL table name for encryption keys.
	DefaultTable = "encryption_keys"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Schema string
	Table  string
}

Config holds the schema and table names for the PostgreSQL keystore.

func ApplyDefaults

func ApplyDefaults(cfg Config) Config

ApplyDefaults fills in the default schema and table names when omitted.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default PostgreSQL keystore configuration.

type EncryptedKey

type EncryptedKey struct {
	Scope        string
	ScopeID      string
	KeyVersion   int
	EncryptedDEK []byte
	SystemKeyID  string
	CreatedAt    time.Time
	RevokedAt    *time.Time
}

EncryptedKey represents a stored encrypted DEK in PostgreSQL.

type RewrapSystemKeysOptions

type RewrapSystemKeysOptions struct {
	FromSystemKeyID string
	ToSystemKeyID   string
	BatchSize       int
	DryRun          bool
}

RewrapSystemKeysOptions configures RewrapSystemKeys.

type RewrapSystemKeysResult

type RewrapSystemKeysResult struct {
	MatchedRows   int
	RewrappedRows int
	SkippedRows   int
	RemainingRows int
	Batches       int
}

RewrapSystemKeysResult summarizes a system-key rewrap run.

func RewrapSystemKeys

func RewrapSystemKeys(ctx context.Context, pool *pgxpool.Pool, cfg Config, keyring systemkey.Keyring, c cipher.Cipher, opts RewrapSystemKeysOptions) (RewrapSystemKeysResult, error)

RewrapSystemKeys re-encrypts stored DEKs from one system key to another in short PostgreSQL-managed batch transactions.

This is an administrative operation for retiring old system keys. It updates only the stored encrypted DEK and system_key_id for matching rows; it does not create new DEK versions or touch application ciphertext.

type Store

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

Store coordinates DEK generation, wrapping, and persistence in PostgreSQL. It is a stateless service; all database operations require an explicit pgx connection.

func NewStore

func NewStore(env *envelope.Envelope, cfg Config) *Store

NewStore creates a new PostgreSQL keystore wired to the given envelope crypto engine.

func (*Store) ActiveKeyVersion

func (s *Store) ActiveKeyVersion(ctx context.Context, conn pgxConn, scope, scopeID string) (int, error)

ActiveKeyVersion returns the version number of the active key for the given scope.

func (*Store) CreateKey

func (s *Store) CreateKey(ctx context.Context, conn pgxConn, scope, scopeID string) (int, error)

CreateKey generates a new DEK via envelope, wraps it with the active system key, and inserts it into PostgreSQL at version 1. Returns ErrKeyExists if an active key already exists.

func (*Store) DestroyKeys

func (s *Store) DestroyKeys(ctx context.Context, conn pgxConn, scope, scopeID string) error

DestroyKeys permanently deletes all keys for the given scope (GDPR crypto-shredding).

func (*Store) GetActiveKey

func (s *Store) GetActiveKey(ctx context.Context, conn pgxConn, scope, scopeID string) (*EncryptedKey, error)

GetActiveKey returns the highest-version non-revoked key for the given scope.

func (*Store) GetKey

func (s *Store) GetKey(ctx context.Context, conn pgxConn, scope, scopeID string, version int) (*EncryptedKey, error)

GetKey returns a specific key version for the given scope.

func (*Store) RevokeKeys

func (s *Store) RevokeKeys(ctx context.Context, conn pgxConn, scope, scopeID string) error

RevokeKeys marks all active keys for the given scope as revoked, except the highest version (the current active key).

func (*Store) RotateKey

func (s *Store) RotateKey(ctx context.Context, conn pgxConn, scope, scopeID string) (int, error)

RotateKey generates a new DEK, wraps it with the active system key, inserts it with version = current_active_version + 1, and revokes all older versions.

Directories

Path Synopsis
Package migrations exposes the embedded PostgreSQL keystore migration.
Package migrations exposes the embedded PostgreSQL keystore migration.

Jump to

Keyboard shortcuts

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