Feat/org epic (#1634)

* 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
This commit is contained in:
sam authored and GitHub committed 2026-10-12 03:24:04 +11:00
1 parent 3edbd6b0a7
commit 3960a00f5d
60 files changed
+1695 -828

No files matched your search

@@ -7,6 +7,8 @@ package postgresdb
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countShippedOrgPolicies = `-- name: CountShippedOrgPolicies :one
@@ -159,12 +161,23 @@ func (q *Queries) InsertAccessResetAudit(ctx context.Context, recordKey string)
const listAccessPolicies = `-- name: ListAccessPolicies :many
SELECT level, org_id, workspace_id, object_kind, object_id, policy_id, text, updated_at
FROM access_policies ORDER BY level, org_id, workspace_id, object_kind, object_id, policy_id
FROM access_policies
WHERE $1::text IS NULL
OR (level = 'instance' AND $2::bool)
OR (level <> 'instance' AND org_id = $1)
ORDER BY level, org_id, workspace_id, object_kind, object_id, policy_id
`
type ListAccessPoliciesParams struct {
ScopeOrg pgtype.Text
InstanceScope bool
}
// Access policies and denial records (internal/domain/access)
func (q *Queries) ListAccessPolicies(ctx context.Context) ([]AccessPolicy, error) {
rows, err := q.db.Query(ctx, listAccessPolicies)
// A scoped read sees its own org's policies, and the instance policies only
// when it may read the instance.
func (q *Queries) ListAccessPolicies(ctx context.Context, arg ListAccessPoliciesParams) ([]AccessPolicy, error) {
rows, err := q.db.Query(ctx, listAccessPolicies, arg.ScopeOrg, arg.InstanceScope)
if err != nil {
return nil, err
}
@@ -13,7 +13,7 @@ import (
)
const armedBindingsForSource = `-- name: ArmedBindingsForSource :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
FROM bindings WHERE source = $1 AND status = 'armed' AND ($2::text IS NULL OR org_id = $2) ORDER BY created_at DESC
`
@@ -23,21 +23,22 @@ type ArmedBindingsForSourceParams struct {
}
type ArmedBindingsForSourceRow struct {
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
WorkspaceID string
}
func (q *Queries) ArmedBindingsForSource(ctx context.Context, arg ArmedBindingsForSourceParams) ([]ArmedBindingsForSourceRow, error) {
@@ -65,6 +66,7 @@ func (q *Queries) ArmedBindingsForSource(ctx context.Context, arg ArmedBindingsF
&i.Inputs,
&i.RepoParam,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -303,7 +305,7 @@ func (q *Queries) EventTypesForSource(ctx context.Context, source string) ([]Eve
}
const getBinding = `-- name: GetBinding :one
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
FROM bindings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
@@ -313,21 +315,22 @@ type GetBindingParams struct {
}
type GetBindingRow struct {
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
WorkspaceID string
}
func (q *Queries) GetBinding(ctx context.Context, arg GetBindingParams) (GetBindingRow, error) {
@@ -349,12 +352,13 @@ func (q *Queries) GetBinding(ctx context.Context, arg GetBindingParams) (GetBind
&i.Inputs,
&i.RepoParam,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
const getEventType = `-- name: GetEventType :one
SELECT id, source, name, rule, schema, created_at, updated_at
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
@@ -363,19 +367,9 @@ type GetEventTypeParams struct {
ScopeOrg pgtype.Text
}
type GetEventTypeRow struct {
ID string
Source string
Name string
Rule string
Schema string
CreatedAt time.Time
UpdatedAt time.Time
}
func (q *Queries) GetEventType(ctx context.Context, arg GetEventTypeParams) (GetEventTypeRow, error) {
func (q *Queries) GetEventType(ctx context.Context, arg GetEventTypeParams) (EventType, error) {
row := q.db.QueryRow(ctx, getEventType, arg.ID, arg.ScopeOrg)
var i GetEventTypeRow
var i EventType
err := row.Scan(
&i.ID,
&i.Source,
@@ -384,6 +378,8 @@ func (q *Queries) GetEventType(ctx context.Context, arg GetEventTypeParams) (Get
&i.Schema,
&i.CreatedAt,
&i.UpdatedAt,
&i.OrgID,
&i.WorkspaceID,
)
return i, err
}
@@ -452,8 +448,10 @@ func (q *Queries) GetSource(ctx context.Context, arg GetSourceParams) (Source, e
}
const insertBinding = `-- name: InsertBinding :exec
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, $9, $10, $11, $12)
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id, workspace_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1,
$9, $10, $11, $12,
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = $3 AND s.org_id = $12), 'default'))
`
type InsertBindingParams struct {
@@ -471,6 +469,7 @@ type InsertBindingParams struct {
OrgID string
}
// A binding lives in its source's workspace.
func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) error {
_, err := q.db.Exec(ctx, insertBinding,
arg.ID,
@@ -577,8 +576,9 @@ func (q *Queries) InsertCaptureRefusal(ctx context.Context, arg InsertCaptureRef
}
const insertEventType = `-- name: InsertEventType :exec
INSERT INTO event_types (id, source, name, rule, schema, org_id)
VALUES ($1, $2, $3, $4, $5, $6)
INSERT INTO event_types (id, source, name, rule, schema, org_id, workspace_id)
VALUES ($1, $2, $3, $4, $5, $6,
COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = $2 AND s.org_id = $6), 'default'))
`
type InsertEventTypeParams struct {
@@ -590,6 +590,7 @@ type InsertEventTypeParams struct {
OrgID string
}
// An event type lives in its source's workspace.
func (q *Queries) InsertEventType(ctx context.Context, arg InsertEventTypeParams) error {
_, err := q.db.Exec(ctx, insertEventType,
arg.ID,
@@ -603,8 +604,9 @@ func (q *Queries) InsertEventType(ctx context.Context, arg InsertEventTypeParams
}
const insertMapping = `-- name: InsertMapping :exec
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id)
VALUES ($1, $2, $3, $4, $5, $6)
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id, workspace_id)
VALUES ($1, $2, $3, $4, $5, $6,
COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = $4 AND y.org_id = $6), 'default'))
`
type InsertMappingParams struct {
@@ -616,6 +618,8 @@ type InsertMappingParams struct {
OrgID string
}
// A mapping lives in its event type's workspace; one with no event type in
// the org's default workspace.
func (q *Queries) InsertMapping(ctx context.Context, arg InsertMappingParams) error {
_, err := q.db.Exec(ctx, insertMapping,
arg.ID,
@@ -665,8 +669,10 @@ func (q *Queries) InsertPlaybookDispatch(ctx context.Context, arg InsertPlaybook
return err
}
const insertSource = `-- name: InsertSource :exec
INSERT INTO sources (path, signing, secret, org_id, workspace_id) VALUES ($1, $2, $3, $4, $5)
const insertSource = `-- name: InsertSource :execrows
INSERT INTO sources (path, signing, secret, org_id, workspace_id)
SELECT $1, $2, $3, w.org_id, w.id
FROM workspaces w WHERE w.org_id = $4 AND w.id = $5
`
type InsertSourceParams struct {
@@ -677,15 +683,19 @@ type InsertSourceParams struct {
WorkspaceID string
}
func (q *Queries) InsertSource(ctx context.Context, arg InsertSourceParams) error {
_, err := q.db.Exec(ctx, insertSource,
// Inserts nothing when the workspace is not one of the org's.
func (q *Queries) InsertSource(ctx context.Context, arg InsertSourceParams) (int64, error) {
result, err := q.db.Exec(ctx, insertSource,
arg.Path,
arg.Signing,
arg.Secret,
arg.OrgID,
arg.WorkspaceID,
)
return err
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const insertToolCall = `-- name: InsertToolCall :exec
@@ -779,26 +789,27 @@ func (q *Queries) ListBindingDispatches(ctx context.Context, arg ListBindingDisp
}
const listBindings = `-- name: ListBindings :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id, workspace_id
FROM bindings WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC
`
type ListBindingsRow struct {
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
ID string
Name string
Source string
Mapping string
Workflow string
Owner string
Repo string
Version int64
Status string
CreatedAt time.Time
UpdatedAt time.Time
Filter string
Inputs string
RepoParam string
OrgID string
WorkspaceID string
}
func (q *Queries) ListBindings(ctx context.Context, scopeOrg pgtype.Text) ([]ListBindingsRow, error) {
@@ -826,6 +837,7 @@ func (q *Queries) ListBindings(ctx context.Context, scopeOrg pgtype.Text) ([]Lis
&i.Inputs,
&i.RepoParam,
&i.OrgID,
&i.WorkspaceID,
); err != nil {
return nil, err
}
@@ -1266,8 +1278,9 @@ const updateBinding = `-- name: UpdateBinding :execrows
UPDATE bindings
SET name = $1, source = $2, mapping = $3, filter = $4,
workflow = $5, owner = $6, repo = $7,
version = version + 1, status = $8, inputs = $9, repo_param = $10, updated_at = now()
WHERE id = $11 AND ($12::text IS NULL OR org_id = $12)
version = version + 1, status = $8, inputs = $9, repo_param = $10, updated_at = now(),
workspace_id = COALESCE((SELECT s.workspace_id FROM sources s WHERE s.path = $2 AND s.org_id = bindings.org_id), 'default')
WHERE bindings.id = $11 AND ($12::text IS NULL OR bindings.org_id = $12)
`
type UpdateBindingParams struct {
@@ -1333,8 +1346,9 @@ func (q *Queries) UpdateEventType(ctx context.Context, arg UpdateEventTypeParams
const updateMapping = `-- name: UpdateMapping :execrows
UPDATE mappings
SET name = $1, source_hint = $2, event_type = $3, fields = $4, updated_at = now()
WHERE id = $5 AND ($6::text IS NULL OR org_id = $6)
SET name = $1, source_hint = $2, event_type = $3, fields = $4, updated_at = now(),
workspace_id = COALESCE((SELECT y.workspace_id FROM event_types y WHERE y.id = $3 AND y.org_id = mappings.org_id), 'default')
WHERE mappings.id = $5 AND ($6::text IS NULL OR mappings.org_id = $6)
`
type UpdateMappingParams struct {
@@ -8,6 +8,8 @@ package postgresdb
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
const claimIdentitySubject = `-- name: ClaimIdentitySubject :one
@@ -75,6 +77,33 @@ func (q *Queries) GetIdentity(ctx context.Context, id string) (Identity, error)
return i, err
}
const getVisibleIdentity = `-- name: GetVisibleIdentity :one
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
FROM identities WHERE id = $1 AND ($2::text IS NULL
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = $2)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = $2))
`
type GetVisibleIdentityParams struct {
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) GetVisibleIdentity(ctx context.Context, arg GetVisibleIdentityParams) (Identity, error) {
row := q.db.QueryRow(ctx, getVisibleIdentity, arg.ID, arg.ScopeOrg)
var i Identity
err := row.Scan(
&i.ID,
&i.Kind,
&i.DisplayName,
&i.Lifecycle,
&i.Version,
&i.CreatedAt,
&i.UpdatedAt,
)
return i, err
}
const insertIdentity = `-- name: InsertIdentity :exec
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
@@ -166,15 +195,21 @@ func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEve
const listIdentities = `-- name: ListIdentities :many
SELECT id, kind, display_name, lifecycle, version, created_at, updated_at
FROM identities ORDER BY created_at, id
FROM identities
WHERE ($1::text IS NULL
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = identities.id AND m.org_id = $1)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = identities.id AND a.org_id = $1))
ORDER BY created_at, id
`
// Identity persistence. The domain owns lifecycle semantics (internal/domain/
// identity); these queries are the durable half of that contract. Aliases are
// unique on lower(alias) rather than the case-sensitive PRIMARY KEY, matching
// the SQLite schema's COLLATE NOCASE.
func (q *Queries) ListIdentities(ctx context.Context) ([]Identity, error) {
rows, err := q.db.Query(ctx, listIdentities)
// A scoped read sees the identities that are members of, or agents assigned
// to, its org.
func (q *Queries) ListIdentities(ctx context.Context, scopeOrg pgtype.Text) ([]Identity, error) {
rows, err := q.db.Query(ctx, listIdentities, scopeOrg)
if err != nil {
return nil, err
}
@@ -240,11 +275,18 @@ func (q *Queries) ListIdentitySubjects(ctx context.Context, arg ListIdentitySubj
const resolveIdentityAlias = `-- name: ResolveIdentityAlias :one
SELECT i.id, i.kind, i.display_name, i.lifecycle, i.version, i.created_at, i.updated_at
FROM identities i JOIN identity_aliases a ON a.identity_id = i.id
WHERE lower(a.alias) = lower($1)
WHERE lower(a.alias) = lower($1) AND ($2::text IS NULL
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = i.id AND m.org_id = $2)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = i.id AND a.org_id = $2))
`
func (q *Queries) ResolveIdentityAlias(ctx context.Context, lower string) (Identity, error) {
row := q.db.QueryRow(ctx, resolveIdentityAlias, lower)
type ResolveIdentityAliasParams struct {
Alias string
ScopeOrg pgtype.Text
}
func (q *Queries) ResolveIdentityAlias(ctx context.Context, arg ResolveIdentityAliasParams) (Identity, error) {
row := q.db.QueryRow(ctx, resolveIdentityAlias, arg.Alias, arg.ScopeOrg)
var i Identity
err := row.Scan(
&i.ID,
@@ -526,7 +526,7 @@ func (q *Queries) InsertTransition(ctx context.Context, arg InsertTransitionPara
const listOpenPRs = `-- name: ListOpenPRs :many
SELECT id, owner, repo, issue_number, pr_number, status, source, identity, attempt, review_cursor, watch_comment_id, outputs
FROM tasks WHERE status = 'pr_open'
FROM tasks WHERE status = 'pr_open' AND ($1::text IS NULL OR org_id = $1)
`
type ListOpenPRsRow struct {
@@ -544,8 +544,8 @@ type ListOpenPRsRow struct {
Outputs string
}
func (q *Queries) ListOpenPRs(ctx context.Context) ([]ListOpenPRsRow, error) {
rows, err := q.db.Query(ctx, listOpenPRs)
func (q *Queries) ListOpenPRs(ctx context.Context, scopeOrg pgtype.Text) ([]ListOpenPRsRow, error) {
rows, err := q.db.Query(ctx, listOpenPRs, scopeOrg)
if err != nil {
return nil, err
}