fix(state): harden the audit feed against audit findings

This commit is contained in:
sam committed 2026-10-25 21:23:52 +11:00
1 parent dd9b378431
commit 691002eb9b
13 files changed
+411 -163

No files matched your search

+21 -2
View File
@@ -155,7 +155,26 @@ type Page struct {
type Store interface {
// ListAudit returns one page of the org's feed, newest first.
ListAudit(ctx context.Context, filter Filter, cursor Cursor, limit int) (Page, error)
// AuditHead returns a cheap token that changes when any trail grows. It
// carries no rows and no org data.
// AuditHead returns a cheap token that changes when any trail the caller
// can read grows. It carries no rows and no org data.
AuditHead(ctx context.Context) (string, error)
}
// instanceAdminKey carries the authenticated principal's instance-wide
// authority. It is a server-side fact derived from the principal, never from
// the requested org: being confined to the system org does not by itself make
// a viewer an instance administrator.
type instanceAdminKey struct{}
// WithInstanceAdmin marks whether the request's principal administers the
// whole instance.
func WithInstanceAdmin(ctx context.Context, admin bool) context.Context {
return context.WithValue(ctx, instanceAdminKey{}, admin)
}
// InstanceAdmin reports whether the request's principal administers the
// instance. It is false for a service call acting for no principal.
func InstanceAdmin(ctx context.Context) bool {
admin, _ := ctx.Value(instanceAdminKey{}).(bool)
return admin
}
+1 -1
View File
@@ -101,7 +101,7 @@ func writeAccount(ctx context.Context, tx pgx.Tx, r identity.AccountRegistration
if p, ok := access.PrincipalFromContext(ctx); ok {
actor = p.IdentityID
}
err = insertIdentityEvent(ctx, postgresdb.New(tx), id, identity.Event{Type: "create", To: identity.LifecycleActive, DisplayName: r.DisplayName}, identity.Audit{ActorID: actor, Source: "accounts", RequestID: "account:" + rand.Text()}, now)
err = insertIdentityEvent(ctx, postgresdb.New(tx), orgID, id, identity.Event{Type: "create", To: identity.LifecycleActive, DisplayName: r.DisplayName}, identity.Audit{ActorID: actor, Source: "accounts", RequestID: "account:" + rand.Text()}, now)
if err != nil {
return identity.Identity{}, err
}
+30 -19
View File
@@ -42,22 +42,23 @@ func (s *Store) ListAudit(ctx context.Context, filter audit.Filter, cursor audit
orgID = scoped
}
rows, err := s.queries().ListAudit(ctx, postgresdb.ListAuditParams{
PageLimit: int64(limit + 1),
OrgID: string(orgID),
Categories: filter.Categories,
FromAt: auditTime(filter.From),
ToAt: auditTime(filter.To),
Actor: auditText(filter.Actor),
Actions: filter.Actions,
Kinds: filter.Kinds,
TargetKinds: filter.TargetKinds,
TargetIds: filter.TargetIDs,
Severities: filter.Severities,
Outcomes: filter.Outcomes,
Q: auditText(filter.Query),
CursorAt: auditTime(cursor.At),
CursorRank: int32(cursor.Rank),
CursorID: cursor.ID,
PageLimit: int64(limit + 1),
OrgID: string(orgID),
InstanceAdmin: audit.InstanceAdmin(ctx),
Categories: filter.Categories,
FromAt: auditTime(filter.From),
ToAt: auditTime(filter.To),
Actor: auditText(filter.Actor),
Actions: filter.Actions,
Kinds: filter.Kinds,
TargetKinds: filter.TargetKinds,
TargetIds: filter.TargetIDs,
Severities: filter.Severities,
Outcomes: filter.Outcomes,
Q: auditText(filter.Query),
CursorAt: auditTime(cursor.At),
CursorRank: int32(cursor.Rank),
CursorID: cursor.ID,
})
if err != nil {
return audit.Page{}, fmt.Errorf("list audit: %w", err)
@@ -80,13 +81,23 @@ func (s *Store) ListAudit(ctx context.Context, filter audit.Filter, cursor audit
return page, nil
}
// AuditHead returns the cheap change token: the newest id of each trail.
// AuditHead returns the cheap change token: the newest id of each trail the
// caller can read, and the denial count sum because a coalesced denial bumps
// its count in place without drawing a new id.
func (s *Store) AuditHead(ctx context.Context) (string, error) {
row, err := s.queries().AuditHead(ctx)
orgID := org.DefaultOrgID
if scoped, ok := org.Scope(ctx); ok {
orgID = scoped
}
row, err := s.queries().AuditHead(ctx, postgresdb.AuditHeadParams{
OrgID: string(orgID),
InstanceAdmin: audit.InstanceAdmin(ctx),
})
if err != nil {
return "", fmt.Errorf("audit head: %w", err)
}
return fmt.Sprintf("%d:%d:%d:%d", row.SysAuditID, row.IdentityEventID, row.AccessDenialID, row.EventID), nil
return fmt.Sprintf("%d:%d:%d:%d:%d",
row.SysAuditID, row.IdentityEventID, row.AccessDenialID, row.AccessDenialCount, row.EventID), nil
}
func auditTime(t time.Time) pgtype.Timestamptz {
@@ -35,6 +35,11 @@ func TestRedactedFields(t *testing.T) {
encoded: `{"webhook":{"old":"` + testJWT + `","new":"` + testJWT + `"}}`,
want: map[string][2]string{"webhook": {`"[redacted]"`, `"[redacted]"`}},
},
{
name: "jwt embedded in a JSON string value is redacted",
encoded: `{"error":{"old":"auth failed for ` + testJWT + `","new":"ok"}}`,
want: map[string][2]string{"error": {`"[redacted]"`, `"ok"`}},
},
{
name: "pem private key is redacted by shape",
encoded: `{"certificate":{"old":"-----BEGIN PRIVATE KEY-----\nsecret\n-----END PRIVATE KEY-----","new":"kept"}}`,
@@ -97,6 +102,14 @@ func TestRedactedData(t *testing.T) {
if value["token"] != "[redacted]" || value["visible"] != true {
t.Fatalf("redactedData() = %v, want the token redacted and visible kept", value)
}
embedded := redactedData(`{"error":"auth failed for ` + testJWT + `"}`)
var nested map[string]any
if err := json.Unmarshal(embedded, &nested); err != nil {
t.Fatalf("redactedData(embedded jwt) = %q, not JSON: %v", embedded, err)
}
if nested["error"] != "[redacted]" {
t.Fatalf("redactedData(embedded jwt) = %v, want the whole value redacted", nested)
}
if got := redactedData(`{"broken":`); got != nil {
t.Fatalf("redactedData(malformed) = %q, want nil", got)
}
+10 -8
View File
@@ -24,8 +24,10 @@ func identityFromRow(i postgresdb.Identity) identity.Identity {
}
// insertIdentityEvent writes one identity audit event through q, refusing an
// audit that cannot answer who acted and whose authority they used.
func insertIdentityEvent(ctx context.Context, q *postgresdb.Queries, id identity.IdentityID, ev identity.Event, audit identity.Audit, at time.Time) error {
// audit that cannot answer who acted and whose authority they used. orgID is
// the org the event acts in, recorded on the row so the org's timeline keeps a
// removed member's earlier lifecycle events.
func insertIdentityEvent(ctx context.Context, q *postgresdb.Queries, orgID org.OrgID, id identity.IdentityID, ev identity.Event, audit identity.Audit, at time.Time) error {
if audit.ActorID == "" || audit.Source == "" || audit.RequestID == "" {
return fmt.Errorf("%w: audit actor, source, and request ID are required", identity.ErrInvalid)
}
@@ -33,7 +35,7 @@ func insertIdentityEvent(ctx context.Context, q *postgresdb.Queries, id identity
IdentityID: string(id), EventType: string(ev.Type),
FromLifecycle: string(ev.From), ToLifecycle: string(ev.To),
DisplayName: ev.DisplayName, ActorID: string(audit.ActorID),
Source: audit.Source, RequestID: audit.RequestID, At: at,
Source: audit.Source, RequestID: audit.RequestID, At: at, OrgID: string(orgID),
})
}
@@ -116,7 +118,7 @@ func (s *Store) create(ctx context.Context, value identity.Identity, audit ident
}); err != nil {
return identity.Identity{}, err
}
if err := insertIdentityEvent(ctx, q, value.ID, identity.Event{
if err := insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), value.ID, identity.Event{
IdentityID: value.ID, Type: "create", To: value.Lifecycle, DisplayName: value.DisplayName,
}, audit, now); err != nil {
return identity.Identity{}, err
@@ -195,7 +197,7 @@ func (s *Store) Apply(ctx context.Context, id identity.IdentityID, expectedVersi
return identity.Identity{}, err
}
}
if err := insertIdentityEvent(ctx, q, id, ev, audit, now); err != nil {
if err := insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), id, ev, audit, now); err != nil {
return identity.Identity{}, err
}
if err := tx.Commit(ctx); err != nil {
@@ -258,7 +260,7 @@ func (s *Store) ClaimSubject(ctx context.Context, placeholder, subject identity.
return err
}
claimed = identityFromRow(row)
return insertIdentityEvent(ctx, q, claimed.ID, identity.Event{
return insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), claimed.ID, identity.Event{
IdentityID: claimed.ID, Type: "claim_subject", From: claimed.Lifecycle, To: claimed.Lifecycle, DisplayName: subject.Subject,
}, identity.Audit{ActorID: claimed.ID, Source: "sign-in", RequestID: placeholder.Subject}, now)
})
@@ -300,7 +302,7 @@ func (s *Store) BindSubject(ctx context.Context, id identity.IdentityID, subject
}); err != nil {
return err
}
if err := insertIdentityEvent(ctx, q, id, identity.Event{
if err := insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), id, identity.Event{
IdentityID: id, Type: "bind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
}, audit, now); err != nil {
return err
@@ -352,7 +354,7 @@ func (s *Store) UnbindSubject(ctx context.Context, id identity.IdentityID, subje
return identity.ErrNotFound
}
cur := identityFromRow(current)
return insertIdentityEvent(ctx, q, id, identity.Event{
return insertIdentityEvent(ctx, q, org.OrgFromContext(ctx), id, identity.Event{
IdentityID: id, Type: "unbind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
}, audit, now)
})
@@ -0,0 +1,33 @@
-- Identity lifecycle events were instance-level: the feed joined live
-- memberships to decide which org could see them, so removing a member erased
-- their earlier lifecycle rows from that org's timeline. org_id on the event
-- makes the org a first-class, immutable fact recorded when the event happens.
-- +goose Up
ALTER TABLE identity_events ADD COLUMN org_id text NOT NULL DEFAULT 'org-sys';
ALTER TABLE identity_events ADD CONSTRAINT identity_events_org_nonempty CHECK (org_id <> '');
-- An agent belongs to exactly one org, so its assignment is authoritative.
-- For a person, use the membership that was already in place when the event
-- happened; a create writes the membership first, so its event resolves. A
-- membership that predates the event is the best remaining answer; a person
-- with none belongs to the system org.
UPDATE identity_events ie SET org_id = COALESCE(
(SELECT a.org_id FROM org_agents a WHERE a.identity_id = ie.identity_id),
(SELECT m.org_id FROM memberships m
WHERE m.identity_id = ie.identity_id AND m.created_at <= ie.at
ORDER BY m.created_at DESC LIMIT 1),
(SELECT m.org_id FROM memberships m
WHERE m.identity_id = ie.identity_id
ORDER BY m.created_at ASC LIMIT 1),
'org-sys'
);
DROP INDEX idx_identity_events_at;
CREATE INDEX idx_identity_events_org_at ON identity_events (org_id, at DESC, id DESC);
-- +goose Down
DROP INDEX idx_identity_events_org_at;
CREATE INDEX idx_identity_events_at ON identity_events (at DESC, id DESC);
ALTER TABLE identity_events DROP CONSTRAINT identity_events_org_nonempty;
ALTER TABLE identity_events DROP COLUMN org_id;
@@ -14,28 +14,38 @@ import (
const auditHead = `-- name: AuditHead :one
SELECT
(SELECT COALESCE(max(id), 0)::bigint FROM sys_audit) AS sys_audit_id,
(SELECT COALESCE(max(id), 0)::bigint FROM identity_events) AS identity_event_id,
(SELECT COALESCE(max(id), 0)::bigint FROM access_denials) AS access_denial_id,
(SELECT COALESCE(max(id), 0)::bigint FROM events) AS event_id
(SELECT COALESCE(max(id), 0)::bigint FROM sys_audit WHERE org_id = $1::text) AS sys_audit_id,
(SELECT COALESCE(max(id), 0)::bigint FROM identity_events
WHERE ($2::boolean OR org_id = $1::text)) AS identity_event_id,
(SELECT COALESCE(max(id), 0)::bigint FROM access_denials WHERE org_id = $1::text) AS access_denial_id,
(SELECT COALESCE(sum(count), 0)::bigint FROM access_denials WHERE org_id = $1::text) AS access_denial_count,
(SELECT COALESCE(max(id), 0)::bigint FROM events WHERE org_id = $1::text) AS event_id
`
type AuditHeadRow struct {
SysAuditID int64
IdentityEventID int64
AccessDenialID int64
EventID int64
type AuditHeadParams struct {
OrgID string
InstanceAdmin bool
}
// A token over the newest id of each trail. It is cheap, carries no rows and
// no org data, and changes whenever any trail grows.
func (q *Queries) AuditHead(ctx context.Context) (AuditHeadRow, error) {
row := q.db.QueryRow(ctx, auditHead)
type AuditHeadRow struct {
SysAuditID int64
IdentityEventID int64
AccessDenialID int64
AccessDenialCount int64
EventID int64
}
// A token over the newest id of each trail the caller can read, plus the
// denials' count sum because a coalesced denial bumps its count in place
// without drawing a new id. It carries no rows and no org data.
func (q *Queries) AuditHead(ctx context.Context, arg AuditHeadParams) (AuditHeadRow, error) {
row := q.db.QueryRow(ctx, auditHead, arg.OrgID, arg.InstanceAdmin)
var i AuditHeadRow
err := row.Scan(
&i.SysAuditID,
&i.IdentityEventID,
&i.AccessDenialID,
&i.AccessDenialCount,
&i.EventID,
)
return i, err
@@ -68,15 +78,25 @@ WITH sys_changes AS (
string_agg(DISTINCT a.field, ' ') AS field_names
FROM sys_audit a
WHERE a.org_id = $2::text
AND a.table_name IN ('resources', 'access_policies')
AND (
COALESCE(cardinality($3::text[]), 0) = 0
OR (a.table_name = 'resources' AND 'settings' = ANY($3::text[]))
OR (a.table_name = 'access_policies' AND 'access' = ANY($3::text[]))
OR (a.table_name IN ('memberships', 'org_agents') AND 'access' = ANY($3::text[]))
OR (a.table_name IN ('bindings', 'mappings', 'event_types') AND 'automation' = ANY($3::text[]))
OR (
a.table_name NOT IN (
'resources', 'access_policies',
'memberships', 'org_agents',
'bindings', 'mappings', 'event_types'
)
AND 'settings' = ANY($3::text[])
)
)
AND ($4::timestamptz IS NULL OR a.at >= $4::timestamptz)
AND ($5::timestamptz IS NULL OR a.at < $5::timestamptz)
AND ($6::text IS NULL OR a.actor = $6::text)
AND ($4::timestamptz IS NULL OR a.at <= $4::timestamptz)
AND ($5::timestamptz IS NULL OR a.at >= $5::timestamptz)
AND ($6::timestamptz IS NULL OR a.at < $6::timestamptz)
AND ($7::text IS NULL OR a.actor = $7::text)
GROUP BY a.org_id, a.table_name, a.record_key, a.request_id, a.record_version, a.at, a.actor, a.source
)
SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principal_id, target_kind, target_id, kind, record_version, request_id, source, fields, action, severity, outcome, detail, search_text, workspace_id, task_id, data_text FROM (
@@ -85,7 +105,7 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
g.org_id AS org_id,
CASE g.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
1 AS source_rank,
g.source_id::bigint AS source_id,
g.source_id,
g.at,
g.actor,
''::text AS actor_kind,
@@ -116,13 +136,14 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
g.fields,
CASE
WHEN g.table_name = 'access_policies' AND g.is_reset THEN 'reset'
WHEN g.all_old_null THEN 'create'
WHEN g.all_new_null THEN 'delete'
WHEN g.table_name = 'access_policies' AND g.all_new_null AND g.record_version > 0 THEN 'delete'
WHEN g.table_name = 'access_policies' AND g.all_old_null THEN 'create'
WHEN g.table_name = 'resources' AND g.record_version = 1 AND g.all_old_null THEN 'create'
ELSE 'update'
END AS action,
CASE
WHEN g.table_name = 'access_policies' AND g.is_reset THEN 'warning'
WHEN g.all_new_null THEN 'warning'
WHEN g.table_name = 'access_policies' AND g.all_new_null AND g.record_version > 0 THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
@@ -145,21 +166,22 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
0::bigint AS task_id,
''::text AS data_text
FROM sys_changes g
WHERE g.table_name IN ('resources', 'access_policies')
) s
WHERE (COALESCE(cardinality($3::text[]), 0) = 0 OR s.category = ANY($3::text[]))
AND (COALESCE(cardinality($7::text[]), 0) = 0 OR s.action = ANY($7::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR s.kind = ANY($8::text[]))
AND ($6::text IS NULL OR s.actor = $6::text)
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR s.target_kind = ANY($9::text[]))
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR s.target_id = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR s.severity = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR s.outcome = ANY($12::text[]))
AND ($13::text IS NULL OR position(lower($13::text) IN lower(s.search_text)) > 0)
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR s.action = ANY($8::text[]))
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR s.kind = ANY($9::text[]))
AND ($7::text IS NULL OR s.actor = $7::text)
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR s.target_kind = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR s.target_id = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR s.severity = ANY($12::text[]))
AND (COALESCE(cardinality($13::text[]), 0) = 0 OR s.outcome = ANY($13::text[]))
AND ($14::text IS NULL OR position(lower($14::text) IN lower(s.search_text)) > 0)
AND (
$14::timestamptz IS NULL
OR s.at < $14::timestamptz
OR (s.at = $14::timestamptz AND s.source_rank < $15::int)
OR (s.at = $14::timestamptz AND s.source_rank = $15::int AND s.source_id < $16::bigint)
$4::timestamptz IS NULL
OR s.at < $4::timestamptz
OR (s.at = $4::timestamptz AND s.source_rank < $15::int)
OR (s.at = $4::timestamptz AND s.source_rank = $15::int AND s.source_id < $16::bigint)
)
ORDER BY s.at DESC, s.source_rank DESC, s.source_id DESC
LIMIT $1::bigint
@@ -193,28 +215,24 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
''::text AS data_text
FROM identity_events ie
WHERE ie.event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND (
$2::text = 'org-sys'
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = ie.identity_id AND m.org_id = $2::text)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = ie.identity_id AND a.org_id = $2::text)
)
AND ($4::timestamptz IS NULL OR ie.at >= $4::timestamptz)
AND ($5::timestamptz IS NULL OR ie.at < $5::timestamptz)
AND ($17::boolean OR ie.org_id = $2::text)
AND ($5::timestamptz IS NULL OR ie.at >= $5::timestamptz)
AND ($6::timestamptz IS NULL OR ie.at < $6::timestamptz)
) si
WHERE (COALESCE(cardinality($3::text[]), 0) = 0 OR si.category = ANY($3::text[]))
AND (COALESCE(cardinality($7::text[]), 0) = 0 OR si.action = ANY($7::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR si.kind = ANY($8::text[]))
AND ($6::text IS NULL OR si.actor = $6::text)
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR si.target_kind = ANY($9::text[]))
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR si.target_id = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR si.severity = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR si.outcome = ANY($12::text[]))
AND ($13::text IS NULL OR position(lower($13::text) IN lower(si.search_text)) > 0)
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR si.action = ANY($8::text[]))
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR si.kind = ANY($9::text[]))
AND ($7::text IS NULL OR si.actor = $7::text)
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR si.target_kind = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR si.target_id = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR si.severity = ANY($12::text[]))
AND (COALESCE(cardinality($13::text[]), 0) = 0 OR si.outcome = ANY($13::text[]))
AND ($14::text IS NULL OR position(lower($14::text) IN lower(si.search_text)) > 0)
AND (
$14::timestamptz IS NULL
OR si.at < $14::timestamptz
OR (si.at = $14::timestamptz AND si.source_rank < $15::int)
OR (si.at = $14::timestamptz AND si.source_rank = $15::int AND si.source_id < $16::bigint)
$4::timestamptz IS NULL
OR si.at < $4::timestamptz
OR (si.at = $4::timestamptz AND si.source_rank < $15::int)
OR (si.at = $4::timestamptz AND si.source_rank = $15::int AND si.source_id < $16::bigint)
)
ORDER BY si.at DESC, si.source_rank DESC, si.source_id DESC
LIMIT $1::bigint
@@ -248,23 +266,23 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
jsonb_build_object('count', d.count, 'policies', d.policies)::text AS data_text
FROM access_denials d
WHERE d.org_id = $2::text
AND ($4::timestamptz IS NULL OR d.minute_at >= $4::timestamptz)
AND ($5::timestamptz IS NULL OR d.minute_at < $5::timestamptz)
AND ($5::timestamptz IS NULL OR d.minute_at >= $5::timestamptz)
AND ($6::timestamptz IS NULL OR d.minute_at < $6::timestamptz)
) sd
WHERE (COALESCE(cardinality($3::text[]), 0) = 0 OR sd.category = ANY($3::text[]))
AND (COALESCE(cardinality($7::text[]), 0) = 0 OR sd.action = ANY($7::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR sd.kind = ANY($8::text[]))
AND ($6::text IS NULL OR sd.actor = $6::text)
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR sd.target_kind = ANY($9::text[]))
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR sd.target_id = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR sd.severity = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR sd.outcome = ANY($12::text[]))
AND ($13::text IS NULL OR position(lower($13::text) IN lower(sd.search_text)) > 0)
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR sd.action = ANY($8::text[]))
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR sd.kind = ANY($9::text[]))
AND ($7::text IS NULL OR sd.actor = $7::text)
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR sd.target_kind = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR sd.target_id = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR sd.severity = ANY($12::text[]))
AND (COALESCE(cardinality($13::text[]), 0) = 0 OR sd.outcome = ANY($13::text[]))
AND ($14::text IS NULL OR position(lower($14::text) IN lower(sd.search_text)) > 0)
AND (
$14::timestamptz IS NULL
OR sd.at < $14::timestamptz
OR (sd.at = $14::timestamptz AND sd.source_rank < $15::int)
OR (sd.at = $14::timestamptz AND sd.source_rank = $15::int AND sd.source_id < $16::bigint)
$4::timestamptz IS NULL
OR sd.at < $4::timestamptz
OR (sd.at = $4::timestamptz AND sd.source_rank < $15::int)
OR (sd.at = $4::timestamptz AND sd.source_rank = $15::int AND sd.source_id < $16::bigint)
)
ORDER BY sd.at DESC, sd.source_rank DESC, sd.source_id DESC
LIMIT $1::bigint
@@ -327,48 +345,108 @@ SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principa
e.data AS data_text
FROM events e
WHERE e.org_id = $2::text
AND ($4::timestamptz IS NULL OR e.at >= $4::timestamptz)
AND ($5::timestamptz IS NULL OR e.at < $5::timestamptz)
AND ($5::timestamptz IS NULL OR e.at >= $5::timestamptz)
AND ($6::timestamptz IS NULL OR e.at < $6::timestamptz)
) se
WHERE (COALESCE(cardinality($3::text[]), 0) = 0 OR se.category = ANY($3::text[]))
AND (COALESCE(cardinality($7::text[]), 0) = 0 OR se.action = ANY($7::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR se.kind = ANY($8::text[]))
AND ($6::text IS NULL OR se.actor = $6::text)
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR se.target_kind = ANY($9::text[]))
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR se.target_id = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR se.severity = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR se.outcome = ANY($12::text[]))
AND ($13::text IS NULL OR position(lower($13::text) IN lower(se.search_text)) > 0)
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR se.action = ANY($8::text[]))
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR se.kind = ANY($9::text[]))
AND ($7::text IS NULL OR se.actor = $7::text)
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR se.target_kind = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR se.target_id = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR se.severity = ANY($12::text[]))
AND (COALESCE(cardinality($13::text[]), 0) = 0 OR se.outcome = ANY($13::text[]))
AND ($14::text IS NULL OR position(lower($14::text) IN lower(se.search_text)) > 0)
AND (
$14::timestamptz IS NULL
OR se.at < $14::timestamptz
OR (se.at = $14::timestamptz AND se.source_rank < $15::int)
OR (se.at = $14::timestamptz AND se.source_rank = $15::int AND se.source_id < $16::bigint)
$4::timestamptz IS NULL
OR se.at < $4::timestamptz
OR (se.at = $4::timestamptz AND se.source_rank < $15::int)
OR (se.at = $4::timestamptz AND se.source_rank = $15::int AND se.source_id < $16::bigint)
)
ORDER BY se.at DESC, se.source_rank DESC, se.source_id DESC
LIMIT $1::bigint
) b4
UNION ALL
SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principal_id, target_kind, target_id, kind, record_version, request_id, source, fields, action, severity, outcome, detail, search_text, workspace_id, task_id, data_text FROM (
SELECT org_id, category, source_rank, source_id, at, actor, actor_kind, principal_id, target_kind, target_id, kind, record_version, request_id, source, fields, action, severity, outcome, detail, search_text, workspace_id, task_id, data_text FROM (
SELECT
g.org_id,
CASE
WHEN g.table_name IN ('memberships', 'org_agents') THEN 'access'
WHEN g.table_name IN ('bindings', 'mappings', 'event_types') THEN 'automation'
ELSE 'settings'
END AS category,
5 AS source_rank,
g.source_id,
g.at,
g.actor,
''::text AS actor_kind,
''::text AS principal_id,
g.table_name::text AS target_kind,
g.record_key::text AS target_id,
g.table_name AS kind,
g.record_version,
g.request_id,
g.source,
g.fields,
CASE
WHEN g.record_version = 0 AND g.all_old_null THEN 'create'
WHEN g.record_version = 0 AND g.all_new_null THEN 'delete'
ELSE 'update'
END AS action,
CASE
WHEN g.record_version = 0 AND g.all_new_null AND NOT g.all_old_null THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
''::text AS detail,
concat_ws(' ', g.record_key, g.actor, g.source, g.field_names, g.table_name) AS search_text,
''::text AS workspace_id,
0::bigint AS task_id,
''::text AS data_text
FROM sys_changes g
WHERE g.table_name NOT IN ('resources', 'access_policies')
) sg
WHERE (COALESCE(cardinality($3::text[]), 0) = 0 OR sg.category = ANY($3::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR sg.action = ANY($8::text[]))
AND (COALESCE(cardinality($9::text[]), 0) = 0 OR sg.kind = ANY($9::text[]))
AND ($7::text IS NULL OR sg.actor = $7::text)
AND (COALESCE(cardinality($10::text[]), 0) = 0 OR sg.target_kind = ANY($10::text[]))
AND (COALESCE(cardinality($11::text[]), 0) = 0 OR sg.target_id = ANY($11::text[]))
AND (COALESCE(cardinality($12::text[]), 0) = 0 OR sg.severity = ANY($12::text[]))
AND (COALESCE(cardinality($13::text[]), 0) = 0 OR sg.outcome = ANY($13::text[]))
AND ($14::text IS NULL OR position(lower($14::text) IN lower(sg.search_text)) > 0)
AND (
$4::timestamptz IS NULL
OR sg.at < $4::timestamptz
OR (sg.at = $4::timestamptz AND sg.source_rank < $15::int)
OR (sg.at = $4::timestamptz AND sg.source_rank = $15::int AND sg.source_id < $16::bigint)
)
ORDER BY sg.at DESC, sg.source_rank DESC, sg.source_id DESC
LIMIT $1::bigint
) merged
ORDER BY merged.at DESC, merged.source_rank DESC, merged.source_id DESC
LIMIT $1::bigint
`
type ListAuditParams struct {
PageLimit int64
OrgID string
Categories []string
FromAt pgtype.Timestamptz
ToAt pgtype.Timestamptz
Actor pgtype.Text
Actions []string
Kinds []string
TargetKinds []string
TargetIds []string
Severities []string
Outcomes []string
Q pgtype.Text
CursorAt pgtype.Timestamptz
CursorRank int32
CursorID int64
PageLimit int64
OrgID string
Categories []string
CursorAt pgtype.Timestamptz
FromAt pgtype.Timestamptz
ToAt pgtype.Timestamptz
Actor pgtype.Text
Actions []string
Kinds []string
TargetKinds []string
TargetIds []string
Severities []string
Outcomes []string
Q pgtype.Text
CursorRank int32
CursorID int64
InstanceAdmin bool
}
type ListAuditRow struct {
@@ -406,20 +484,27 @@ type ListAuditRow struct {
// Each branch emits the same row shape. The outer query merges the per-branch
// top-K and takes the page. Ranks are fixed per origin and never reused:
//
// 1 sys_audit (resources, access_policies), grouped into change sets
// 1 sys_audit resources and access_policies, grouped into change sets
// 2 identity_events lifecycle events
// 3 access_denials, one aggregated denial
// 4 events
// 5 sys_audit coverage tables (orgs, memberships, sources, ...), grouped
//
// search_text is metadata only. It never contains a value, an event's data,
// or policy text: substring search over unredacted values would be a
// token-guessing oracle. The adapter redacts fields, data and detail before
// an entry leaves the process, and fail-closes on undecodable JSON.
//
// Identity lifecycle rows carry the org they happened in, so removing a
// member does not erase their earlier history from that org's feed. Only a
// principal with instance-wide authority sees every org's identity events;
// being confined to the system org is not that authority.
func (q *Queries) ListAudit(ctx context.Context, arg ListAuditParams) ([]ListAuditRow, error) {
rows, err := q.db.Query(ctx, listAudit,
arg.PageLimit,
arg.OrgID,
arg.Categories,
arg.CursorAt,
arg.FromAt,
arg.ToAt,
arg.Actor,
@@ -430,9 +515,9 @@ func (q *Queries) ListAudit(ctx context.Context, arg ListAuditParams) ([]ListAud
arg.Severities,
arg.Outcomes,
arg.Q,
arg.CursorAt,
arg.CursorRank,
arg.CursorID,
arg.InstanceAdmin,
)
if err != nil {
return nil, err
@@ -161,8 +161,8 @@ func (q *Queries) InsertIdentityAliasIgnoreConflict(ctx context.Context, arg Ins
}
const insertIdentityEvent = `-- name: InsertIdentityEvent :exec
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at, org_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
`
type InsertIdentityEventParams struct {
@@ -175,6 +175,7 @@ type InsertIdentityEventParams struct {
Source string
RequestID string
At time.Time
OrgID string
}
func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEventParams) error {
@@ -188,6 +189,7 @@ func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEve
arg.Source,
arg.RequestID,
arg.At,
arg.OrgID,
)
return err
}
@@ -258,6 +258,7 @@ type IdentityEvent struct {
Source string
RequestID string
At time.Time
OrgID string
}
type IdentitySubject struct {
@@ -7,15 +7,21 @@
-- Each branch emits the same row shape. The outer query merges the per-branch
-- top-K and takes the page. Ranks are fixed per origin and never reused:
-- 1 sys_audit (resources, access_policies), grouped into change sets
-- 1 sys_audit resources and access_policies, grouped into change sets
-- 2 identity_events lifecycle events
-- 3 access_denials, one aggregated denial
-- 4 events
-- 5 sys_audit coverage tables (orgs, memberships, sources, ...), grouped
--
-- search_text is metadata only. It never contains a value, an event's data,
-- or policy text: substring search over unredacted values would be a
-- token-guessing oracle. The adapter redacts fields, data and detail before
-- an entry leaves the process, and fail-closes on undecodable JSON.
--
-- Identity lifecycle rows carry the org they happened in, so removing a
-- member does not erase their earlier history from that org's feed. Only a
-- principal with instance-wide authority sees every org's identity events;
-- being confined to the system org is not that authority.
-- name: ListAudit :many
WITH sys_changes AS (
@@ -42,12 +48,22 @@ WITH sys_changes AS (
string_agg(DISTINCT a.field, ' ') AS field_names
FROM sys_audit a
WHERE a.org_id = sqlc.arg(org_id)::text
AND a.table_name IN ('resources', 'access_policies')
AND (
COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0
OR (a.table_name = 'resources' AND 'settings' = ANY(sqlc.arg(categories)::text[]))
OR (a.table_name = 'access_policies' AND 'access' = ANY(sqlc.arg(categories)::text[]))
OR (a.table_name IN ('memberships', 'org_agents') AND 'access' = ANY(sqlc.arg(categories)::text[]))
OR (a.table_name IN ('bindings', 'mappings', 'event_types') AND 'automation' = ANY(sqlc.arg(categories)::text[]))
OR (
a.table_name NOT IN (
'resources', 'access_policies',
'memberships', 'org_agents',
'bindings', 'mappings', 'event_types'
)
AND 'settings' = ANY(sqlc.arg(categories)::text[])
)
)
AND (sqlc.narg(cursor_at)::timestamptz IS NULL OR a.at <= sqlc.narg(cursor_at)::timestamptz)
AND (sqlc.narg(from_at)::timestamptz IS NULL OR a.at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR a.at < sqlc.narg(to_at)::timestamptz)
AND (sqlc.narg(actor)::text IS NULL OR a.actor = sqlc.narg(actor)::text)
@@ -59,7 +75,7 @@ SELECT * FROM (
g.org_id AS org_id,
CASE g.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
1 AS source_rank,
g.source_id::bigint AS source_id,
g.source_id,
g.at,
g.actor,
''::text AS actor_kind,
@@ -90,13 +106,14 @@ SELECT * FROM (
g.fields,
CASE
WHEN g.table_name = 'access_policies' AND g.is_reset THEN 'reset'
WHEN g.all_old_null THEN 'create'
WHEN g.all_new_null THEN 'delete'
WHEN g.table_name = 'access_policies' AND g.all_new_null AND g.record_version > 0 THEN 'delete'
WHEN g.table_name = 'access_policies' AND g.all_old_null THEN 'create'
WHEN g.table_name = 'resources' AND g.record_version = 1 AND g.all_old_null THEN 'create'
ELSE 'update'
END AS action,
CASE
WHEN g.table_name = 'access_policies' AND g.is_reset THEN 'warning'
WHEN g.all_new_null THEN 'warning'
WHEN g.table_name = 'access_policies' AND g.all_new_null AND g.record_version > 0 THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
@@ -119,6 +136,7 @@ SELECT * FROM (
0::bigint AS task_id,
''::text AS data_text
FROM sys_changes g
WHERE g.table_name IN ('resources', 'access_policies')
) s
WHERE (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR s.category = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR s.action = ANY(sqlc.arg(actions)::text[]))
@@ -167,11 +185,7 @@ SELECT * FROM (
''::text AS data_text
FROM identity_events ie
WHERE ie.event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND (
sqlc.arg(org_id)::text = 'org-sys'
OR EXISTS (SELECT 1 FROM memberships m WHERE m.identity_id = ie.identity_id AND m.org_id = sqlc.arg(org_id)::text)
OR EXISTS (SELECT 1 FROM org_agents a WHERE a.identity_id = ie.identity_id AND a.org_id = sqlc.arg(org_id)::text)
)
AND (sqlc.arg(instance_admin)::boolean OR ie.org_id = sqlc.arg(org_id)::text)
AND (sqlc.narg(from_at)::timestamptz IS NULL OR ie.at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR ie.at < sqlc.narg(to_at)::timestamptz)
) si
@@ -321,15 +335,77 @@ SELECT * FROM (
)
ORDER BY se.at DESC, se.source_rank DESC, se.source_id DESC
LIMIT sqlc.arg(page_limit)::bigint
) b4
UNION ALL
SELECT * FROM (
SELECT * FROM (
SELECT
g.org_id,
CASE
WHEN g.table_name IN ('memberships', 'org_agents') THEN 'access'
WHEN g.table_name IN ('bindings', 'mappings', 'event_types') THEN 'automation'
ELSE 'settings'
END AS category,
5 AS source_rank,
g.source_id,
g.at,
g.actor,
''::text AS actor_kind,
''::text AS principal_id,
g.table_name::text AS target_kind,
g.record_key::text AS target_id,
g.table_name AS kind,
g.record_version,
g.request_id,
g.source,
g.fields,
CASE
WHEN g.record_version = 0 AND g.all_old_null THEN 'create'
WHEN g.record_version = 0 AND g.all_new_null THEN 'delete'
ELSE 'update'
END AS action,
CASE
WHEN g.record_version = 0 AND g.all_new_null AND NOT g.all_old_null THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
''::text AS detail,
concat_ws(' ', g.record_key, g.actor, g.source, g.field_names, g.table_name) AS search_text,
''::text AS workspace_id,
0::bigint AS task_id,
''::text AS data_text
FROM sys_changes g
WHERE g.table_name NOT IN ('resources', 'access_policies')
) sg
WHERE (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR sg.category = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR sg.action = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR sg.kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR sg.actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR sg.target_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR sg.target_id = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR sg.severity = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR sg.outcome = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(sg.search_text)) > 0)
AND (
sqlc.narg(cursor_at)::timestamptz IS NULL
OR sg.at < sqlc.narg(cursor_at)::timestamptz
OR (sg.at = sqlc.narg(cursor_at)::timestamptz AND sg.source_rank < sqlc.arg(cursor_rank)::int)
OR (sg.at = sqlc.narg(cursor_at)::timestamptz AND sg.source_rank = sqlc.arg(cursor_rank)::int AND sg.source_id < sqlc.arg(cursor_id)::bigint)
)
ORDER BY sg.at DESC, sg.source_rank DESC, sg.source_id DESC
LIMIT sqlc.arg(page_limit)::bigint
) merged
ORDER BY merged.at DESC, merged.source_rank DESC, merged.source_id DESC
LIMIT sqlc.arg(page_limit)::bigint;
-- name: AuditHead :one
-- A token over the newest id of each trail. It is cheap, carries no rows and
-- no org data, and changes whenever any trail grows.
-- A token over the newest id of each trail the caller can read, plus the
-- denials' count sum because a coalesced denial bumps its count in place
-- without drawing a new id. It carries no rows and no org data.
SELECT
(SELECT COALESCE(max(id), 0)::bigint FROM sys_audit) AS sys_audit_id,
(SELECT COALESCE(max(id), 0)::bigint FROM identity_events) AS identity_event_id,
(SELECT COALESCE(max(id), 0)::bigint FROM access_denials) AS access_denial_id,
(SELECT COALESCE(max(id), 0)::bigint FROM events) AS event_id;
(SELECT COALESCE(max(id), 0)::bigint FROM sys_audit WHERE org_id = sqlc.arg(org_id)::text) AS sys_audit_id,
(SELECT COALESCE(max(id), 0)::bigint FROM identity_events
WHERE (sqlc.arg(instance_admin)::boolean OR org_id = sqlc.arg(org_id)::text)) AS identity_event_id,
(SELECT COALESCE(max(id), 0)::bigint FROM access_denials WHERE org_id = sqlc.arg(org_id)::text) AS access_denial_id,
(SELECT COALESCE(sum(count), 0)::bigint FROM access_denials WHERE org_id = sqlc.arg(org_id)::text) AS access_denial_count,
(SELECT COALESCE(max(id), 0)::bigint FROM events WHERE org_id = sqlc.arg(org_id)::text) AS event_id;
@@ -41,8 +41,8 @@ INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2);
INSERT INTO identity_aliases (alias, identity_id) VALUES ($1, $2) ON CONFLICT DO NOTHING;
-- name: InsertIdentityEvent :exec
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9);
INSERT INTO identity_events (identity_id, event_type, from_lifecycle, to_lifecycle, display_name, actor_id, source, request_id, at, org_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);
-- name: UpdateIdentity :execrows
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
+11 -2
View File
@@ -11,11 +11,20 @@ import (
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/audit"
)
var errAuditUnavailable = status.Error(codes.Unavailable, "audit feed unavailable")
// withInstanceAdmin marks whether the authenticated principal administers the
// whole instance. The feed uses it instead of the org id: a viewer confined to
// the system org is not an instance administrator.
func withInstanceAdmin(ctx context.Context) context.Context {
principal, ok := access.PrincipalFromContext(ctx)
return audit.WithInstanceAdmin(ctx, ok && principal.InstanceAdmin())
}
type auditServer struct {
pb.UnimplementedAuditServiceServer
*server
@@ -169,7 +178,7 @@ func (s *auditServer) ListAudit(ctx context.Context, request *pb.ListAuditReques
cursor = parsed
}
}
page, err := s.deps.Audit.ListAudit(ctx, filter, cursor, int(request.GetLimit()))
page, err := s.deps.Audit.ListAudit(withInstanceAdmin(ctx), filter, cursor, int(request.GetLimit()))
if err != nil {
return nil, s.logErr("ListAudit", err)
}
@@ -188,7 +197,7 @@ func (s *auditServer) GetAuditHead(ctx context.Context, _ *pb.GetAuditHeadReques
if s.deps.Audit == nil {
return nil, errAuditUnavailable
}
token, err := s.deps.Audit.AuditHead(ctx)
token, err := s.deps.Audit.AuditHead(withInstanceAdmin(ctx))
if err != nil {
return nil, s.logErr("GetAuditHead", err)
}
+9 -12
View File
@@ -20,13 +20,10 @@ var SensitiveKeyMarkers = []string{
}
// jwtTokenRe matches a compact JWT (header.payload.signature) -- the header's
// base64url always begins "eyJ" -- which is an unambiguous secret token even
// when a sender puts it under a key the name heuristic misses.
var jwtTokenRe = regexp.MustCompile(`^eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$`)
// jwtSubstringRe matches a JWT embedded in free text, where the anchored form
// above does not.
var jwtSubstringRe = regexp.MustCompile(`eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+`)
// base64url always begins "eyJ" -- anywhere in a string. It is unanchored so
// a token embedded in prose is still caught, both in JSON string values and
// in free text.
var jwtTokenRe = regexp.MustCompile(`eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+`)
// RedactPayload replaces values under sensitive keys, or with secret-shaped
// values, with a marker and returns the re-encoded JSON. Best-effort.
@@ -49,7 +46,7 @@ func RedactPayload(payload []byte) ([]byte, error) {
// shape, for fields that carry prose rather than a JSON document. Key-based
// redaction does not apply: there is no key.
func RedactString(value string) string {
if sensitiveValueShape(value) || jwtSubstringRe.MatchString(value) {
if sensitiveValueShape(value) {
return redactedValue
}
return value
@@ -81,10 +78,10 @@ func redactValue(value any) any {
}
}
// sensitiveValueShape reports whether a string is an unambiguous secret even
// under a non-sensitive key: a compact JWT or a PEM private-key block. These
// shapes are never a meaningful field-mapping value, so redacting them does
// not hurt the schema-by-example use case.
// sensitiveValueShape reports whether a string contains an unambiguous secret
// even under a non-sensitive key: a compact JWT anywhere in the string, or a
// PEM private-key block. These shapes are never a meaningful field-mapping
// value, so redacting them does not hurt the schema-by-example use case.
func sensitiveValueShape(s string) bool {
if len(s) < 20 {
return false