Documentation
¶
Overview ¶
Package bootstrap applies deploy-time state that must not live in a committed migration, such as secrets that come from the environment.
Index ¶
- Constants
- Variables
- func CheckDefaultEngineAlias(ctx context.Context, q DefaultEngineAliasStore, alias string) error
- func EnsureDefaultTrinoEngine(ctx context.Context, logger *slog.Logger, q DefaultEngineStore, ...) error
- func EnsureTrinoRole(ctx context.Context, db *gorm.DB) error
- func EnsureTrinoRoleSQL(ctx context.Context, db *sql.DB) error
- func LoadKeyring(ctx context.Context, logger *slog.Logger, cfg *config.Config, ...) (*crypto.Keyring, error)
- func LoadRegistryKeyring(cfg *config.Config) (*crypto.Keyring, error)
- func PrepareAPI(ctx context.Context, logger *slog.Logger, cfg *config.Config, st APIState) error
- func PublishInitialDataPolicies(ctx context.Context, logger *slog.Logger, p InitialPublisher)
- func ReconcileIncompleteDirectConnections(ctx context.Context, store *gorm.DB, kr *crypto.Keyring, ...) error
- func SetTrinoRolePassword(ctx context.Context, db *gorm.DB, password string) error
- func SplitDualPathConnections(ctx context.Context, store *gorm.DB, kr *crypto.Keyring, logger *slog.Logger) error
- type APIState
- type CredentialCounter
- type DefaultEngineAliasStore
- type DefaultEngineStore
- type DefaultTrinoEngine
- type InitialPublisher
Constants ¶
const TrinoPasswordEnv = "BALEIA_TRINO_PASSWORD" //nolint:gosec // G101: a variable name, not a credential
TrinoPasswordEnv is the variable the default engine's password reference names. It is Baleia's own configuration, which a connection may not read, but Baleia stores this reference itself, so the resolver resolves it (connections.SecretResolver.SystemEnv); a request may not store it.
const TrinoPasswordRef = "@baleia-secret[env:" + TrinoPasswordEnv + "]"
TrinoPasswordRef is the external secret reference the default engine stores in place of the password.
Variables ¶
var ErrEncryptionKeyRequired = errors.New("BALEIA_ENCRYPTION_KEY is required: the database holds stored credentials")
ErrEncryptionKeyRequired is returned when stored credentials exist but no BALEIA_ENCRYPTION_KEY is configured: serving would make every one of them unreadable.
Functions ¶
func CheckDefaultEngineAlias ¶
func CheckDefaultEngineAlias(ctx context.Context, q DefaultEngineAliasStore, alias string) error
CheckDefaultEngineAlias refuses a BALEIA_DEFAULT_ENGINE_ID that is the id of an engine other than the default Trino engine. The default engine's OPA reports its status under that id (ADR 0028), and Baleia resolves it to the default engine, so it must not name another one.
func EnsureDefaultTrinoEngine ¶
func EnsureDefaultTrinoEngine(ctx context.Context, logger *slog.Logger, q DefaultEngineStore, env DefaultTrinoEngine) error
EnsureDefaultTrinoEngine keeps the default Trino cluster's engine in the database (docs/adr/0002). Engine endpoints live in the database; the environment only seeds and updates the engine Baleia created itself:
- no engine on the default cluster: create a system-owned trino connection (created_by NULL) with url = trinoURL and a federated engine on it;
- a system-owned connection with another url: update it to trinoURL;
- a connection someone owns: leave it, TRINO_URL no longer applies.
Run it in one transaction.
func EnsureTrinoRole ¶
EnsureTrinoRole creates the baleia_trino login role if it is missing and grants it the registry and resource-group reads the plugin needs. It is idempotent.
func EnsureTrinoRoleSQL ¶
EnsureTrinoRoleSQL is EnsureTrinoRole over database/sql, for the migration command and tests.
func LoadKeyring ¶
func LoadKeyring(ctx context.Context, logger *slog.Logger, cfg *config.Config, count CredentialCounter) (*crypto.Keyring, error)
LoadKeyring builds the credential keyring from configuration.
With no key configured, a fresh install (no stored credentials) starts with a nil keyring and a warning, so first boot is not blocked; saving a credential then fails with a clear error until a key is set. If credentials already exist, startup is refused.
func LoadRegistryKeyring ¶
LoadRegistryKeyring builds the catalog-registry keyring from configuration (Phase B of #31 / D4). An absent key returns a nil keyring and keeps Phase B disabled: registry rows stay plaintext.
func PrepareAPI ¶
PrepareAPI applies the API's deploy-time state in order: the baleia_trino role and its password, the default Trino engine seeded from TRINO_URL, the one-time split of dual-path connections (ADR 0021 CR-17), then the initial data policy revision. It is idempotent and runs before the server starts serving.
func PublishInitialDataPolicies ¶
func PublishInitialDataPolicies(ctx context.Context, logger *slog.Logger, p InitialPublisher)
PublishInitialDataPolicies publishes the initial data policy revision when nothing is published (ADR 0028): until OPA activates a bundle, the engines it gates are not ready, and an engine must be ready before the catalog DDL the first policy needs. A failure does not stop the API: the worker's re-issue pass publishes it too.
func ReconcileIncompleteDirectConnections ¶
func ReconcileIncompleteDirectConnections( ctx context.Context, store *gorm.DB, kr *crypto.Keyring, registry *connections.Registry, logger *slog.Logger, ) error
ReconcileIncompleteDirectConnections repairs rows left by the legacy two-request workflow. Remote validation and discovery happen before the transaction; the engine, native catalogs and bindings then commit together. A failed connection is retained and logged for operator inspection.
func SetTrinoRolePassword ¶
SetTrinoRolePassword sets the baleia_trino login password from password, which callers read from BALEIA_TRINO_DB_PASSWORD — the same env var the baleia-trino-catalog-store plugin reads — so the role and the Trino connection cannot diverge. An empty password is a no-op, leaving the role untouched.
The value is escaped here (quoteLiteral) because Postgres does not accept a bind parameter in ALTER ROLE ... PASSWORD. Operators who enable full statement logging should be aware the generated DDL carries the value.
func SplitDualPathConnections ¶
func SplitDualPathConnections(ctx context.Context, store *gorm.DB, kr *crypto.Keyring, logger *slog.Logger) error
SplitDualPathConnections splits connections that today have both a direct engine and Trino catalogs (ADR 0021 CR-17). The original connection keeps its ID for the Trino path — its catalogs, bindings and grants reference it — and a new connection takes the direct engine, with the credentials re-sealed under its new identity. Every split is logged. A connection whose credentials cannot be re-sealed is left intact and reported, so no access path is ever dropped. It is idempotent: the store migration backfills single-path connections, so only NULL access_mode rows with both paths are considered.
Types ¶
type APIState ¶
type APIState struct {
Store *gorm.DB
Keyring *crypto.Keyring
// TrinoCA is the PEM bundle of BALEIA_TRINO_CA_FILE, empty when unset.
TrinoCA string
// Connections validates and discovers incomplete direct connections.
Connections *connections.Registry
// DataPolicies publishes the initial data policy revision; nil while data
// policies are off.
DataPolicies InitialPublisher
}
APIState is the part of the API dependency graph PrepareAPI needs. It is a struct instead of *app.Core because internal/app imports this package.
type CredentialCounter ¶
CredentialCounter reports how many connections already hold stored credentials. It is a function so callers can adapt any database handle.
type DefaultEngineAliasStore ¶
type DefaultEngineAliasStore interface {
GetEngineByID(ctx context.Context, id db.UUID) (db.Engine, error)
GetDefaultTrinoEngine(ctx context.Context) (db.GetDefaultTrinoEngineRow, error)
}
DefaultEngineAliasStore is what CheckDefaultEngineAlias reads.
type DefaultEngineStore ¶
type DefaultEngineStore interface {
GetDefaultTrinoEngine(ctx context.Context) (db.GetDefaultTrinoEngineRow, error)
GetDefaultTrinoCluster(ctx context.Context) (db.TrinoCluster, error)
CatalogConnectionNameExists(ctx context.Context, name string) (bool, error)
CreateCatalogConnection(ctx context.Context, arg db.CreateCatalogConnectionParams) (db.CatalogConnection, error)
UpdateCatalogConnection(ctx context.Context, arg db.UpdateCatalogConnectionParams) (db.CatalogConnection, error)
CreateEngine(ctx context.Context, arg db.CreateEngineParams) (db.Engine, error)
LinkTrinoClusterEngine(ctx context.Context, arg db.LinkTrinoClusterEngineParams) error
}
DefaultEngineStore is the subset of db.Querier EnsureDefaultTrinoEngine uses.
type DefaultTrinoEngine ¶
type DefaultTrinoEngine struct {
URL string
ServiceUser string
AdminUser string
// Password reports that BALEIA_TRINO_PASSWORD is set. The connection then
// logs in as AdminUser with a sealed reference to that variable, never the
// password itself.
Password bool
// CA is the PEM bundle trusted for the coordinator's certificate.
CA string
// Seal encrypts the connection's credentials bound to its id (the keyring);
// required when Password is set.
Seal func(plain []byte, connID db.UUID) (string, error)
}
DefaultTrinoEngine is the environment's view of the default Trino engine: TRINO_URL, BALEIA_TRINO_SERVICE_USER, BALEIA_TRINO_ADMIN_USER and, for an authenticated coordinator (#646), BALEIA_TRINO_PASSWORD and BALEIA_TRINO_CA_FILE.
type InitialPublisher ¶
type InitialPublisher interface {
PublishInitial(ctx context.Context) (datapolicy.Revision, bool, error)
}
InitialPublisher publishes the data policy revision OPA starts from; *datapolicy.Publisher implements it.