* fix(postgres): scope OpenPRs to the caller's org A principal-carrying caller read every org's pr_open tasks. The query now honours the request scope; the daemon's unscoped review scan still sees all orgs. Issue: archie-core-kgxw.23 * fix(statestore): serve task logs only to the task's org ReadTaskLog and StreamTaskLogContent took a task ID and read the file with no org check. The State Store's task-log reader now confirms the task is visible under the caller's scope first; unscoped services read as before. Issue: archie-core-kgxw.24 * fix(postgres): scope ListPolicies to the caller's org Every org's policy text reached any principal-carrying caller. A scoped read now sees its own org's policies, plus instance policies when scoped to the system org. The engines and the lockout guard decide for every org, so they read the full chain unscoped: the dashboard engine loads without the request's principal and the guard lifts the caller's scope. Issue: archie-core-kgxw.25 * fix(postgres): scope identity reads to the caller's org ListIdentities and GetIdentity (and its alias fallback) returned every identity to any caller. A caller scoped to an org now sees only that org's members and assigned agents; the system org and unscoped services still see the whole instance. Issue: archie-core-kgxw.26 * fix(postgres): hide instance status from other orgs ListPresence, ListChannelStatus, ListApplyStatus and GetConfigSnapshot answered any org principal with instance-wide state. They now return nothing unless the request is unscoped or scoped to the system org. The dashboard reads the published config as itself, since its handlers project it for callers of every org. Issue: archie-core-kgxw.27 * feat(postgres): assign new agents an org and role at creation A bot or service account created through the API served no org until the next State Store boot granted it one. Create now assigns it to the org the request acts in with the developer role in the same transaction, so it is authorized immediately. Issue: archie-core-kgxw.14 * feat(edastore): create event records in a chosen workspace Sources are created in a workspace of the caller's org, the default one when none is named; event types and bindings take their source's workspace and mappings their event type's, the same rule the org backfill applied. The binding, mapping and event type contracts carry the workspace, the four list endpoints accept ?workspace=, and the Events page has a workspace selector that filters every step and picks where a new source goes. Issue: archie-core-kgxw.11 * feat(webui): authorize a record in its workspace The API built every access request with no workspace, so workspace policies and per-workspace roles never applied. A request naming a source, binding, mapping, event type or task now reads the record's workspace under the caller's scope and authorizes in it; tasks carry their workspace on the State Store contract for this. Issue: archie-core-kgxw.9 * feat(access): expose workspace environment to policies Policies can test resource.workspace.environment. The workspace attribute is the workspace entity, which carries its environment; the principal brings its org's workspaces from the State Store, so the dashboard and the daemon's run checks describe a resource's workspace without another lookup, and the daemon now authorizes runs in the task's or binding's workspace. Issue: archie-core-kgxw.10 * fix(webui): ask read_secret when a source secret is read Rotating a source's secret returns its value, but the route asked update, which every developer holds, so no policy could narrow who reads a secret. It now asks read_secret on the secret, which the shipped roles give admins and an object policy can narrow further. Issue: archie-core-kgxw.17 * refactor(org): delete the unused upgrade-incomplete sentinel Nothing returns org.ErrUpgradeIncomplete: the State Store finishes the org upgrade before it serves. Issue: archie-core-kgxw.18
102 lines
3.8 KiB
Go
102 lines
3.8 KiB
Go
// Package binding owns the playbook binding vocabulary: the matcher + mapping +
|
|
// workflow + state-machine entity that turns a captured webhook into an archie
|
|
// task.
|
|
package binding
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
)
|
|
|
|
// Status is a binding's state. New bindings are pending_approval; only armed
|
|
// bindings dispatch; any edit returns to pending_approval; Approve arms. Pause
|
|
// stops an armed binding, and Resume returns it to pending_approval so it is
|
|
// approved again before it fires.
|
|
type Status string
|
|
|
|
const (
|
|
StatusPendingApproval Status = "pending_approval"
|
|
StatusArmed Status = "armed"
|
|
StatusPaused Status = "paused"
|
|
)
|
|
|
|
// Matcher selects the captures a binding applies to, by source path segment.
|
|
type Matcher struct {
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
// Binding ties a source matcher, a payload mapping and a workflow together.
|
|
// Version increments on every update.
|
|
type Binding struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Matcher Matcher `json:"matcher"`
|
|
MappingID string `json:"mapping_id"`
|
|
// Filter is an optional CEL expression over the mapping's parameters
|
|
// (CompileFilter). An event it excludes is not dispatched.
|
|
Filter string `json:"filter,omitempty"`
|
|
Workflow string `json:"workflow"`
|
|
// Owner and Repo pin the binding to a repo. Both or neither must be set.
|
|
Owner string `json:"owner,omitempty"`
|
|
Repo string `json:"repo,omitempty"`
|
|
// RepoParam names the mapped parameter holding "owner/name" when the
|
|
// repository comes from the event instead of a fixed Owner/Repo pin. The
|
|
// named repository must still be a configured one.
|
|
RepoParam string `json:"repo_param,omitempty"`
|
|
// Inputs assigns the workflow's declared inputs, each from a mapped
|
|
// parameter or a constant; CheckWorkflow checks them when the binding is
|
|
// saved.
|
|
Inputs map[string]InputSource `json:"inputs,omitempty"`
|
|
// OrgID is the org that owns the binding; the store sets it and a
|
|
// binding may target only a workflow that org has enabled.
|
|
OrgID org.OrgID `json:"org_id"`
|
|
// WorkspaceID is its source's workspace; the store sets it.
|
|
WorkspaceID org.WorkspaceID `json:"workspace_id"`
|
|
Version int `json:"version"`
|
|
Status Status `json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// Validate checks a binding is well-formed before it is persisted: a
|
|
// non-empty Name, Matcher.Source, MappingID and Workflow, and a complete
|
|
// owner/repo pin or none. Workflow existence is checked at the API/store
|
|
// layer because the domain package does not import the workflow registry.
|
|
func (b Binding) Validate() error {
|
|
if strings.TrimSpace(b.Name) == "" {
|
|
return fmt.Errorf("binding: name is required")
|
|
}
|
|
if strings.TrimSpace(b.Matcher.Source) == "" {
|
|
return fmt.Errorf("binding: matcher.source is required")
|
|
}
|
|
if strings.TrimSpace(b.MappingID) == "" {
|
|
return fmt.Errorf("binding: mapping_id is required")
|
|
}
|
|
if strings.TrimSpace(b.Workflow) == "" {
|
|
return fmt.Errorf("binding: workflow is required")
|
|
}
|
|
if (b.Owner == "") != (b.Repo == "") {
|
|
return fmt.Errorf("binding: owner and repo must both be set or both be empty")
|
|
}
|
|
if b.Owner != "" && b.RepoParam != "" {
|
|
return fmt.Errorf("binding: a fixed owner/repo and a repository parameter cannot both be set")
|
|
}
|
|
for _, name := range sortedKeys(b.Inputs) {
|
|
if err := b.Inputs[name].validate(name); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Matches reports whether a captured event would trigger this binding. An
|
|
// event that is not dispatchable (no valid signature, and not from an
|
|
// approved unsigned source) never matches, however well it would otherwise.
|
|
// Matches is pure and has no I/O.
|
|
func (m Matcher) Matches(source string, dispatchable bool) bool {
|
|
return dispatchable && m.Source == source
|
|
}
|