Files
archie/internal/daemon/state_store_grants_test.go
T
sam 8fef2e7eed fix(state-store): task-scope container credentials, stream large capture lists, validate Update, wire dev's state-store (archie-core-8cda.4.7)
Closes six review findings against .4.6 (PR #775):

- daemon.StateStoreGrantIssuer + staterpc.TaskGrants: containers get a
  per-task credential authorized only for their own Update/Transition/
  InsertEvent, never the daemon's administrative token. Issued before
  Acquire, revoked on Release; fails closed if no issuer is wired.
- StreamCaptures/StreamUndispatchedCaptures: new streaming RPCs so a
  batch of large capture bodies can't exceed gRPC's 4MiB unary cap.
  The original unary RPCs are kept, unmodified (deprecated) rather than
  reshaped, so buf breaking stays green for an in-flight rolling deploy.
- Update rejects a nil Task with InvalidArgument instead of panicking in
  the store layer.
- RequireHostGateway only forces on for embedded NATS, so an externally
  reachable NATS/State Store no longer requires a local Docker bridge.
- Client-side token resolution now matches the server's (target_token,
  then STATE_STORE_TOKEN secret/env).
- task dev now runs archie-state-store (dev:state-store) alongside
  archied/archie-gateway/vite; deployments/dev.toml points them at it.
2026-09-07 13:18:44 +10:00

78 lines
2.6 KiB
Go

package daemon
import (
"slices"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// fakeStateStoreGrants issues a fixed, task-scoped token and records revoke
// calls, standing in for staterpc.GrantIssuer in daemon-level tests.
type fakeStateStoreGrants struct {
token string
err error
revoked int
}
func (f *fakeStateStoreGrants) Issue(*workflow.Task) (string, func(), error) {
if f.err != nil {
return "", nil, f.err
}
return f.token, func() { f.revoked++ }, nil
}
func TestContainerEnvNeverExposesStateStoreAdminToken(t *testing.T) {
d := &Daemon{Cfg: config.NewHolder(config.Config{}), ConnectedStateStore: StateStoreEndpoint{URL: "127.0.0.1:9090", Token: "admin-secret"}}
for _, entry := range d.containerEnv(&workflow.Task{ID: 42}, "task-scoped-token") {
if strings.Contains(entry, "admin-secret") {
t.Fatalf("container receives administrative credential: %s", entry)
}
}
}
func TestContainerEnvCarriesTaskScopedStateStoreToken(t *testing.T) {
d := &Daemon{Cfg: config.NewHolder(config.Config{}), ConnectedStateStore: StateStoreEndpoint{URL: "127.0.0.1:9090", Token: "admin-secret"}}
got := d.containerEnv(&workflow.Task{ID: 42}, "task-scoped-token")
if !slices.Contains(got, "STATE_STORE_TOKEN=task-scoped-token") {
t.Fatalf("containerEnv() = %q, want STATE_STORE_TOKEN=task-scoped-token", got)
}
}
func TestStateStoreGrantTokenIssuesFromWiredGrants(t *testing.T) {
grants := &fakeStateStoreGrants{token: "task-scoped-token"}
d := &Daemon{ConnectedStateStore: StateStoreEndpoint{URL: "127.0.0.1:9090"}, StateStoreGrants: grants}
token, revoke, err := d.stateStoreGrantToken(&workflow.Task{ID: 42})
if err != nil {
t.Fatalf("stateStoreGrantToken: %v", err)
}
if token != "task-scoped-token" {
t.Fatalf("token = %q, want %q", token, "task-scoped-token")
}
revoke()
if grants.revoked != 1 {
t.Fatalf("revoked = %d, want 1", grants.revoked)
}
}
func TestStateStoreGrantTokenFailsClosedWithoutIssuer(t *testing.T) {
d := &Daemon{ConnectedStateStore: StateStoreEndpoint{URL: "127.0.0.1:9090"}}
if _, _, err := d.stateStoreGrantToken(&workflow.Task{ID: 42}); err == nil {
t.Fatal("stateStoreGrantToken: want error when State Store is configured but no grant issuer is wired")
}
}
func TestStateStoreGrantTokenNoOpWhenStateStoreUnconfigured(t *testing.T) {
d := &Daemon{}
token, revoke, err := d.stateStoreGrantToken(&workflow.Task{ID: 42})
if err != nil || token != "" || revoke == nil {
t.Fatalf("stateStoreGrantToken = (%q, revoke==nil:%v, %v), want (\"\", false, nil)", token, revoke == nil, err)
}
revoke() // must not panic
}