feat(state): unified org-scoped audit feed and AuditService

This commit is contained in:
sam committed 2026-10-25 21:37:56 +11:00
commit 8edf57c688
26 files changed
+3450 -20

No files matched your search

+11
View File
@@ -9,6 +9,7 @@ import (
"github.com/samcharles93/archie-core/internal/app/servicekit"
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/messaging"
@@ -95,6 +96,7 @@ func compose(ctx context.Context, d deps) *webui.Server {
if policies, ok := d.Store.(access.PolicyStore); ok {
srv.Policies = policies
}
wireAudit(srv, d.Store)
wireOrgSurface(srv, d.Store)
wireAlerts(srv, d.Store)
if presence, ok := d.Store.(storecontract.PresenceStore); ok {
@@ -132,6 +134,15 @@ func compose(ctx context.Context, d deps) *webui.Server {
return srv
}
// wireAudit attaches the State Store's org-scoped audit feed. A store that
// cannot serve it leaves the audit routes answering 503 rather than showing an
// empty timeline.
func wireAudit(srv *webui.Server, store storecontract.TaskStore) {
if feed, ok := store.(audit.Store); ok {
srv.Audit = feed
}
}
// wireOrgSurface attaches the dashboard's org surface when the state client
// carries it.
func wireOrgSurface(srv *webui.Server, store storecontract.TaskStore) {
+4
View File
@@ -20,6 +20,7 @@ import (
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
@@ -423,6 +424,9 @@ func (b *server) accessDeps(deps *staterpc.Deps) {
if as, ok := b.st.(alert.Store); ok {
deps.Alerts = as
}
if feed, ok := b.st.(audit.Store); ok {
deps.Audit = feed
}
}
// serveStateStore registers the StateStore gRPC service and serves it until
+755
View File
@@ -0,0 +1,755 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.11
// protoc (unknown)
// source: state/v1/audit.proto
package statev1
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
reflect "reflect"
sync "sync"
unsafe "unsafe"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// AuditFilter selects the entries a page returns. An empty list places no
// restriction; categories, severities and outcomes are closed vocabularies
// owned by internal/domain/audit.
type AuditFilter struct {
state protoimpl.MessageState `protogen:"open.v1"`
Categories []string `protobuf:"bytes,1,rep,name=categories,proto3" json:"categories,omitempty"`
Actions []string `protobuf:"bytes,2,rep,name=actions,proto3" json:"actions,omitempty"`
Kinds []string `protobuf:"bytes,3,rep,name=kinds,proto3" json:"kinds,omitempty"`
Actor string `protobuf:"bytes,4,opt,name=actor,proto3" json:"actor,omitempty"`
TargetKinds []string `protobuf:"bytes,5,rep,name=target_kinds,json=targetKinds,proto3" json:"target_kinds,omitempty"`
TargetIds []string `protobuf:"bytes,6,rep,name=target_ids,json=targetIds,proto3" json:"target_ids,omitempty"`
Severities []string `protobuf:"bytes,7,rep,name=severities,proto3" json:"severities,omitempty"`
Outcomes []string `protobuf:"bytes,8,rep,name=outcomes,proto3" json:"outcomes,omitempty"`
// from is inclusive and to is exclusive, both in UTC.
From *timestamppb.Timestamp `protobuf:"bytes,9,opt,name=from,proto3" json:"from,omitempty"`
To *timestamppb.Timestamp `protobuf:"bytes,10,opt,name=to,proto3" json:"to,omitempty"`
// q is a substring match over metadata only: record key, kind, actor,
// source, field names, task/workflow/repo/stage. Never a value.
Q string `protobuf:"bytes,11,opt,name=q,proto3" json:"q,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *AuditFilter) Reset() {
*x = AuditFilter{}
mi := &file_state_v1_audit_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *AuditFilter) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*AuditFilter) ProtoMessage() {}
func (x *AuditFilter) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[0]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use AuditFilter.ProtoReflect.Descriptor instead.
func (*AuditFilter) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{0}
}
func (x *AuditFilter) GetCategories() []string {
if x != nil {
return x.Categories
}
return nil
}
func (x *AuditFilter) GetActions() []string {
if x != nil {
return x.Actions
}
return nil
}
func (x *AuditFilter) GetKinds() []string {
if x != nil {
return x.Kinds
}
return nil
}
func (x *AuditFilter) GetActor() string {
if x != nil {
return x.Actor
}
return ""
}
func (x *AuditFilter) GetTargetKinds() []string {
if x != nil {
return x.TargetKinds
}
return nil
}
func (x *AuditFilter) GetTargetIds() []string {
if x != nil {
return x.TargetIds
}
return nil
}
func (x *AuditFilter) GetSeverities() []string {
if x != nil {
return x.Severities
}
return nil
}
func (x *AuditFilter) GetOutcomes() []string {
if x != nil {
return x.Outcomes
}
return nil
}
func (x *AuditFilter) GetFrom() *timestamppb.Timestamp {
if x != nil {
return x.From
}
return nil
}
func (x *AuditFilter) GetTo() *timestamppb.Timestamp {
if x != nil {
return x.To
}
return nil
}
func (x *AuditFilter) GetQ() string {
if x != nil {
return x.Q
}
return ""
}
type ListAuditRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Filter *AuditFilter `protobuf:"bytes,1,opt,name=filter,proto3" json:"filter,omitempty"`
// cursor is opaque; a malformed one means newest.
Cursor string `protobuf:"bytes,2,opt,name=cursor,proto3" json:"cursor,omitempty"`
// limit is the page size. Zero means 100; the server caps it at 500.
Limit int32 `protobuf:"varint,3,opt,name=limit,proto3" json:"limit,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *ListAuditRequest) Reset() {
*x = ListAuditRequest{}
mi := &file_state_v1_audit_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *ListAuditRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ListAuditRequest) ProtoMessage() {}
func (x *ListAuditRequest) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[1]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ListAuditRequest.ProtoReflect.Descriptor instead.
func (*ListAuditRequest) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{1}
}
func (x *ListAuditRequest) GetFilter() *AuditFilter {
if x != nil {
return x.Filter
}
return nil
}
func (x *ListAuditRequest) GetCursor() string {
if x != nil {
return x.Cursor
}
return ""
}
func (x *ListAuditRequest) GetLimit() int32 {
if x != nil {
return x.Limit
}
return 0
}
// AuditField is one changed field of a change set. old_value_json and
// new_value_json are JSON values; an absent side is the JSON literal null.
type AuditField struct {
state protoimpl.MessageState `protogen:"open.v1"`
Field string `protobuf:"bytes,1,opt,name=field,proto3" json:"field,omitempty"`
OldValueJson string `protobuf:"bytes,2,opt,name=old_value_json,json=oldValueJson,proto3" json:"old_value_json,omitempty"`
NewValueJson string `protobuf:"bytes,3,opt,name=new_value_json,json=newValueJson,proto3" json:"new_value_json,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *AuditField) Reset() {
*x = AuditField{}
mi := &file_state_v1_audit_proto_msgTypes[2]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *AuditField) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*AuditField) ProtoMessage() {}
func (x *AuditField) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[2]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use AuditField.ProtoReflect.Descriptor instead.
func (*AuditField) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{2}
}
func (x *AuditField) GetField() string {
if x != nil {
return x.Field
}
return ""
}
func (x *AuditField) GetOldValueJson() string {
if x != nil {
return x.OldValueJson
}
return ""
}
func (x *AuditField) GetNewValueJson() string {
if x != nil {
return x.NewValueJson
}
return ""
}
// AuditEntry is one row of the feed: a grouped change set, a lifecycle event,
// an aggregated denial, or a runtime event. Free-form values are redacted
// before they reach this message.
type AuditEntry struct {
state protoimpl.MessageState `protogen:"open.v1"`
// id is stable across pages and live refreshes.
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Category string `protobuf:"bytes,2,opt,name=category,proto3" json:"category,omitempty"`
At *timestamppb.Timestamp `protobuf:"bytes,3,opt,name=at,proto3" json:"at,omitempty"`
Actor string `protobuf:"bytes,4,opt,name=actor,proto3" json:"actor,omitempty"`
ActorKind string `protobuf:"bytes,5,opt,name=actor_kind,json=actorKind,proto3" json:"actor_kind,omitempty"`
PrincipalId string `protobuf:"bytes,6,opt,name=principal_id,json=principalId,proto3" json:"principal_id,omitempty"`
TargetKind string `protobuf:"bytes,7,opt,name=target_kind,json=targetKind,proto3" json:"target_kind,omitempty"`
TargetId string `protobuf:"bytes,8,opt,name=target_id,json=targetId,proto3" json:"target_id,omitempty"`
Kind string `protobuf:"bytes,9,opt,name=kind,proto3" json:"kind,omitempty"`
RecordVersion int64 `protobuf:"varint,10,opt,name=record_version,json=recordVersion,proto3" json:"record_version,omitempty"`
RequestId string `protobuf:"bytes,11,opt,name=request_id,json=requestId,proto3" json:"request_id,omitempty"`
Source string `protobuf:"bytes,12,opt,name=source,proto3" json:"source,omitempty"`
Fields []*AuditField `protobuf:"bytes,13,rep,name=fields,proto3" json:"fields,omitempty"`
Action string `protobuf:"bytes,14,opt,name=action,proto3" json:"action,omitempty"`
Severity string `protobuf:"bytes,15,opt,name=severity,proto3" json:"severity,omitempty"`
Outcome string `protobuf:"bytes,16,opt,name=outcome,proto3" json:"outcome,omitempty"`
Detail string `protobuf:"bytes,17,opt,name=detail,proto3" json:"detail,omitempty"`
WorkspaceId string `protobuf:"bytes,18,opt,name=workspace_id,json=workspaceId,proto3" json:"workspace_id,omitempty"`
TaskId int64 `protobuf:"varint,19,opt,name=task_id,json=taskId,proto3" json:"task_id,omitempty"`
// data_json is the redacted event data, empty when the row has none.
DataJson string `protobuf:"bytes,20,opt,name=data_json,json=dataJson,proto3" json:"data_json,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *AuditEntry) Reset() {
*x = AuditEntry{}
mi := &file_state_v1_audit_proto_msgTypes[3]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *AuditEntry) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*AuditEntry) ProtoMessage() {}
func (x *AuditEntry) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[3]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use AuditEntry.ProtoReflect.Descriptor instead.
func (*AuditEntry) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{3}
}
func (x *AuditEntry) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *AuditEntry) GetCategory() string {
if x != nil {
return x.Category
}
return ""
}
func (x *AuditEntry) GetAt() *timestamppb.Timestamp {
if x != nil {
return x.At
}
return nil
}
func (x *AuditEntry) GetActor() string {
if x != nil {
return x.Actor
}
return ""
}
func (x *AuditEntry) GetActorKind() string {
if x != nil {
return x.ActorKind
}
return ""
}
func (x *AuditEntry) GetPrincipalId() string {
if x != nil {
return x.PrincipalId
}
return ""
}
func (x *AuditEntry) GetTargetKind() string {
if x != nil {
return x.TargetKind
}
return ""
}
func (x *AuditEntry) GetTargetId() string {
if x != nil {
return x.TargetId
}
return ""
}
func (x *AuditEntry) GetKind() string {
if x != nil {
return x.Kind
}
return ""
}
func (x *AuditEntry) GetRecordVersion() int64 {
if x != nil {
return x.RecordVersion
}
return 0
}
func (x *AuditEntry) GetRequestId() string {
if x != nil {
return x.RequestId
}
return ""
}
func (x *AuditEntry) GetSource() string {
if x != nil {
return x.Source
}
return ""
}
func (x *AuditEntry) GetFields() []*AuditField {
if x != nil {
return x.Fields
}
return nil
}
func (x *AuditEntry) GetAction() string {
if x != nil {
return x.Action
}
return ""
}
func (x *AuditEntry) GetSeverity() string {
if x != nil {
return x.Severity
}
return ""
}
func (x *AuditEntry) GetOutcome() string {
if x != nil {
return x.Outcome
}
return ""
}
func (x *AuditEntry) GetDetail() string {
if x != nil {
return x.Detail
}
return ""
}
func (x *AuditEntry) GetWorkspaceId() string {
if x != nil {
return x.WorkspaceId
}
return ""
}
func (x *AuditEntry) GetTaskId() int64 {
if x != nil {
return x.TaskId
}
return 0
}
func (x *AuditEntry) GetDataJson() string {
if x != nil {
return x.DataJson
}
return ""
}
type ListAuditResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
Entries []*AuditEntry `protobuf:"bytes,1,rep,name=entries,proto3" json:"entries,omitempty"`
NextCursor string `protobuf:"bytes,2,opt,name=next_cursor,json=nextCursor,proto3" json:"next_cursor,omitempty"`
HasMore bool `protobuf:"varint,3,opt,name=has_more,json=hasMore,proto3" json:"has_more,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *ListAuditResponse) Reset() {
*x = ListAuditResponse{}
mi := &file_state_v1_audit_proto_msgTypes[4]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *ListAuditResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*ListAuditResponse) ProtoMessage() {}
func (x *ListAuditResponse) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[4]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use ListAuditResponse.ProtoReflect.Descriptor instead.
func (*ListAuditResponse) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{4}
}
func (x *ListAuditResponse) GetEntries() []*AuditEntry {
if x != nil {
return x.Entries
}
return nil
}
func (x *ListAuditResponse) GetNextCursor() string {
if x != nil {
return x.NextCursor
}
return ""
}
func (x *ListAuditResponse) GetHasMore() bool {
if x != nil {
return x.HasMore
}
return false
}
type GetAuditHeadRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *GetAuditHeadRequest) Reset() {
*x = GetAuditHeadRequest{}
mi := &file_state_v1_audit_proto_msgTypes[5]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GetAuditHeadRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetAuditHeadRequest) ProtoMessage() {}
func (x *GetAuditHeadRequest) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[5]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetAuditHeadRequest.ProtoReflect.Descriptor instead.
func (*GetAuditHeadRequest) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{5}
}
type GetAuditHeadResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *GetAuditHeadResponse) Reset() {
*x = GetAuditHeadResponse{}
mi := &file_state_v1_audit_proto_msgTypes[6]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *GetAuditHeadResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetAuditHeadResponse) ProtoMessage() {}
func (x *GetAuditHeadResponse) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[6]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetAuditHeadResponse.ProtoReflect.Descriptor instead.
func (*GetAuditHeadResponse) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{6}
}
func (x *GetAuditHeadResponse) GetToken() string {
if x != nil {
return x.Token
}
return ""
}
var File_state_v1_audit_proto protoreflect.FileDescriptor
const file_state_v1_audit_proto_rawDesc = "" +
"\n" +
"\x14state/v1/audit.proto\x12\bstate.v1\x1a\x1fgoogle/protobuf/timestamp.proto\"\xdb\x02\n" +
"\vAuditFilter\x12\x1e\n" +
"\n" +
"categories\x18\x01 \x03(\tR\n" +
"categories\x12\x18\n" +
"\aactions\x18\x02 \x03(\tR\aactions\x12\x14\n" +
"\x05kinds\x18\x03 \x03(\tR\x05kinds\x12\x14\n" +
"\x05actor\x18\x04 \x01(\tR\x05actor\x12!\n" +
"\ftarget_kinds\x18\x05 \x03(\tR\vtargetKinds\x12\x1d\n" +
"\n" +
"target_ids\x18\x06 \x03(\tR\ttargetIds\x12\x1e\n" +
"\n" +
"severities\x18\a \x03(\tR\n" +
"severities\x12\x1a\n" +
"\boutcomes\x18\b \x03(\tR\boutcomes\x12.\n" +
"\x04from\x18\t \x01(\v2\x1a.google.protobuf.TimestampR\x04from\x12*\n" +
"\x02to\x18\n" +
" \x01(\v2\x1a.google.protobuf.TimestampR\x02to\x12\f\n" +
"\x01q\x18\v \x01(\tR\x01q\"o\n" +
"\x10ListAuditRequest\x12-\n" +
"\x06filter\x18\x01 \x01(\v2\x15.state.v1.AuditFilterR\x06filter\x12\x16\n" +
"\x06cursor\x18\x02 \x01(\tR\x06cursor\x12\x14\n" +
"\x05limit\x18\x03 \x01(\x05R\x05limit\"n\n" +
"\n" +
"AuditField\x12\x14\n" +
"\x05field\x18\x01 \x01(\tR\x05field\x12$\n" +
"\x0eold_value_json\x18\x02 \x01(\tR\foldValueJson\x12$\n" +
"\x0enew_value_json\x18\x03 \x01(\tR\fnewValueJson\"\xd9\x04\n" +
"\n" +
"AuditEntry\x12\x0e\n" +
"\x02id\x18\x01 \x01(\tR\x02id\x12\x1a\n" +
"\bcategory\x18\x02 \x01(\tR\bcategory\x12*\n" +
"\x02at\x18\x03 \x01(\v2\x1a.google.protobuf.TimestampR\x02at\x12\x14\n" +
"\x05actor\x18\x04 \x01(\tR\x05actor\x12\x1d\n" +
"\n" +
"actor_kind\x18\x05 \x01(\tR\tactorKind\x12!\n" +
"\fprincipal_id\x18\x06 \x01(\tR\vprincipalId\x12\x1f\n" +
"\vtarget_kind\x18\a \x01(\tR\n" +
"targetKind\x12\x1b\n" +
"\ttarget_id\x18\b \x01(\tR\btargetId\x12\x12\n" +
"\x04kind\x18\t \x01(\tR\x04kind\x12%\n" +
"\x0erecord_version\x18\n" +
" \x01(\x03R\rrecordVersion\x12\x1d\n" +
"\n" +
"request_id\x18\v \x01(\tR\trequestId\x12\x16\n" +
"\x06source\x18\f \x01(\tR\x06source\x12,\n" +
"\x06fields\x18\r \x03(\v2\x14.state.v1.AuditFieldR\x06fields\x12\x16\n" +
"\x06action\x18\x0e \x01(\tR\x06action\x12\x1a\n" +
"\bseverity\x18\x0f \x01(\tR\bseverity\x12\x18\n" +
"\aoutcome\x18\x10 \x01(\tR\aoutcome\x12\x16\n" +
"\x06detail\x18\x11 \x01(\tR\x06detail\x12!\n" +
"\fworkspace_id\x18\x12 \x01(\tR\vworkspaceId\x12\x17\n" +
"\atask_id\x18\x13 \x01(\x03R\x06taskId\x12\x1b\n" +
"\tdata_json\x18\x14 \x01(\tR\bdataJson\"\x7f\n" +
"\x11ListAuditResponse\x12.\n" +
"\aentries\x18\x01 \x03(\v2\x14.state.v1.AuditEntryR\aentries\x12\x1f\n" +
"\vnext_cursor\x18\x02 \x01(\tR\n" +
"nextCursor\x12\x19\n" +
"\bhas_more\x18\x03 \x01(\bR\ahasMore\"\x15\n" +
"\x13GetAuditHeadRequest\",\n" +
"\x14GetAuditHeadResponse\x12\x14\n" +
"\x05token\x18\x01 \x01(\tR\x05token2\xa3\x01\n" +
"\fAuditService\x12D\n" +
"\tListAudit\x12\x1a.state.v1.ListAuditRequest\x1a\x1b.state.v1.ListAuditResponse\x12M\n" +
"\fGetAuditHead\x12\x1d.state.v1.GetAuditHeadRequest\x1a\x1e.state.v1.GetAuditHeadResponseB\xa4\x01\n" +
"\fcom.state.v1B\n" +
"AuditProtoP\x01ZGgithub.com/samcharles93/archie-core/internal/contracts/state/v1;statev1\xa2\x02\x03SXX\xaa\x02\bState.V1\xca\x02\bState\\V1\xe2\x02\x14State\\V1\\GPBMetadata\xea\x02\tState::V1b\x06proto3"
var (
file_state_v1_audit_proto_rawDescOnce sync.Once
file_state_v1_audit_proto_rawDescData []byte
)
func file_state_v1_audit_proto_rawDescGZIP() []byte {
file_state_v1_audit_proto_rawDescOnce.Do(func() {
file_state_v1_audit_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_state_v1_audit_proto_rawDesc), len(file_state_v1_audit_proto_rawDesc)))
})
return file_state_v1_audit_proto_rawDescData
}
var file_state_v1_audit_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_state_v1_audit_proto_goTypes = []any{
(*AuditFilter)(nil), // 0: state.v1.AuditFilter
(*ListAuditRequest)(nil), // 1: state.v1.ListAuditRequest
(*AuditField)(nil), // 2: state.v1.AuditField
(*AuditEntry)(nil), // 3: state.v1.AuditEntry
(*ListAuditResponse)(nil), // 4: state.v1.ListAuditResponse
(*GetAuditHeadRequest)(nil), // 5: state.v1.GetAuditHeadRequest
(*GetAuditHeadResponse)(nil), // 6: state.v1.GetAuditHeadResponse
(*timestamppb.Timestamp)(nil), // 7: google.protobuf.Timestamp
}
var file_state_v1_audit_proto_depIdxs = []int32{
7, // 0: state.v1.AuditFilter.from:type_name -> google.protobuf.Timestamp
7, // 1: state.v1.AuditFilter.to:type_name -> google.protobuf.Timestamp
0, // 2: state.v1.ListAuditRequest.filter:type_name -> state.v1.AuditFilter
7, // 3: state.v1.AuditEntry.at:type_name -> google.protobuf.Timestamp
2, // 4: state.v1.AuditEntry.fields:type_name -> state.v1.AuditField
3, // 5: state.v1.ListAuditResponse.entries:type_name -> state.v1.AuditEntry
1, // 6: state.v1.AuditService.ListAudit:input_type -> state.v1.ListAuditRequest
5, // 7: state.v1.AuditService.GetAuditHead:input_type -> state.v1.GetAuditHeadRequest
4, // 8: state.v1.AuditService.ListAudit:output_type -> state.v1.ListAuditResponse
6, // 9: state.v1.AuditService.GetAuditHead:output_type -> state.v1.GetAuditHeadResponse
8, // [8:10] is the sub-list for method output_type
6, // [6:8] is the sub-list for method input_type
6, // [6:6] is the sub-list for extension type_name
6, // [6:6] is the sub-list for extension extendee
0, // [0:6] is the sub-list for field type_name
}
func init() { file_state_v1_audit_proto_init() }
func file_state_v1_audit_proto_init() {
if File_state_v1_audit_proto != nil {
return
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: unsafe.Slice(unsafe.StringData(file_state_v1_audit_proto_rawDesc), len(file_state_v1_audit_proto_rawDesc)),
NumEnums: 0,
NumMessages: 7,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_state_v1_audit_proto_goTypes,
DependencyIndexes: file_state_v1_audit_proto_depIdxs,
MessageInfos: file_state_v1_audit_proto_msgTypes,
}.Build()
File_state_v1_audit_proto = out.File
file_state_v1_audit_proto_goTypes = nil
file_state_v1_audit_proto_depIdxs = nil
}
@@ -0,0 +1,177 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.6.2
// - protoc (unknown)
// source: state/v1/audit.proto
package statev1
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.64.0 or later.
const _ = grpc.SupportPackageIsVersion9
const (
AuditService_ListAudit_FullMethodName = "/state.v1.AuditService/ListAudit"
AuditService_GetAuditHead_FullMethodName = "/state.v1.AuditService/GetAuditHead"
)
// AuditServiceClient is the client API for AuditService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
//
// The instance audit feed: one org-scoped timeline over the settings trail,
// access-policy changes and denials, identity lifecycle events, and runtime
// events. The org and the reader's authority come from the authenticated
// caller; no request names an org.
type AuditServiceClient interface {
// ListAudit returns one page of the caller's org feed, newest first. A
// cursor reused with different filters is refused.
ListAudit(ctx context.Context, in *ListAuditRequest, opts ...grpc.CallOption) (*ListAuditResponse, error)
// GetAuditHead returns a cheap token that changes when any trail grows. It
// carries no rows and no org data; it is the live tail's signal.
GetAuditHead(ctx context.Context, in *GetAuditHeadRequest, opts ...grpc.CallOption) (*GetAuditHeadResponse, error)
}
type auditServiceClient struct {
cc grpc.ClientConnInterface
}
func NewAuditServiceClient(cc grpc.ClientConnInterface) AuditServiceClient {
return &auditServiceClient{cc}
}
func (c *auditServiceClient) ListAudit(ctx context.Context, in *ListAuditRequest, opts ...grpc.CallOption) (*ListAuditResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ListAuditResponse)
err := c.cc.Invoke(ctx, AuditService_ListAudit_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *auditServiceClient) GetAuditHead(ctx context.Context, in *GetAuditHeadRequest, opts ...grpc.CallOption) (*GetAuditHeadResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetAuditHeadResponse)
err := c.cc.Invoke(ctx, AuditService_GetAuditHead_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// AuditServiceServer is the server API for AuditService service.
// All implementations must embed UnimplementedAuditServiceServer
// for forward compatibility.
//
// The instance audit feed: one org-scoped timeline over the settings trail,
// access-policy changes and denials, identity lifecycle events, and runtime
// events. The org and the reader's authority come from the authenticated
// caller; no request names an org.
type AuditServiceServer interface {
// ListAudit returns one page of the caller's org feed, newest first. A
// cursor reused with different filters is refused.
ListAudit(context.Context, *ListAuditRequest) (*ListAuditResponse, error)
// GetAuditHead returns a cheap token that changes when any trail grows. It
// carries no rows and no org data; it is the live tail's signal.
GetAuditHead(context.Context, *GetAuditHeadRequest) (*GetAuditHeadResponse, error)
mustEmbedUnimplementedAuditServiceServer()
}
// UnimplementedAuditServiceServer must be embedded to have
// forward compatible implementations.
//
// NOTE: this should be embedded by value instead of pointer to avoid a nil
// pointer dereference when methods are called.
type UnimplementedAuditServiceServer struct{}
func (UnimplementedAuditServiceServer) ListAudit(context.Context, *ListAuditRequest) (*ListAuditResponse, error) {
return nil, status.Error(codes.Unimplemented, "method ListAudit not implemented")
}
func (UnimplementedAuditServiceServer) GetAuditHead(context.Context, *GetAuditHeadRequest) (*GetAuditHeadResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAuditHead not implemented")
}
func (UnimplementedAuditServiceServer) mustEmbedUnimplementedAuditServiceServer() {}
func (UnimplementedAuditServiceServer) testEmbeddedByValue() {}
// UnsafeAuditServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to AuditServiceServer will
// result in compilation errors.
type UnsafeAuditServiceServer interface {
mustEmbedUnimplementedAuditServiceServer()
}
func RegisterAuditServiceServer(s grpc.ServiceRegistrar, srv AuditServiceServer) {
// If the following call panics, it indicates UnimplementedAuditServiceServer was
// embedded by pointer and is nil. This will cause panics if an
// unimplemented method is ever invoked, so we test this at initialization
// time to prevent it from happening at runtime later due to I/O.
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
t.testEmbeddedByValue()
}
s.RegisterService(&AuditService_ServiceDesc, srv)
}
func _AuditService_ListAudit_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ListAuditRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(AuditServiceServer).ListAudit(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: AuditService_ListAudit_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(AuditServiceServer).ListAudit(ctx, req.(*ListAuditRequest))
}
return interceptor(ctx, in, info, handler)
}
func _AuditService_GetAuditHead_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetAuditHeadRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(AuditServiceServer).GetAuditHead(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: AuditService_GetAuditHead_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(AuditServiceServer).GetAuditHead(ctx, req.(*GetAuditHeadRequest))
}
return interceptor(ctx, in, info, handler)
}
// AuditService_ServiceDesc is the grpc.ServiceDesc for AuditService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var AuditService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "state.v1.AuditService",
HandlerType: (*AuditServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "ListAudit",
Handler: _AuditService_ListAudit_Handler,
},
{
MethodName: "GetAuditHead",
Handler: _AuditService_GetAuditHead_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "state/v1/audit.proto",
}
+180
View File
@@ -0,0 +1,180 @@
// Package audit owns the instance audit feed: the closed vocabularies its
// filters and rows use, the keyset cursor, and the read-only store contract.
// Every durable trail the State Store owns is projected into one timeline by
// the SQL read model; this package never imports infrastructure or app code.
package audit
import (
"context"
"encoding/json"
"strconv"
"time"
)
// Category is a feed row's origin, as the filter and the dashboard group them.
type Category string
const (
CategorySettings Category = "settings"
CategoryAccess Category = "access"
CategoryIdentity Category = "identity"
CategoryRuntime Category = "runtime"
CategoryAutomation Category = "automation"
)
// Severity is how loudly a row asks for attention. It is a closed set: the
// feed's filters validate against it, so a new value is a wire change.
type Severity string
const (
SeverityInfo Severity = "info"
SeverityWarning Severity = "warning"
SeverityError Severity = "error"
)
// Outcome is whether the action the row records succeeded. A denied action is
// its own outcome: it is not a failure of the system.
type Outcome string
const (
OutcomeSuccess Outcome = "success"
OutcomeFailure Outcome = "failure"
OutcomeDenied Outcome = "denied"
)
// Actions recorded by the sys_audit branches. Other sources use their own
// action vocabulary (identity lifecycle events and runtime event kinds), so
// the filter does not close over actions globally.
const (
ActionCreate = "create"
ActionUpdate = "update"
ActionDelete = "delete"
ActionReset = "reset"
ActionDeny = "deny"
)
// Valid reports whether c is one of the feed's categories.
func (c Category) Valid() bool {
switch c {
case CategorySettings, CategoryAccess, CategoryIdentity, CategoryRuntime, CategoryAutomation:
return true
}
return false
}
// Valid reports whether s is one of the feed's severities.
func (s Severity) Valid() bool {
switch s {
case SeverityInfo, SeverityWarning, SeverityError:
return true
}
return false
}
// Valid reports whether o is one of the feed's outcomes.
func (o Outcome) Valid() bool {
switch o {
case OutcomeSuccess, OutcomeFailure, OutcomeDenied:
return true
}
return false
}
// Field is one changed field of a change set. Old and New are JSON values;
// an absent side of the change is JSON null.
type Field struct {
Name string
Old json.RawMessage
New json.RawMessage
}
// Entry is one row of the audit feed: a grouped change set, a lifecycle
// event, an aggregated denial, or a runtime event.
type Entry struct {
// Rank and SourceID place the entry in the feed order. Together with At
// they form the resume cursor and, through ID, the stable record id.
Rank int
SourceID int64
Category Category
At time.Time
Actor string
ActorKind string
PrincipalID string
TargetKind string
TargetID string
Kind string
RecordVersion int64
RequestID string
Source string
Fields []Field
Action string
// Severity and Outcome are derived by the read model, where the filters
// must see them before pagination.
Severity Severity
Outcome Outcome
Detail string
WorkspaceID string
TaskID int64
// Data is the redacted event data, absent for rows with none.
Data json.RawMessage
}
// ID is the entry's stable identity across pages and live refreshes.
func (e Entry) ID() string {
return strconv.Itoa(e.Rank) + ":" + strconv.FormatInt(e.SourceID, 10)
}
// Summary is the entry's one-line description. It is built from metadata
// only and must be called on a redacted entry: Detail can carry recorded
// prose, which the adapter redacts before the entry leaves the State Store.
func (e Entry) Summary() string {
if e.Detail != "" {
return e.Detail
}
if e.Action != "" {
return e.Action + " " + e.Kind
}
return e.Kind
}
// Page is one keyset page of the feed. HasMore means at least one row older
// than the last entry exists; NextCursor resumes from it.
type Page struct {
Entries []Entry
NextCursor string
HasMore bool
}
// Store is the audit feed's read surface. The org is derived from the request
// context, never from the call: a caller cannot name another org's trail.
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 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
}
+83
View File
@@ -0,0 +1,83 @@
package audit
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
)
// The cursor is a fixed-width sort key so a cursor string sorts like the row
// order it names: the at layout, the fixed origin rank, the zero-padded row
// id. The filter hash follows as an opaque suffix and binds the cursor to the
// filter that produced it.
const (
cursorSeparator = "|"
cursorRankWidth = 2
cursorIDWidth = 20
cursorParts = 4
)
// Cursor is a keyset resume point: the last entry a page returned. Only the
// fields are compared by the read model; the encoded string is opaque to
// every client.
type Cursor struct {
At time.Time
Rank int
ID int64
FilterHash string
}
// IsZero reports whether the cursor names no position, which means "newest".
func (c Cursor) IsZero() bool {
return c.At.IsZero() && c.Rank == 0 && c.ID == 0
}
// Encode returns the opaque cursor a response carries.
func (c Cursor) Encode() string {
return c.At.UTC().Format(storecontract.EventCursorLayout) + cursorSeparator +
fmt.Sprintf("%0*d", cursorRankWidth, c.Rank) + cursorSeparator +
fmt.Sprintf("%0*d", cursorIDWidth, c.ID) + cursorSeparator + c.FilterHash
}
// ParseCursor splits an opaque cursor. ok is false for a malformed cursor,
// which means "newest": a bad resume point is not worth failing a feed over.
func ParseCursor(raw string) (Cursor, bool) {
parts := strings.Split(raw, cursorSeparator)
if len(parts) != cursorParts || parts[3] == "" {
return Cursor{}, false
}
at, err := time.Parse(storecontract.EventCursorLayout, parts[0])
if err != nil {
return Cursor{}, false
}
rank, err := strconv.Atoi(parts[1])
if err != nil || rank < 0 {
return Cursor{}, false
}
id, err := strconv.ParseInt(parts[2], 10, 64)
if err != nil || id < 0 {
return Cursor{}, false
}
return Cursor{At: at, Rank: rank, ID: id, FilterHash: parts[3]}, true
}
// MatchesFilter reports whether the cursor was issued for f. A cursor reused
// with other filters is refused rather than silently resuming elsewhere.
func (c Cursor) MatchesFilter(f Filter) bool {
return c.FilterHash == f.Hash()
}
// After reports whether c is newer than other in the feed's descending order
// (at, rank, id), which is the order the cursor resumes.
func (c Cursor) After(other Cursor) bool {
if !c.At.Equal(other.At) {
return c.At.After(other.At)
}
if c.Rank != other.Rank {
return c.Rank > other.Rank
}
return c.ID > other.ID
}
+92
View File
@@ -0,0 +1,92 @@
package audit
import (
"testing"
"time"
)
func TestCursorRoundTrip(t *testing.T) {
t.Parallel()
at := time.Date(2026, 10, 25, 12, 30, 45, 123456789, time.UTC)
cursor := Cursor{At: at, Rank: 3, ID: 9007199254740991, FilterHash: "0123456789abcdef"}
parsed, ok := ParseCursor(cursor.Encode())
if !ok {
t.Fatalf("ParseCursor(%q) = !ok", cursor.Encode())
}
if !parsed.At.Equal(at) || parsed.Rank != cursor.Rank || parsed.ID != cursor.ID || parsed.FilterHash != cursor.FilterHash {
t.Fatalf("round trip = %+v, want %+v", parsed, cursor)
}
if parsed.Encode() != cursor.Encode() {
t.Fatalf("re-encoded cursor = %q, want %q", parsed.Encode(), cursor.Encode())
}
}
func TestCursorOrder(t *testing.T) {
t.Parallel()
at := time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC)
tests := []struct {
name string
newer Cursor
older Cursor
}{
{
name: "later time is newer regardless of rank",
newer: Cursor{At: at.Add(time.Second), Rank: 1, ID: 1},
older: Cursor{At: at, Rank: 4, ID: 99},
},
{
name: "equal time, higher rank is newer",
newer: Cursor{At: at, Rank: 2, ID: 1},
older: Cursor{At: at, Rank: 1, ID: 99},
},
{
name: "equal time and rank, higher id is newer",
newer: Cursor{At: at, Rank: 3, ID: 42},
older: Cursor{At: at, Rank: 3, ID: 41},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if !tt.newer.After(tt.older) {
t.Fatalf("%+v is not after %+v", tt.newer, tt.older)
}
if tt.older.After(tt.newer) {
t.Fatalf("%+v is after %+v", tt.older, tt.newer)
}
})
}
}
func TestCursorMalformedMeansNewest(t *testing.T) {
t.Parallel()
for _, raw := range []string{
"",
"not-a-cursor",
"2026-10-25T12:00:00.000000000Z|01|0000000000000000001",
"2026-10-25T12:00:00.000000000Z|xx|0000000000000000001|0123456789abcdef",
"2026-10-25T12:00:00.000000000Z|01|xx|0123456789abcdef",
"2026-10-25T12:00:00.000000000Z|01|0000000000000000001|",
} {
if _, ok := ParseCursor(raw); ok {
t.Fatalf("ParseCursor(%q) = ok, want malformed", raw)
}
}
}
func TestCursorRefusesAnotherFilter(t *testing.T) {
t.Parallel()
filter := Filter{Categories: []string{"settings"}}
if err := filter.Normalize(); err != nil {
t.Fatal(err)
}
cursor := Cursor{At: time.Now().UTC(), Rank: 1, ID: 7}
cursor.FilterHash = filter.Hash()
if !cursor.MatchesFilter(filter) {
t.Fatal("cursor does not match the filter it was issued for")
}
other := Filter{Categories: []string{"access"}}
if cursor.MatchesFilter(other) {
t.Fatal("cursor matched a different filter")
}
}
+188
View File
@@ -0,0 +1,188 @@
package audit
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"slices"
"strconv"
"strings"
"time"
)
// Page limits. A zero limit means the default; a caller cannot ask for more
// than MaxLimit in one page.
const (
DefaultLimit = 100
MaxLimit = 500
)
// Filter bounds. They keep one request from carrying an unbounded predicate
// into the read model; repeated-value filters are deduplicated first.
const (
maxFilterValues = 10
maxFilterValueLength = 128
maxQueryLength = 200
)
// ErrInvalidFilter reports a filter the read model cannot evaluate.
var ErrInvalidFilter = errors.New("invalid audit filter")
// Filter selects the entries a page returns. Every field is evaluated on the
// server; an empty list means no restriction. From is inclusive and To is
// exclusive, so adjacent windows neither overlap nor skip a row.
type Filter struct {
Categories []string
Actions []string
Kinds []string
Actor string
TargetKinds []string
TargetIDs []string
Severities []string
Outcomes []string
From time.Time
To time.Time
Query string
}
// Normalize validates the closed vocabularies, trims and deduplicates the
// open ones, applies the caps and checks the time range. It sorts every list
// so the filter hash is stable for equal filters.
func (f *Filter) Normalize() error {
categories, err := normalizeClosed(f.Categories, "category", func(value string) bool {
return Category(value).Valid()
})
if err != nil {
return err
}
severities, err := normalizeClosed(f.Severities, "severity", func(value string) bool {
return Severity(value).Valid()
})
if err != nil {
return err
}
outcomes, err := normalizeClosed(f.Outcomes, "outcome", func(value string) bool {
return Outcome(value).Valid()
})
if err != nil {
return err
}
f.Categories = categories
f.Severities = severities
f.Outcomes = outcomes
if f.Actions, err = normalizeOpen(f.Actions, "action"); err != nil {
return err
}
if f.Kinds, err = normalizeOpen(f.Kinds, "kind"); err != nil {
return err
}
if f.TargetKinds, err = normalizeOpen(f.TargetKinds, "target kind"); err != nil {
return err
}
if f.TargetIDs, err = normalizeOpen(f.TargetIDs, "target id"); err != nil {
return err
}
f.Actor = strings.TrimSpace(f.Actor)
if len(f.Actor) > maxFilterValueLength {
return fmt.Errorf("%w: actor is too long", ErrInvalidFilter)
}
f.Query = strings.TrimSpace(f.Query)
if len(f.Query) > maxQueryLength {
return fmt.Errorf("%w: query is too long", ErrInvalidFilter)
}
if !f.From.IsZero() && !f.To.IsZero() && !f.From.Before(f.To) {
return fmt.Errorf("%w: from must be before to", ErrInvalidFilter)
}
return nil
}
// NormalizeLimit resolves a requested page size against the feed's bounds.
func NormalizeLimit(limit int) int {
if limit <= 0 {
return DefaultLimit
}
if limit > MaxLimit {
return MaxLimit
}
return limit
}
// Hash is the fingerprint of the normalized filter a cursor is bound to. A
// cursor replayed with a different filter would resume at a position that no
// longer means the same page, so the reader refuses it.
func (f *Filter) Hash() string {
sum := sha256.New()
writeList(sum, "categories", f.Categories)
writeList(sum, "actions", f.Actions)
writeList(sum, "kinds", f.Kinds)
writeString(sum, "actor", f.Actor)
writeList(sum, "target_kinds", f.TargetKinds)
writeList(sum, "target_ids", f.TargetIDs)
writeList(sum, "severities", f.Severities)
writeList(sum, "outcomes", f.Outcomes)
writeString(sum, "from", formatBound(f.From))
writeString(sum, "to", formatBound(f.To))
writeString(sum, "query", f.Query)
return hex.EncodeToString(sum.Sum(nil))[:16]
}
func normalizeClosed(values []string, name string, valid func(string) bool) ([]string, error) {
normalized, err := normalizeOpen(values, name)
if err != nil {
return nil, err
}
for _, value := range normalized {
if !valid(value) {
return nil, fmt.Errorf("%w: unknown %s %q", ErrInvalidFilter, name, value)
}
}
return normalized, nil
}
// normalizeOpen trims, drops empties, deduplicates and sorts one open list.
func normalizeOpen(values []string, name string) ([]string, error) {
out := make([]string, 0, len(values))
seen := make(map[string]struct{}, len(values))
for _, value := range values {
value = strings.TrimSpace(value)
if value == "" {
continue
}
if len(value) > maxFilterValueLength {
return nil, fmt.Errorf("%w: %s is too long", ErrInvalidFilter, name)
}
if _, ok := seen[value]; ok {
continue
}
seen[value] = struct{}{}
out = append(out, value)
}
if len(out) > maxFilterValues {
return nil, fmt.Errorf("%w: too many %s values", ErrInvalidFilter, name)
}
slices.Sort(out)
return out, nil
}
func formatBound(t time.Time) string {
if t.IsZero() {
return ""
}
return t.UTC().Format(time.RFC3339Nano)
}
func writeList(sum hash.Hash, key string, values []string) {
writeString(sum, key, strconv.Itoa(len(values)))
for _, value := range values {
writeString(sum, "", value)
}
}
func writeString(sum hash.Hash, key, value string) {
_, _ = sum.Write([]byte(key))
_, _ = sum.Write([]byte{0})
_, _ = sum.Write([]byte(value))
_, _ = sum.Write([]byte{1})
}
+123
View File
@@ -0,0 +1,123 @@
package audit
import (
"strings"
"testing"
"time"
)
func TestFilterNormalize(t *testing.T) {
t.Parallel()
from := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
to := from.Add(24 * time.Hour)
tooMany := make([]string, maxFilterValues+1)
for i := range tooMany {
tooMany[i] = string(rune('a' + i))
}
tests := []struct {
name string
filter Filter
wantErr bool
check func(*testing.T, Filter)
}{
{
name: "empty filter keeps no silent window",
filter: Filter{},
check: func(t *testing.T, f Filter) {
if !f.From.IsZero() || !f.To.IsZero() {
t.Fatalf("from/to = %v/%v, want zero", f.From, f.To)
}
},
},
{
name: "closed vocabularies accept their values",
filter: Filter{
Categories: []string{"settings", "access"},
Severities: []string{"warning"},
Outcomes: []string{"denied"},
},
check: func(t *testing.T, f Filter) {
if len(f.Categories) != 2 || f.Severities[0] != "warning" || f.Outcomes[0] != "denied" {
t.Fatalf("normalized filter = %+v", f)
}
},
},
{
name: "unknown category is refused",
filter: Filter{Categories: []string{"billing"}},
wantErr: true,
},
{
name: "unknown severity is refused",
filter: Filter{Severities: []string{"critical"}},
wantErr: true,
},
{
name: "unknown outcome is refused",
filter: Filter{Outcomes: []string{"skipped"}},
wantErr: true,
},
{
name: "from at or after to is refused",
filter: Filter{From: to, To: to},
wantErr: true,
},
{
name: "too many repeated values are refused",
filter: Filter{Kinds: tooMany},
wantErr: true,
},
{
name: "overlong query is refused",
filter: Filter{Query: strings.Repeat("x", maxQueryLength+1)},
wantErr: true,
},
{
name: "repeated values are trimmed, deduplicated and sorted",
filter: Filter{Kinds: []string{" zeta ", "alpha", "alpha", ""}},
check: func(t *testing.T, f Filter) {
if got := strings.Join(f.Kinds, ","); got != "alpha,zeta" {
t.Fatalf("kinds = %q, want alpha,zeta", got)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
filter := tt.filter
err := filter.Normalize()
if tt.wantErr {
if err == nil {
t.Fatal("Normalize() = nil, want an error")
}
return
}
if err != nil {
t.Fatalf("Normalize() = %v", err)
}
if tt.check != nil {
tt.check(t, filter)
}
})
}
}
func TestFilterHashIsStableForEqualFilters(t *testing.T) {
t.Parallel()
a := Filter{Categories: []string{"settings", "access"}, Actor: "sam"}
b := Filter{Categories: []string{"access", "settings"}, Actor: " sam "}
if err := a.Normalize(); err != nil {
t.Fatal(err)
}
if err := b.Normalize(); err != nil {
t.Fatal(err)
}
if a.Hash() != b.Hash() {
t.Fatalf("equal filters hash differently: %s != %s", a.Hash(), b.Hash())
}
b.Actor = "someone-else"
if a.Hash() == b.Hash() {
t.Fatal("a different filter hashes the same")
}
}
+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 = insertIdentityEventInOrg(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
}
@@ -0,0 +1,201 @@
// Audit feed: the read model over every durable trail the State Store owns,
// mapped into internal/domain/audit's row shape. Every entry is redacted here,
// before it crosses the gRPC boundary; undecodable JSON is dropped rather than
// passed through.
package postgres
import (
"cmp"
"context"
"encoding/json"
"fmt"
"slices"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
"github.com/samcharles93/archie-core/internal/webhookguard"
)
var _ audit.Store = (*Store)(nil)
// auditFieldJSON is one field of a change set on the wire: two JSON values,
// either of which is null when that side of the change is absent.
type auditFieldJSON struct {
Old json.RawMessage `json:"old"`
New json.RawMessage `json:"new"`
}
// ListAudit returns one org-scoped page of the feed. The org comes from the
// request context; a request that names no org reads the system org, which is
// the org an unattributed call acts in.
func (s *Store) ListAudit(ctx context.Context, filter audit.Filter, cursor audit.Cursor, limit int) (audit.Page, error) {
if err := filter.Normalize(); err != nil {
return audit.Page{}, err
}
limit = audit.NormalizeLimit(limit)
orgID := org.DefaultOrgID
if scoped, ok := org.Scope(ctx); ok {
orgID = scoped
}
rows, err := s.queries().ListAudit(ctx, postgresdb.ListAuditParams{
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)
}
page := audit.Page{}
if len(rows) > limit {
page.HasMore = true
rows = rows[:limit]
}
page.Entries = make([]audit.Entry, len(rows))
for i := range rows {
page.Entries[i] = auditEntry(rows[i])
}
if page.HasMore {
last := page.Entries[len(page.Entries)-1]
page.NextCursor = audit.Cursor{
At: last.At, Rank: last.Rank, ID: last.SourceID, FilterHash: filter.Hash(),
}.Encode()
}
return page, nil
}
// 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) {
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:%d",
row.SysAuditID, row.IdentityEventID, row.AccessDenialID, row.AccessDenialCount, row.EventID), nil
}
func auditTime(t time.Time) pgtype.Timestamptz {
if t.IsZero() {
return pgtype.Timestamptz{}
}
return pgtype.Timestamptz{Time: t, Valid: true}
}
func auditText(value string) pgtype.Text {
if value == "" {
return pgtype.Text{}
}
return pgtype.Text{String: value, Valid: true}
}
// auditEntry maps one read-model row to the domain entry, redacting every
// free-form part. A field list or data document that cannot be decoded is
// dropped, never forwarded unredacted.
func auditEntry(row postgresdb.ListAuditRow) audit.Entry {
return audit.Entry{
Rank: int(row.SourceRank),
SourceID: row.SourceID,
Category: audit.Category(row.Category),
At: row.At,
Actor: row.Actor,
ActorKind: row.ActorKind,
PrincipalID: row.PrincipalID,
TargetKind: row.TargetKind,
TargetID: row.TargetID,
Kind: row.Kind,
RecordVersion: row.RecordVersion,
RequestID: row.RequestID,
Source: row.Source,
Fields: redactedFields(row.Fields),
Action: row.Action,
Severity: audit.Severity(row.Severity),
Outcome: audit.Outcome(row.Outcome),
Detail: webhookguard.RedactString(row.Detail),
WorkspaceID: row.WorkspaceID,
TaskID: row.TaskID,
Data: redactedData(row.DataText),
}
}
// redactedFields decodes the change set's fields object, redacts each side
// under its own field name, and returns them sorted for a stable view.
func redactedFields(encoded []byte) []audit.Field {
if len(encoded) == 0 {
return nil
}
var raw map[string]auditFieldJSON
if err := json.Unmarshal(encoded, &raw); err != nil {
return nil
}
fields := make([]audit.Field, 0, len(raw))
for name, side := range raw {
fields = append(fields, audit.Field{
Name: name,
Old: redactFieldValue(name, side.Old),
New: redactFieldValue(name, side.New),
})
}
slices.SortFunc(fields, func(a, b audit.Field) int { return cmp.Compare(a.Name, b.Name) })
return fields
}
// redactFieldValue redacts one side of one field. Wrapping it under the field
// name lets the existing redactor apply both its key and value rules, so a
// secret reference survives as a hidden value while the field name stays.
func redactFieldValue(name string, value json.RawMessage) json.RawMessage {
if len(value) == 0 || string(value) == "null" {
return value
}
wrapped, err := json.Marshal(map[string]json.RawMessage{name: value})
if err != nil {
return nil
}
redacted, err := webhookguard.RedactPayload(wrapped)
if err != nil {
return nil
}
var out map[string]json.RawMessage
if err := json.Unmarshal(redacted, &out); err != nil {
return nil
}
return out[name]
}
// redactedData redacts an event's data document, dropping one that cannot be
// decoded. An empty document is absent rather than an empty object.
func redactedData(text string) json.RawMessage {
if text == "" || text == "{}" {
return nil
}
redacted, err := webhookguard.RedactPayload([]byte(text))
if err != nil {
return nil
}
return redacted
}
@@ -0,0 +1,127 @@
package postgres
import (
"encoding/json"
"testing"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
const testJWT = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzYW0ifQ.c2lnbmF0dXJl"
// TestRedactedFields pins the read-time redaction policy: a value under a
// sensitive field name, or one shaped like an unambiguous secret, never
// leaves the adapter; null and absent sides survive; and undecodable JSON
// drops the list instead of passing it through.
func TestRedactedFields(t *testing.T) {
t.Parallel()
tests := []struct {
name string
encoded string
want map[string][2]string
}{
{
name: "secret-reference field is redacted on both sides",
encoded: `{"webhook_secret":{"old":"vault://old","new":"vault://new"}}`,
want: map[string][2]string{"webhook_secret": {`"[redacted]"`, `"[redacted]"`}},
},
{
name: "nested token under a benign field name is redacted",
encoded: `{"settings":{"old":{"bot_token":"abc"},"new":{"bot_token":"def"}}}`,
want: map[string][2]string{"settings": {`{"bot_token":"[redacted]"}`, `{"bot_token":"[redacted]"}`}},
},
{
name: "jwt under a benign field name is redacted by shape",
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"}}`,
want: map[string][2]string{"certificate": {`"[redacted]"`, `"kept"`}},
},
{
name: "whole-document array row is redacted recursively",
encoded: `{"":{"old":[{"name":"visible","password":"hidden"}],"new":null}}`,
want: map[string][2]string{"": {`[{"name":"visible","password":"[redacted]"}]`, `null`}},
},
{
name: "null and absent sides survive",
encoded: `{"new_field":{"old":null,"new":"on"}}`,
want: map[string][2]string{"new_field": {`null`, `"on"`}},
},
{
name: "non-secret values are unchanged",
encoded: `{"enabled":{"old":false,"new":true}}`,
want: map[string][2]string{"enabled": {`false`, `true`}},
},
{
name: "malformed json drops the list",
encoded: `{"enabled":`,
want: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
fields := redactedFields([]byte(tt.encoded))
if tt.want == nil {
if fields != nil {
t.Fatalf("redactedFields() = %+v, want nil", fields)
}
return
}
if len(fields) != len(tt.want) {
t.Fatalf("redactedFields() = %+v, want %d fields", fields, len(tt.want))
}
for _, field := range fields {
want, ok := tt.want[field.Name]
if !ok {
t.Fatalf("unexpected field %q", field.Name)
}
if string(field.Old) != want[0] || string(field.New) != want[1] {
t.Fatalf("field %q = %s/%s, want %s/%s", field.Name, field.Old, field.New, want[0], want[1])
}
}
})
}
}
func TestRedactedData(t *testing.T) {
t.Parallel()
redacted := redactedData(`{"token":"abc","visible":true}`)
var value map[string]any
if err := json.Unmarshal(redacted, &value); err != nil {
t.Fatalf("redactedData() = %q, not JSON: %v", redacted, err)
}
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)
}
if got := redactedData("{}"); got != nil {
t.Fatalf("redactedData(empty object) = %q, want nil", got)
}
}
func TestAuditEntryRedactsDetail(t *testing.T) {
t.Parallel()
entry := auditEntry(postgresdb.ListAuditRow{Detail: "issued " + testJWT})
if entry.Detail != "[redacted]" {
t.Fatalf("entry detail = %q, want the whole value redacted", entry.Detail)
}
}
+50 -8
View File
@@ -8,6 +8,7 @@ import (
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
@@ -23,17 +24,58 @@ 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 {
// insertIdentityEvent writes one identity audit event under the subject's own
// org, not the actor's: an org-sys operator suspending an org A member must
// not file that event under org-sys. fallback is used when the identity has no
// org yet, as on a create that has not been assigned one.
func insertIdentityEvent(ctx context.Context, q *postgresdb.Queries, fallback org.OrgID, id identity.IdentityID, ev identity.Event, audit identity.Audit, at time.Time) error {
orgID, err := auditSubjectOrg(ctx, q, fallback, id)
if err != nil {
return err
}
return writeIdentityEvent(ctx, q, orgID, id, ev, audit, at)
}
// insertIdentityEventInOrg records an event under a target org the caller
// already knows, without resolving the subject's orgs. Account signup uses it:
// the account's new org is the target, whoever else the person already belongs
// to.
func insertIdentityEventInOrg(ctx context.Context, q *postgresdb.Queries, orgID org.OrgID, id identity.IdentityID, ev identity.Event, audit identity.Audit, at time.Time) error {
return writeIdentityEvent(ctx, q, orgID, id, ev, audit, at)
}
// auditSubjectOrg is the one definition of an identity event's org. A subject
// in exactly one org is attributed to it whatever org the actor acts in; a
// subject in two or more is deliberately unattributed (NULL), because picking
// one would hide the event from the other org's feed. An identity with no org
// yet uses fallback, the acting org.
func auditSubjectOrg(ctx context.Context, q *postgresdb.Queries, fallback org.OrgID, id identity.IdentityID) (org.OrgID, error) {
single, err := q.IdentityAuditOrg(ctx, string(id))
if errors.Is(err, pgx.ErrNoRows) {
return fallback, nil
}
if err != nil {
return "", fmt.Errorf("identity audit org: %w", err)
}
return org.OrgID(single), nil
}
// writeIdentityEvent records the event under orgID. An empty org is stored as
// NULL, which the audit feed shows only to instance administrators.
func writeIdentityEvent(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)
}
storedOrg := pgtype.Text{}
if orgID != "" {
storedOrg = pgtype.Text{String: string(orgID), Valid: true}
}
return q.InsertIdentityEvent(ctx, postgresdb.InsertIdentityEventParams{
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,
OrgID: storedOrg,
})
}
@@ -116,7 +158,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 +237,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, "", id, ev, audit, now); err != nil {
return identity.Identity{}, err
}
if err := tx.Commit(ctx); err != nil {
@@ -258,7 +300,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, "", 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 +342,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, "", 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 +394,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, "", id, identity.Event{
IdentityID: id, Type: "unbind_subject", From: cur.Lifecycle, To: cur.Lifecycle, DisplayName: subject.Subject,
}, audit, now)
})
@@ -0,0 +1,50 @@
-- 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.
--
-- Historical rows have no recorded org. A membership row carries only the
-- moment it started (no end), so the memberships in effect at an event are
-- those created at or before it. An event is attributed only when exactly one
-- org qualifies: a person in two or more orgs at the time is deliberately left
-- NULL, because picking one would hide the event from the other org's feed.
-- The agent-assignment fallback follows the same exactly-one rule. A row with
-- no qualifying org stays NULL and is visible only to an instance
-- administrator. The write path resolves an identity's orgs with the same
-- definition, so a new multi-org event is NULL by design too.
-- +goose Up
ALTER TABLE identity_events ADD COLUMN org_id text;
ALTER TABLE identity_events ADD CONSTRAINT identity_events_org_nonempty
CHECK (org_id IS NULL OR org_id <> '');
UPDATE identity_events ie SET org_id = CASE
WHEN (
SELECT count(DISTINCT m.org_id) FROM memberships m
WHERE m.identity_id = ie.identity_id AND m.created_at <= ie.at
) = 1 THEN (
SELECT min(m.org_id) FROM memberships m
WHERE m.identity_id = ie.identity_id AND m.created_at <= ie.at
)
WHEN (
SELECT count(DISTINCT m.org_id) FROM memberships m
WHERE m.identity_id = ie.identity_id AND m.created_at <= ie.at
) = 0
AND (
SELECT count(DISTINCT a.org_id) FROM org_agents a
WHERE a.identity_id = ie.identity_id AND a.created_at <= ie.at
) = 1 THEN (
SELECT min(a.org_id) FROM org_agents a
WHERE a.identity_id = ie.identity_id AND a.created_at <= ie.at
)
ELSE NULL
END;
-- Keep the instance-admin read ordered by time as well: its predicate has no
-- org equality, so the org-leading index alone would not serve it.
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;
ALTER TABLE identity_events DROP CONSTRAINT identity_events_org_nonempty;
ALTER TABLE identity_events DROP COLUMN org_id;
@@ -0,0 +1,577 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: audit_feed.sql
package postgresdb
import (
"context"
"time"
"github.com/jackc/pgx/v5/pgtype"
)
const auditHead = `-- name: AuditHead :one
SELECT
(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 IS NOT NULL AND 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 AuditHeadParams struct {
OrgID string
InstanceAdmin bool
}
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
}
const listAudit = `-- name: ListAudit :many
WITH sys_changes AS (
SELECT
a.org_id,
a.table_name,
a.record_key,
a.request_id,
a.record_version,
a.at,
a.actor,
a.source,
max(a.id)::bigint AS source_id,
jsonb_object_agg(
a.field,
jsonb_build_object(
'old', CASE WHEN a.old_value IS NULL THEN 'null'::jsonb ELSE a.old_value END,
'new', CASE WHEN a.new_value IS NULL THEN 'null'::jsonb ELSE a.new_value END
)
) AS fields,
bool_and(a.old_value IS NULL) AS all_old_null,
bool_and(a.new_value IS NULL) AS all_new_null,
bool_or(a.field = 'reset') AS is_reset,
count(DISTINCT a.field)::int AS field_count,
bool_or(a.field IN (
'org', 'workspace', 'role', 'org_id',
'mapping', 'binding', 'source', 'event_type',
'install', 'remove', 'credential'
)) AS whole_record_field,
string_agg(DISTINCT a.field, ' ') AS field_names
FROM sys_audit a
WHERE a.org_id = $2::text
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::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 (
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 AS org_id,
CASE g.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
1 AS source_rank,
g.source_id,
g.at,
g.actor,
''::text AS actor_kind,
''::text AS principal_id,
CASE g.table_name
WHEN 'resources' THEN 'resource'
ELSE 'policy'
END AS target_kind,
CASE g.table_name
WHEN 'resources' THEN
CASE WHEN position('/' IN g.record_key) > 0
THEN split_part(g.record_key, '/', 2)
ELSE g.record_key
END
ELSE g.record_key
END::text AS target_id,
CASE g.table_name
WHEN 'resources' THEN
CASE WHEN position('/' IN g.record_key) > 0
THEN split_part(g.record_key, '/', 2)
ELSE g.record_key
END
ELSE 'access_policy'
END AS kind,
g.record_version,
g.request_id,
g.source,
g.fields,
CASE
WHEN g.table_name = 'access_policies' AND g.is_reset THEN 'reset'
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.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,
''::text AS detail,
concat_ws(' ', g.record_key, g.actor, g.source, g.field_names,
CASE g.table_name
WHEN 'resources' THEN
CASE WHEN position('/' IN g.record_key) > 0
THEN split_part(g.record_key, '/', 2)
ELSE g.record_key
END
ELSE 'access_policy'
END) AS search_text,
CASE
WHEN g.table_name = 'access_policies'
AND split_part(g.record_key, '/', 1) IN ('workspace', 'object')
THEN split_part(g.record_key, '/', 3)
ELSE ''
END AS workspace_id,
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($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 (
$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
) b1
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
$2::text AS org_id,
'identity'::text AS category,
2 AS source_rank,
ie.id::bigint AS source_id,
ie.at,
ie.actor_id AS actor,
''::text AS actor_kind,
''::text AS principal_id,
'identity'::text AS target_kind,
ie.identity_id::text AS target_id,
ie.event_type AS kind,
0::bigint AS record_version,
ie.request_id,
ie.source,
'{}'::jsonb AS fields,
ie.event_type AS action,
CASE WHEN ie.event_type IN ('suspend', 'retire') THEN 'warning' ELSE 'info' END AS severity,
'success'::text AS outcome,
ie.from_lifecycle || ' → ' || ie.to_lifecycle AS detail,
concat_ws(' ', ie.identity_id, ie.event_type, ie.display_name, ie.actor_id, ie.source) AS search_text,
''::text AS workspace_id,
0::bigint AS task_id,
''::text AS data_text
FROM identity_events ie
WHERE ie.event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND ($17::boolean
OR (ie.org_id IS NOT NULL AND 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($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 (
$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
) b2
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
d.org_id,
'access'::text AS category,
3 AS source_rank,
d.id::bigint AS source_id,
d.minute_at AS at,
d.principal AS actor,
''::text AS actor_kind,
d.principal AS principal_id,
d.resource_kind AS target_kind,
d.resource_id::text AS target_id,
'access_denial'::text AS kind,
0::bigint AS record_version,
''::text AS request_id,
'access'::text AS source,
'{}'::jsonb AS fields,
'deny'::text AS action,
'warning'::text AS severity,
'denied'::text AS outcome,
CASE WHEN cardinality(d.policies) > 0 THEN array_to_string(d.policies, ', ') ELSE '' END AS detail,
concat_ws(' ', d.principal, d.action, d.resource_kind, d.resource_id, d.level, array_to_string(d.policies, ' ')) AS search_text,
''::text AS workspace_id,
0::bigint AS task_id,
jsonb_build_object('count', d.count, 'policies', d.policies)::text AS data_text
FROM access_denials d
WHERE d.org_id = $2::text
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($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 (
$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
) b3
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
e.org_id,
CASE
WHEN e.kind = ANY(ARRAY[
'binding_changed', 'binding_started', 'mapping_changed',
'curator_run', 'curator_action', 'curator_error',
'job_run', 'job_error',
'workflow_call_started', 'workflow_call_finished',
'turn_completed', 'work_request_submitted'
]::text[]) THEN 'automation'
ELSE 'runtime'
END AS category,
4 AS source_rank,
e.id::bigint AS source_id,
e.at,
e.actor_id AS actor,
e.actor_kind,
e.principal_id,
CASE WHEN e.task_id > 0 THEN 'task' ELSE '' END AS target_kind,
(CASE WHEN e.task_id > 0 THEN e.task_id::text ELSE '' END)::text AS target_id,
e.kind,
0::bigint AS record_version,
''::text AS request_id,
''::text AS source,
'{}'::jsonb AS fields,
e.kind AS action,
CASE
WHEN e.kind = ANY(ARRAY[
'task_dead', 'pr_rejected',
'operator_rejected', 'agent_rejected', 'task_rejected',
'task_abandoned', 'task_withdrawn', 'step_retried',
'curator_error', 'job_error', 'binding_dispatch_failure'
]::text[]) THEN 'error'
WHEN e.kind = ANY(ARRAY[
'parked', 'task_retried', 'task_cancelled', 'task_stopped',
'alert', 'update_report'
]::text[]) THEN 'warning'
ELSE 'info'
END AS severity,
CASE
WHEN e.kind = ANY(ARRAY[
'task_dead', 'pr_rejected',
'operator_rejected', 'agent_rejected', 'task_rejected',
'task_abandoned', 'task_withdrawn', 'step_retried',
'curator_error', 'job_error', 'binding_dispatch_failure'
]::text[]) THEN 'failure'
ELSE 'success'
END AS outcome,
e.detail,
concat_ws(' ', e.kind, e.actor_id, e.actor_kind, e.repo, e.workflow, e.stage, e.task_id::text) AS search_text,
e.workspace_id,
e.task_id,
e.data AS data_text
FROM events e
WHERE e.org_id = $2::text
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($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 (
$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.field_count = 1 AND g.whole_record_field AND g.all_old_null THEN 'create'
WHEN g.field_count = 1 AND g.whole_record_field AND g.all_new_null THEN 'delete'
ELSE 'update'
END AS action,
CASE
WHEN g.field_count = 1 AND g.whole_record_field 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
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 {
OrgID string
Category string
SourceRank int32
SourceID int64
At time.Time
Actor string
ActorKind string
PrincipalID string
TargetKind string
TargetID string
Kind string
RecordVersion int64
RequestID string
Source string
Fields []byte
Action string
Severity string
Outcome string
Detail string
SearchText string
WorkspaceID string
TaskID int64
DataText string
}
// The audit feed read model: one org-scoped timeline over every durable
// trail the State Store owns. It is a parameterised UNION ALL, not a view,
// because the filters and the keyset cursor must reach each source before
// that source's ORDER BY ... LIMIT; otherwise a page of the merged feed
// groups or scans the whole trail first.
//
// 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 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. A historical row
// with no defensible org stays NULL and is visible only to that authority.
//
// Coverage-table change sets are classified by their whole-record field, not
// by a version: a create is a whole-record field with a null old side, a
// delete the same field with a null new side, and everything else an update.
// A partial add or remove is never a create or delete.
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,
arg.Actions,
arg.Kinds,
arg.TargetKinds,
arg.TargetIds,
arg.Severities,
arg.Outcomes,
arg.Q,
arg.CursorRank,
arg.CursorID,
arg.InstanceAdmin,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListAuditRow
for rows.Next() {
var i ListAuditRow
if err := rows.Scan(
&i.OrgID,
&i.Category,
&i.SourceRank,
&i.SourceID,
&i.At,
&i.Actor,
&i.ActorKind,
&i.PrincipalID,
&i.TargetKind,
&i.TargetID,
&i.Kind,
&i.RecordVersion,
&i.RequestID,
&i.Source,
&i.Fields,
&i.Action,
&i.Severity,
&i.Outcome,
&i.Detail,
&i.SearchText,
&i.WorkspaceID,
&i.TaskID,
&i.DataText,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
@@ -104,6 +104,26 @@ func (q *Queries) GetVisibleIdentity(ctx context.Context, arg GetVisibleIdentity
return i, err
}
const identityAuditOrg = `-- name: IdentityAuditOrg :one
SELECT min(org_id)::text AS org_id
FROM (
SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
UNION
SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1
) belonging
HAVING count(DISTINCT org_id) = 1
`
// The identity's org only when it belongs to exactly one, across memberships
// and agent assignments; no row otherwise. One statement, so a concurrent
// membership add cannot make the count and the org disagree.
func (q *Queries) IdentityAuditOrg(ctx context.Context, identityID string) (string, error) {
row := q.db.QueryRow(ctx, identityAuditOrg, identityID)
var org_id string
err := row.Scan(&org_id)
return org_id, err
}
const insertIdentity = `-- name: InsertIdentity :exec
INSERT INTO identities (id, kind, display_name, lifecycle, version, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
@@ -161,8 +181,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 +195,7 @@ type InsertIdentityEventParams struct {
Source string
RequestID string
At time.Time
OrgID pgtype.Text
}
func (q *Queries) InsertIdentityEvent(ctx context.Context, arg InsertIdentityEventParams) error {
@@ -188,6 +209,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 pgtype.Text
}
type IdentitySubject struct {
@@ -0,0 +1,426 @@
-- The audit feed read model: one org-scoped timeline over every durable
-- trail the State Store owns. It is a parameterised UNION ALL, not a view,
-- because the filters and the keyset cursor must reach each source before
-- that source's ORDER BY ... LIMIT; otherwise a page of the merged feed
-- groups or scans the whole trail first.
--
-- 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 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. A historical row
-- with no defensible org stays NULL and is visible only to that authority.
--
-- Coverage-table change sets are classified by their whole-record field, not
-- by a version: a create is a whole-record field with a null old side, a
-- delete the same field with a null new side, and everything else an update.
-- A partial add or remove is never a create or delete.
-- name: ListAudit :many
WITH sys_changes AS (
SELECT
a.org_id,
a.table_name,
a.record_key,
a.request_id,
a.record_version,
a.at,
a.actor,
a.source,
max(a.id)::bigint AS source_id,
jsonb_object_agg(
a.field,
jsonb_build_object(
'old', CASE WHEN a.old_value IS NULL THEN 'null'::jsonb ELSE a.old_value END,
'new', CASE WHEN a.new_value IS NULL THEN 'null'::jsonb ELSE a.new_value END
)
) AS fields,
bool_and(a.old_value IS NULL) AS all_old_null,
bool_and(a.new_value IS NULL) AS all_new_null,
bool_or(a.field = 'reset') AS is_reset,
count(DISTINCT a.field)::int AS field_count,
bool_or(a.field IN (
'org', 'workspace', 'role', 'org_id',
'mapping', 'binding', 'source', 'event_type',
'install', 'remove', 'credential'
)) AS whole_record_field,
string_agg(DISTINCT a.field, ' ') AS field_names
FROM sys_audit a
WHERE a.org_id = sqlc.arg(org_id)::text
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)
GROUP BY a.org_id, a.table_name, a.record_key, a.request_id, a.record_version, a.at, a.actor, a.source
)
SELECT * FROM (
SELECT * FROM (
SELECT
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,
g.at,
g.actor,
''::text AS actor_kind,
''::text AS principal_id,
CASE g.table_name
WHEN 'resources' THEN 'resource'
ELSE 'policy'
END AS target_kind,
CASE g.table_name
WHEN 'resources' THEN
CASE WHEN position('/' IN g.record_key) > 0
THEN split_part(g.record_key, '/', 2)
ELSE g.record_key
END
ELSE g.record_key
END::text AS target_id,
CASE g.table_name
WHEN 'resources' THEN
CASE WHEN position('/' IN g.record_key) > 0
THEN split_part(g.record_key, '/', 2)
ELSE g.record_key
END
ELSE 'access_policy'
END AS kind,
g.record_version,
g.request_id,
g.source,
g.fields,
CASE
WHEN g.table_name = 'access_policies' AND g.is_reset THEN 'reset'
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.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,
''::text AS detail,
concat_ws(' ', g.record_key, g.actor, g.source, g.field_names,
CASE g.table_name
WHEN 'resources' THEN
CASE WHEN position('/' IN g.record_key) > 0
THEN split_part(g.record_key, '/', 2)
ELSE g.record_key
END
ELSE 'access_policy'
END) AS search_text,
CASE
WHEN g.table_name = 'access_policies'
AND split_part(g.record_key, '/', 1) IN ('workspace', 'object')
THEN split_part(g.record_key, '/', 3)
ELSE ''
END AS workspace_id,
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[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR s.kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR s.actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR s.target_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR s.target_id = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR s.severity = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR s.outcome = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(s.search_text)) > 0)
AND (
sqlc.narg(cursor_at)::timestamptz IS NULL
OR s.at < sqlc.narg(cursor_at)::timestamptz
OR (s.at = sqlc.narg(cursor_at)::timestamptz AND s.source_rank < sqlc.arg(cursor_rank)::int)
OR (s.at = sqlc.narg(cursor_at)::timestamptz AND s.source_rank = sqlc.arg(cursor_rank)::int AND s.source_id < sqlc.arg(cursor_id)::bigint)
)
ORDER BY s.at DESC, s.source_rank DESC, s.source_id DESC
LIMIT sqlc.arg(page_limit)::bigint
) b1
UNION ALL
SELECT * FROM (
SELECT * FROM (
SELECT
sqlc.arg(org_id)::text AS org_id,
'identity'::text AS category,
2 AS source_rank,
ie.id::bigint AS source_id,
ie.at,
ie.actor_id AS actor,
''::text AS actor_kind,
''::text AS principal_id,
'identity'::text AS target_kind,
ie.identity_id::text AS target_id,
ie.event_type AS kind,
0::bigint AS record_version,
ie.request_id,
ie.source,
'{}'::jsonb AS fields,
ie.event_type AS action,
CASE WHEN ie.event_type IN ('suspend', 'retire') THEN 'warning' ELSE 'info' END AS severity,
'success'::text AS outcome,
ie.from_lifecycle || ' → ' || ie.to_lifecycle AS detail,
concat_ws(' ', ie.identity_id, ie.event_type, ie.display_name, ie.actor_id, ie.source) AS search_text,
''::text AS workspace_id,
0::bigint AS task_id,
''::text AS data_text
FROM identity_events ie
WHERE ie.event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND (sqlc.arg(instance_admin)::boolean
OR (ie.org_id IS NOT NULL AND 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
WHERE (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR si.category = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR si.action = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR si.kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR si.actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR si.target_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR si.target_id = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR si.severity = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR si.outcome = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(si.search_text)) > 0)
AND (
sqlc.narg(cursor_at)::timestamptz IS NULL
OR si.at < sqlc.narg(cursor_at)::timestamptz
OR (si.at = sqlc.narg(cursor_at)::timestamptz AND si.source_rank < sqlc.arg(cursor_rank)::int)
OR (si.at = sqlc.narg(cursor_at)::timestamptz AND si.source_rank = sqlc.arg(cursor_rank)::int AND si.source_id < sqlc.arg(cursor_id)::bigint)
)
ORDER BY si.at DESC, si.source_rank DESC, si.source_id DESC
LIMIT sqlc.arg(page_limit)::bigint
) b2
UNION ALL
SELECT * FROM (
SELECT * FROM (
SELECT
d.org_id,
'access'::text AS category,
3 AS source_rank,
d.id::bigint AS source_id,
d.minute_at AS at,
d.principal AS actor,
''::text AS actor_kind,
d.principal AS principal_id,
d.resource_kind AS target_kind,
d.resource_id::text AS target_id,
'access_denial'::text AS kind,
0::bigint AS record_version,
''::text AS request_id,
'access'::text AS source,
'{}'::jsonb AS fields,
'deny'::text AS action,
'warning'::text AS severity,
'denied'::text AS outcome,
CASE WHEN cardinality(d.policies) > 0 THEN array_to_string(d.policies, ', ') ELSE '' END AS detail,
concat_ws(' ', d.principal, d.action, d.resource_kind, d.resource_id, d.level, array_to_string(d.policies, ' ')) AS search_text,
''::text AS workspace_id,
0::bigint AS task_id,
jsonb_build_object('count', d.count, 'policies', d.policies)::text AS data_text
FROM access_denials d
WHERE d.org_id = sqlc.arg(org_id)::text
AND (sqlc.narg(from_at)::timestamptz IS NULL OR d.minute_at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR d.minute_at < sqlc.narg(to_at)::timestamptz)
) sd
WHERE (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR sd.category = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR sd.action = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR sd.kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR sd.actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR sd.target_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR sd.target_id = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR sd.severity = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR sd.outcome = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(sd.search_text)) > 0)
AND (
sqlc.narg(cursor_at)::timestamptz IS NULL
OR sd.at < sqlc.narg(cursor_at)::timestamptz
OR (sd.at = sqlc.narg(cursor_at)::timestamptz AND sd.source_rank < sqlc.arg(cursor_rank)::int)
OR (sd.at = sqlc.narg(cursor_at)::timestamptz AND sd.source_rank = sqlc.arg(cursor_rank)::int AND sd.source_id < sqlc.arg(cursor_id)::bigint)
)
ORDER BY sd.at DESC, sd.source_rank DESC, sd.source_id DESC
LIMIT sqlc.arg(page_limit)::bigint
) b3
UNION ALL
SELECT * FROM (
SELECT * FROM (
SELECT
e.org_id,
CASE
WHEN e.kind = ANY(ARRAY[
'binding_changed', 'binding_started', 'mapping_changed',
'curator_run', 'curator_action', 'curator_error',
'job_run', 'job_error',
'workflow_call_started', 'workflow_call_finished',
'turn_completed', 'work_request_submitted'
]::text[]) THEN 'automation'
ELSE 'runtime'
END AS category,
4 AS source_rank,
e.id::bigint AS source_id,
e.at,
e.actor_id AS actor,
e.actor_kind,
e.principal_id,
CASE WHEN e.task_id > 0 THEN 'task' ELSE '' END AS target_kind,
(CASE WHEN e.task_id > 0 THEN e.task_id::text ELSE '' END)::text AS target_id,
e.kind,
0::bigint AS record_version,
''::text AS request_id,
''::text AS source,
'{}'::jsonb AS fields,
e.kind AS action,
CASE
WHEN e.kind = ANY(ARRAY[
'task_dead', 'pr_rejected',
'operator_rejected', 'agent_rejected', 'task_rejected',
'task_abandoned', 'task_withdrawn', 'step_retried',
'curator_error', 'job_error', 'binding_dispatch_failure'
]::text[]) THEN 'error'
WHEN e.kind = ANY(ARRAY[
'parked', 'task_retried', 'task_cancelled', 'task_stopped',
'alert', 'update_report'
]::text[]) THEN 'warning'
ELSE 'info'
END AS severity,
CASE
WHEN e.kind = ANY(ARRAY[
'task_dead', 'pr_rejected',
'operator_rejected', 'agent_rejected', 'task_rejected',
'task_abandoned', 'task_withdrawn', 'step_retried',
'curator_error', 'job_error', 'binding_dispatch_failure'
]::text[]) THEN 'failure'
ELSE 'success'
END AS outcome,
e.detail,
concat_ws(' ', e.kind, e.actor_id, e.actor_kind, e.repo, e.workflow, e.stage, e.task_id::text) AS search_text,
e.workspace_id,
e.task_id,
e.data AS data_text
FROM events e
WHERE e.org_id = sqlc.arg(org_id)::text
AND (sqlc.narg(from_at)::timestamptz IS NULL OR e.at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR e.at < sqlc.narg(to_at)::timestamptz)
) se
WHERE (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR se.category = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR se.action = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR se.kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR se.actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR se.target_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR se.target_id = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR se.severity = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR se.outcome = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(se.search_text)) > 0)
AND (
sqlc.narg(cursor_at)::timestamptz IS NULL
OR se.at < sqlc.narg(cursor_at)::timestamptz
OR (se.at = sqlc.narg(cursor_at)::timestamptz AND se.source_rank < sqlc.arg(cursor_rank)::int)
OR (se.at = sqlc.narg(cursor_at)::timestamptz AND se.source_rank = sqlc.arg(cursor_rank)::int AND se.source_id < sqlc.arg(cursor_id)::bigint)
)
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.field_count = 1 AND g.whole_record_field AND g.all_old_null THEN 'create'
WHEN g.field_count = 1 AND g.whole_record_field AND g.all_new_null THEN 'delete'
ELSE 'update'
END AS action,
CASE
WHEN g.field_count = 1 AND g.whole_record_field 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 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 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 IS NOT NULL AND 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,20 @@ 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: IdentityAuditOrg :one
-- The identity's org only when it belongs to exactly one, across memberships
-- and agent assignments; no row otherwise. One statement, so a concurrent
-- membership add cannot make the count and the org disagree.
SELECT min(org_id)::text AS org_id
FROM (
SELECT m.org_id FROM memberships m WHERE m.identity_id = $1
UNION
SELECT a.org_id FROM org_agents a WHERE a.identity_id = $1
) belonging
HAVING count(DISTINCT org_id) = 1;
-- name: UpdateIdentity :execrows
UPDATE identities SET display_name = $2, lifecycle = $3, version = $4, updated_at = $5
+205
View File
@@ -0,0 +1,205 @@
// Audit: the org-scoped feed RPCs. The State Store derives the org from the
// authenticated caller's context; no request field names one.
package staterpc
import (
"context"
"strconv"
"strings"
"google.golang.org/grpc/codes"
"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
}
func auditFilterValue(f *pb.AuditFilter) audit.Filter {
filter := audit.Filter{
Categories: f.GetCategories(),
Actions: f.GetActions(),
Kinds: f.GetKinds(),
Actor: f.GetActor(),
TargetKinds: f.GetTargetKinds(),
TargetIDs: f.GetTargetIds(),
Severities: f.GetSeverities(),
Outcomes: f.GetOutcomes(),
Query: f.GetQ(),
}
if from := f.GetFrom(); from != nil {
filter.From = from.AsTime()
}
if to := f.GetTo(); to != nil {
filter.To = to.AsTime()
}
return filter
}
func auditFilterProto(f audit.Filter) *pb.AuditFilter {
out := &pb.AuditFilter{
Categories: f.Categories,
Actions: f.Actions,
Kinds: f.Kinds,
Actor: f.Actor,
TargetKinds: f.TargetKinds,
TargetIds: f.TargetIDs,
Severities: f.Severities,
Outcomes: f.Outcomes,
Q: f.Query,
}
if !f.From.IsZero() {
out.From = timestamp(f.From)
}
if !f.To.IsZero() {
out.To = timestamp(f.To)
}
return out
}
func auditEntryProto(e audit.Entry) *pb.AuditEntry {
fields := make([]*pb.AuditField, len(e.Fields))
for i, field := range e.Fields {
fields[i] = &pb.AuditField{
Field: field.Name,
OldValueJson: string(field.Old),
NewValueJson: string(field.New),
}
}
return &pb.AuditEntry{
Id: e.ID(),
Category: string(e.Category),
At: timestamp(e.At),
Actor: e.Actor,
ActorKind: e.ActorKind,
PrincipalId: e.PrincipalID,
TargetKind: e.TargetKind,
TargetId: e.TargetID,
Kind: e.Kind,
RecordVersion: e.RecordVersion,
RequestId: e.RequestID,
Source: e.Source,
Fields: fields,
Action: e.Action,
Severity: string(e.Severity),
Outcome: string(e.Outcome),
Detail: e.Detail,
WorkspaceId: e.WorkspaceID,
TaskId: e.TaskID,
DataJson: string(e.Data),
}
}
func auditEntryValue(e *pb.AuditEntry) audit.Entry {
fields := make([]audit.Field, len(e.GetFields()))
for i, field := range e.GetFields() {
fields[i] = audit.Field{
Name: field.GetField(),
Old: []byte(field.GetOldValueJson()),
New: []byte(field.GetNewValueJson()),
}
}
entry := audit.Entry{
Category: audit.Category(e.GetCategory()),
At: timeValue(e.GetAt()),
Actor: e.GetActor(),
ActorKind: e.GetActorKind(),
PrincipalID: e.GetPrincipalId(),
TargetKind: e.GetTargetKind(),
TargetID: e.GetTargetId(),
Kind: e.GetKind(),
RecordVersion: e.GetRecordVersion(),
RequestID: e.GetRequestId(),
Source: e.GetSource(),
Fields: fields,
Action: e.GetAction(),
Severity: audit.Severity(e.GetSeverity()),
Outcome: audit.Outcome(e.GetOutcome()),
Detail: e.GetDetail(),
WorkspaceID: e.GetWorkspaceId(),
TaskID: e.GetTaskId(),
}
if data := e.GetDataJson(); data != "" {
entry.Data = []byte(data)
}
entry.Rank, entry.SourceID = auditRankAndID(e.GetId())
return entry
}
// auditRankAndID restores the feed position a remote entry was read at, so
// the entry's stable id survives the wire unchanged.
func auditRankAndID(id string) (int, int64) {
rankText, idText, ok := strings.Cut(id, ":")
if !ok {
return 0, 0
}
rank, err := strconv.Atoi(rankText)
if err != nil {
return 0, 0
}
sourceID, err := strconv.ParseInt(idText, 10, 64)
if err != nil {
return 0, 0
}
return rank, sourceID
}
func (s *auditServer) ListAudit(ctx context.Context, request *pb.ListAuditRequest) (*pb.ListAuditResponse, error) {
if s.deps.Audit == nil {
return nil, errAuditUnavailable
}
filter := auditFilterValue(request.GetFilter())
if err := filter.Normalize(); err != nil {
return nil, validationFailure(err)
}
cursor := audit.Cursor{}
if raw := strings.TrimSpace(request.GetCursor()); raw != "" {
// A malformed cursor means newest; a well-formed one reused with
// other filters is refused rather than resuming somewhere else.
if parsed, ok := audit.ParseCursor(raw); ok {
if !parsed.MatchesFilter(filter) {
return nil, status.Error(codes.InvalidArgument, "cursor does not match the requested filter")
}
cursor = parsed
}
}
page, err := s.deps.Audit.ListAudit(withInstanceAdmin(ctx), filter, cursor, int(request.GetLimit()))
if err != nil {
return nil, s.logErr("ListAudit", err)
}
entries := make([]*pb.AuditEntry, len(page.Entries))
for i := range page.Entries {
entries[i] = auditEntryProto(page.Entries[i])
}
return &pb.ListAuditResponse{
Entries: entries,
NextCursor: page.NextCursor,
HasMore: page.HasMore,
}, nil
}
func (s *auditServer) GetAuditHead(ctx context.Context, _ *pb.GetAuditHeadRequest) (*pb.GetAuditHeadResponse, error) {
if s.deps.Audit == nil {
return nil, errAuditUnavailable
}
token, err := s.deps.Audit.AuditHead(withInstanceAdmin(ctx))
if err != nil {
return nil, s.logErr("GetAuditHead", err)
}
return &pb.GetAuditHeadResponse{Token: token}, nil
}
@@ -0,0 +1,39 @@
// Audit: the client half of the org-scoped feed RPCs.
package staterpc
import (
"context"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/audit"
)
var _ audit.Store = (*Client)(nil)
func (c *Client) ListAudit(ctx context.Context, filter audit.Filter, cursor audit.Cursor, limit int) (audit.Page, error) {
rawCursor := ""
if !cursor.IsZero() {
rawCursor = cursor.Encode()
}
response, err := c.audit.ListAudit(ctx, &pb.ListAuditRequest{
Filter: auditFilterProto(filter),
Cursor: rawCursor,
Limit: int32(limit),
})
if err != nil {
return audit.Page{}, unmapError(err)
}
return audit.Page{
Entries: mapValues(response.GetEntries(), auditEntryValue),
NextCursor: response.GetNextCursor(),
HasMore: response.GetHasMore(),
}, nil
}
func (c *Client) AuditHead(ctx context.Context) (string, error) {
response, err := c.audit.GetAuditHead(ctx, &pb.GetAuditHeadRequest{})
if err != nil {
return "", unmapError(err)
}
return response.GetToken(), nil
}
@@ -39,6 +39,7 @@ type Client struct {
access pb.AccessServiceClient
org pb.OrgServiceClient
alert pb.AlertServiceClient
audit pb.AuditServiceClient
controlPlane controlpb.ControlPlaneServiceClient
}
@@ -67,6 +68,7 @@ func NewClient(conn grpc.ClientConnInterface) *Client {
access: pb.NewAccessServiceClient(conn),
org: pb.NewOrgServiceClient(conn),
alert: pb.NewAlertServiceClient(conn),
audit: pb.NewAuditServiceClient(conn),
controlPlane: controlpb.NewControlPlaneServiceClient(conn),
}
}
@@ -18,6 +18,7 @@ import (
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/alert"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
@@ -143,6 +144,10 @@ type Deps struct {
// Alerts is the operator alert store. Optional: nil answers the alert
// RPCs with codes.Unavailable.
Alerts alert.Store
// Audit is the org-scoped audit feed. Optional: nil answers both RPCs
// with codes.Unavailable. It is deliberately not task-scoped: a task
// grant must never reach it.
Audit audit.Store
// Orgs persists orgs, workspaces, memberships and agent assignments.
// Optional: nil answers the org RPCs with codes.Unavailable.
Orgs org.Repository
@@ -189,6 +194,7 @@ func RegisterServer(registrar grpc.ServiceRegistrar, deps Deps) {
pb.RegisterAccessServiceServer(registrar, &accessServer{server: s})
pb.RegisterOrgServiceServer(registrar, &orgServer{server: s})
pb.RegisterAlertServiceServer(registrar, &alertServer{server: s})
pb.RegisterAuditServiceServer(registrar, &auditServer{server: s})
if deps.ControlPlane != nil {
controlpb.RegisterControlPlaneServiceServer(registrar, deps.ControlPlane)
}
+18 -7
View File
@@ -20,9 +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_-]+$`)
// 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.
@@ -41,6 +42,16 @@ func RedactPayload(payload []byte) ([]byte, error) {
return encoded, nil
}
// RedactString replaces a free-text value that has an unambiguous secret's
// 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) {
return redactedValue
}
return value
}
func redactValue(value any) any {
switch typed := value.(type) {
case map[string]any:
@@ -67,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
+4
View File
@@ -14,6 +14,7 @@ import (
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/audit"
"github.com/samcharles93/archie-core/internal/domain/health"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
@@ -115,6 +116,9 @@ type Server struct {
// Policies is the stored access policy chain, behind the policies page.
// Nil answers 503.
Policies access.PolicyStore
// Audit is the org-scoped audit feed, behind the audit page and the live
// tail. Nil answers 503 on every audit route.
Audit audit.Store
// now is the clock applyState reads staleness against. Nil means
// time.Now; tests set it to pin a record's age.
now func() time.Time
+92
View File
@@ -0,0 +1,92 @@
syntax = "proto3";
package state.v1;
import "google/protobuf/timestamp.proto";
// The instance audit feed: one org-scoped timeline over the settings trail,
// access-policy changes and denials, identity lifecycle events, and runtime
// events. The org and the reader's authority come from the authenticated
// caller; no request names an org.
service AuditService {
// ListAudit returns one page of the caller's org feed, newest first. A
// cursor reused with different filters is refused.
rpc ListAudit(ListAuditRequest) returns (ListAuditResponse);
// GetAuditHead returns a cheap token that changes when any trail grows. It
// carries no rows and no org data; it is the live tail's signal.
rpc GetAuditHead(GetAuditHeadRequest) returns (GetAuditHeadResponse);
}
// AuditFilter selects the entries a page returns. An empty list places no
// restriction; categories, severities and outcomes are closed vocabularies
// owned by internal/domain/audit.
message AuditFilter {
repeated string categories = 1;
repeated string actions = 2;
repeated string kinds = 3;
string actor = 4;
repeated string target_kinds = 5;
repeated string target_ids = 6;
repeated string severities = 7;
repeated string outcomes = 8;
// from is inclusive and to is exclusive, both in UTC.
google.protobuf.Timestamp from = 9;
google.protobuf.Timestamp to = 10;
// q is a substring match over metadata only: record key, kind, actor,
// source, field names, task/workflow/repo/stage. Never a value.
string q = 11;
}
message ListAuditRequest {
AuditFilter filter = 1;
// cursor is opaque; a malformed one means newest.
string cursor = 2;
// limit is the page size. Zero means 100; the server caps it at 500.
int32 limit = 3;
}
// AuditField is one changed field of a change set. old_value_json and
// new_value_json are JSON values; an absent side is the JSON literal null.
message AuditField {
string field = 1;
string old_value_json = 2;
string new_value_json = 3;
}
// AuditEntry is one row of the feed: a grouped change set, a lifecycle event,
// an aggregated denial, or a runtime event. Free-form values are redacted
// before they reach this message.
message AuditEntry {
// id is stable across pages and live refreshes.
string id = 1;
string category = 2;
google.protobuf.Timestamp at = 3;
string actor = 4;
string actor_kind = 5;
string principal_id = 6;
string target_kind = 7;
string target_id = 8;
string kind = 9;
int64 record_version = 10;
string request_id = 11;
string source = 12;
repeated AuditField fields = 13;
string action = 14;
string severity = 15;
string outcome = 16;
string detail = 17;
string workspace_id = 18;
int64 task_id = 19;
// data_json is the redacted event data, empty when the row has none.
string data_json = 20;
}
message ListAuditResponse {
repeated AuditEntry entries = 1;
string next_cursor = 2;
bool has_more = 3;
}
message GetAuditHeadRequest {}
message GetAuditHeadResponse { string token = 1; }