diff --git a/internal/app/archieui/config.go b/internal/app/archieui/config.go index 7c58278bc..71d952930 100644 --- a/internal/app/archieui/config.go +++ b/internal/app/archieui/config.go @@ -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 == "" { diff --git a/internal/app/archieui/config_test.go b/internal/app/archieui/config_test.go index 958a34b0f..c70dc1bf6 100644 --- a/internal/app/archieui/config_test.go +++ b/internal/app/archieui/config_test.go @@ -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) + } +}