Files
archie/internal/infrastructure/postgres/postgresdb/access.sql.go
T
sam f34f60f9c7 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
2026-10-12 03:24:04 +11:00

340 lines
8.9 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: access.sql
package postgresdb
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
)
const countShippedOrgPolicies = `-- name: CountShippedOrgPolicies :one
SELECT count(*) FROM access_policies
WHERE level = 'org' AND org_id = $1 AND policy_id = ANY($2::text[])
`
type CountShippedOrgPoliciesParams struct {
OrgID string
Column2 []string
}
func (q *Queries) CountShippedOrgPolicies(ctx context.Context, arg CountShippedOrgPoliciesParams) (int64, error) {
row := q.db.QueryRow(ctx, countShippedOrgPolicies, arg.OrgID, arg.Column2)
var count int64
err := row.Scan(&count)
return count, err
}
const deleteAccessPolicy = `-- name: DeleteAccessPolicy :execrows
DELETE FROM access_policies
WHERE level = $1 AND org_id = $2 AND workspace_id = $3 AND object_kind = $4
AND object_id = $5 AND policy_id = $6
`
type DeleteAccessPolicyParams struct {
Level string
OrgID string
WorkspaceID string
ObjectKind string
ObjectID string
PolicyID string
}
func (q *Queries) DeleteAccessPolicy(ctx context.Context, arg DeleteAccessPolicyParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteAccessPolicy,
arg.Level,
arg.OrgID,
arg.WorkspaceID,
arg.ObjectKind,
arg.ObjectID,
arg.PolicyID,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const deleteAccessPolicyVersion = `-- name: DeleteAccessPolicyVersion :exec
DELETE FROM access_policy_versions v
WHERE v.level = $1 AND v.org_id = $2 AND v.workspace_id = $3
AND NOT EXISTS (SELECT 1 FROM access_policies p
WHERE p.level = v.level AND p.org_id = v.org_id AND p.workspace_id = v.workspace_id)
`
type DeleteAccessPolicyVersionParams struct {
Level string
OrgID string
WorkspaceID string
}
func (q *Queries) DeleteAccessPolicyVersion(ctx context.Context, arg DeleteAccessPolicyVersionParams) error {
_, err := q.db.Exec(ctx, deleteAccessPolicyVersion, arg.Level, arg.OrgID, arg.WorkspaceID)
return err
}
const deleteOrgPoliciesNotShipped = `-- name: DeleteOrgPoliciesNotShipped :exec
DELETE FROM access_policies
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]))
`
type DeleteOrgPoliciesNotShippedParams struct {
OrgID string
Column2 []string
}
func (q *Queries) DeleteOrgPoliciesNotShipped(ctx context.Context, arg DeleteOrgPoliciesNotShippedParams) error {
_, err := q.db.Exec(ctx, deleteOrgPoliciesNotShipped, arg.OrgID, arg.Column2)
return err
}
const getAccessPolicy = `-- name: GetAccessPolicy :one
SELECT text FROM access_policies
WHERE level = $1 AND org_id = $2 AND workspace_id = $3 AND object_kind = $4
AND object_id = $5 AND policy_id = $6
`
type GetAccessPolicyParams struct {
Level string
OrgID string
WorkspaceID string
ObjectKind string
ObjectID string
PolicyID string
}
func (q *Queries) GetAccessPolicy(ctx context.Context, arg GetAccessPolicyParams) (string, error) {
row := q.db.QueryRow(ctx, getAccessPolicy,
arg.Level,
arg.OrgID,
arg.WorkspaceID,
arg.ObjectKind,
arg.ObjectID,
arg.PolicyID,
)
var text string
err := row.Scan(&text)
return text, err
}
const insertAccessPolicyAudit = `-- name: InsertAccessPolicyAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id)
VALUES (now(), 'access_policies', $1, 'text', $2, $3, $4, $5, $6, $7)
`
type InsertAccessPolicyAuditParams struct {
RecordKey string
OldValue []byte
NewValue []byte
RecordVersion int64
Actor string
Source string
RequestID string
}
func (q *Queries) InsertAccessPolicyAudit(ctx context.Context, arg InsertAccessPolicyAuditParams) error {
_, err := q.db.Exec(ctx, insertAccessPolicyAudit,
arg.RecordKey,
arg.OldValue,
arg.NewValue,
arg.RecordVersion,
arg.Actor,
arg.Source,
arg.RequestID,
)
return err
}
const insertAccessResetAudit = `-- name: InsertAccessResetAudit :exec
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id)
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '')
`
func (q *Queries) InsertAccessResetAudit(ctx context.Context, recordKey string) error {
_, err := q.db.Exec(ctx, insertAccessResetAudit, recordKey)
return err
}
const listAccessPolicies = `-- name: ListAccessPolicies :many
SELECT level, org_id, workspace_id, object_kind, object_id, policy_id, text, updated_at
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)
// 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
}
defer rows.Close()
var items []AccessPolicy
for rows.Next() {
var i AccessPolicy
if err := rows.Scan(
&i.Level,
&i.OrgID,
&i.WorkspaceID,
&i.ObjectKind,
&i.ObjectID,
&i.PolicyID,
&i.Text,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listDenials = `-- name: ListDenials :many
SELECT id, org_id, principal, action, resource_kind, resource_id, level, policies, minute_at, count, created_at, updated_at
FROM access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2
`
type ListDenialsParams struct {
OrgID string
Limit int32
}
func (q *Queries) ListDenials(ctx context.Context, arg ListDenialsParams) ([]AccessDenial, error) {
rows, err := q.db.Query(ctx, listDenials, arg.OrgID, arg.Limit)
if err != nil {
return nil, err
}
defer rows.Close()
var items []AccessDenial
for rows.Next() {
var i AccessDenial
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.Principal,
&i.Action,
&i.ResourceKind,
&i.ResourceID,
&i.Level,
&i.Policies,
&i.MinuteAt,
&i.Count,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const recordDenial = `-- name: RecordDenial :exec
INSERT INTO access_denials (org_id, principal, action, resource_kind, resource_id, level, policies)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (org_id, principal, action, resource_kind, resource_id, level, policies, minute_at)
DO UPDATE SET count = access_denials.count + 1, updated_at = now()
`
type RecordDenialParams struct {
OrgID string
Principal string
Action string
ResourceKind string
ResourceID string
Level string
Policies []string
}
// One row per (principal, action, resource, level, policies, minute);
func (q *Queries) RecordDenial(ctx context.Context, arg RecordDenialParams) error {
_, err := q.db.Exec(ctx, recordDenial,
arg.OrgID,
arg.Principal,
arg.Action,
arg.ResourceKind,
arg.ResourceID,
arg.Level,
arg.Policies,
)
return err
}
const resetInstancePolicies = `-- name: ResetInstancePolicies :exec
DELETE FROM access_policies WHERE level = 'instance'
`
func (q *Queries) ResetInstancePolicies(ctx context.Context) error {
_, err := q.db.Exec(ctx, resetInstancePolicies)
return err
}
const upsertAccessPolicy = `-- name: UpsertAccessPolicy :exec
INSERT INTO access_policies (level, org_id, workspace_id, object_kind, object_id, policy_id, text)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (level, org_id, workspace_id, object_kind, object_id, policy_id)
DO UPDATE SET text = excluded.text, updated_at = now()
`
type UpsertAccessPolicyParams struct {
Level string
OrgID string
WorkspaceID string
ObjectKind string
ObjectID string
PolicyID string
Text string
}
func (q *Queries) UpsertAccessPolicy(ctx context.Context, arg UpsertAccessPolicyParams) error {
_, err := q.db.Exec(ctx, upsertAccessPolicy,
arg.Level,
arg.OrgID,
arg.WorkspaceID,
arg.ObjectKind,
arg.ObjectID,
arg.PolicyID,
arg.Text,
)
return err
}
const upsertAccessPolicyVersion = `-- name: UpsertAccessPolicyVersion :one
INSERT INTO access_policy_versions (level, org_id, workspace_id, version) VALUES ($1, $2, $3, 2)
ON CONFLICT (level, org_id, workspace_id)
DO UPDATE SET version = access_policy_versions.version + 1, updated_at = now()
RETURNING version
`
type UpsertAccessPolicyVersionParams struct {
Level string
OrgID string
WorkspaceID string
}
func (q *Queries) UpsertAccessPolicyVersion(ctx context.Context, arg UpsertAccessPolicyVersionParams) (int64, error) {
row := q.db.QueryRow(ctx, upsertAccessPolicyVersion, arg.Level, arg.OrgID, arg.WorkspaceID)
var version int64
err := row.Scan(&version)
return version, err
}