diff --git a/internal/app/controlplane/controlplane.go b/internal/app/controlplane/controlplane.go index 2f2d0e2e8..405680346 100644 --- a/internal/app/controlplane/controlplane.go +++ b/internal/app/controlplane/controlplane.go @@ -11,6 +11,7 @@ import ( "google.golang.org/grpc/status" "google.golang.org/protobuf/types/known/timestamppb" + "github.com/samcharles93/archie-core/internal/config" pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/storecontract" @@ -33,6 +34,7 @@ var ( // root asserts the State Store against it, so this is the single definition of // what a control-plane backing store must offer. type ResourceStore interface { + ListResources(context.Context) ([]storecontract.Resource, error) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) ResourceHistory(ctx context.Context, orgID, kind string, limit int) ([]storecontract.Resource, error) Audit(ctx context.Context, table string, keys []string, limit int) ([]storecontract.AuditEntry, error) @@ -80,7 +82,7 @@ func (s *Server) Query(ctx context.Context, request *pb.QueryRequest) (*pb.Query if _, ok := s.definitions[request.GetKind()]; !ok { return nil, status.Error(codes.NotFound, "resource not found") } - resource, err := s.store.Resource(ctx, requestOrg(ctx), request.Kind) + resource, err := s.resource(ctx, requestOrg(ctx), request.Kind) if err != nil { return nil, mapError(err) } @@ -182,7 +184,7 @@ func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_W ticker := time.NewTicker(250 * time.Millisecond) defer ticker.Stop() for { - resource, err := s.store.Resource(stream.Context(), requestOrg(stream.Context()), request.Kind) + resource, err := s.resource(stream.Context(), requestOrg(stream.Context()), request.Kind) if err == nil && resource.Version > version { if err := stream.Send(&pb.WatchResponse{Resource: resourceProto(resource)}); err != nil { return err @@ -199,6 +201,19 @@ func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_W } } +// resource seeds an org's workflow documents on first use, including orgs +// created after this process started. File-derived settings are never copied. +func (s *Server) resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) { + resource, err := s.store.Resource(ctx, orgID, kind) + if !errors.Is(err, storecontract.ErrResourceNotFound) || (kind != WorkflowDefinitionsKind && kind != WorkflowEnablementKind) { + return resource, err + } + if _, err := s.seedKind(ctx, orgID, s.definitions[kind], config.Config{}); err != nil && !errors.Is(err, storecontract.ErrResourceVersionConflict) { + return storecontract.Resource{}, err + } + return s.store.Resource(ctx, orgID, kind) +} + func resourceProto(resource storecontract.Resource) *pb.Resource { return &pb.Resource{OrgId: resource.OrgID, Kind: resource.Kind, Version: resource.Version, ValueJson: resource.Value, UpdatedAt: timestamp(resource.At)} } diff --git a/internal/app/controlplane/registry.go b/internal/app/controlplane/registry.go index 63cf56776..f63182194 100644 --- a/internal/app/controlplane/registry.go +++ b/internal/app/controlplane/registry.go @@ -89,7 +89,7 @@ func (s *Server) ImportConfig(ctx context.Context, cfg config.Config) (map[strin versions := make(map[string]int64, len(s.ordered)) var skipped []SeedSkip for _, definition := range s.ordered { - version, err := s.seedKind(ctx, definition, cfg) + version, err := s.seedKind(ctx, storecontract.DefaultOrgID, definition, cfg) var refusal *seedRefusal switch { case errors.As(err, &refusal): @@ -100,6 +100,18 @@ func (s *Server) ImportConfig(ctx context.Context, cfg config.Config) (map[strin versions[definition.Kind] = version } } + resources, err := s.store.ListResources(ctx) + if err != nil { + return nil, nil, err + } + for _, resource := range resources { + if resource.OrgID == storecontract.DefaultOrgID || resource.Kind != WorkflowDefinitionsKind { + continue + } + if _, err := s.seedKind(ctx, resource.OrgID, s.definitions[resource.Kind], config.Config{}); err != nil { + return nil, nil, err + } + } return versions, skipped, nil } @@ -115,8 +127,8 @@ func (r *seedRefusal) Unwrap() error { return r.err } // seedKind seeds, refreshes or leaves one kind and returns its version. A // *seedRefusal means the seed failed validation and nothing was written. -func (s *Server) seedKind(ctx context.Context, definition Definition, cfg config.Config) (int64, error) { - stored, storedErr := s.store.Resource(ctx, storecontract.DefaultOrgID, definition.Kind) +func (s *Server) seedKind(ctx context.Context, orgID string, definition Definition, cfg config.Config) (int64, error) { + stored, storedErr := s.store.Resource(ctx, orgID, definition.Kind) absent := errors.Is(storedErr, storecontract.ErrResourceNotFound) if storedErr != nil && !absent { return 0, storedErr @@ -133,7 +145,7 @@ func (s *Server) seedKind(ctx context.Context, definition Definition, cfg config return stored.Version, nil } if !absent { - refresh, err := s.shippedValueIsStale(ctx, definition) + refresh, err := s.shippedValueIsStale(ctx, orgID, definition) if err != nil { return 0, err } @@ -142,7 +154,7 @@ func (s *Server) seedKind(ctx context.Context, definition Definition, cfg config } } write := storecontract.ResourceWrite{ - OrgID: storecontract.DefaultOrgID, Kind: definition.Kind, Value: value, + OrgID: orgID, Kind: definition.Kind, Value: value, Actor: seedActor, Source: seedSource, RequestID: seedRequestID(definition.Kind, value), ExpectedVersion: expectedVersion(stored, absent), At: time.Now().UTC(), @@ -165,18 +177,18 @@ func expectedVersion(stored storecontract.Resource, absent bool) int64 { // shippedValueIsStale reports whether the newest stored revision of a // shipped document was written by the seed. -func (s *Server) shippedValueIsStale(ctx context.Context, definition Definition) (bool, error) { +func (s *Server) shippedValueIsStale(ctx context.Context, orgID string, definition Definition) (bool, error) { if definition.Defaults == nil { return false, nil } - return s.seedWroteNewest(ctx, definition.Kind) + return s.seedWroteNewest(ctx, orgID, definition.Kind) } // seedWroteNewest reports whether the newest revision of kind was written by a // seed. It reads the ledger rather than the current row because the ledger is // what records who wrote a value, and the row does not. -func (s *Server) seedWroteNewest(ctx context.Context, kind string) (bool, error) { - history, err := s.store.ResourceHistory(ctx, storecontract.DefaultOrgID, kind, 1) +func (s *Server) seedWroteNewest(ctx context.Context, orgID, kind string) (bool, error) { + history, err := s.store.ResourceHistory(ctx, orgID, kind, 1) if err != nil { return false, fmt.Errorf("read %s history: %w", kind, err) } @@ -231,19 +243,20 @@ func (s *Server) Owns(kind string) bool { // ValidateStored decodes every stored resource with its definition and // returns how many it checked. Errors name the kind and revision. func (s *Server) ValidateStored(ctx context.Context) (int, error) { + resources, err := s.store.ListResources(ctx) + if err != nil { + return 0, err + } checked := 0 var failures []error - for _, definition := range s.ordered { - resource, err := s.store.Resource(ctx, storecontract.DefaultOrgID, definition.Kind) - if errors.Is(err, storecontract.ErrResourceNotFound) { + for _, resource := range resources { + definition, ok := s.definitions[resource.Kind] + if !ok { continue } - if err != nil { - return checked, fmt.Errorf("read %s: %w", definition.Kind, err) - } checked++ if _, err := definition.Decode(resource.Value); err != nil { - failures = append(failures, fmt.Errorf("%s at version %d: %w", definition.Kind, resource.Version, err)) + failures = append(failures, fmt.Errorf("org %s: %s at version %d: %w", resource.OrgID, definition.Kind, resource.Version, err)) } } return checked, errors.Join(failures...) diff --git a/internal/app/controlplane/workflow_org_test.go b/internal/app/controlplane/workflow_org_test.go new file mode 100644 index 000000000..b00bd2b24 --- /dev/null +++ b/internal/app/controlplane/workflow_org_test.go @@ -0,0 +1,211 @@ +package controlplane_test + +import ( + "context" + "encoding/json" + "net" + "net/http" + "net/http/httptest" + "os" + "strings" + "testing" + "time" + + "google.golang.org/grpc" + + "github.com/samcharles93/archie-core/internal/app/controlplane" + "github.com/samcharles93/archie-core/internal/config" + pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/identity" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/domain/workflow" + infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" + "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" + "github.com/samcharles93/archie-core/internal/infrastructure/workflowsteps" + "github.com/samcharles93/archie-core/internal/webui" +) + +func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) } + +func TestOrgWorkflowResources(t *testing.T) { + for _, kind := range []string{controlplane.WorkflowDefinitionsKind, controlplane.WorkflowEnablementKind} { + t.Run(kind, func(t *testing.T) { + ctx := t.Context() + db := pgstore.Open(t) + if err := db.BootstrapIdentities(ctx, nil); err != nil { + t.Fatal(err) + } + if err := db.UpgradeDefaultOrg(ctx); err != nil { + t.Fatal(err) + } + steps, err := workflowsteps.NewManager() + if err != nil { + t.Fatal(err) + } + server, err := controlplane.NewServer(db, steps) + if err != nil { + t.Fatal(err) + } + if _, _, err := server.ImportConfig(ctx, config.Config{}); err != nil { + t.Fatal(err) + } + owner, err := db.Create(ctx, identity.Identity{ID: "acme-owner", Kind: identity.KindUser, DisplayName: "Owner", Lifecycle: identity.LifecycleActive}, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: "owner", At: time.Now()}) + if err != nil { + t.Fatal(err) + } + if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, owner.ID); err != nil { + t.Fatal(err) + } + client := workflowRPC(t, server, db) + principal, err := db.PrincipalFor(ctx, owner.ID) + if err != nil { + t.Fatal(err) + } + other := access.WithPrincipal(ctx, principal) + initial, err := client.ControlPlane().Query(other, &pb.QueryRequest{Kind: kind}) + if err != nil { + t.Fatalf("new org cannot read its shipped %s: %v", kind, err) + } + if initial.Resource.OrgId != "acme" { + t.Fatalf("read org %q", initial.Resource.OrgId) + } + value := []byte(`{"orgs":{"acme":{"disabled":["tdd"]}}}`) + if kind == controlplane.WorkflowDefinitionsKind { + collection := workflow.ShippedDefinitions() + collection.Definitions = append(collection.Definitions, workflow.WorkflowDefinitionEntry{ID: "acme-only", YAML: "id: acme-only\nrepository: none\nsteps:\n - type: workflow.finish\n"}) + value, err = json.Marshal(collection) + if err != nil { + t.Fatal(err) + } + } + changed, err := client.ControlPlane().Command(other, &pb.CommandRequest{Kind: kind, Command: "replace", ValueJson: value, ExpectedVersion: initial.Resource.Version, Actor: "owner", Source: "test", RequestId: "edit"}) + if err != nil { + t.Fatal(err) + } + if _, _, err := server.ImportConfig(ctx, config.Config{}); err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + ctxOrg org.OrgID + want bool + }{{"acme", true}, {org.DefaultOrgID, false}} { + queryCtx := ctx + if tc.ctxOrg == "acme" { + queryCtx = other + } + got, err := client.ControlPlane().Query(queryCtx, &pb.QueryRequest{Kind: kind}) + if err != nil { + t.Fatal(err) + } + if strings.Contains(string(got.Resource.ValueJson), "acme") != tc.want { + t.Fatalf("org %q got %s", tc.ctxOrg, got.Resource.ValueJson) + } + if tc.want && got.Resource.Version != changed.Resource.Version { + t.Fatalf("boot replaced operator's document") + } + } + if kind == controlplane.WorkflowDefinitionsKind { + checkWorkflowDashboard(t, db, client) + } + }) + } +} + +func workflowRPC(t *testing.T, control *controlplane.Server, db *pgstore.TaskDB) *staterpc.Client { + t.Helper() + listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = listener.Close() }) + grants := &staterpc.TaskGrants{} + callers := staterpc.Callers{Principals: db} + srv := grpc.NewServer(grpc.ChainUnaryInterceptor(grants.UnaryInterceptor("test-token"), callers.Unary()), grpc.ChainStreamInterceptor(grants.StreamInterceptor("test-token"), callers.Stream())) + staterpc.RegisterServer(srv, staterpc.Deps{ControlPlane: control, Tasks: db, Principals: db, Identities: db}) + t.Cleanup(srv.Stop) + go func() { _ = srv.Serve(listener) }() + client, closeClient, err := staterpc.Dial(listener.Addr().String(), "archie-ui", "test-token") + if err != nil { + t.Fatal(err) + } + t.Cleanup(closeClient) + return client +} + +func TestValidateStoredOrgWorkflows(t *testing.T) { + ctx := t.Context() + db := pgstore.Open(t) + if err := db.BootstrapIdentities(ctx, nil); err != nil { + t.Fatal(err) + } + if err := db.UpgradeDefaultOrg(ctx); err != nil { + t.Fatal(err) + } + steps, err := workflowsteps.NewManager() + if err != nil { + t.Fatal(err) + } + server, err := controlplane.NewServer(db, steps) + if err != nil { + t.Fatal(err) + } + if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, identity.SystemID); err != nil { + t.Fatal(err) + } + if _, err := db.PutResource(ctx, storecontract.ResourceWrite{OrgID: "acme", Kind: controlplane.WorkflowDefinitionsKind, Value: []byte(`{"definitions":[{"id":"broken","yaml":"id: broken\nsteps:\n - type: nonexistent\n"}]}`), Actor: "owner", Source: "test", RequestID: "invalid"}); err != nil { + t.Fatal(err) + } + if _, err := server.ValidateStored(ctx); err == nil || !strings.Contains(err.Error(), "acme") { + t.Fatalf("invalid org workflow was not reported: %v", err) + } +} + +func checkWorkflowDashboard(t *testing.T, db *pgstore.TaskDB, client *staterpc.Client) { + t.Helper() + ctx := t.Context() + policies := append(access.ShippedOrgPolicies("acme"), access.ShippedOrgPolicies(org.DefaultOrgID)...) + chain, err := infraaccess.New(policies) + if err != nil { + t.Fatal(err) + } + srv := &webui.Server{Store: client, ControlPlane: client.ControlPlane(), Access: chain, Principals: client, Authenticate: func(ctx context.Context, token string) (identity.Identity, error) { + return db.Get(ctx, identity.IdentityID(token)) + }} + handler := srv.Handler() + for _, tc := range []struct { + method, path, body string + status int + }{ + {"GET", "/api/workflows", "", http.StatusOK}, + {"POST", "/api/work-requests", `{"identity":"acme-owner","workflow":"acme-only","title":"Org work","instructions":"Finish"}`, http.StatusCreated}, + } { + r := httptest.NewRequestWithContext(ctx, tc.method, tc.path, strings.NewReader(tc.body)) + r.Header.Set("Authorization", "Bearer acme-owner") + r.Header.Set("Content-Type", "application/json") + r.Header.Set("X-Archie-CSRF", "1") + w := httptest.NewRecorder() + handler.ServeHTTP(w, r) + if w.Code != tc.status { + t.Fatalf("%s %s: %d %s", tc.method, tc.path, w.Code, w.Body.String()) + } + if tc.method == "GET" && !strings.Contains(w.Body.String(), "acme-only") { + t.Fatalf("dashboard omitted the org's workflow: %s", w.Body.String()) + } + if tc.method == "POST" { + var reply struct { + TaskID int64 `json:"task_id"` + } + if err := json.Unmarshal(w.Body.Bytes(), &reply); err != nil { + t.Fatal(err) + } + run, err := db.TaskByID(ctx, reply.TaskID) + if err != nil || run.Org != "acme" || run.Workflow != "acme-only" { + t.Fatalf("admitted task %#v err %v", run, err) + } + } + } +} diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 29691fc5c..2b95a89dc 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -571,7 +571,32 @@ func (d *Daemon) dispatchBindings(ctx context.Context) { if d.Bindings == nil || d.BindingDispatcher == nil || d.BindingTaskCreator == nil { return } + if d.Principals == nil { + d.dispatchOrgBindings(ctx) + return + } + identities := []identity.IdentityID{d.RootIdentityID} + for _, runner := range d.Identities { + if runner != nil { + identities = append(identities, runner.ID) + } + } + seen := make(map[org.OrgID]bool) + for _, id := range identities { + principal, err := d.Principals.PrincipalFor(ctx, id) + if err != nil { + d.Log.Warn("binding dispatch: principal unavailable", "identity", id, "err", err) + continue + } + if seen[principal.Org] { + continue + } + seen[principal.Org] = true + d.dispatchOrgBindings(org.WithOrg(access.WithPrincipal(ctx, principal), principal.Org)) + } +} +func (d *Daemon) dispatchOrgBindings(ctx context.Context) { // Gather armed sources first so the captures query filters down to // only relevant rows. bindings, err := d.Bindings.ListBindings(ctx) @@ -581,7 +606,7 @@ func (d *Daemon) dispatchBindings(ctx context.Context) { } sources := make([]string, 0, len(bindings)) for _, b := range bindings { - if b.Status == binding.StatusArmed && b.Matcher.Source != "" { + if b.OrgID == org.OrgFromContext(ctx) && b.Status == binding.StatusArmed && b.Matcher.Source != "" { sources = append(sources, b.Matcher.Source) } } @@ -619,7 +644,7 @@ func (d *Daemon) dispatchCapture(ctx context.Context, c storecontract.CapturedEv return } for _, b := range armed { - if b.Matcher.Matches(c.Source, c.Dispatchable()) && enablement.Enabled(b.OrgID, b.Workflow) { + if b.OrgID == org.OrgFromContext(ctx) && b.Matcher.Matches(c.Source, c.Dispatchable()) && enablement.Enabled(b.OrgID, b.Workflow) { d.dispatchOneBinding(ctx, b, c, workflows) } } @@ -703,7 +728,11 @@ func (d *Daemon) claimAndEnqueue(ctx context.Context, b binding.Binding, c store if target.declared { body = fmt.Sprintf("Started by binding %q from capture %q; the workflow's inputs travel with the task.", b.Name, c.ID) } - task, err := d.BindingTaskCreator.EnqueueBindingTask(ctx, target.owner, target.repo, title, body, b.Workflow, "", b.ID, b.Version, target.inputs) + taskIdentity := "" + if principal, ok := access.PrincipalFromContext(ctx); ok && (principal.IdentityID != d.RootIdentityID || principal.Org != org.DefaultOrgID) { + taskIdentity = string(principal.IdentityID) + } + task, err := d.BindingTaskCreator.EnqueueBindingTask(ctx, target.owner, target.repo, title, body, b.Workflow, taskIdentity, b.ID, b.Version, target.inputs) if err != nil { d.Log.Warn("binding dispatch: enqueue", "binding", b.ID, "capture", c.ID, "error", err) return @@ -720,7 +749,11 @@ func (d *Daemon) authorizeDispatch(ctx context.Context, b binding.Binding, c sto if d.Access == nil || d.Principals == nil { return true } - principal, err := d.Principals.PrincipalFor(ctx, d.RootIdentityID) + principal, ok := access.PrincipalFromContext(ctx) + var err error + if !ok { + principal, err = d.Principals.PrincipalFor(ctx, d.RootIdentityID) + } if err != nil { d.Log.Warn("binding dispatch: principal unavailable; dispatch parked for the next cycle", "binding", b.ID, "capture", c.ID, "error", err) @@ -1858,6 +1891,10 @@ func validateProfileMeetsNeeds(profile config.AgentProfile, needs workflowtask.W // passes through here, whatever started it, so this is the one enablement // check for issue labels, handoffs, approvals, calls and playbooks alike. func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task) error { + ctx, err := d.workflowContext(ctx, task) + if err != nil { + return err + } if err := d.pinDefinition(ctx, task); err != nil { return err } @@ -2188,7 +2225,7 @@ func (d *Daemon) identityFor(task *workflow.Task) *IdentityRunner { // class if not. Empty is the root identity. An unknown or inactive identity // never falls back to the root forge. func (d *Daemon) identityMayAct(ctx context.Context, name string) (bool, string, taskstate.ParkClass) { - if name == "" { + if name == "" || name == string(d.RootIdentityID) { return true, "", "" } runner := d.identityFor(&workflow.Task{Identity: name}) diff --git a/internal/daemon/workflow_org.go b/internal/daemon/workflow_org.go new file mode 100644 index 000000000..f6d327d6e --- /dev/null +++ b/internal/daemon/workflow_org.go @@ -0,0 +1,38 @@ +package daemon + +import ( + "context" + "fmt" + + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/workflow" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +// workflowContext forwards a task's identity so the State Store selects its +// org's definitions. An identity assigned elsewhere cannot supply the pin. +func (d *Daemon) workflowContext(ctx context.Context, task *workflow.Task) (context.Context, error) { + owner := task.Org + if owner == "" { + owner = org.DefaultOrgID + } + if d.Principals == nil { + principal, forwarded := access.PrincipalFromContext(ctx) + if owner != org.DefaultOrgID || org.OrgFromContext(ctx) != owner || (forwarded && principal.Org != "" && principal.Org != owner) { + return nil, pinFailure{taskstate.ParkNeedsHuman, fmt.Errorf("workflow org %s: principal unavailable", owner)} + } + return ctx, nil + } + principal, err := d.Principals.PrincipalFor(ctx, d.publisherIdentity(task.Identity)) + if err != nil { + return nil, pinFailure{taskstate.ParkTransient, fmt.Errorf("workflow principal: %w", err)} + } + if principal.Org == "" { + principal.Org = org.DefaultOrgID + } + if principal.Org != owner { + return nil, pinFailure{taskstate.ParkNeedsHuman, fmt.Errorf("workflow identity belongs to org %s, task belongs to %s", principal.Org, owner)} + } + return org.WithOrg(access.WithPrincipal(ctx, principal), owner), nil +} diff --git a/internal/daemon/workflow_org_test.go b/internal/daemon/workflow_org_test.go new file mode 100644 index 000000000..d953383be --- /dev/null +++ b/internal/daemon/workflow_org_test.go @@ -0,0 +1,155 @@ +package daemon + +import ( + "context" + "fmt" + "log/slog" + "slices" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/binding" + "github.com/samcharles93/archie-core/internal/domain/identity" + "github.com/samcharles93/archie-core/internal/domain/mapping" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/domain/workflow" + workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/taskstate" +) + +type workflowPrincipals map[identity.IdentityID]org.OrgID + +func (p workflowPrincipals) PrincipalFor(_ context.Context, id identity.IdentityID) (access.Principal, error) { + owner, ok := p[id] + if !ok { + return access.Principal{}, fmt.Errorf("principal %q unavailable", id) + } + return access.Principal{IdentityID: id, Org: owner}, nil +} + +type orgBindingStore struct { + storecontract.TaskStore + storecontract.BindingStore + storecontract.MappingStore + bindings []binding.Binding + started map[org.OrgID]*workflow.Task +} + +func (s *orgBindingStore) ListBindings(context.Context) ([]binding.Binding, error) { + return s.bindings, nil +} + +func (s *orgBindingStore) ArmedBindingsForSource(_ context.Context, source string) ([]binding.Binding, error) { + return slices.DeleteFunc(slices.Clone(s.bindings), func(b binding.Binding) bool { return b.Matcher.Source != source }), nil +} + +func (*orgBindingStore) ListUndispatchedCaptures(_ context.Context, sources []string, _ int) ([]storecontract.CapturedEvent, error) { + var captures []storecontract.CapturedEvent + for _, source := range sources { + captures = append(captures, storecontract.CapturedEvent{ID: source, Source: source, EventType: source, Body: "{}", Authenticated: true}) + } + return captures, nil +} + +func (*orgBindingStore) RecordDispatch(context.Context, string, int64, string, int64, string) error { + return nil +} +func (*orgBindingStore) SetDispatchTask(context.Context, string, string, int64) error { return nil } +func (*orgBindingStore) InsertEvent(context.Context, events.Event) (int64, error) { return 0, nil } + +func (*orgBindingStore) GetMapping(_ context.Context, id string) (*mapping.Mapping, error) { + return &mapping.Mapping{EventTypeID: id}, nil +} + +func (s *orgBindingStore) EnqueueBindingTask(ctx context.Context, owner, repo, _, _, wf, identity, bindingID string, _ int, _ map[string]any) (*workflow.Task, error) { + run := &workflow.Task{ID: 1, Owner: owner, Repo: repo, Workflow: wf, Identity: identity, Org: org.OrgFromContext(ctx), BindingID: bindingID} + s.started[run.Org] = run + return run, nil +} + +func TestDispatchOrgWorkflows(t *testing.T) { + for _, disabled := range []bool{false, true} { + t.Run(fmt.Sprintf("acme disabled %v", disabled), func(t *testing.T) { + store := &orgBindingStore{started: map[org.OrgID]*workflow.Task{}, bindings: []binding.Binding{ + {ID: "root", MappingID: "root", Matcher: binding.Matcher{Source: "root"}, OrgID: org.DefaultOrgID, Workflow: "deploy", Status: binding.StatusArmed, Owner: "owner", Repo: "repo"}, + {ID: "acme", MappingID: "acme", Matcher: binding.Matcher{Source: "acme"}, OrgID: "acme", Workflow: "deploy", Status: binding.StatusArmed}, + }} + d := &Daemon{Cfg: config.NewHolder(config.Config{}), Store: store, Log: slog.New(slog.DiscardHandler), Bindings: store, BindingDispatcher: store, BindingTaskCreator: store, Mappings: store, RootIdentityID: "root", Identities: []*IdentityRunner{{ID: "acme-bot", Name: "bot"}}, Principals: workflowPrincipals{"root": org.DefaultOrgID, "acme-bot": "acme"}, WorkflowDefinitions: orgDefinitions{ + org.DefaultOrgID: "id: deploy\nrepository: required\nsteps:\n - type: workflow.finish\n", + "acme": "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n", + }} + if disabled { + d.WorkflowEnablement = enablementOf(workflowtask.WorkflowEnablement{}.SetEnabled("acme", "deploy", false)) + } + d.dispatchBindings(t.Context()) + root := store.started[org.DefaultOrgID] + if root == nil || root.Repo != "repo" || root.Identity != "" { + t.Fatalf("root dispatch %#v", root) + } + acme := store.started["acme"] + if disabled { + if acme != nil { + t.Fatalf("disabled org dispatched %#v", acme) + } + return + } + if acme == nil || acme.Repo != "" || acme.Identity != "acme-bot" { + t.Fatalf("org's repo-free workflow did not dispatch as its identity: %#v", acme) + } + }) + } +} + +type orgDefinitions map[org.OrgID]string + +func (d orgDefinitions) WorkflowDefinitions(ctx context.Context) (workflow.WorkflowDefinitionCollection, int64, error) { + owner := org.DefaultOrgID + if p, ok := access.PrincipalFromContext(ctx); ok && p.Org != "" { + owner = p.Org + } + return workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "deploy", YAML: d[owner]}}}, 3, nil +} + +func TestPinWorkflowInTaskOrg(t *testing.T) { + const shared = "id: deploy\nsteps:\n - type: workflow.finish\n" + const custom = "id: deploy\nrepository: none\nsteps:\n - type: workflow.finish\n" + for _, tc := range []struct { + name string + owner org.OrgID + principals workflowPrincipals + want string + refused bool + contextOrg org.OrgID + }{ + {"legacy default org", "", nil, shared, false, ""}, + {"non-default org definition", "acme", workflowPrincipals{"bot": "acme"}, custom, false, ""}, + {"another org cannot supply the definition", "acme", workflowPrincipals{"bot": org.DefaultOrgID}, "", true, ""}, + {"unresolved org is refused", "acme", nil, "", true, ""}, + {"unresolved context cannot select another org", "", nil, "", true, "acme"}, + } { + t.Run(tc.name, func(t *testing.T) { + d := &Daemon{Store: &identityStore{}, RootIdentityID: "bot", WorkflowDefinitions: orgDefinitions{org.DefaultOrgID: shared, "acme": custom}} + if tc.principals != nil { + d.Principals = tc.principals + } + task := &workflow.Task{ID: 1, Org: tc.owner, Workflow: "deploy"} + ctx := t.Context() + if tc.contextOrg != "" { + ctx = org.WithOrg(access.WithPrincipal(ctx, access.Principal{IdentityID: "foreign", Org: tc.contextOrg}), tc.contextOrg) + } + err := d.pinWorkflowDefinition(ctx, task) + if tc.refused { + if err == nil || pinParkClass(err) != taskstate.ParkNeedsHuman || task.WorkflowDefinitionYAML != "" { + t.Fatalf("cross-org pin accepted: task %#v err %v", task, err) + } + return + } + if err != nil || task.WorkflowDefinitionYAML != tc.want { + t.Fatalf("pinned %q, want %q; err %v", task.WorkflowDefinitionYAML, tc.want, err) + } + }) + } +} diff --git a/internal/infrastructure/postgres/dispatch_org_test.go b/internal/infrastructure/postgres/dispatch_org_test.go new file mode 100644 index 000000000..4b9f24e2c --- /dev/null +++ b/internal/infrastructure/postgres/dispatch_org_test.go @@ -0,0 +1,82 @@ +package postgres_test + +import ( + "testing" + "time" + + "github.com/samcharles93/archie-core/internal/domain/binding" + "github.com/samcharles93/archie-core/internal/domain/eventtype" + "github.com/samcharles93/archie-core/internal/domain/identity" + "github.com/samcharles93/archie-core/internal/domain/mapping" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore" +) + +func TestPendingDispatchInCallerOrg(t *testing.T) { + ctx := t.Context() + db := pgstore.Open(t) + if err := db.BootstrapIdentities(ctx, nil); err != nil { + t.Fatal(err) + } + if err := db.UpgradeDefaultOrg(ctx); err != nil { + t.Fatal(err) + } + for _, who := range []identity.Identity{{ID: "acme-owner", Kind: identity.KindUser, DisplayName: "Owner", Lifecycle: identity.LifecycleActive}, {ID: "acme-bot", Kind: identity.KindBot, DisplayName: "bot", Lifecycle: identity.LifecycleActive}} { + if _, err := db.Create(ctx, who, identity.Audit{ActorID: identity.SystemID, Source: "test", RequestID: string(who.ID)}); err != nil { + t.Fatal(err) + } + } + if _, err := db.CreateOrg(ctx, org.Org{ID: "acme", Name: "Acme"}, "acme-owner"); err != nil { + t.Fatal(err) + } + if err := db.AssignAgent(ctx, org.AgentAssignment{IdentityID: "acme-bot", OrgID: "acme"}); err != nil { + t.Fatal(err) + } + eda := postgres.NewEDA(db.Pool, nil) + ids := map[org.OrgID]string{} + for n, owner := range []org.OrgID{org.DefaultOrgID, "acme"} { + typeID, err := eda.InsertEventType(ctx, eventtype.EventType{Source: "webhook", Name: string(owner), Rule: eventtype.Rule{Headers: []eventtype.HeaderCondition{{Name: "X-Org", Value: string(owner)}}}}) + if err != nil { + t.Fatal(err) + } + mappingID, err := eda.InsertMapping(ctx, mapping.Mapping{Name: string(owner), EventTypeID: typeID}) + if err != nil { + t.Fatal(err) + } + bindingID, err := eda.InsertBinding(ctx, binding.Binding{Name: string(owner), Matcher: binding.Matcher{Source: "webhook"}, MappingID: mappingID, Workflow: "deploy"}) + if err != nil { + t.Fatal(err) + } + if err := eda.ApproveBinding(ctx, bindingID); err != nil { + t.Fatal(err) + } + captureID, err := eda.InsertCapture(ctx, storecontract.CapturedEvent{Source: "webhook", Body: "{}", Headers: `{"X-Org":["` + string(owner) + `"]}`, Authenticated: true, EventType: typeID, ReceivedAt: time.Now().Add(time.Duration(n) * time.Minute)}, time.Hour, 100) + if err != nil { + t.Fatal(err) + } + // EDA writes do not yet take org ownership; model their persisted org + // here to exercise the dispatch read independently of that work. + for _, record := range []struct{ table, id string }{{"event_types", typeID}, {"mappings", mappingID}, {"bindings", bindingID}, {"captures", captureID}} { + if _, err := db.Pool.Exec(ctx, "UPDATE "+record.table+" SET org_id=$1 WHERE id=$2", owner, record.id); err != nil { + t.Fatal(err) + } + } + ids[owner] = captureID + if owner == "acme" { + run, err := db.EnqueueBindingTask(org.WithOrg(ctx, owner), "", "", "Org dispatch", "", "deploy", "acme-bot", bindingID, 1, nil) + if err != nil || run.Org != owner || run.Identity != "acme-bot" { + t.Fatalf("binding task %#v err %v", run, err) + } + } + } + for _, owner := range []org.OrgID{org.DefaultOrgID, "acme"} { + t.Run(string(owner), func(t *testing.T) { + captures, err := eda.ListUndispatchedCaptures(org.WithOrg(t.Context(), owner), []string{"webhook"}, 1) + if err != nil || len(captures) != 1 || captures[0].ID != ids[owner] { + t.Fatalf("org %s got captures %#v err %v", owner, captures, err) + } + }) + } +} diff --git a/internal/infrastructure/postgres/edastore.go b/internal/infrastructure/postgres/edastore.go index 59903045f..9ac847119 100644 --- a/internal/infrastructure/postgres/edastore.go +++ b/internal/infrastructure/postgres/edastore.go @@ -162,6 +162,7 @@ func (s *EDA) ListUndispatchedCaptures(ctx context.Context, sources []string, li return nil, nil } rows, err := s.q.ListUndispatchedCaptures(ctx, postgresdb.ListUndispatchedCapturesParams{ + OrgID: string(org.OrgFromContext(ctx)), Sources: sources, EntryLimit: int32(limit), }) diff --git a/internal/infrastructure/postgres/postgresdb/edastore.sql.go b/internal/infrastructure/postgres/postgresdb/edastore.sql.go index 3d02c33c2..85b143e40 100644 --- a/internal/infrastructure/postgres/postgresdb/edastore.sql.go +++ b/internal/infrastructure/postgres/postgresdb/edastore.sql.go @@ -897,24 +897,25 @@ func (q *Queries) ListSources(ctx context.Context) ([]Source, error) { const listUndispatchedCaptures = `-- name: ListUndispatchedCaptures :many SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery FROM captures c -WHERE c.source = ANY($1::text[]) +WHERE c.org_id = $1 AND c.source = ANY($2::text[]) AND c.event_type <> '' AND EXISTS ( SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping - WHERE b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type + WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery) ) ORDER BY c.received_at DESC -LIMIT $2 +LIMIT $3 ` type ListUndispatchedCapturesParams struct { + OrgID string Sources []string EntryLimit int32 } func (q *Queries) ListUndispatchedCaptures(ctx context.Context, arg ListUndispatchedCapturesParams) ([]Capture, error) { - rows, err := q.db.Query(ctx, listUndispatchedCaptures, arg.Sources, arg.EntryLimit) + rows, err := q.db.Query(ctx, listUndispatchedCaptures, arg.OrgID, arg.Sources, arg.EntryLimit) if err != nil { return nil, err } diff --git a/internal/infrastructure/postgres/postgresdb/resources.sql.go b/internal/infrastructure/postgres/postgresdb/resources.sql.go index d1c8c64ce..505d10a98 100644 --- a/internal/infrastructure/postgres/postgresdb/resources.sql.go +++ b/internal/infrastructure/postgres/postgresdb/resources.sql.go @@ -106,6 +106,37 @@ func (q *Queries) InsertResourceHistory(ctx context.Context, arg InsertResourceH return i, err } +const listResources = `-- name: ListResources :many +SELECT kind, value, version, updated_at, org_id FROM resources +ORDER BY org_id, kind +` + +func (q *Queries) ListResources(ctx context.Context) ([]Resource, error) { + rows, err := q.db.Query(ctx, listResources) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Resource + for rows.Next() { + var i Resource + if err := rows.Scan( + &i.Kind, + &i.Value, + &i.Version, + &i.UpdatedAt, + &i.OrgID, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const lockResourceWrite = `-- name: LockResourceWrite :exec SELECT pg_advisory_xact_lock(hashtextextended($1::text || '/' || $2::text, 0)) ` diff --git a/internal/infrastructure/postgres/queries/edastore.sql b/internal/infrastructure/postgres/queries/edastore.sql index db54e09c8..35e2fb400 100644 --- a/internal/infrastructure/postgres/queries/edastore.sql +++ b/internal/infrastructure/postgres/queries/edastore.sql @@ -13,11 +13,11 @@ LIMIT $1; -- name: ListUndispatchedCaptures :many SELECT c.id, c.source, c.remote_addr, c.content_type, c.headers, c.body, c.authenticated, c.received_at, c.unsigned, c.event_type, c.org_id, c.workspace_id, c.delivery FROM captures c -WHERE c.source = ANY(@sources::text[]) +WHERE c.org_id = @org_id AND c.source = ANY(@sources::text[]) AND c.event_type <> '' AND EXISTS ( SELECT 1 FROM bindings b JOIN mappings m ON m.id = b.mapping - WHERE b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type + WHERE b.org_id = c.org_id AND b.source = c.source AND b.status = 'armed' AND m.event_type = c.event_type AND NOT EXISTS (SELECT 1 FROM binding_dispatches d WHERE d.binding = b.id AND d.delivery = c.delivery) ) ORDER BY c.received_at DESC diff --git a/internal/infrastructure/postgres/queries/resources.sql b/internal/infrastructure/postgres/queries/resources.sql index 6d9117a4c..d6a5ba291 100644 --- a/internal/infrastructure/postgres/queries/resources.sql +++ b/internal/infrastructure/postgres/queries/resources.sql @@ -9,6 +9,10 @@ SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg(org_id)::text || '/' || s SELECT kind, value, version, updated_at, org_id FROM resources WHERE org_id = $1 AND kind = $2; +-- name: ListResources :many +SELECT kind, value, version, updated_at, org_id FROM resources +ORDER BY org_id, kind; + -- name: ResourceByRequest :one SELECT org_id, kind, value, version, actor, source, request_id, expected_version, current_version, at diff --git a/internal/infrastructure/postgres/resources.go b/internal/infrastructure/postgres/resources.go index e47f349e1..7617f5cd5 100644 --- a/internal/infrastructure/postgres/resources.go +++ b/internal/infrastructure/postgres/resources.go @@ -25,6 +25,19 @@ func NewResources(pool *pgxpool.Pool) *Resources { return &Resources{pool: pool} } +// ListResources supplies every org's documents for startup validation. +func (s *Resources) ListResources(ctx context.Context) ([]storecontract.Resource, error) { + rows, err := postgresdb.New(s.pool).ListResources(ctx) + if err != nil { + return nil, err + } + resources := make([]storecontract.Resource, 0, len(rows)) + for _, row := range rows { + resources = append(resources, resourceFromCurrent(row)) + } + return resources, nil +} + func (s *Resources) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) { if err := checkResourceKey(orgID, kind); err != nil { return storecontract.Resource{}, err