feat(state): audit access resets, org and EDA writes
This commit is contained in:
1 parent
0cccef6bb7
commit
99b1f8a2d9
12 files changed
+758
-167
No files matched your search
@@ -133,9 +133,10 @@ func (q *Queries) DeleteAccessPolicyVersion(ctx context.Context, arg DeleteAcces
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteOrgPoliciesNotShipped = `-- name: DeleteOrgPoliciesNotShipped :exec
|
||||
const deleteOrgPoliciesNotShipped = `-- name: DeleteOrgPoliciesNotShipped :many
|
||||
DELETE FROM access_policies
|
||||
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]))
|
||||
RETURNING level, org_id, workspace_id, object_kind, object_id, policy_id, text
|
||||
`
|
||||
|
||||
type DeleteOrgPoliciesNotShippedParams struct {
|
||||
@@ -143,9 +144,42 @@ type DeleteOrgPoliciesNotShippedParams struct {
|
||||
Column2 []string
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteOrgPoliciesNotShipped(ctx context.Context, arg DeleteOrgPoliciesNotShippedParams) error {
|
||||
_, err := q.db.Exec(ctx, deleteOrgPoliciesNotShipped, arg.OrgID, arg.Column2)
|
||||
return err
|
||||
type DeleteOrgPoliciesNotShippedRow struct {
|
||||
Level string
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
ObjectKind string
|
||||
ObjectID string
|
||||
PolicyID string
|
||||
Text string
|
||||
}
|
||||
|
||||
func (q *Queries) DeleteOrgPoliciesNotShipped(ctx context.Context, arg DeleteOrgPoliciesNotShippedParams) ([]DeleteOrgPoliciesNotShippedRow, error) {
|
||||
rows, err := q.db.Query(ctx, deleteOrgPoliciesNotShipped, arg.OrgID, arg.Column2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []DeleteOrgPoliciesNotShippedRow
|
||||
for rows.Next() {
|
||||
var i DeleteOrgPoliciesNotShippedRow
|
||||
if err := rows.Scan(
|
||||
&i.Level,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.ObjectKind,
|
||||
&i.ObjectID,
|
||||
&i.PolicyID,
|
||||
&i.Text,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const insertAccessPolicyAudit = `-- name: InsertAccessPolicyAudit :exec
|
||||
@@ -180,7 +214,7 @@ func (q *Queries) InsertAccessPolicyAudit(ctx context.Context, arg InsertAccessP
|
||||
|
||||
const insertAccessResetAudit = `-- name: InsertAccessResetAudit :exec
|
||||
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id, org_id)
|
||||
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '', $2)
|
||||
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archie-state-store access reset', 'archie-state-store', '', $2)
|
||||
`
|
||||
|
||||
type InsertAccessResetAuditParams struct {
|
||||
@@ -326,13 +360,47 @@ func (q *Queries) RecordDenial(ctx context.Context, arg RecordDenialParams) erro
|
||||
return err
|
||||
}
|
||||
|
||||
const resetInstancePolicies = `-- name: ResetInstancePolicies :exec
|
||||
const resetInstancePolicies = `-- name: ResetInstancePolicies :many
|
||||
DELETE FROM access_policies WHERE level = 'instance'
|
||||
RETURNING level, org_id, workspace_id, object_kind, object_id, policy_id, text
|
||||
`
|
||||
|
||||
func (q *Queries) ResetInstancePolicies(ctx context.Context) error {
|
||||
_, err := q.db.Exec(ctx, resetInstancePolicies)
|
||||
return err
|
||||
type ResetInstancePoliciesRow struct {
|
||||
Level string
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
ObjectKind string
|
||||
ObjectID string
|
||||
PolicyID string
|
||||
Text string
|
||||
}
|
||||
|
||||
func (q *Queries) ResetInstancePolicies(ctx context.Context) ([]ResetInstancePoliciesRow, error) {
|
||||
rows, err := q.db.Query(ctx, resetInstancePolicies)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []ResetInstancePoliciesRow
|
||||
for rows.Next() {
|
||||
var i ResetInstancePoliciesRow
|
||||
if err := rows.Scan(
|
||||
&i.Level,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.ObjectKind,
|
||||
&i.ObjectID,
|
||||
&i.PolicyID,
|
||||
&i.Text,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const upsertAccessPolicy = `-- name: UpsertAccessPolicy :exec
|
||||
|
||||
@@ -150,3 +150,20 @@ func (q *Queries) InsertResourceAudit(ctx context.Context, arg InsertResourceAud
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const lockAuditRecord = `-- name: LockAuditRecord :exec
|
||||
SELECT pg_advisory_xact_lock(hashtextextended($1::text || ':' || $2::text, 0))
|
||||
`
|
||||
|
||||
type LockAuditRecordParams struct {
|
||||
TableName string
|
||||
RecordKey string
|
||||
}
|
||||
|
||||
// Serialises a read-modify-write on one audited record so the old side the
|
||||
// audit row reports cannot change between the read and the write. Row locks
|
||||
// cannot hold a key that does not exist yet; this can.
|
||||
func (q *Queries) LockAuditRecord(ctx context.Context, arg LockAuditRecordParams) error {
|
||||
_, err := q.db.Exec(ctx, lockAuditRecord, arg.TableName, arg.RecordKey)
|
||||
return err
|
||||
}
|
||||
@@ -11,6 +11,17 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const agentAssignmentOrg = `-- name: AgentAssignmentOrg :one
|
||||
SELECT org_id FROM org_agents WHERE identity_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) AgentAssignmentOrg(ctx context.Context, identityID string) (string, error) {
|
||||
row := q.db.QueryRow(ctx, agentAssignmentOrg, identityID)
|
||||
var org_id string
|
||||
err := row.Scan(&org_id)
|
||||
return org_id, err
|
||||
}
|
||||
|
||||
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
|
||||
@@ -297,7 +308,28 @@ func (q *Queries) ListWorkspaces(ctx context.Context, orgID string) ([]Workspace
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const removeMembership = `-- name: RemoveMembership :exec
|
||||
const membershipRole = `-- name: MembershipRole :one
|
||||
SELECT role FROM memberships
|
||||
WHERE identity_id = $1 AND org_id = $2
|
||||
AND COALESCE(workspace_id, '') = $3::text
|
||||
`
|
||||
|
||||
type MembershipRoleParams struct {
|
||||
IdentityID string
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
}
|
||||
|
||||
// The current role of the membership EnsureMembership/RemoveMembership targets,
|
||||
// read in the same transaction so the audit row carries both sides.
|
||||
func (q *Queries) MembershipRole(ctx context.Context, arg MembershipRoleParams) (string, error) {
|
||||
row := q.db.QueryRow(ctx, membershipRole, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
||||
var role string
|
||||
err := row.Scan(&role)
|
||||
return role, err
|
||||
}
|
||||
|
||||
const removeMembership = `-- name: RemoveMembership :execrows
|
||||
DELETE FROM memberships
|
||||
WHERE identity_id = $1 AND org_id = $2 AND COALESCE(workspace_id, '') = $3::text
|
||||
`
|
||||
@@ -310,9 +342,12 @@ type RemoveMembershipParams struct {
|
||||
|
||||
// The workspace is matched through the same COALESCE the unique index uses, so
|
||||
// an org-wide membership (NULL) and the empty string are the same target.
|
||||
func (q *Queries) RemoveMembership(ctx context.Context, arg RemoveMembershipParams) error {
|
||||
_, err := q.db.Exec(ctx, removeMembership, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
||||
return err
|
||||
func (q *Queries) RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, removeMembership, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const resolveIdentityOrg = `-- name: ResolveIdentityOrg :one
|
||||
|
||||
Reference in new issue
Block a user