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,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
|
||||
}
|
||||
Reference in new issue
Block a user