fix(archieui): run the service-token env fallback without a config file

Cloud review of ac7b449. The GATEWAY_TOKEN / STATE_STORE_TOKEN fallback was
written inside project(), which readProjection only calls after its
file-exists check, and only when -config is non-empty. The flags-only
deployment (both targets on the command line, tokens in the environment, no
config.toml) therefore skipped the fallback entirely and dialled with an empty
bearer, which staterpc.Dial refuses on a non-loopback target. That is the
exact deployment the fallback was added for.

TestResolveTokenFallsBackToEnv missed it because it writes a config file
first. Hoisted into Resolve as withEnvTokens, after withDefaults, so it fires
whether or not a file was loaded; project() goes back to being purely the
file's contribution.

Also narrows the parity claim the old comment made. The daemon resolves these
two names through a secret.Registry, which registers bws by default and can
load sops/Vault engines; this process reads the environment only, because
ui-service-boundary.md's deletion gate forbids a secret runtime package in the
UI process. The operational consequence (whoever starts archie-ui must export
those tokens into its environment) is recorded on archie-core-8cda.5.5, which
writes the deployment profile.

Adds pins for the no-config-file fallback, flag-beats-env, and the
Listen = "off" coercion whose own comment asked for one.
This commit is contained in:
sam committed 2026-09-09 02:56:07 +10:00
1 parent 536fa20254
commit d1e5731d04
2 files changed
+96 -19

No files matched your search

+31 -17
View File
@@ -36,6 +36,7 @@ func Resolve(o Options, log *slog.Logger) (Options, error) {
}
}
o = withDefaults(o)
o = withEnvTokens(o)
token, err := resolveToken(o)
if err != nil {
return Options{}, err
@@ -71,29 +72,42 @@ type projection struct {
}
func project(cfg config.Config) projection {
// Token resolution mirrors the daemon's (stateStoreResolvedToken /
// gatewayResolvedToken): the explicit [services.*].target_token key,
// then the secret/env var. The config loader does not expand env, so a
// deployment that presents the gateway/state tokens by environment —
// which config.example.toml documents for remote consumers — would
// otherwise leave the UI with an empty token and staterpc.Dial would
// refuse a non-loopback start.
gatewayToken := cfg.Services.Gateway.TargetToken
if gatewayToken == "" {
gatewayToken = os.Getenv("GATEWAY_TOKEN")
}
stateToken := cfg.Services.State.TargetToken
if stateToken == "" {
stateToken = os.Getenv("STATE_STORE_TOKEN")
}
return projection{
listen: cfg.Web.Listen,
trustForwardedHeaders: cfg.Web.TrustForwardedHeaders,
gateway: ServiceTarget{Target: cfg.Services.Gateway.Target, Token: gatewayToken},
state: ServiceTarget{Target: cfg.Services.State.Target, Token: stateToken},
gateway: ServiceTarget{Target: cfg.Services.Gateway.Target, Token: cfg.Services.Gateway.TargetToken},
state: ServiceTarget{Target: cfg.Services.State.Target, Token: cfg.Services.State.TargetToken},
}
}
// withEnvTokens fills a service token the operator gave neither on the command
// line nor in [services.*].target_token. The config loader does not expand
// environment variables, and config.example.toml documents the environment as
// how a remote consumer presents these tokens, so without this a deployment
// that supplies them by env dials with an empty bearer and staterpc.Dial
// refuses a non-loopback start.
//
// It runs here rather than inside project() because the process is fully
// drivable by flags: readProjection returns early when there is no config
// file, so a fallback living in the file's projection would miss exactly the
// flags-only deployment that needs it.
//
// This reads the process environment only. The daemon resolves the same names
// through a secret.Registry, which also consults bws and any Yaegi-loaded
// engine; the UI process deliberately does not compose that runtime, because
// ui-service-boundary.md's deletion gate forbids a secret runtime package
// here. A token held in a secret manager must therefore be exported into
// archie-ui's environment by whatever starts it.
func withEnvTokens(o Options) Options {
if o.Gateway.Token == "" {
o.Gateway.Token = os.Getenv("GATEWAY_TOKEN")
}
if o.State.Token == "" {
o.State.Token = os.Getenv("STATE_STORE_TOKEN")
}
return o
}
// merge fills only the fields the flags left empty.
func merge(o Options, p projection) Options {
if o.Listen == "" {
+65 -2
View File
@@ -139,8 +139,9 @@ func TestResolveRequiresBothServiceTargets(t *testing.T) {
// not expand environment variables. A deployment that presents the gateway
// and state tokens by environment (config.example.toml documents this for
// remote consumers) must still authenticate, so an empty target_token falls
// back to GATEWAY_TOKEN / STATE_STORE_TOKEN exactly as the daemon's
// stateStoreResolvedToken and gatewayResolvedToken do.
// back to GATEWAY_TOKEN / STATE_STORE_TOKEN. The daemon resolves the same two
// names through a secret.Registry, which also consults bws; this process reads
// the environment only, deliberately (see withEnvTokens).
func TestResolveTokenFallsBackToEnv(t *testing.T) {
path := t.TempDir() + "/ui.toml"
contents := `bot_user = "widget"
@@ -221,3 +222,65 @@ func TestResolveDefaultsTheEventPollInterval(t *testing.T) {
t.Fatalf("EventPollInterval = %s, want the operator's 250ms", explicit.EventPollInterval)
}
}
// The flags-only deployment is the one the env fallback exists for: a
// container or unit that passes both targets on the command line and presents
// the tokens by environment, with no config.toml on disk. readProjection
// returns early for an absent file, so a fallback that lives inside the
// file's projection never runs there.
func TestResolveTokenFallsBackToEnvWithoutAConfigFile(t *testing.T) {
t.Setenv("GATEWAY_TOKEN", "env-gateway")
t.Setenv("STATE_STORE_TOKEN", "env-state")
resolved, err := Resolve(Options{
Config: filepath.Join(t.TempDir(), "absent.toml"),
Listen: "127.0.0.1:8484",
Gateway: ServiceTarget{Target: "10.0.0.1:8585"},
State: ServiceTarget{Target: "10.0.0.1:9090"},
}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if resolved.Gateway.Token != "env-gateway" {
t.Errorf("Gateway.Token = %q, want env GATEWAY_TOKEN: staterpc.Dial refuses a non-loopback target without one", resolved.Gateway.Token)
}
if resolved.State.Token != "env-state" {
t.Errorf("State.Token = %q, want env STATE_STORE_TOKEN", resolved.State.Token)
}
}
// An explicit flag still beats the environment, so an operator can override a
// unit-wide token for one process without unsetting it.
func TestResolveFlagTokenBeatsEnv(t *testing.T) {
t.Setenv("GATEWAY_TOKEN", "env-gateway")
t.Setenv("STATE_STORE_TOKEN", "env-state")
resolved, err := Resolve(Options{
Config: filepath.Join(t.TempDir(), "absent.toml"),
Gateway: ServiceTarget{Target: "10.0.0.1:8585", Token: "flag-gateway"},
State: ServiceTarget{Target: "10.0.0.1:9090", Token: "flag-state"},
}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if resolved.Gateway.Token != "flag-gateway" || resolved.State.Token != "flag-state" {
t.Errorf("tokens = %q/%q, want the flag values to beat the environment", resolved.Gateway.Token, resolved.State.Token)
}
}
// withDefaults' "off" coercion is the one key the UI and daemon read with
// opposite meanings, and its own comment asks for this pin.
func TestResolveCoercesListenOffToTheDefault(t *testing.T) {
resolved, err := Resolve(Options{
Config: filepath.Join(t.TempDir(), "absent.toml"),
Listen: "off",
Gateway: ServiceTarget{Target: "127.0.0.1:8585"},
State: ServiceTarget{Target: "127.0.0.1:9090"},
}, slog.New(slog.DiscardHandler))
if err != nil {
t.Fatalf("Resolve: %v", err)
}
if resolved.Listen != defaultListen {
t.Fatalf("Listen = %q, want %q: a dedicated UI process has nothing to disable", resolved.Listen, defaultListen)
}
}