diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 37845c5d5..d23de5d2f 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -227,9 +227,13 @@ action from the dashboard or chat, capped by `max_retries`. verifies a GitHub webhook's HMAC signature (`internal/webhookguard.VerifyHMAC`), decodes an `issues` event, applies the same dispatch predicate the poller uses, and calls the same `PublishTask` the poller calls. It is opt-in via -`Forge.Intake` (`poll` default, `webhook`, `both`) and single-identity only -- -a deployment with `[[identities]]` configured keeps polling per identity -instead. See `docs/prds/event-sources-and-reactions.md` for why this stayed a +`Forge.Intake` (`poll` default, `webhook`, `both`) and single-identity only: a +config naming `webhook`/`both` together with `[[identities]]`, or setting +intake on an identity, is rejected at validation rather than started with the +setting ignored -- the receiver binds one secret and one dispatch predicate, +so it cannot judge a second identity's events by the right rules. A +multi-identity deployment polls per identity until per-identity intake lands. +See `docs/prds/event-sources-and-reactions.md` for why this stayed a thin decode-and-reuse layer rather than a second intake mechanism. ## Worktree Manager @@ -251,7 +255,8 @@ Daemon-level TOML (`~/.config/archie/config.toml`): - `[forge]` -- type (github/gitea), host, token secret reference; `intake` (`poll` default, `webhook`, or `both`) with `webhook_secret`/`webhook_addr` when reacting to forge events instead of, or alongside, polling - (single-identity deployments only) + (single-identity deployments only; rejected, not ignored, alongside + `[[identities]]` or on a `[identities.forge]` entry) - `[dispatch]` -- trigger (assignee/label/either), labels, ack reaction - `[[repos]]` -- owner, name, base branch, gate commands, protected paths, ecosystem diff --git a/config.example.toml b/config.example.toml index e42afd58e..e5f582a80 100644 --- a/config.example.toml +++ b/config.example.toml @@ -47,14 +47,24 @@ token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" } # on the GitHub webhook, and the host:port the receiver listens on. On # GitHub, set the webhook's Content type to application/json (the UI # default) -- the alternate form-encoded delivery is not supported. -# Not supported alongside [[identities]] (each identity keeps polling). +# Webhook intake is single-identity, and that is enforced: with [[identities]] +# configured this combination is REJECTED at startup rather than silently +# downgraded to polling. The receiver binds one secret and one dispatch +# predicate, so it could only judge another identity's events by the wrong +# rules. Either drop the [[identities]] blocks, or keep intake = "poll" and +# let each identity poll. # webhook_secret = { engine = "env", key = "ARCHIE_WEBHOOK_SECRET" } # webhook_addr = "0.0.0.0:8645" +# intake, webhook_secret and webhook_addr exist only here. Set on a +# [identities.forge] entry they are REJECTED for the same reason: nothing +# reads them, so the identity would silently keep polling. Serving webhook +# intake per identity is a pending migration, not a setting. # [[identities]] runs more than one bot identity from a single daemon, each # with its own forge/dispatch/models/providers/budgets/notify -- see # docs/architecture/identity.md for the full design and constraints (e.g. -# not combinable with [forge].intake = "webhook"/"both"). Minimal shape: +# intake settings are rejected per identity, and [forge].intake = +# "webhook"/"both" is rejected alongside [[identities]]). Minimal shape: # [[identities]] # name = "personal" # required; namespaces logs/events/NATS subjects # bot_user = "your-bot" # required diff --git a/internal/infrastructure/configuration/validate.go b/internal/infrastructure/configuration/validate.go index 6145c1ae9..9cf0f0300 100644 --- a/internal/infrastructure/configuration/validate.go +++ b/internal/infrastructure/configuration/validate.go @@ -253,6 +253,13 @@ func DispatchTriggerValid(trigger string) bool { // the secret and listen address it needs. An unset intake resolves to "poll" // without mutating cfg, so Validate (which does not apply defaults) accepts a // hand-built config the same way Loader.File accepts its on-disk form. +// +// It also refuses the intake settings that no code path reads: the ones on a +// [[identities]] entry, and a root webhook intake paired with [[identities]]. +// The receiver is single-identity by construction +// (bootstrap.setupForgeWebhook binds one dispatch predicate and one secret), +// so those two spellings could only have been accepted-and-ignored -- the root +// one silently downgrading to polling at startup. func validateForgeIntake(cfg *config.Config) error { intake := cfg.Forge.Intake if intake == "" { @@ -262,6 +269,10 @@ func validateForgeIntake(cfg *config.Config) error { return fmt.Errorf("%w: forge.intake %q (want %s)", ErrInvalidInput, cfg.Forge.Intake, list(forgeIntakes)) } if intake == config.ForgeIntakeWebhook || intake == config.ForgeIntakeBoth { + if len(cfg.Identities) > 0 { + return fmt.Errorf("%w: forge.intake %q is not supported alongside [[identities]]: webhook intake is single-identity only, so every identity would silently keep polling (drop the [[identities]] blocks, or set forge.intake = %q and let each identity poll)", + ErrInvalidInput, intake, config.ForgeIntakePoll) + } if cfg.Forge.WebhookSecret == (config.SecretRef{}) { return fmt.Errorf("%w: forge.webhook_secret is required when forge.intake is %q", ErrInvalidInput, intake) } @@ -269,9 +280,59 @@ func validateForgeIntake(cfg *config.Config) error { return fmt.Errorf("%w: forge.webhook_addr is required when forge.intake is %q", ErrInvalidInput, intake) } } + return validateIdentityForgeIntake(cfg.Identities) +} + +// identityIntakes is the intake modes a [[identities]] entry may declare. +// "poll" is the whole list: the receiver reads the root [forge] block only, so +// a per-identity "webhook" would have no effect (see +// validateIdentityForgeIntake). Named separately from forgeIntakes so a +// rejection for a typo does not advertise modes the identity cannot have. +var identityIntakes = []string{config.ForgeIntakePoll} + +// validateIdentityForgeIntake refuses the intake settings on a [[identities]] +// entry. IdentityConfig.Forge is the same type as the root [forge] block, so +// identities[N].forge.intake, .webhook_secret and .webhook_addr all DECODE -- +// but every read of them is of the root block (this file and +// bootstrap.setupForgeWebhook), and per-identity intake never reached even +// this function. An operator could therefore write a webhook intake that +// parsed, validated, and did nothing while the identity went on polling. +// Reject it at the source: intake per identity is a migration +// (internal/domain/workintake routing), not a setting. +func validateIdentityForgeIntake(identities []config.IdentityConfig) error { + for i, id := range identities { + switch id.Forge.Intake { + case "", config.ForgeIntakePoll: + case config.ForgeIntakeWebhook, config.ForgeIntakeBoth: + return fmt.Errorf("%w: %s %q is not supported: per-identity webhook intake is not implemented, so this identity would silently keep polling (want %q)", + ErrInvalidInput, identitySettingPath(i, id, "forge.intake"), id.Forge.Intake, config.ForgeIntakePoll) + default: + return fmt.Errorf("%w: %s %q (want %s)", ErrInvalidInput, identitySettingPath(i, id, "forge.intake"), id.Forge.Intake, list(identityIntakes)) + } + if id.Forge.WebhookSecret != (config.SecretRef{}) { + return fmt.Errorf("%w: %s is not supported: per-identity webhook intake is not implemented, so only the top-level forge.webhook_secret is read", + ErrInvalidInput, identitySettingPath(i, id, "forge.webhook_secret")) + } + if id.Forge.WebhookAddr != "" { + return fmt.Errorf("%w: %s is not supported: per-identity webhook intake is not implemented, so only the top-level forge.webhook_addr is read", + ErrInvalidInput, identitySettingPath(i, id, "forge.webhook_addr")) + } + } return nil } +// identitySettingPath renders the location of a per-identity setting, naming +// the identity as well as indexing it: an operator reading a rejection for a +// four-entry [[identities]] block finds the offender by its name instead of +// counting entries. The name is omitted when it is empty, because this runs +// before validateIdentities has required it. +func identitySettingPath(i int, id config.IdentityConfig, field string) string { + if id.Name == "" { + return fmt.Sprintf("identities[%d].%s", i, field) + } + return fmt.Sprintf("identities[%d] (%q).%s", i, id.Name, field) +} + // validateProviders rejects base URLs carrying credentials or query state. // A provider URL is used to build API requests, so userinfo or a stray query // string would be silently attached to every call. diff --git a/internal/infrastructure/configuration/validate_test.go b/internal/infrastructure/configuration/validate_test.go index b7176a713..29bbeaf6c 100644 --- a/internal/infrastructure/configuration/validate_test.go +++ b/internal/infrastructure/configuration/validate_test.go @@ -3,6 +3,7 @@ package configuration import ( "errors" "os" + "path/filepath" "strings" "testing" "time" @@ -317,6 +318,251 @@ func TestValidateForgeIntake(t *testing.T) { } } +// TestValidateForgeIntakeRejectsIdentityIntake pins that an intake setting on +// a [[identities]] entry is refused instead of accepted-and-ignored. +// IdentityConfig.Forge is the same type as the root [forge] block, so +// identities[N].forge.intake, .webhook_secret and .webhook_addr all decode -- +// but every intake read is of the root block, so before this check they +// parsed, reached no code path, and were never even validated. A setting an +// operator can spell that nothing reads is a lie in the config file. +func TestValidateForgeIntakeRejectsIdentityIntake(t *testing.T) { + // identity builds one entry whose forge is otherwise valid, so a + // rejection can only come from the field the case sets. + identity := func(mutate func(*config.Forge)) config.IdentityConfig { + forge := config.Forge{ + Type: "github", + Token: config.SecretRef{Engine: "env", Key: "ARCHIE_GITHUB_TOKEN"}, + } + if mutate != nil { + mutate(&forge) + } + return config.IdentityConfig{Name: "personal", BotUser: "archie-personal", Forge: forge} + } + + tests := []struct { + name string + identities []config.IdentityConfig + // want lists fragments the error must carry; empty means no error. + want []string + }{ + { + name: "unset intake on an identity is valid", + identities: []config.IdentityConfig{identity(nil)}, + }, + { + name: "poll intake on an identity is valid", + identities: []config.IdentityConfig{identity(func(f *config.Forge) { + f.Intake = config.ForgeIntakePoll + })}, + }, + { + name: "webhook intake on an identity is rejected with the identity, setting, and reason", + identities: []config.IdentityConfig{identity(func(f *config.Forge) { + f.Intake = config.ForgeIntakeWebhook + })}, + want: []string{`identities[0] ("personal").forge.intake "webhook"`, "per-identity webhook intake is not implemented"}, + }, + { + name: "both intake on an identity is rejected with the identity, setting, and reason", + identities: []config.IdentityConfig{identity(func(f *config.Forge) { + f.Intake = config.ForgeIntakeBoth + })}, + want: []string{`identities[0] ("personal").forge.intake "both"`, "per-identity webhook intake is not implemented"}, + }, + { + name: "an unknown intake on an identity names the accepted value", + identities: []config.IdentityConfig{identity(func(f *config.Forge) { + f.Intake = "sometimes" + })}, + want: []string{`identities[0] ("personal").forge.intake "sometimes"`, "want poll"}, + }, + { + name: "webhook_secret on an identity is rejected", + identities: []config.IdentityConfig{identity(func(f *config.Forge) { + f.WebhookSecret = config.SecretRef{Engine: "env", Key: "ARCHIE_WEBHOOK_SECRET"} + })}, + want: []string{`identities[0] ("personal").forge.webhook_secret`, "per-identity webhook intake is not implemented"}, + }, + { + name: "webhook_addr on an identity is rejected", + identities: []config.IdentityConfig{identity(func(f *config.Forge) { + f.WebhookAddr = "0.0.0.0:8645" + })}, + want: []string{`identities[0] ("personal").forge.webhook_addr`, "per-identity webhook intake is not implemented"}, + }, + { + // The offender is the second entry: the message must locate it, not + // report "an identity" for a file whose first identity is fine. + name: "the rejected identity is the one named, not the first", + identities: []config.IdentityConfig{ + identity(nil), + { + Name: "work", + BotUser: "archie-work", + Forge: config.Forge{ + Type: "gitea", + Intake: config.ForgeIntakeWebhook, + Token: config.SecretRef{Engine: "env", Key: "ARCHIE_GITEA_TOKEN"}, + }, + }, + }, + want: []string{`identities[1] ("work").forge.intake "webhook"`}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := minimalValidConfig() + cfg.Identities = tc.identities + requireValidateError(t, validateForgeIntake(&cfg), tc.want) + }) + } +} + +// TestValidateForgeIntakeRejectsRootWebhookIntakeWithIdentities pins the other +// half of the same lie: [forge].intake = "webhook"/"both" alongside +// [[identities]] used to start the daemon, which logged "forge webhook +// disabled: multi-identity deployments are not supported yet" and polled +// instead. Failing closed at validation replaces that silent downgrade. +func TestValidateForgeIntakeRejectsRootWebhookIntakeWithIdentities(t *testing.T) { + identity := config.IdentityConfig{ + Name: "personal", + BotUser: "archie-personal", + Forge: config.Forge{ + Type: "github", + Token: config.SecretRef{Engine: "env", Key: "ARCHIE_GITHUB_TOKEN"}, + }, + } + + tests := []struct { + name string + intake string + identities []config.IdentityConfig + want []string + }{ + { + name: "webhook intake plus identities is rejected", + intake: config.ForgeIntakeWebhook, + identities: []config.IdentityConfig{identity}, + want: []string{`forge.intake "webhook"`, "alongside [[identities]]", `forge.intake = "poll"`}, + }, + { + name: "both intake plus identities is rejected", + intake: config.ForgeIntakeBoth, + identities: []config.IdentityConfig{identity}, + want: []string{`forge.intake "both"`, "alongside [[identities]]", `forge.intake = "poll"`}, + }, + { + name: "poll intake plus identities stays valid", + intake: config.ForgeIntakePoll, + identities: []config.IdentityConfig{identity}, + }, + { + name: "webhook intake with no identities stays valid", + intake: config.ForgeIntakeWebhook, + identities: nil, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := minimalValidConfig() + cfg.Forge.Intake = tc.intake + cfg.Forge.WebhookAddr = "0.0.0.0:8645" + if tc.intake == config.ForgeIntakeWebhook || tc.intake == config.ForgeIntakeBoth { + // Supply the secret and address the receiver would need, so a + // rejection can only come from the combination itself and not + // from a missing webhook setting. + cfg.Forge.WebhookSecret = config.SecretRef{Engine: "env", Key: "ARCHIE_WEBHOOK_SECRET"} + } + cfg.Identities = tc.identities + requireValidateError(t, validateForgeIntake(&cfg), tc.want) + }) + } +} + +// TestLoaderRejectsIdentityIntakeFromFile drives the rejection through the +// real file path, because the defect was that the key DECODES. Asserting on +// validateForgeIntake alone would not prove an operator's +// [identities.forge] intake = "webhook" is refused where they would hit it. +func TestLoaderRejectsIdentityIntakeFromFile(t *testing.T) { + const doc = ` +bot_user = "archie-bot" + +[services.gateway] +target = "127.0.0.1:8585" + +[forge] +type = "none" + +[containers] +image = "ghcr.io/samcharles93/archie-agent:latest" + +[[identities]] +name = "personal" +bot_user = "archie-personal" + +[identities.forge] +type = "github" +intake = "webhook" +token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" } + +[[identities.repos]] +owner = "my-org" +name = "my-repo" +` + path := filepath.Join(t.TempDir(), "config.toml") + if err := os.WriteFile(path, []byte(doc), 0o600); err != nil { + t.Fatal(err) + } + + _, err := New(nil).File(path) + requireValidateError(t, err, []string{`identities[0] ("personal").forge.intake "webhook"`, "not implemented"}) +} + +// TestShippedProfilesValidate guards the shipped deployments against a +// validation rule that would reject a profile the repository supports: +// multi-forge-github-gitea.toml is the only profile using [[identities]], and +// single-forge-github.toml the only documented webhook-capable shape, so an +// intake rule that fails either has broken a supported assembly. +func TestShippedProfilesValidate(t *testing.T) { + for _, name := range []string{"single-forge-github.toml", "multi-forge-github-gitea.toml"} { + t.Run(name, func(t *testing.T) { + path := filepath.Join("..", "..", "..", "deployments", name) + doc, err := New(nil).File(path) + if err != nil { + t.Fatalf("load %s: %v", path, err) + } + if err := Validate(&doc.Config); err != nil { + t.Fatalf("validate %s: %v", path, err) + } + }) + } +} + +// requireValidateError asserts err wraps ErrInvalidInput and carries every +// fragment in want. An error that only said "invalid input" would leave the +// operator guessing which of the file's settings to fix, so every rejection +// above is asserted on the words that name the setting. +func requireValidateError(t *testing.T, err error, want []string) { + t.Helper() + if len(want) == 0 { + if err != nil { + t.Fatalf("got error %v, want the configuration accepted", err) + } + return + } + if err == nil { + t.Fatal("got nil error, want a rejection") + } + if !errors.Is(err, ErrInvalidInput) { + t.Fatalf("error %v does not wrap ErrInvalidInput", err) + } + for _, fragment := range want { + if !strings.Contains(err.Error(), fragment) { + t.Errorf("error %q does not name %q", err, fragment) + } + } +} + // Validate must run the exact same checks Loader.File applies internally -- // this pins that a config Validate accepts on its own, once the same // defaults Loader.File would apply are filled in by hand, is also accepted