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.
This commit is contained in:
5 files changed
+105
-1
No files matched your search
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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))
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"} {
|
||||
|
||||
Reference in new issue
Block a user