feat(access): policy, denial and principal persistence on the State Store contract (archie-core-qc8p)
This commit is contained in:
17 files changed
+2925
-198
No files matched your search
@@ -0,0 +1,234 @@
|
||||
// Access policies and denial records: the Postgres implementation of
|
||||
// internal/domain/access's PolicyStore, Resetter and DenialStore
|
||||
// (docs/prds/orgs-and-access.md). Policy changes are versioned per level
|
||||
// scope and audited as one sys_audit row per change naming who made it.
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"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/infrastructure/postgres/postgresdb"
|
||||
)
|
||||
|
||||
var (
|
||||
_ access.PolicyStore = (*Store)(nil)
|
||||
_ access.Resetter = (*Store)(nil)
|
||||
_ access.DenialStore = (*Store)(nil)
|
||||
)
|
||||
|
||||
// shippedOrgPolicyIDs lists an org's shipped role policy IDs: the seed's
|
||||
// conflict keys, and what an org reset keeps.
|
||||
func shippedOrgPolicyIDs() []string {
|
||||
ids := make([]string, 0, 4)
|
||||
for _, p := range access.ShippedOrgPolicies("") {
|
||||
ids = append(ids, p.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
// accessScopeOf is the scope tuple every access query carries.
|
||||
func accessScopeOf(p access.Policy) (string, string, string, string, string, string) {
|
||||
return string(p.Level), string(p.OrgID), string(p.WorkspaceID),
|
||||
string(p.ObjectKind), p.ObjectID, p.ID
|
||||
}
|
||||
|
||||
// auditKey is the sys_audit record_key for one policy: its scope and ID, so
|
||||
// an operator can trace one policy document's changes.
|
||||
func auditKey(p access.Policy) string {
|
||||
joined := strings.Join([]string{
|
||||
string(p.Level), string(p.OrgID), string(p.WorkspaceID),
|
||||
string(p.ObjectKind), p.ObjectID, p.ID,
|
||||
}, "/")
|
||||
return strings.Trim(joined, "/")
|
||||
}
|
||||
|
||||
// textJSON wraps a policy text as the JSON string sys_audit stores.
|
||||
func textJSON(text string) []byte {
|
||||
out, _ := json.Marshal(text)
|
||||
return out
|
||||
}
|
||||
|
||||
// ListPolicies returns every stored policy across levels.
|
||||
func (s *Store) ListPolicies(ctx context.Context) ([]access.Policy, error) {
|
||||
rows, err := s.queries().ListAccessPolicies(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]access.Policy, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, access.Policy{
|
||||
ID: r.PolicyID,
|
||||
Level: access.Level(r.Level),
|
||||
OrgID: org.OrgID(r.OrgID),
|
||||
WorkspaceID: org.WorkspaceID(r.WorkspaceID),
|
||||
ObjectKind: access.ResourceKind(r.ObjectKind),
|
||||
ObjectID: r.ObjectID,
|
||||
Text: r.Text,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PutPolicy stores or replaces one policy by its scope+ID key, bumps the
|
||||
// level's version, and writes the audit row -- in one transaction, so a
|
||||
// version that never moved cannot advertise a change that half-landed.
|
||||
func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) {
|
||||
if err := p.Validate(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
q := s.queries()
|
||||
level, orgID, wsID, kind, id, policyID := accessScopeOf(p)
|
||||
var version int64
|
||||
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||
q := q.WithTx(tx)
|
||||
if err := q.UpsertAccessPolicy(ctx, postgresdb.UpsertAccessPolicyParams{
|
||||
Level: level, OrgID: orgID, WorkspaceID: wsID,
|
||||
ObjectKind: kind, ObjectID: id,
|
||||
PolicyID: policyID, Text: p.Text,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
v, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{
|
||||
Level: level, OrgID: orgID, WorkspaceID: wsID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
version = v
|
||||
return q.InsertAccessPolicyAudit(ctx, postgresdb.InsertAccessPolicyAuditParams{
|
||||
RecordKey: auditKey(p),
|
||||
NewValue: textJSON(p.Text),
|
||||
RecordVersion: version,
|
||||
Actor: "archied",
|
||||
Source: "archied",
|
||||
RequestID: "",
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// DeletePolicy removes one stored policy; an unknown key is
|
||||
// ErrPolicyNotFound.
|
||||
func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error {
|
||||
if err := p.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
level, orgID, wsID, kind, id, policyID := accessScopeOf(p)
|
||||
deleted, err := s.queries().DeleteAccessPolicy(ctx, postgresdb.DeleteAccessPolicyParams{
|
||||
Level: level, OrgID: orgID, WorkspaceID: wsID,
|
||||
ObjectKind: kind, ObjectID: id, PolicyID: policyID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if deleted == 0 {
|
||||
return access.ErrPolicyNotFound
|
||||
}
|
||||
if _, err := s.queries().UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{
|
||||
Level: level, OrgID: orgID, WorkspaceID: wsID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
// A level-scope with no policies left keeps no version row.
|
||||
return s.queries().DeleteAccessPolicyVersion(ctx, postgresdb.DeleteAccessPolicyVersionParams{
|
||||
Level: level, OrgID: orgID, WorkspaceID: wsID,
|
||||
})
|
||||
}
|
||||
|
||||
// EnsureShippedOrgPolicies seeds an org's shipped role policies when it
|
||||
// carries none of them, so a restart never overwrites an edited role policy.
|
||||
func (s *Store) EnsureShippedOrgPolicies(ctx context.Context, orgID org.OrgID) error {
|
||||
shipped := access.ShippedOrgPolicies(orgID)
|
||||
count, err := s.queries().CountShippedOrgPolicies(ctx, postgresdb.CountShippedOrgPoliciesParams{
|
||||
OrgID: string(orgID), Column2: shippedOrgPolicyIDs(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
for _, p := range shipped {
|
||||
if _, err := s.PutPolicy(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetOrgPolicies restores the shipped role policies for one org and
|
||||
// removes its other org-level policies. It is the store-side half of
|
||||
// `archied access reset --org`, which runs on this host and records the
|
||||
// reset as an audit event.
|
||||
func (s *Store) ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error {
|
||||
if orgID == "" {
|
||||
return fmt.Errorf("%w: no org", access.ErrInvalidPolicy)
|
||||
}
|
||||
if err := s.queries().DeleteOrgPoliciesNotShipped(ctx, postgresdb.DeleteOrgPoliciesNotShippedParams{
|
||||
OrgID: string(orgID), Column2: shippedOrgPolicyIDs(),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, p := range access.ShippedOrgPolicies(orgID) {
|
||||
if _, err := s.PutPolicy(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetInstancePolicies removes every stored instance policy. The
|
||||
// cross-org forbid is engine-enforced and survives any reset.
|
||||
func (s *Store) ResetInstancePolicies(ctx context.Context) error {
|
||||
return s.queries().ResetInstancePolicies(ctx)
|
||||
}
|
||||
|
||||
// RecordDenial records one refusal. The store coalesces identical denials
|
||||
// within a minute into one row with a count, so a retry storm does not
|
||||
// flood the surface.
|
||||
func (s *Store) RecordDenial(ctx context.Context, d access.Denial) error {
|
||||
if d.Org == "" || d.Principal == "" {
|
||||
return fmt.Errorf("%w: org and principal are required", access.ErrInvalidPolicy)
|
||||
}
|
||||
return s.queries().RecordDenial(ctx, postgresdb.RecordDenialParams{
|
||||
OrgID: string(d.Org), Principal: string(d.Principal), Action: string(d.Action),
|
||||
ResourceKind: string(d.Kind), ResourceID: d.ResourceID, Level: string(d.Level),
|
||||
Policies: d.Policies,
|
||||
})
|
||||
}
|
||||
|
||||
// ListDenials returns an org's most recent denials, newest first.
|
||||
func (s *Store) ListDenials(ctx context.Context, orgID org.OrgID, limit int) ([]access.Denial, error) {
|
||||
rows, err := s.queries().ListDenials(ctx, postgresdb.ListDenialsParams{
|
||||
OrgID: string(orgID), Limit: int32(limit),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]access.Denial, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, access.Denial{
|
||||
Principal: identity.IdentityID(r.Principal),
|
||||
Org: org.OrgID(r.OrgID),
|
||||
Action: access.Action(r.Action),
|
||||
Kind: access.ResourceKind(r.ResourceKind),
|
||||
ResourceID: r.ResourceID,
|
||||
Level: access.Level(r.Level),
|
||||
Policies: r.Policies,
|
||||
At: r.CreatedAt,
|
||||
Count: r.Count,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/access"
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
)
|
||||
|
||||
func newStoreForAccess(t *testing.T) *Store {
|
||||
t.Helper()
|
||||
pool, _ := migrated(t)
|
||||
return New(pool)
|
||||
}
|
||||
|
||||
func TestAccessPolicyStore(t *testing.T) {
|
||||
s := newStoreForAccess(t)
|
||||
ctx := t.Context()
|
||||
|
||||
// PutPolicy stores one policy and reports a versioned level.
|
||||
p := access.Policy{
|
||||
ID: "test-policy", Level: access.LevelOrg, OrgID: "soc",
|
||||
Text: `permit(principal, action, resource) when { resource.kind == "binding" };`,
|
||||
}
|
||||
v1, err := s.PutPolicy(ctx, p)
|
||||
if err != nil {
|
||||
t.Fatalf("PutPolicy: %v", err)
|
||||
}
|
||||
if v1 != 2 { // the counter starts at 1; the first write bumps it
|
||||
t.Fatalf("PutPolicy version = %d, want 2", v1)
|
||||
}
|
||||
v2, err := s.PutPolicy(ctx, p)
|
||||
if err != nil {
|
||||
t.Fatalf("second PutPolicy: %v", err)
|
||||
}
|
||||
if v2 <= v1 {
|
||||
t.Fatalf("second PutPolicy version = %d, want > %d", v2, v1)
|
||||
}
|
||||
|
||||
// A malformed policy is refused by the store's own structural check.
|
||||
bad := access.Policy{ID: "bad", Level: access.LevelOrg, OrgID: "soc"}
|
||||
if _, err := s.PutPolicy(ctx, bad); !errors.Is(err, access.ErrInvalidPolicy) {
|
||||
t.Fatalf("PutPolicy(structurally invalid) = %v, want ErrInvalidPolicy", err)
|
||||
}
|
||||
|
||||
// ListPolicies round-trips the scope tuple.
|
||||
list, err := s.ListPolicies(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPolicies: %v", err)
|
||||
}
|
||||
if len(list) != 1 {
|
||||
t.Fatalf("ListPolicies = %d policies, want 1", len(list))
|
||||
}
|
||||
if list[0].ID != "test-policy" || list[0].OrgID != "soc" {
|
||||
t.Fatalf("ListPolicies[0] = %+v, want the stored policy", list[0])
|
||||
}
|
||||
|
||||
// DeletePolicy removes it and reports an unknown key as a sentinel.
|
||||
if err := s.DeletePolicy(ctx, p); err != nil {
|
||||
t.Fatalf("DeletePolicy: %v", err)
|
||||
}
|
||||
if err := s.DeletePolicy(ctx, p); !errors.Is(err, access.ErrPolicyNotFound) {
|
||||
t.Fatalf("second DeletePolicy = %v, want ErrPolicyNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureShippedOrgPolicies(t *testing.T) {
|
||||
s := newStoreForAccess(t)
|
||||
ctx := t.Context()
|
||||
|
||||
if err := s.EnsureShippedOrgPolicies(ctx, "soc"); err != nil {
|
||||
t.Fatalf("EnsureShippedOrgPolicies: %v", err)
|
||||
}
|
||||
list, err := s.ListPolicies(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPolicies: %v", err)
|
||||
}
|
||||
if len(list) != 4 {
|
||||
t.Fatalf("seeding produced %d policies, want the 4 shipped roles", len(list))
|
||||
}
|
||||
|
||||
// An edited role policy survives a re-seed: an org that already carries
|
||||
// the shipped IDs is left untouched.
|
||||
edited := list[0]
|
||||
edited.Text = `permit(principal, action, resource) when { resource.kind == "edited" };`
|
||||
if _, err := s.PutPolicy(ctx, edited); err != nil {
|
||||
t.Fatalf("PutPolicy(edit): %v", err)
|
||||
}
|
||||
if err := s.EnsureShippedOrgPolicies(ctx, "soc"); err != nil {
|
||||
t.Fatalf("re-seed: %v", err)
|
||||
}
|
||||
after, err := s.ListPolicies(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPolicies after re-seed: %v", err)
|
||||
}
|
||||
for _, p := range after {
|
||||
if p.ID == edited.ID && p.Text != edited.Text {
|
||||
t.Fatalf("re-seed overwrote an edited role policy %s", p.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetPolicies(t *testing.T) {
|
||||
s := newStoreForAccess(t)
|
||||
ctx := t.Context()
|
||||
|
||||
if err := s.ResetOrgPolicies(ctx, "soc"); err != nil {
|
||||
t.Fatalf("ResetOrgPolicies: %v", err)
|
||||
}
|
||||
// An operator's custom org policy on top of the shipped set.
|
||||
custom := access.Policy{
|
||||
ID: "org-lockdown", Level: access.LevelOrg, OrgID: "soc",
|
||||
Text: `forbid(principal, action, resource) when { resource.kind == "source" };`,
|
||||
}
|
||||
if _, err := s.PutPolicy(ctx, custom); err != nil {
|
||||
t.Fatalf("PutPolicy(custom): %v", err)
|
||||
}
|
||||
// A workspace policy must survive an org reset.
|
||||
ws := access.Policy{
|
||||
ID: "ws-rule", Level: access.LevelWorkspace, OrgID: "soc", WorkspaceID: "net",
|
||||
Text: `permit(principal, action, resource) when { resource.kind == "binding" };`,
|
||||
}
|
||||
if _, err := s.PutPolicy(ctx, ws); err != nil {
|
||||
t.Fatalf("PutPolicy(ws): %v", err)
|
||||
}
|
||||
// And an instance policy must survive an org reset but not an instance
|
||||
// reset.
|
||||
instance := access.Policy{
|
||||
ID: "instance-rule", Level: access.LevelInstance,
|
||||
Text: `forbid(principal, action, resource) when { resource.kind == "secret" };`,
|
||||
}
|
||||
if _, err := s.PutPolicy(ctx, instance); err != nil {
|
||||
t.Fatalf("PutPolicy(instance): %v", err)
|
||||
}
|
||||
|
||||
if err := s.ResetOrgPolicies(ctx, "soc"); err != nil {
|
||||
t.Fatalf("org reset: %v", err)
|
||||
}
|
||||
after, err := s.ListPolicies(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPolicies: %v", err)
|
||||
}
|
||||
orgPolicies := 0
|
||||
for _, p := range after {
|
||||
switch {
|
||||
case p.Level == access.LevelOrg && p.OrgID == "soc":
|
||||
orgPolicies++
|
||||
case p.Level == access.LevelOrg:
|
||||
t.Fatalf("org reset touched policy %s of another org", p.ID)
|
||||
}
|
||||
}
|
||||
if orgPolicies != 4 {
|
||||
t.Fatalf("org-level policies after reset = %d, want the 4 shipped roles", orgPolicies)
|
||||
}
|
||||
|
||||
if err := s.ResetInstancePolicies(ctx); err != nil {
|
||||
t.Fatalf("ResetInstancePolicies: %v", err)
|
||||
}
|
||||
after, err = s.ListPolicies(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("ListPolicies after instance reset: %v", err)
|
||||
}
|
||||
for _, p := range after {
|
||||
if p.Level == access.LevelInstance {
|
||||
t.Fatalf("instance reset left policy %s", p.ID)
|
||||
}
|
||||
}
|
||||
if len(after) == 0 {
|
||||
t.Fatal("instance reset removed non-instance policies")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDenialStore(t *testing.T) {
|
||||
s := newStoreForAccess(t)
|
||||
ctx := t.Context()
|
||||
|
||||
denial := access.Denial{
|
||||
Principal: identity.IdentityID("11111111-1111-5111-8111-111111111111"),
|
||||
Org: "soc", Action: access.ActionRead, Kind: access.KindBinding,
|
||||
ResourceID: "b1", Level: access.LevelOrg,
|
||||
Policies: []string{access.PolicyOrgRead, access.PolicyOrgEdit},
|
||||
}
|
||||
if err := s.RecordDenial(ctx, denial); err != nil {
|
||||
t.Fatalf("RecordDenial: %v", err)
|
||||
}
|
||||
if err := s.RecordDenial(ctx, denial); err != nil {
|
||||
t.Fatalf("repeat RecordDenial: %v", err)
|
||||
}
|
||||
// A different action is a separate row.
|
||||
other := denial
|
||||
other.Action = access.ActionDelete
|
||||
if err := s.RecordDenial(ctx, other); err != nil {
|
||||
t.Fatalf("RecordDenial(other action): %v", err)
|
||||
}
|
||||
|
||||
rows, err := s.ListDenials(ctx, "soc", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListDenials: %v", err)
|
||||
}
|
||||
if len(rows) != 2 {
|
||||
t.Fatalf("ListDenials = %d rows, want 2", len(rows))
|
||||
}
|
||||
if rows[0].Action != access.ActionDelete {
|
||||
t.Fatalf("newest denial = %v, want the delete denial", rows[0].Action)
|
||||
}
|
||||
for _, r := range rows {
|
||||
if r.Action == access.ActionRead {
|
||||
if r.Count != 2 {
|
||||
t.Fatalf("repeated denial count = %d, want 2 (coalesced within the minute)", r.Count)
|
||||
}
|
||||
if len(r.Policies) != 2 {
|
||||
t.Fatalf("denial policies = %v, want the deciding policies", r.Policies)
|
||||
}
|
||||
}
|
||||
}
|
||||
// Another org sees none of these.
|
||||
if rows, err := s.ListDenials(ctx, "other", 10); err != nil || len(rows) != 0 {
|
||||
t.Fatalf("ListDenials(other org) = %d rows %v, want none", len(rows), err)
|
||||
}
|
||||
// An org-less denial is refused at the producer.
|
||||
if err := s.RecordDenial(ctx, access.Denial{Action: access.ActionRead}); err == nil {
|
||||
t.Fatal("RecordDenial accepted a denial with no org or principal")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
-- Access policies and denial records: the stored half of the policy chain
|
||||
-- (docs/prds/orgs-and-access.md, "Storing and changing policies" and
|
||||
-- "Denials"). The shipped cross-org forbid is engine-enforced and has no row
|
||||
-- here; the shipped role policies are seeded per org as ordinary rows with
|
||||
-- the shipped IDs, so a reset overwrites them and an edit sticks until reset.
|
||||
--
|
||||
-- The scope tuple (level, org_id, workspace_id, object_kind, object_id) is
|
||||
-- the policy's place in the chain; policy_id names the document inside it.
|
||||
-- The version counter is per level-scope: PutPolicy bumps it and returns it,
|
||||
-- so the engine's next load can detect a change.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
CREATE TABLE access_policies (
|
||||
level text NOT NULL CHECK (level IN ('instance', 'org', 'workspace', 'object')),
|
||||
org_id text NOT NULL DEFAULT '',
|
||||
workspace_id text NOT NULL DEFAULT '',
|
||||
object_kind text NOT NULL DEFAULT '',
|
||||
object_id text NOT NULL DEFAULT '',
|
||||
policy_id text NOT NULL,
|
||||
text text NOT NULL,
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (level, org_id, workspace_id, object_kind, object_id, policy_id)
|
||||
);
|
||||
|
||||
CREATE TABLE access_policy_versions (
|
||||
level text NOT NULL CHECK (level IN ('instance', 'org', 'workspace', 'object')),
|
||||
org_id text NOT NULL DEFAULT '',
|
||||
workspace_id text NOT NULL DEFAULT '',
|
||||
version bigint NOT NULL DEFAULT 1,
|
||||
updated_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (level, org_id, workspace_id)
|
||||
);
|
||||
|
||||
-- One denial row per (principal, action, resource, level, policies, minute):
|
||||
-- a retry storm inside one minute is a count bump, not a row flood
|
||||
-- (docs/prds/orgs-and-access.md, "Audit retention").
|
||||
CREATE TABLE access_denials (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
org_id text NOT NULL,
|
||||
principal text NOT NULL,
|
||||
action text NOT NULL,
|
||||
resource_kind text NOT NULL,
|
||||
resource_id text NOT NULL,
|
||||
level text NOT NULL,
|
||||
policies text[] NOT NULL DEFAULT '{}',
|
||||
minute_at timestamptz NOT NULL DEFAULT date_trunc('minute', now()),
|
||||
count bigint NOT NULL DEFAULT 1,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE UNIQUE INDEX idx_access_denials_dedup ON access_denials
|
||||
(org_id, principal, action, resource_kind, resource_id, level, policies, minute_at);
|
||||
|
||||
-- +goose Down
|
||||
|
||||
DROP INDEX idx_access_denials_dedup;
|
||||
DROP TABLE access_denials;
|
||||
DROP TABLE access_policy_versions;
|
||||
DROP TABLE access_policies;
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"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/infrastructure/postgres/postgresdb"
|
||||
@@ -150,6 +151,43 @@ func (s *Store) AssignAgent(ctx context.Context, value org.AgentAssignment) erro
|
||||
})
|
||||
}
|
||||
|
||||
// EnsureAgentOrgMembership grants every agent identity the shipped developer
|
||||
// role in the org it serves. It runs at the State Store's boot, after the
|
||||
// default-org upgrade, so dispatch's chain has a principal to evaluate.
|
||||
// Idempotent: an existing membership (any role) is left alone.
|
||||
func (s *Store) EnsureAgentOrgMembership(ctx context.Context) error {
|
||||
return s.queries().EnsureAgentOrgMembership(ctx)
|
||||
}
|
||||
|
||||
// PrincipalFor assembles the access principal for one identity: the org it
|
||||
// serves and its memberships (internal/domain/access). An identity in no org
|
||||
// is the default org with no role, which the shipped role policies never
|
||||
// permit.
|
||||
func (s *Store) PrincipalFor(ctx context.Context, id identity.IdentityID) (access.Principal, error) {
|
||||
resolved, err := s.OrgForIdentity(ctx, id)
|
||||
if err != nil {
|
||||
return access.Principal{}, err
|
||||
}
|
||||
rows, err := s.queries().ListMembershipsByIdentity(ctx, string(id))
|
||||
if err != nil {
|
||||
return access.Principal{}, err
|
||||
}
|
||||
memberships := make([]org.Membership, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
var ws org.WorkspaceID
|
||||
if r.WorkspaceID.Valid {
|
||||
ws = org.WorkspaceID(r.WorkspaceID.String)
|
||||
}
|
||||
memberships = append(memberships, org.Membership{
|
||||
IdentityID: id,
|
||||
OrgID: org.OrgID(r.OrgID),
|
||||
WorkspaceID: ws,
|
||||
Role: org.Role(r.Role),
|
||||
})
|
||||
}
|
||||
return access.Principal{IdentityID: id, Org: resolved, Memberships: memberships}, nil
|
||||
}
|
||||
|
||||
// OrgForIdentity returns the org an identity serves: the assigned org for an
|
||||
// agent, the org of one of its memberships for a person, and the default org
|
||||
// for an identity in no org.
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: access.sql
|
||||
|
||||
package postgresdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// How many of an org's shipped role policies are stored. The seeding runs
|
||||
// only when the org carries none of them, so a restart never overwrites an
|
||||
// edited role policy (the shipped IDs are reserved for the shipped text).
|
||||
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
|
||||
}
|
||||
|
||||
// Tidy a level-scope with no policies left: its version row has no reader.
|
||||
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
|
||||
}
|
||||
|
||||
// The org reset's removal half: every org-level policy except the shipped
|
||||
// role set.
|
||||
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
|
||||
}
|
||||
|
||||
// One audit row per policy change: the policy's old and new text
|
||||
// (docs/prds/orgs-and-access.md, "Storing and changing policies").
|
||||
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 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
|
||||
`
|
||||
|
||||
// Access policies and denial records (internal/domain/access): the durable
|
||||
// half of the policy-chain contract. Versioning is per level-scope; every
|
||||
// policy change writes one sys_audit row naming who made it.
|
||||
func (q *Queries) ListAccessPolicies(ctx context.Context) ([]AccessPolicy, error) {
|
||||
rows, err := q.db.Query(ctx, listAccessPolicies)
|
||||
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);
|
||||
// a repeat inside the minute bumps the count (docs/prds/orgs-and-access.md,
|
||||
// "Audit retention").
|
||||
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
|
||||
}
|
||||
@@ -10,6 +10,40 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type AccessDenial struct {
|
||||
ID int64
|
||||
OrgID string
|
||||
Principal string
|
||||
Action string
|
||||
ResourceKind string
|
||||
ResourceID string
|
||||
Level string
|
||||
Policies []string
|
||||
MinuteAt time.Time
|
||||
Count int64
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type AccessPolicy struct {
|
||||
Level string
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
ObjectKind string
|
||||
ObjectID string
|
||||
PolicyID string
|
||||
Text string
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type AccessPolicyVersion struct {
|
||||
Level string
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
Version int64
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
type ApplyStatus struct {
|
||||
Process string
|
||||
Kind string
|
||||
|
||||
@@ -11,6 +11,22 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const ensureAgentOrgMembership = `-- name: EnsureAgentOrgMembership :exec
|
||||
INSERT INTO memberships (identity_id, org_id, role)
|
||||
SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a
|
||||
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO NOTHING
|
||||
`
|
||||
|
||||
// Every agent identity serves its org with the shipped developer role: an
|
||||
// agent is granted access the way a user is
|
||||
// (docs/prds/orgs-and-access.md, "Identity"). Idempotent, so an operator's
|
||||
// later role change survives a restart, and the shipped seed never
|
||||
// overwrites it.
|
||||
func (q *Queries) EnsureAgentOrgMembership(ctx context.Context) error {
|
||||
_, err := q.db.Exec(ctx, ensureAgentOrgMembership)
|
||||
return err
|
||||
}
|
||||
|
||||
const ensureMembership = `-- name: EnsureMembership :exec
|
||||
INSERT INTO memberships (identity_id, org_id, workspace_id, role)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
-- Access policies and denial records (internal/domain/access): the durable
|
||||
-- half of the policy-chain contract. Versioning is per level-scope; every
|
||||
-- policy change writes one sys_audit row naming who made it.
|
||||
|
||||
-- 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;
|
||||
|
||||
-- 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();
|
||||
|
||||
-- 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;
|
||||
|
||||
-- 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;
|
||||
|
||||
-- name: CountShippedOrgPolicies :one
|
||||
-- How many of an org's shipped role policies are stored. The seeding runs
|
||||
-- only when the org carries none of them, so a restart never overwrites an
|
||||
-- edited role policy (the shipped IDs are reserved for the shipped text).
|
||||
SELECT count(*) FROM access_policies
|
||||
WHERE level = 'org' AND org_id = $1 AND policy_id = ANY($2::text[]);
|
||||
|
||||
-- name: DeleteOrgPoliciesNotShipped :exec
|
||||
-- The org reset's removal half: every org-level policy except the shipped
|
||||
-- role set.
|
||||
DELETE FROM access_policies
|
||||
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]));
|
||||
|
||||
-- name: ResetInstancePolicies :exec
|
||||
DELETE FROM access_policies WHERE level = 'instance';
|
||||
|
||||
-- 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;
|
||||
|
||||
-- name: DeleteAccessPolicyVersion :exec
|
||||
-- Tidy a level-scope with no policies left: its version row has no reader.
|
||||
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);
|
||||
|
||||
-- name: InsertAccessPolicyAudit :exec
|
||||
-- One audit row per policy change: the policy's old and new text
|
||||
-- (docs/prds/orgs-and-access.md, "Storing and changing policies").
|
||||
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);
|
||||
|
||||
-- name: RecordDenial :exec
|
||||
-- One row per (principal, action, resource, level, policies, minute);
|
||||
-- a repeat inside the minute bumps the count (docs/prds/orgs-and-access.md,
|
||||
-- "Audit retention").
|
||||
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();
|
||||
|
||||
-- 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;
|
||||
@@ -40,4 +40,13 @@ SELECT COALESCE(
|
||||
(SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
|
||||
ORDER BY m.created_at, m.org_id, m.workspace_id NULLS LAST LIMIT 1),
|
||||
'default'
|
||||
)::text AS org_id;
|
||||
)::text AS org_id;
|
||||
-- name: EnsureAgentOrgMembership :exec
|
||||
-- Every agent identity serves its org with the shipped developer role: an
|
||||
-- agent is granted access the way a user is
|
||||
-- (docs/prds/orgs-and-access.md, "Identity"). Idempotent, so an operator's
|
||||
-- later role change survives a restart, and the shipped seed never
|
||||
-- overwrites it.
|
||||
INSERT INTO memberships (identity_id, org_id, role)
|
||||
SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a
|
||||
ON CONFLICT (identity_id, org_id, COALESCE(workspace_id, '')) DO NOTHING;
|
||||
Reference in new issue
Block a user