From c56caa45c2338f42a7574053e447ab94306b6120 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 17 Oct 2026 03:07:09 +1100 Subject: [PATCH] feat(controlplane): the instance forge is its root connection Layering reads forge-connections and takes the root connection for the instance's own forge, keeping the file's webhook intake and secret, which are instance infrastructure rather than an account. A document with no root connection disables the forge instead of falling back to a credential the operator may have withdrawn. --- .../forge_connection_layering_test.go | 75 +++++++++++++++++++ .../app/controlplane/forge_connections.go | 11 +++ internal/app/controlplane/org_models.go | 3 + internal/app/controlplane/runtime_config.go | 15 ++++ internal/domain/applystatus/applystatus.go | 2 +- 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 internal/app/controlplane/forge_connection_layering_test.go diff --git a/internal/app/controlplane/forge_connection_layering_test.go b/internal/app/controlplane/forge_connection_layering_test.go new file mode 100644 index 000000000..0a101a73f --- /dev/null +++ b/internal/app/controlplane/forge_connection_layering_test.go @@ -0,0 +1,75 @@ +package controlplane + +import ( + "context" + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +// stubResources answers one kind and reports every other as absent, which is +// the shape a boot sees for a kind nothing has been stored for. +type stubResources map[string][]byte + +func (s stubResources) Query(_ context.Context, kind string, decode func([]byte) error) (int64, bool, error) { + value, ok := s[kind] + if !ok { + return 0, false, nil + } + return 1, true, decode(value) +} + +// TestForgeLayeringReadsTheConnection pins where the instance's forge comes +// from and what a missing connection means. The file's [forge] is only the +// seed: once the resource holds a document, that document is the forge, and a +// document without a root connection disables it rather than falling back to +// the file's credential -- which the operator may have withdrawn. +func TestForgeLayeringReadsTheConnection(t *testing.T) { + base := config.Config{ + Forge: config.Forge{ + Type: "github", Host: "file.example.com", + Token: config.SecretRef{Engine: "env", Key: "FILE_TOKEN"}, + Intake: config.ForgeIntakeBoth, + WebhookSecret: config.SecretRef{Engine: "env", Key: "HOOK"}, + }, + } + tests := []struct { + name string + connections string + wantType string + wantHost string + wantKey string + wantWebhookKept bool + }{ + { + name: "the root connection replaces the file's forge", + connections: `[{"id":"default","name":"Instance","type":"gitea","host":"gitea.example.com","token":{"engine":"vault","key":"forge/root"}}]`, + wantType: "gitea", + wantHost: "gitea.example.com", + wantKey: "forge/root", + wantWebhookKept: true, + }, + { + name: "a document with no root connection disables the forge", + connections: `[{"id":"acme","name":"Acme","type":"github","host":"github.com"}]`, + wantType: "", + wantHost: "", + wantKey: "", + wantWebhookKept: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfg, _, err := runtimeConfigFrom(t.Context(), stubResources{ForgeConnectionsKind: []byte(tt.connections)}, base) + if err != nil { + t.Fatalf("layer: %v", err) + } + if cfg.Forge.Type != tt.wantType || cfg.Forge.Host != tt.wantHost || cfg.Forge.Token.Key != tt.wantKey { + t.Fatalf("forge = %+v, want type %q host %q key %q", cfg.Forge, tt.wantType, tt.wantHost, tt.wantKey) + } + if kept := cfg.Forge.WebhookSecret.Key == "HOOK" && cfg.Forge.Intake == config.ForgeIntakeBoth; kept != tt.wantWebhookKept { + t.Fatalf("webhook intake/secret = %q/%q, want them kept from the file", cfg.Forge.Intake, cfg.Forge.WebhookSecret.Key) + } + }) + } +} diff --git a/internal/app/controlplane/forge_connections.go b/internal/app/controlplane/forge_connections.go index 62d960fed..c398e3ac1 100644 --- a/internal/app/controlplane/forge_connections.go +++ b/internal/app/controlplane/forge_connections.go @@ -71,6 +71,17 @@ func connectionFromForge(id, name string, cfg config.Forge) (config.ForgeConnect }, true } +// rootConnection is the connection an instance's own tasks act through: the +// one the file's [forge] section migrated into. +func rootConnection(connections []config.ForgeConnection) (config.ForgeConnection, bool) { + for _, c := range connections { + if c.ID == rootConnectionID { + return c, true + } + } + return config.ForgeConnection{}, false +} + func validateForgeConnections(input []byte) error { return validateAs(input, func(connections []config.ForgeConnection) error { seen := make(map[string]struct{}, len(connections)) diff --git a/internal/app/controlplane/org_models.go b/internal/app/controlplane/org_models.go index 7dd74c4e7..552a30970 100644 --- a/internal/app/controlplane/org_models.go +++ b/internal/app/controlplane/org_models.go @@ -154,6 +154,9 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any) // instanceOnlyKinds are the kinds the runtime layers from the system org's // copy alone. Another org's write would be stored and never applied. +// Repository policies stay here until the daemon resolves a task's bindings +// in the task's own organisation: while only the system org's document is +// read, another org's write would be stored and never applied. var instanceOnlyKinds = map[string]bool{ RepositoryPoliciesKind: true, ChannelSettingsKind: true, SchedulingPolicyKind: true, ReviewSettingsKind: true, TaskPolicyKind: true, ToolSettingsKind: true, diff --git a/internal/app/controlplane/runtime_config.go b/internal/app/controlplane/runtime_config.go index efcca8f78..17476514d 100644 --- a/internal/app/controlplane/runtime_config.go +++ b/internal/app/controlplane/runtime_config.go @@ -90,6 +90,21 @@ func runtimeConfigFrom(ctx context.Context, reader controlplanerpc.ResourceReade if err := layerResourceJSON(ctx, reader, versions, RepositoryPoliciesKind, &out.Repos); err != nil { return config.Config{}, nil, err } + // The instance's own forge is its root connection: the file's [forge] + // section migrated into that resource, so a connection the operator + // edits or removes is what the daemon reaches the forge with. Intake and + // the webhook secret are instance infrastructure and stay the file's. + if err := layerResource(ctx, reader, versions, ForgeConnectionsKind, func(connections []config.ForgeConnection) error { + forge := out.Forge + forge.Type, forge.Host, forge.Token = "", "", config.SecretRef{} + if connection, ok := rootConnection(connections); ok { + forge.Type, forge.Host, forge.Token = connection.Type, connection.Host, connection.Token + } + out.Forge = forge + return nil + }); err != nil { + return config.Config{}, nil, err + } chat, chatVersion, err := controlplanerpc.RuntimeChatConfigFrom(ctx, reader, out.Chat) if err != nil { return config.Config{}, nil, err diff --git a/internal/domain/applystatus/applystatus.go b/internal/domain/applystatus/applystatus.go index fe801f2a7..3697ac201 100644 --- a/internal/domain/applystatus/applystatus.go +++ b/internal/domain/applystatus/applystatus.go @@ -30,7 +30,7 @@ func Processes() []string { return []string{Daemon, Gateway, Messaging} } // Applicability names the consumers of each resource, including boot-only settings. func Applicability() map[string][]string { kinds := map[string][]string{} - for _, kind := range []string{"task-policy", "provider-settings", "model-aliases", "repository-policies", "scheduling-policy", "tool-settings", "plugin-settings", "review-settings", "channel-settings", "container-runtime-policies", "credential-bindings", "identity-grants", "agent-profiles", "workflow-execution-settings", "extension-settings", "workflow-definitions", "workflow-enablement", "schedules", "eda-playbooks"} { + for _, kind := range []string{"task-policy", "provider-settings", "model-aliases", "repository-policies", "scheduling-policy", "tool-settings", "plugin-settings", "review-settings", "channel-settings", "container-runtime-policies", "credential-bindings", "identity-grants", "agent-profiles", "workflow-execution-settings", "extension-settings", "workflow-definitions", "workflow-enablement", "schedules", "eda-playbooks", "forge-connections"} { kinds[kind] = []string{Daemon} } for _, kind := range []string{"provider-settings", "model-aliases", "tool-settings", "extension-settings", "soul", "skills", "curators"} {