Documentation
¶
Overview ¶
Package config is the portable host configuration document: everything that makes a pstack host *this* host, in a form that can be carried to a new one.
── WHAT TRAVELS, AND WHY THAT LIST AND NOT A BACKUP ─────────────────────────────────────────────
Portable, host-independent state only: accounts (argon2 hashes, so logins keep working), personal API tokens (sha256 hashes, same reasoning), host vars and secrets, notifier registrations with their signing secrets, the SSO providers and their client secrets, registry credentials, Traefik routing files, named specs.
OUT, deliberately: deployments, sessions, `sso_state`, terminal audit, delivery history. Those describe what is happening on ONE host. Restoring a deployment row onto a machine where that stack does not run is not merely useless, it is a lie about the world (invariant 10), and a restored session id is a live credential nobody minted. Loki's settings stay behind too: a bucket and a cutover date belong to one host's Loki. Assemble names them in Skipped.
── THIS PACKAGE ADDS A ROUTE, NOT A READ PATH ───────────────────────────────────────────────────
Every unmasked value here already had an accessor for the deploy path — `hostvars.ResolveMaps`, `webhooks.RawConfigOf`/`SecretOf`, `auth.ListSsoProviders`, `specs.Source`, `routing.Read`. This package calls those and writes no second way to read a secret. Two exceptions, both stated rather than hidden:
- Users and tokens had no hash-bearing listing at all. `auth.ExportUsers`/`ExportTokens` are new read-only helpers, in the package that owns those tables, named to be conspicuous in a diff.
- `registries` deliberately has NO read path for a stored credential (its header says so). The `auths` block of `config.json` is therefore read here, directly, and only for entries this document can faithfully rebuild — see registryEntries.
── THE DOCUMENT IS A CREDENTIAL DUMP. TREAT EVERY CALLER AS ONE ─────────────────────────────────
Assemble's return value is the most dangerous object in this codebase: one struct holding every secret on the host. It is plaintext by design (the CLI seals it, see seal.go), which puts the whole burden on WHO may call — `auth.KindRoot` only, never a browser session, or an XSS is a full credential exfiltration. That gate is the API's to enforce; this package cannot.
── APPLY CREATES OR SKIPS. IT NEVER UPDATES AND NEVER DELETES ───────────────────────────────────
Upsert by natural key, where "upsert" stops at insert: an existing name is left exactly as it is and reported as skipped. Three reasons, all of them the same reason:
- Overwriting is how a hostile file replaces a working registry credential or repoints a notifier. Refusing to touch what exists means a bad file can only ADD, which is visible in the summary before it is written (Trusts).
- It makes apply-twice a byte-level no-op. An update path would re-run `specs.Put` and bump `updatedAt` on every run.
- It makes a PARTIAL apply safe. There is no outer transaction — `hostvars.Put` opens its own `Store.Tx`, and a nested `db.*` call inside one is a permanent self-deadlock (Go rule 16) — so a failure halfway leaves a subset of the document applied. Because nothing is ever replaced or deleted, re-running completes it.
seal.go — the sealed envelope: scrypt for the key, AES-256-GCM for the payload.
── WHY SEALING IS THE CLI'S JOB AND NOT THE SERVER'S ────────────────────────────────────────────
The API hands the document out in plaintext over an authenticated connection and never learns the passphrase. Sealing server-side would mean POSTing the passphrase to the box whose credentials are being exported — strictly worse than sealing where the file lands. So Seal/Unseal live here, are pure functions of (bytes, passphrase), and touch no store.
── CHOOSING N, r, p AGAINST THIS ARTIFACT ───────────────────────────────────────────────────────
What a stolen envelope is worth if cracked: every account's argon2id hash (m=64 MiB, t=2 — so the passwords behind them still cost real money per guess), and, with no second factor at all, the registry passwords, the notifier signing secrets, the SSO client secret and every host secret. Those last four are the reason the seal has to carry the whole weight: they are plaintext the moment the envelope opens.
The cost is therefore set by the WEAKEST box that must legitimately unseal, not by a laptop. `pstack cloud-init --config` unseals at first boot on the VM being provisioned, which may have 512 MiB–1 GiB of RAM. scrypt's footprint is 128·N·r bytes:
N=1<<15 (32 MiB) too cheap — a GPU rig fits thousands of these in VRAM N=1<<17 (128 MiB) ~1 s here; a 24 GB GPU holds ~190 in parallel, not ~6000 ← chosen N=1<<20 (1 GiB) OOM-kills the small instance it was meant to provision
So N=1<<17, r=8, p=1. p stays 1: raising it multiplies CPU without raising the memory floor that is doing the actual work against parallel hardware.
The parameters are stored IN the envelope so changing these defaults later still opens old files. That means unseal reads N, r and p from an UNTRUSTED file and hands them to an allocator — a hostile envelope claiming N=1<<30 is a 128 GiB allocation and a dead process. checkKDF bounds them before scrypt is called; that guard is not optional.
── WHAT THE ENVELOPE DOES NOT AUTHENTICATE, AND WHY THAT IS FINE ────────────────────────────────
The header is not passed as GCM additional data. It does not need to be: N, r, p and the salt are all key-derivation inputs, so flipping any of them derives a different key and the tag fails. `version` and `sealed` are checked before decryption and refused by name. There is no header bit whose modification could change the plaintext rather than destroy it.
GCM cannot tell a wrong passphrase from a modified file — the outcome is one failed tag either way — so there is ONE error for both, and its text says both. An `ErrWrongPassphrase` that also fires on a truncated download sends the operator hunting the wrong thing.
Index ¶
- Constants
- Variables
- func IsError(err error) bool
- func Seal(plaintext []byte, passphrase string) ([]byte, error)
- func Unseal(sealed []byte, passphrase string) ([]byte, error)
- type Document
- type Envelope
- type Error
- type KDF
- type Notifier
- type Registry
- type RoutingFile
- type SSO
- type SSOProvider
- type Sources
- type Spec
- type Summary
- type Var
Constants ¶
const FormatVersion = 1
FormatVersion is the document AND envelope version. Bump only for a shape a previous pstack could not apply correctly — an added field that older code ignores is not a bump.
const SealScheme = "scrypt-aes256gcm"
SealScheme names the construction. A future scheme is a new value, refused by name here rather than mis-parsed.
Variables ¶
var ErrPassphrase = errors.New("could not open the sealed config: wrong passphrase, or the file was modified or truncated since it was sealed")
ErrPassphrase is a failed GCM tag. Deliberately ONE error for two causes — see the header.
Functions ¶
Types ¶
type Document ¶
type Document struct {
Version int `json:"version"`
// PstackVersion is what wrote it — outside any seal, so a refusal can name it.
PstackVersion string `json:"pstackVersion"`
ExportedAt int64 `json:"exportedAt"`
// Skipped is what Assemble could NOT carry, with the reason. Informational: apply ignores it.
// It is in the document rather than in a second return value so the reason survives the seal
// and is visible to whoever opens the file, not only to whoever ran the export.
Skipped []string `json:"skipped"`
Users []auth.ExportUser `json:"users"`
Tokens []auth.ExportToken `json:"tokens"`
Vars []Var `json:"vars"`
Notifiers []Notifier `json:"notifiers"`
SSOProviders []SSOProvider `json:"ssoProviders"`
// SSO is the single-provider shape a pre-multi-provider pstack (≤0.30.0) exported. Accepted on
// DECODE only — Parse folds it into SSOProviders under sso.Config.DerivedKey and clears it, so
// an old export keeps applying — and never written: Assemble leaves it nil, and omitempty keeps
// a nil out of new documents.
SSO *SSO `json:"sso,omitempty"`
Registry []Registry `json:"registries"`
Routing []RoutingFile `json:"routing"`
Specs []Spec `json:"specs"`
}
Document is the whole portable configuration. Every slice is non-nil at construction (Go rule 3).
func Parse ¶
Parse decodes a plaintext document and refuses a version it does not understand — by name, and before touching anything, rather than applying the half of it that happens to still fit.
func (*Document) String ¶
String keeps a Document out of a log by accident. Every field on it is a credential or points at one, and `%v` on a struct prints all of them; the JSON encoders do not consult this method, so the export path is unaffected.
func (*Document) Trusts ¶
Trusts is the pre-write summary: the registries the new host would pull images from and the URLs it would post to. Every string in a notifier's config is listed, not just the field named `url` — for a chat notifier the URL *is* the credential, and a type added later may name its field anything. A hostile config file otherwise silently repoints image pulls at an attacker.
type Envelope ¶
type Envelope struct {
Version int `json:"version"`
Sealed string `json:"sealed"`
KDF KDF `json:"kdf"`
// Nonce is standard base64, 12 random bytes, FRESH PER SEAL. Never derived, never a counter:
// two payloads sealed under one key and nonce leak their XOR and forge the tag.
Nonce string `json:"nonce"`
// Payload is standard base64 of the AES-256-GCM ciphertext with its 16-byte tag appended.
Payload string `json:"payload"`
PstackVersion string `json:"pstackVersion"`
}
Envelope is the sealed file. Field order is the contract's, with pstackVersion appended: without a writer version OUTSIDE the seal, refusing an unknown `version` could not name what wrote it, which is the difference between "upgrade pstack" and a shrug.
type Error ¶
type Error struct{ Msg string }
Error is a refused document, envelope or passphrase. The API maps it to 400.
type KDF ¶
type KDF struct {
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
// Salt is standard base64. Fresh per seal.
Salt string `json:"salt"`
}
KDF is the scrypt cost, stored so a change of defaults still opens an old file.
type Notifier ¶
type Notifier struct {
Type string `json:"type"`
Name string `json:"name"`
Config *omap.Map `json:"config"`
Events []string `json:"events"`
Enabled bool `json:"enabled"`
Secret string `json:"secret"`
CreatedAt int64 `json:"createdAt"`
}
Notifier is one registration, with the UNMASKED config and the signing secret. The secret travels because it is what receivers verify: minting a new one on the new host silently breaks every receiver's signature check, which is the failure this whole feature exists to avoid.
type Registry ¶
type Registry struct {
Registry string `json:"registry"`
Username string `json:"username"`
Password string `json:"password"`
}
Registry is one docker registry credential, split into the pair `registries.Put` accepts.
type RoutingFile ¶
RoutingFile is one Traefik dynamic-config file.
type SSOProvider ¶ added in v0.31.0
type SSOProvider struct {
Key string `json:"key"`
Config *sso.Config `json:"config"`
ClientSecret string `json:"clientSecret"`
}
SSOProvider is one stored identity provider: its slug, the config and the client secret it is paired with.
type Sources ¶
type Sources struct {
Store *store.Store
Auth *auth.Auth
HostVars *hostvars.HostVars
Webhooks *webhooks.Webhooks
Registries *registries.RegistryAuthStore
Routing *routing.RoutingStore
Specs *specs.SpecStore
}
Sources is everything the document is read from and written to. The API server already holds every one of these; Store is here because apply writes users, tokens and notifiers, and those three packages expose no way to insert a value that is ALREADY hashed or minted.
type Spec ¶
type Spec struct {
Name string `json:"name"`
Source string `json:"source"`
Compose *string `json:"compose,omitempty"`
Description *string `json:"description,omitempty"`
}
Spec is one named spec. Compose is the compose file stored beside it, when there is one.