From 5af4428fc3dae1fc67170b3ffa5bb6cf47883a98 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 13 Oct 2026 16:17:38 +1100 Subject: [PATCH] fix(controlplane): instance settings are written by the system org only Issue: archie-core-f5zso --- .../app/controlplane/instance_only_test.go | 27 +++++++++++++++++++ internal/app/controlplane/org_models.go | 14 +++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 internal/app/controlplane/instance_only_test.go diff --git a/internal/app/controlplane/instance_only_test.go b/internal/app/controlplane/instance_only_test.go new file mode 100644 index 000000000..1430a3939 --- /dev/null +++ b/internal/app/controlplane/instance_only_test.go @@ -0,0 +1,27 @@ +package controlplane + +import ( + "errors" + "testing" + + "github.com/samcharles93/archie-core/internal/domain/storecontract" +) + +func TestInstanceOnlyKindsRefuseOtherOrgs(t *testing.T) { + tests := []struct { + name, org, kind string + refused bool + }{ + {"another org writes an instance setting", "acme", ToolSettingsKind, true}, + {"the system org writes an instance setting", storecontract.DefaultOrgID, ToolSettingsKind, false}, + {"another org writes an org-scoped kind", "acme", PersonasKind, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := (&Server{}).checkOrgWrite(t.Context(), tt.org, tt.kind, []byte(`{}`)) + if refused := errors.Is(err, ErrValidation); refused != tt.refused { + t.Fatalf("refused = %v, want %v (err %v)", refused, tt.refused, err) + } + }) + } +} diff --git a/internal/app/controlplane/org_models.go b/internal/app/controlplane/org_models.go index acb576a03..7dd74c4e7 100644 --- a/internal/app/controlplane/org_models.go +++ b/internal/app/controlplane/org_models.go @@ -152,11 +152,23 @@ func (s *Server) readResource(ctx context.Context, orgID, kind string, into any) return nil } +// instanceOnlyKinds are the kinds the runtime layers from the system org's +// copy alone. 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, + PluginSettingsKind: true, ContainerRuntimePoliciesKind: true, AgentProfileKind: true, + ExtensionSettingsKind: true, UpdateSettingsKind: true, WorkflowExecutionSettingsKind: true, +} + // checkOrgWrite applies the rules a resource's static validation cannot: the // policy is the system org's alone, an org's providers need its policy's // leave and its own credentials, and every alias must name a model the -// writing org may use. +// writing org may use. Instance settings are refused from any other org. func (s *Server) checkOrgWrite(ctx context.Context, orgID, kind string, value []byte) error { + if instanceOnlyKinds[kind] && orgID != storecontract.DefaultOrgID { + return fmt.Errorf("%w: %s is an instance setting and only the system org writes it", ErrValidation, kind) + } switch kind { case OrgModelPolicyKind: if orgID != storecontract.DefaultOrgID {