Files
archie/internal/infrastructure/configuration/setup/secretref.go
T
sam 89cfa44b5e feat(configuration): let setup reference a secret without carrying its value
A parameterised install could not configure a cloud provider without a terminal:
the only way a secret value entered was the prompt, so an unattended run was
limited to the keyless self-hosted provider. The missing piece is the reference
form.

A reference is not a secret. An engine name and a key name are not sensitive --
which is exactly why a reference may be a parameter while a value may not -- so
setup gains -forge-secret-ref, -provider-secret-ref and -telegram-secret-ref,
each taking engine:key, parsed by one new setup.ParseSecretRef. When a reference
is given the step writes it to TOML and asks nothing, and setup neither reads nor
stores the value, because the value is not its to hold. That is the difference
from the prompt path, where the operator supplies a value and setup writes it
into the env engine and references it there.

Engine names are identifiers, not paths: the shipped engines are registered as
age, sops and vault, so a reference like vault/openbao:KEY is refused rather than
written into a config that could never resolve it.

Two refusals guard the silent no-op. A reference naming a key for the keyless
self-hosted provider, or for a forge that is disabled, would sit in the config
looking configured while nothing read it -- the failure shape this flow exists to
remove -- so both are refused rather than written.

telegram keeps writing token_env for the prompt path, because that form names an
env var and the prompt path is the env engine; a supplied reference may name any
engine, which token_env cannot express, so the reference path writes the
preferred token = {engine, key} form. Noted as the one asymmetry.

Also folded stepCloudProvider's key handling into cloudKeyEdit and cloudModel.
That was a lint fix for nested blocks, but the split is the honest shape: the two
branches obtain a key edit by different means and then resolve a model the same
way.

Every new test was mutation-verified by reintroducing the absence: ignoring the
forge reference, making ParseSecretRef validate nothing, and dropping the keyless
-provider guard each fail the corresponding test.
2026-09-15 03:23:08 +10:00

40 lines
1.6 KiB
Go

package setup
import (
"fmt"
"regexp"
"strings"
"github.com/samcharles93/archie-core/internal/config"
)
// engineNameRe is the shape a secret engine name must have. Engine names are
// identifiers -- "env", "bws", "builtin" -- and for operator-supplied engines
// they are also Yaegi plugin filenames, so this is deliberately no tighter than
// an identifier.
var engineNameRe = regexp.MustCompile(`^[A-Za-z0-9_-]+$`)
// ParseSecretRef parses an "engine:key" secret reference.
//
// A reference is not a secret. An engine name and a key name are not sensitive,
// which is exactly why a reference may be given as a parameter while a value may
// not: the value lives in the engine, and this flow never sees it. Setup writes
// the reference to TOML and stops there.
func ParseSecretRef(s string) (config.SecretRef, error) {
engine, key, ok := strings.Cut(s, ":")
if !ok {
return config.SecretRef{}, fmt.Errorf("secret reference %q must be engine:key, e.g. env:ARCHIE_GITHUB_TOKEN", s)
}
engine, key = strings.TrimSpace(engine), strings.TrimSpace(key)
if engine == "" || key == "" {
return config.SecretRef{}, fmt.Errorf("secret reference %q must name both an engine and a key, e.g. env:ARCHIE_GITHUB_TOKEN", s)
}
if !engineNameRe.MatchString(engine) {
return config.SecretRef{}, fmt.Errorf("secret reference engine %q is not a valid engine name", engine)
}
if strings.ContainsAny(key, ": \t\n") {
return config.SecretRef{}, fmt.Errorf("secret reference key %q must not contain whitespace or a colon", key)
}
return config.SecretRef{Engine: engine, Key: key}, nil
}