diff --git a/internal/app/archieui/config.go b/internal/app/archieui/config.go index c248fd1b7..24a548897 100644 --- a/internal/app/archieui/config.go +++ b/internal/app/archieui/config.go @@ -71,11 +71,26 @@ 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: cfg.Services.Gateway.TargetToken}, - state: ServiceTarget{Target: cfg.Services.State.Target, Token: cfg.Services.State.TargetToken}, + gateway: ServiceTarget{Target: cfg.Services.Gateway.Target, Token: gatewayToken}, + state: ServiceTarget{Target: cfg.Services.State.Target, Token: stateToken}, } } @@ -104,6 +119,14 @@ func merge(o Options, p projection) Options { } func withDefaults(o Options) Options { + // This is the one config key the UI and the daemon read with opposite + // meanings: in the daemon, Web.Listen = "off" disables the dashboard + // (config.Config "off" is a sentinel, not an address). In a dedicated UI + // process there is no sense disabling the only thing it does, so "off" + // falls through to the default listener. None of the other projected + // values is overloaded this way; if "off" ever means something else here + // it must be pinned with a test, because it is exactly where an operator + // preparing the split would set it. if o.Listen == "" || o.Listen == "off" { o.Listen = defaultListen } diff --git a/internal/app/archieui/config_test.go b/internal/app/archieui/config_test.go index 1382dece8..d6d6550e9 100644 --- a/internal/app/archieui/config_test.go +++ b/internal/app/archieui/config_test.go @@ -130,3 +130,63 @@ func TestResolveRequiresBothServiceTargets(t *testing.T) { }) } } + +// TestResolveTokenFallsBackToEnv pins D1: the UI process reads the same +// [services.*].target_token keys the daemon does, but the config loader does +// 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. +func TestResolveTokenFallsBackToEnv(t *testing.T) { + path := t.TempDir() + "/ui.toml" + contents := `bot_user = "widget" + +[web] +listen = "127.0.0.1:9999" + +[services.gateway] +target = "127.0.0.1:8585" + +[services.state] +target = "127.0.0.1:9090" +` + if err := os.WriteFile(path, []byte(contents), 0o600); err != nil { + t.Fatal(err) + } + + t.Setenv("GATEWAY_TOKEN", "env-gateway") + t.Setenv("STATE_STORE_TOKEN", "env-state") + + resolved, err := Resolve(Options{Config: path}, 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", resolved.Gateway.Token) + } + if resolved.State.Token != "env-state" { + t.Errorf("State.Token = %q, want env STATE_STORE_TOKEN", resolved.State.Token) + } + + // The explicit key still wins over the environment. + keyed := `bot_user = "widget" + +[services.gateway] +target = "127.0.0.1:8585" +target_token = "keyed-gateway" +[services.state] +target = "127.0.0.1:9090" +` + path2 := t.TempDir() + "/keyed.toml" + if err := os.WriteFile(path2, []byte(keyed), 0o600); err != nil { + t.Fatal(err) + } + resolved2, err := Resolve(Options{Config: path2}, slog.New(slog.DiscardHandler)) + if err != nil { + t.Fatalf("Resolve (keyed): %v", err) + } + if resolved2.Gateway.Token != "keyed-gateway" { + t.Errorf("Gateway.Token = %q, want the explicit target_token over env", resolved2.Gateway.Token) + } +} diff --git a/internal/config/services.go b/internal/config/services.go index e93bd4e84..6f9e03057 100644 --- a/internal/config/services.go +++ b/internal/config/services.go @@ -3,13 +3,13 @@ package config // Services selects contract adapters at application composition time. type Services struct { Gateway ServiceConnection `toml:"gateway" yaml:"gateway"` - // State selects the State Store contract adapter. Unlike Gateway, an - // empty Target means local (the *store.Store in-process adapter): the - // State Store service is not yet extracted into its own process. The - // standalone archie-state-store binary (.4.3) is the consumer of - // [services.state].target_token; the daemon does not read it yet, so - // it remains dead configuration until the daemon points at the - // extracted store. Setting Target would dial the gRPC StateStoreService. + // State selects the State Store contract adapter. The State Store is its + // own process (cmd/archie-state-store) and owns archie.db; an empty Target + // is a startup error in archied/archie-gateway, which no longer serve a + // local store (docs/prds/state-store-contract.md §12 step 7). The daemon + // and agent dial the remote StateStoreService and present target_token + // when the target is non-loopback. Token resolution mirrors the daemon's: + // the explicit key, then the STATE_STORE_TOKEN secret/env var. State ServiceConnection `toml:"state" yaml:"state"` }