diff --git a/internal/app/statestore/access_reset.go b/internal/app/statestore/access_reset.go index 1bdf08954..80c3f3b23 100644 --- a/internal/app/statestore/access_reset.go +++ b/internal/app/statestore/access_reset.go @@ -19,6 +19,7 @@ import ( "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) @@ -103,8 +104,10 @@ func resetTarget(orgID string, instance bool) string { // recovery. func recordResetAudit(ctx context.Context, pool *pgxpool.Pool, orgID string, instance bool) error { key := "reset/instance" + auditOrg := storecontract.DefaultOrgID if !instance { key = fmt.Sprintf("reset/org/%s", orgID) + auditOrg = orgID } - return postgres.New(pool).RecordResetAudit(ctx, key) + return postgres.New(pool).RecordResetAudit(ctx, key, auditOrg) } diff --git a/internal/domain/org/org.go b/internal/domain/org/org.go index b4e3b38f6..603553546 100644 --- a/internal/domain/org/org.go +++ b/internal/domain/org/org.go @@ -61,11 +61,15 @@ type Org struct { Name string `json:"name"` } -// Validate requires an org to have an ID and a name. +// Validate requires an org to have an ID and a name. A slash is refused +// because audit record keys encode the org as a path segment. func (o Org) Validate() error { if o.ID == "" || strings.TrimSpace(o.Name) == "" { return fmt.Errorf("%w: ID and name are required", ErrInvalidOrg) } + if strings.Contains(string(o.ID), "/") { + return fmt.Errorf("%w: ID must not contain a slash", ErrInvalidOrg) + } return nil } @@ -78,7 +82,9 @@ type Workspace struct { Environment string `json:"environment,omitempty"` } -// Validate requires a workspace to belong to an org and to have a name. +// Validate requires a workspace to belong to an org and to have a name. A +// slash is refused for the same reason an org refuses one: audit record keys +// encode scope as path segments. func (w Workspace) Validate() error { if w.OrgID == "" { return fmt.Errorf("%w: no org", ErrInvalidWorkspace) @@ -86,6 +92,9 @@ func (w Workspace) Validate() error { if strings.TrimSpace(w.Name) == "" { return fmt.Errorf("%w: no name", ErrInvalidWorkspace) } + if strings.Contains(string(w.ID), "/") || strings.Contains(string(w.OrgID), "/") { + return fmt.Errorf("%w: IDs must not contain a slash", ErrInvalidWorkspace) + } return nil } diff --git a/internal/infrastructure/postgres/access.go b/internal/infrastructure/postgres/access.go index b58970f10..823bed920 100644 --- a/internal/infrastructure/postgres/access.go +++ b/internal/infrastructure/postgres/access.go @@ -9,6 +9,7 @@ package postgres import ( "context" "encoding/json" + "errors" "fmt" "strings" @@ -17,6 +18,7 @@ import ( "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/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb" ) @@ -124,6 +126,17 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { level, orgID, wsID, kind, id, policyID := accessScopeOf(p) var version int64 _, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error { + previous, err := q.AccessPolicyText(ctx, postgresdb.AccessPolicyTextParams{ + Level: level, OrgID: orgID, WorkspaceID: wsID, + ObjectKind: kind, ObjectID: id, PolicyID: policyID, + }) + var oldValue []byte + switch { + case err == nil: + oldValue = textJSON(previous) + case !errors.Is(err, pgx.ErrNoRows): + return err + } if err := q.UpsertAccessPolicy(ctx, postgresdb.UpsertAccessPolicyParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, ObjectKind: kind, ObjectID: id, @@ -140,11 +153,13 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { version = v return q.InsertAccessPolicyAudit(ctx, postgresdb.InsertAccessPolicyAuditParams{ RecordKey: auditKey(p), + OldValue: oldValue, NewValue: textJSON(p.Text), RecordVersion: version, Actor: access.ActorFromContext(ctx), Source: access.SourceFromContext(ctx), RequestID: "", + OrgID: auditOrg(p), }) }) if err != nil { @@ -153,6 +168,15 @@ func (s *Store) PutPolicy(ctx context.Context, p access.Policy) (int64, error) { return version, nil } +// auditOrg is the org a policy change belongs to. Instance-level policies +// govern the whole deployment, so they belong to the system org. +func auditOrg(p access.Policy) string { + if p.OrgID == "" { + return storecontract.DefaultOrgID + } + return string(p.OrgID) +} + // DeletePolicy removes one stored policy; an unknown key is // ErrPolicyNotFound. func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error { @@ -161,18 +185,29 @@ func (s *Store) DeletePolicy(ctx context.Context, p access.Policy) error { } level, orgID, wsID, kind, id, policyID := accessScopeOf(p) _, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error { - deleted, err := q.DeleteAccessPolicy(ctx, postgresdb.DeleteAccessPolicyParams{ + oldText, err := q.DeleteAccessPolicy(ctx, postgresdb.DeleteAccessPolicyParams{ Level: level, OrgID: orgID, WorkspaceID: wsID, ObjectKind: kind, ObjectID: id, PolicyID: policyID, }) + if errors.Is(err, pgx.ErrNoRows) { + return access.ErrPolicyNotFound + } + if err != nil { + return err + } + version, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{ + Level: level, OrgID: orgID, WorkspaceID: wsID, + }) if err != nil { return err } - if deleted == 0 { - return access.ErrPolicyNotFound - } - if _, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{ - Level: level, OrgID: orgID, WorkspaceID: wsID, + if err := q.InsertAccessPolicyAudit(ctx, postgresdb.InsertAccessPolicyAuditParams{ + RecordKey: auditKey(p), + OldValue: textJSON(oldText), + RecordVersion: version, + Actor: access.ActorFromContext(ctx), + Source: access.SourceFromContext(ctx), + OrgID: auditOrg(p), }); err != nil { return err } @@ -236,8 +271,8 @@ func (s *Store) ResetInstancePolicies(ctx context.Context) error { } // RecordResetAudit records the one audit row a reset is written as. -func (s *Store) RecordResetAudit(ctx context.Context, key string) error { - return s.queries().InsertAccessResetAudit(ctx, key) +func (s *Store) RecordResetAudit(ctx context.Context, key, orgID string) error { + return s.queries().InsertAccessResetAudit(ctx, postgresdb.InsertAccessResetAuditParams{RecordKey: key, OrgID: orgID}) } // RecordDenial records one refusal The store coalesces identical denials diff --git a/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql b/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql new file mode 100644 index 000000000..fc3331956 --- /dev/null +++ b/internal/infrastructure/postgres/migrations/0062_audit_read_model.sql @@ -0,0 +1,49 @@ +-- sys_audit is the field-level trail for settings resources and access +-- policies. It had no org column: the org lived inside record_key, in a shape +-- that differed per writer, so no caller could read one org's trail without +-- knowing every writer's key grammar. org_id makes the org a first-class +-- column and the indexes make an org-scoped, time-ordered page cheap. + +-- +goose Up +ALTER TABLE sys_audit ADD COLUMN org_id text NOT NULL DEFAULT 'org-sys'; +ALTER TABLE sys_audit ADD CONSTRAINT sys_audit_org_nonempty CHECK (org_id <> ''); + +-- Backfill from the two key grammars written before this migration. +-- resources: bare kind for org-sys, / otherwise. +-- access_policies: /////; +-- instance-level policies carry no org; reset rows are reset/instance or +-- reset/org/. +UPDATE sys_audit SET org_id = + CASE table_name + WHEN 'resources' THEN + CASE + WHEN position('/' IN record_key) > 0 THEN split_part(record_key, '/', 1) + ELSE 'org-sys' + END + WHEN 'access_policies' THEN + CASE + WHEN record_key LIKE 'reset/org/%' THEN split_part(record_key, '/', 3) + WHEN record_key LIKE 'reset/%' THEN 'org-sys' + WHEN split_part(record_key, '/', 1) = 'instance' THEN 'org-sys' + ELSE COALESCE(NULLIF(split_part(record_key, '/', 2), ''), 'org-sys') + END + ELSE 'org-sys' + END; + +CREATE INDEX idx_sys_audit_org_at ON sys_audit (org_id, at DESC, id DESC); +CREATE INDEX idx_sys_audit_org_record ON sys_audit (org_id, table_name, record_key, at DESC, id DESC); + +CREATE INDEX idx_events_org_at ON events (org_id, at DESC, id DESC); +CREATE INDEX idx_identity_events_at ON identity_events (at DESC, id DESC); +CREATE INDEX idx_access_denials_org_minute ON access_denials (org_id, minute_at DESC, id DESC); +CREATE INDEX idx_access_denials_minute ON access_denials (minute_at, id); + +-- +goose Down +DROP INDEX idx_access_denials_minute; +DROP INDEX idx_access_denials_org_minute; +DROP INDEX idx_identity_events_at; +DROP INDEX idx_events_org_at; +DROP INDEX idx_sys_audit_org_record; +DROP INDEX idx_sys_audit_org_at; +ALTER TABLE sys_audit DROP CONSTRAINT sys_audit_org_nonempty; +ALTER TABLE sys_audit DROP COLUMN org_id; diff --git a/internal/infrastructure/postgres/postgresdb/access.sql.go b/internal/infrastructure/postgres/postgresdb/access.sql.go index b21aa5f01..8799ced0b 100644 --- a/internal/infrastructure/postgres/postgresdb/access.sql.go +++ b/internal/infrastructure/postgres/postgresdb/access.sql.go @@ -22,6 +22,36 @@ func (q *Queries) AccessPolicyRevision(ctx context.Context) (int64, error) { return version, err } +const accessPolicyText = `-- name: AccessPolicyText :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 +FOR UPDATE +` + +type AccessPolicyTextParams struct { + Level string + OrgID string + WorkspaceID string + ObjectKind string + ObjectID string + PolicyID string +} + +func (q *Queries) AccessPolicyText(ctx context.Context, arg AccessPolicyTextParams) (string, error) { + row := q.db.QueryRow(ctx, accessPolicyText, + arg.Level, + arg.OrgID, + arg.WorkspaceID, + arg.ObjectKind, + arg.ObjectID, + arg.PolicyID, + ) + var text string + err := row.Scan(&text) + return text, err +} + const bumpAccessPolicyRevision = `-- name: BumpAccessPolicyRevision :one INSERT INTO access_policy_revision (only_row, version) VALUES (true, 1) ON CONFLICT (only_row) @@ -55,10 +85,11 @@ func (q *Queries) CountShippedOrgPolicies(ctx context.Context, arg CountShippedO return count, err } -const deleteAccessPolicy = `-- name: DeleteAccessPolicy :execrows +const deleteAccessPolicy = `-- name: DeleteAccessPolicy :one 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 +RETURNING text ` type DeleteAccessPolicyParams struct { @@ -70,8 +101,8 @@ type DeleteAccessPolicyParams struct { PolicyID string } -func (q *Queries) DeleteAccessPolicy(ctx context.Context, arg DeleteAccessPolicyParams) (int64, error) { - result, err := q.db.Exec(ctx, deleteAccessPolicy, +func (q *Queries) DeleteAccessPolicy(ctx context.Context, arg DeleteAccessPolicyParams) (string, error) { + row := q.db.QueryRow(ctx, deleteAccessPolicy, arg.Level, arg.OrgID, arg.WorkspaceID, @@ -79,10 +110,9 @@ func (q *Queries) DeleteAccessPolicy(ctx context.Context, arg DeleteAccessPolicy arg.ObjectID, arg.PolicyID, ) - if err != nil { - return 0, err - } - return result.RowsAffected(), nil + var text string + err := row.Scan(&text) + return text, err } const deleteAccessPolicyVersion = `-- name: DeleteAccessPolicyVersion :exec @@ -119,8 +149,8 @@ func (q *Queries) DeleteOrgPoliciesNotShipped(ctx context.Context, arg DeleteOrg } 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) +INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id) +VALUES (now(), 'access_policies', $1, 'text', $2, $3, $4, $5, $6, $7, $8) ` type InsertAccessPolicyAuditParams struct { @@ -131,6 +161,7 @@ type InsertAccessPolicyAuditParams struct { Actor string Source string RequestID string + OrgID string } func (q *Queries) InsertAccessPolicyAudit(ctx context.Context, arg InsertAccessPolicyAuditParams) error { @@ -142,17 +173,23 @@ func (q *Queries) InsertAccessPolicyAudit(ctx context.Context, arg InsertAccessP arg.Actor, arg.Source, arg.RequestID, + arg.OrgID, ) 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', '') +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) ` -func (q *Queries) InsertAccessResetAudit(ctx context.Context, recordKey string) error { - _, err := q.db.Exec(ctx, insertAccessResetAudit, recordKey) +type InsertAccessResetAuditParams struct { + RecordKey string + OrgID string +} + +func (q *Queries) InsertAccessResetAudit(ctx context.Context, arg InsertAccessResetAuditParams) error { + _, err := q.db.Exec(ctx, insertAccessResetAudit, arg.RecordKey, arg.OrgID) return err } diff --git a/internal/infrastructure/postgres/postgresdb/audit.sql.go b/internal/infrastructure/postgres/postgresdb/audit.sql.go index 88d919337..25baeecf3 100644 --- a/internal/infrastructure/postgres/postgresdb/audit.sql.go +++ b/internal/infrastructure/postgres/postgresdb/audit.sql.go @@ -24,15 +24,29 @@ type AuditForRecordsParams struct { EntryLimit int64 } -func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]SysAudit, error) { +type AuditForRecordsRow struct { + ID int64 + At time.Time + TableName string + RecordKey string + Field string + OldValue []byte + NewValue []byte + RecordVersion int64 + Actor string + Source string + RequestID string +} + +func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]AuditForRecordsRow, error) { rows, err := q.db.Query(ctx, auditForRecords, arg.TableName, arg.RecordKeys, arg.EntryLimit) if err != nil { return nil, err } defer rows.Close() - var items []SysAudit + var items []AuditForRecordsRow for rows.Next() { - var i SysAudit + var i AuditForRecordsRow if err := rows.Scan( &i.ID, &i.At, @@ -57,9 +71,10 @@ func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams } const insertResourceAudit = `-- name: InsertResourceAudit :exec -INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id) +INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id) SELECT $1::timestamptz, 'resources', $2::text, d.field, d.old_value, d.new_value, - $3::bigint, $4::text, $5::text, $6::text + $3::bigint, $4::text, $5::text, $6::text, + $7::text FROM audit_json_diff( (SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h WHERE h.org_id = $7::text AND h.kind = $8::text AND h.version = $9::bigint), diff --git a/internal/infrastructure/postgres/postgresdb/models.go b/internal/infrastructure/postgres/postgresdb/models.go index d98da94ae..123d3bbf6 100644 --- a/internal/infrastructure/postgres/postgresdb/models.go +++ b/internal/infrastructure/postgres/postgresdb/models.go @@ -495,6 +495,7 @@ type SysAudit struct { Actor string Source string RequestID string + OrgID string } type Task struct { diff --git a/internal/infrastructure/postgres/queries/access.sql b/internal/infrastructure/postgres/queries/access.sql index 35566d626..f82d1381d 100644 --- a/internal/infrastructure/postgres/queries/access.sql +++ b/internal/infrastructure/postgres/queries/access.sql @@ -16,10 +16,17 @@ 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: DeleteAccessPolicy :execrows +-- name: DeleteAccessPolicy :one 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; + AND object_id = $5 AND policy_id = $6 +RETURNING text; + +-- name: AccessPolicyText :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 +FOR UPDATE; -- name: CountShippedOrgPolicies :one SELECT count(*) FROM access_policies @@ -45,8 +52,8 @@ WHERE v.level = $1 AND v.org_id = $2 AND v.workspace_id = $3 WHERE p.level = v.level AND p.org_id = v.org_id AND p.workspace_id = v.workspace_id); -- 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); +INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id) +VALUES (now(), 'access_policies', $1, 'text', $2, $3, $4, $5, $6, $7, $8); -- name: RecordDenial :exec -- One row per (principal, action, resource, level, policies, minute); @@ -60,8 +67,8 @@ SELECT id, org_id, principal, action, resource_kind, resource_id, level, policie FROM access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2; -- 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', ''); +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); -- name: BumpAccessPolicyRevision :one -- The instance-wide version. It moves inside the transaction of every policy diff --git a/internal/infrastructure/postgres/queries/audit.sql b/internal/infrastructure/postgres/queries/audit.sql index 00f608b40..9707d7e6c 100644 --- a/internal/infrastructure/postgres/queries/audit.sql +++ b/internal/infrastructure/postgres/queries/audit.sql @@ -1,7 +1,8 @@ -- name: InsertResourceAudit :exec -INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id) +INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id) SELECT sqlc.arg(at)::timestamptz, 'resources', sqlc.arg(record_key)::text, d.field, d.old_value, d.new_value, - sqlc.arg(version)::bigint, sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text + sqlc.arg(version)::bigint, sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text, + sqlc.arg(org_id)::text FROM audit_json_diff( (SELECT convert_from(h.value, 'UTF8')::jsonb FROM resource_history h WHERE h.org_id = sqlc.arg(org_id)::text AND h.kind = sqlc.arg(kind)::text AND h.version = sqlc.arg(previous_version)::bigint),