From 717f9d5219d418d5719c1db22b1ef35e5437fec9 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 25 Oct 2026 21:49:22 +1100 Subject: [PATCH] feat(webui): audit feed API, export and read_audit grant --- docs/security/authorization.md | 4 +- internal/app/controlplane/audit_test.go | 61 -- internal/app/controlplane/controlplane.go | 45 -- internal/app/controlplane/org_models_test.go | 4 - .../controlplane/v1/controlplane.pb.go | 337 ++------- .../controlplane/v1/controlplane_grpc.pb.go | 38 - internal/domain/access/access.go | 6 + internal/domain/audit/audit.go | 33 + .../domain/storecontract/storecontract.go | 21 - internal/infrastructure/access/engine.go | 1 + internal/infrastructure/access/engine_test.go | 29 + .../postgres/postgresdb/audit.sql.go | 60 -- .../infrastructure/postgres/queries/audit.sql | 8 - internal/infrastructure/postgres/resources.go | 24 - internal/webui/api_audit.go | 669 +++++++++++++++++- internal/webui/api_audit_test.go | 90 +++ internal/webui/api_control_plane.go | 2 + internal/webui/authorization_surfaces_test.go | 46 +- internal/webui/authorize.go | 5 + internal/webui/server.go | 10 +- internal/webui/sse.go | 58 +- proto/buf.yaml | 12 + proto/controlplane/v1/controlplane.proto | 32 +- 23 files changed, 969 insertions(+), 626 deletions(-) delete mode 100644 internal/app/controlplane/audit_test.go create mode 100644 internal/webui/api_audit_test.go diff --git a/docs/security/authorization.md b/docs/security/authorization.md index 224b2ec1c..9e7fe81fc 100644 --- a/docs/security/authorization.md +++ b/docs/security/authorization.md @@ -80,6 +80,7 @@ accounts by unverified email, or create an administrator. | Workflow/task actions: `webui/api_tasks.go`, `domain/taskactions`, `staterpc` | Policy and stored workspace before action; domain transition, authority and durable dispatch guards remain in force. | HTTP deny matrix, task action/domain tests, task grant tests | | Incoming webhooks: `forge/webhook`, `captureintake`, access delivery engine | Verify HMAC before parsing; derive source/org from stored source; evaluate source delivery policies and egress rules. No browser-user credential is inferred. | Webhook/capture tests, `TestAuthorizeDelivery` | | Control-plane configuration, secrets, harness and extensions | Browser policy and instance-only restrictions where registered; service contracts carry principal/org; task grants cannot invoke administrative RPCs. | HTTP deny matrix, `TestAdministrationRoutes`, `TestChannelReloadRoute`, `TestConfineHarnessOrg` | +| Audit feed: `webui/api_audit.go`, `domain/audit` | `read_audit` on the `audit` resource; org scope and the instance-wide exception come from the authenticated principal, never the query string. Export carries the same grant, needs an explicit range of at most a year, and is rate-limited per identity, one at a time. Redaction runs in the State Store adapter before an entry crosses gRPC. | `TestReadAuditRoleBoundary`, `TestAuditRoutesFollowTheReadAuditRole`, `TestHTTPPolicyCannotBeBypassed`, `TestAuditErrorMapping`, `TestAuditCSVCellNeutralisesFormulas` | | Live HTTP/SSE and diagnostics | Authenticated policy gate applies before stream creation; org-scoped event reads; instance logs require administrative visibility. | HTTP deny matrix, instance-only tests, `TestServiceLogsRequireInstanceCredential` | | Background schedules, dispatch, curation | Service/agent authority, not an implicit browser user. Task-scoped grants and org scopes remain distinct from initiating operator attribution. | Existing dispatch, grant, memory and org-scope tests | @@ -121,6 +122,8 @@ action/resource; the handler may impose a stricter record or instance boundary. | DELETE | `/api/tokens/7` | | GET | `/api/access/policies` | | GET | `/api/alerts` | +| GET | `/api/audit` | +| GET | `/api/audit/export` | | GET | `/api/authentication` | | GET | `/api/bindings` | | GET | `/api/bindings/7` | @@ -136,7 +139,6 @@ action/resource; the handler may impose a stricter record or instance boundary. | GET | `/api/chat/update` | | GET | `/api/config` | | GET | `/api/control-plane/apply-status` | -| GET | `/api/control-plane/audit` | | GET | `/api/control-plane/catalog` | | GET | `/api/control-plane/resources/sample` | | GET | `/api/control-plane/resources/sample/history` | diff --git a/internal/app/controlplane/audit_test.go b/internal/app/controlplane/audit_test.go deleted file mode 100644 index 7208dce7f..000000000 --- a/internal/app/controlplane/audit_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package controlplane - -import ( - "context" - "slices" - "testing" - - pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" - "github.com/samcharles93/archie-core/internal/domain/org" - "github.com/samcharles93/archie-core/internal/domain/storecontract" -) - -type auditKeys struct { - ResourceStore - asked []string -} - -func (a *auditKeys) Audit(_ context.Context, _ string, keys []string, _ int) ([]storecontract.AuditEntry, error) { - a.asked = keys - out := make([]storecontract.AuditEntry, 0, len(keys)) - for _, key := range keys { - out = append(out, storecontract.AuditEntry{RecordKey: key}) - } - return out, nil -} - -// TestAuditStaysInCallerOrg pins that an audit request reads only the -// caller's org: kinds become that org's record keys, and a kind cannot spell -// another org's. -func TestAuditStaysInCallerOrg(t *testing.T) { - tests := []struct { - name string - org org.OrgID - kind string - wantKey string - rejected bool - }{ - {"default org reads its own kind", org.DefaultOrgID, "workflows", "workflows", false}, - {"other org reads its own kind", "acme", "workflows", "acme/workflows", false}, - {"a slash cannot reach another org", org.DefaultOrgID, "acme/workflows", "", true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - store := &auditKeys{} - ctx := org.WithOrg(context.Background(), tt.org) - got, err := (&Server{store: store}).Audit(ctx, &pb.AuditRequest{Kinds: []string{tt.kind}}) - if tt.rejected { - if err == nil || store.asked != nil { - t.Fatalf("want rejection before the store, got err %v asked %v", err, store.asked) - } - return - } - if err != nil { - t.Fatal(err) - } - if !slices.Equal(store.asked, []string{tt.wantKey}) || got.Entries[0].RecordKey != tt.kind { - t.Fatalf("asked %v, returned %q", store.asked, got.Entries[0].RecordKey) - } - }) - } -} diff --git a/internal/app/controlplane/controlplane.go b/internal/app/controlplane/controlplane.go index af9fa6e8a..3e61ecd2a 100644 --- a/internal/app/controlplane/controlplane.go +++ b/internal/app/controlplane/controlplane.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "strings" "time" "google.golang.org/grpc/codes" @@ -39,7 +38,6 @@ type ResourceStore interface { ListResources(context.Context) ([]storecontract.Resource, error) Resource(ctx context.Context, orgID, kind string) (storecontract.Resource, error) ResourceHistory(ctx context.Context, orgID, kind string, limit int) ([]storecontract.Resource, error) - Audit(ctx context.Context, table string, keys []string, limit int) ([]storecontract.AuditEntry, error) PutResource(context.Context, storecontract.ResourceWrite) (storecontract.Resource, error) } @@ -117,49 +115,6 @@ func (s *Server) History(ctx context.Context, request *pb.HistoryRequest) (*pb.H return &pb.HistoryResponse{Revisions: out}, nil } -// defaultAuditLimit caps an audit read: a page shows recent changes, not the -// whole trail. -const defaultAuditLimit = 500 - -// resourcesAuditTable is the sys_audit table name resource writes record -// under. -const resourcesAuditTable = "resources" - -func (s *Server) Audit(ctx context.Context, request *pb.AuditRequest) (*pb.AuditResponse, error) { - orgID := requestOrg(ctx) - keys := make([]string, 0, len(request.GetKinds())) - kindOf := make(map[string]string, len(request.GetKinds())) - for _, kind := range request.GetKinds() { - // A slash would let a kind spell another org's record key. - if kind == "" || strings.Contains(kind, "/") { - return nil, status.Errorf(codes.InvalidArgument, "invalid kind %q", kind) - } - key := storecontract.ResourceAuditKey(orgID, kind) - keys = append(keys, key) - kindOf[key] = kind - } - if len(keys) == 0 { - return nil, status.Error(codes.InvalidArgument, "kinds are required") - } - limit := int(request.GetLimit()) - if limit <= 0 || limit > defaultAuditLimit { - limit = defaultAuditLimit - } - entries, err := s.store.Audit(ctx, resourcesAuditTable, keys, limit) - if err != nil { - return nil, mapError(err) - } - out := make([]*pb.AuditEntry, 0, len(entries)) - for _, entry := range entries { - out = append(out, &pb.AuditEntry{ - Id: entry.ID, Table: entry.Table, RecordKey: kindOf[entry.RecordKey], Field: entry.Field, - OldValueJson: entry.OldValue, NewValueJson: entry.NewValue, Version: entry.Version, - Actor: entry.Actor, Source: entry.Source, RequestId: entry.RequestID, At: timestamp(entry.At), - }) - } - return &pb.AuditResponse{Entries: out}, nil -} - func (s *Server) Command(ctx context.Context, request *pb.CommandRequest) (*pb.CommandResponse, error) { definition, ok := s.definitions[request.GetKind()] command := request.GetCommand() diff --git a/internal/app/controlplane/org_models_test.go b/internal/app/controlplane/org_models_test.go index 559dd3ca4..16c1962cf 100644 --- a/internal/app/controlplane/org_models_test.go +++ b/internal/app/controlplane/org_models_test.go @@ -31,10 +31,6 @@ func (resourceStore) ResourceHistory(context.Context, string, string, int) ([]st return nil, nil } -func (resourceStore) Audit(context.Context, string, []string, int) ([]storecontract.AuditEntry, error) { - return nil, nil -} - func (resourceStore) PutResource(context.Context, storecontract.ResourceWrite) (storecontract.Resource, error) { return storecontract.Resource{}, nil } diff --git a/internal/contracts/controlplane/v1/controlplane.pb.go b/internal/contracts/controlplane/v1/controlplane.pb.go index 36a998bdd..21b5139fd 100644 --- a/internal/contracts/controlplane/v1/controlplane.pb.go +++ b/internal/contracts/controlplane/v1/controlplane.pb.go @@ -557,232 +557,6 @@ func (x *HistoryResponse) GetRevisions() []*Revision { return nil } -// AuditEntry is one changed field of one record. Old and new values are JSON; -// an absent side is empty. -type AuditEntry struct { - state protoimpl.MessageState `protogen:"open.v1"` - Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` - Table string `protobuf:"bytes,2,opt,name=table,proto3" json:"table,omitempty"` - RecordKey string `protobuf:"bytes,3,opt,name=record_key,json=recordKey,proto3" json:"record_key,omitempty"` - Field string `protobuf:"bytes,4,opt,name=field,proto3" json:"field,omitempty"` - OldValueJson []byte `protobuf:"bytes,5,opt,name=old_value_json,json=oldValueJson,proto3" json:"old_value_json,omitempty"` - NewValueJson []byte `protobuf:"bytes,6,opt,name=new_value_json,json=newValueJson,proto3" json:"new_value_json,omitempty"` - Version int64 `protobuf:"varint,7,opt,name=version,proto3" json:"version,omitempty"` - Actor string `protobuf:"bytes,8,opt,name=actor,proto3" json:"actor,omitempty"` - Source string `protobuf:"bytes,9,opt,name=source,proto3" json:"source,omitempty"` - RequestId string `protobuf:"bytes,10,opt,name=request_id,json=requestId,proto3" json:"request_id,omitempty"` - At *timestamppb.Timestamp `protobuf:"bytes,11,opt,name=at,proto3" json:"at,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *AuditEntry) Reset() { - *x = AuditEntry{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[9] - 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_controlplane_v1_controlplane_proto_msgTypes[9] - 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_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{9} -} - -func (x *AuditEntry) GetId() int64 { - if x != nil { - return x.Id - } - return 0 -} - -func (x *AuditEntry) GetTable() string { - if x != nil { - return x.Table - } - return "" -} - -func (x *AuditEntry) GetRecordKey() string { - if x != nil { - return x.RecordKey - } - return "" -} - -func (x *AuditEntry) GetField() string { - if x != nil { - return x.Field - } - return "" -} - -func (x *AuditEntry) GetOldValueJson() []byte { - if x != nil { - return x.OldValueJson - } - return nil -} - -func (x *AuditEntry) GetNewValueJson() []byte { - if x != nil { - return x.NewValueJson - } - return nil -} - -func (x *AuditEntry) GetVersion() int64 { - if x != nil { - return x.Version - } - return 0 -} - -func (x *AuditEntry) GetActor() string { - if x != nil { - return x.Actor - } - return "" -} - -func (x *AuditEntry) GetSource() string { - if x != nil { - return x.Source - } - return "" -} - -func (x *AuditEntry) GetRequestId() string { - if x != nil { - return x.RequestId - } - return "" -} - -func (x *AuditEntry) GetAt() *timestamppb.Timestamp { - if x != nil { - return x.At - } - return nil -} - -// AuditRequest names resource kinds; the trail returned is theirs in the -// caller's org, so no request can name another org's records. -type AuditRequest struct { - state protoimpl.MessageState `protogen:"open.v1"` - Kinds []string `protobuf:"bytes,4,rep,name=kinds,proto3" json:"kinds,omitempty"` - // Limit caps the entries returned, newest first. Zero means the server's - // own default. - Limit int32 `protobuf:"varint,3,opt,name=limit,proto3" json:"limit,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *AuditRequest) Reset() { - *x = AuditRequest{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[10] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *AuditRequest) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*AuditRequest) ProtoMessage() {} - -func (x *AuditRequest) ProtoReflect() protoreflect.Message { - mi := &file_controlplane_v1_controlplane_proto_msgTypes[10] - 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 AuditRequest.ProtoReflect.Descriptor instead. -func (*AuditRequest) Descriptor() ([]byte, []int) { - return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{10} -} - -func (x *AuditRequest) GetKinds() []string { - if x != nil { - return x.Kinds - } - return nil -} - -func (x *AuditRequest) GetLimit() int32 { - if x != nil { - return x.Limit - } - return 0 -} - -type AuditResponse struct { - state protoimpl.MessageState `protogen:"open.v1"` - Entries []*AuditEntry `protobuf:"bytes,1,rep,name=entries,proto3" json:"entries,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache -} - -func (x *AuditResponse) Reset() { - *x = AuditResponse{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[11] - ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) - ms.StoreMessageInfo(mi) -} - -func (x *AuditResponse) String() string { - return protoimpl.X.MessageStringOf(x) -} - -func (*AuditResponse) ProtoMessage() {} - -func (x *AuditResponse) ProtoReflect() protoreflect.Message { - mi := &file_controlplane_v1_controlplane_proto_msgTypes[11] - 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 AuditResponse.ProtoReflect.Descriptor instead. -func (*AuditResponse) Descriptor() ([]byte, []int) { - return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{11} -} - -func (x *AuditResponse) GetEntries() []*AuditEntry { - if x != nil { - return x.Entries - } - return nil -} - type CommandRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Kind string `protobuf:"bytes,1,opt,name=kind,proto3" json:"kind,omitempty"` @@ -800,7 +574,7 @@ type CommandRequest struct { func (x *CommandRequest) Reset() { *x = CommandRequest{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[12] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[9] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -812,7 +586,7 @@ func (x *CommandRequest) String() string { func (*CommandRequest) ProtoMessage() {} func (x *CommandRequest) ProtoReflect() protoreflect.Message { - mi := &file_controlplane_v1_controlplane_proto_msgTypes[12] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[9] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -825,7 +599,7 @@ func (x *CommandRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use CommandRequest.ProtoReflect.Descriptor instead. func (*CommandRequest) Descriptor() ([]byte, []int) { - return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{12} + return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{9} } func (x *CommandRequest) GetKind() string { @@ -886,7 +660,7 @@ type CommandResponse struct { func (x *CommandResponse) Reset() { *x = CommandResponse{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[13] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[10] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -898,7 +672,7 @@ func (x *CommandResponse) String() string { func (*CommandResponse) ProtoMessage() {} func (x *CommandResponse) ProtoReflect() protoreflect.Message { - mi := &file_controlplane_v1_controlplane_proto_msgTypes[13] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[10] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -911,7 +685,7 @@ func (x *CommandResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use CommandResponse.ProtoReflect.Descriptor instead. func (*CommandResponse) Descriptor() ([]byte, []int) { - return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{13} + return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{10} } func (x *CommandResponse) GetResource() *Resource { @@ -931,7 +705,7 @@ type WatchRequest struct { func (x *WatchRequest) Reset() { *x = WatchRequest{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[14] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[11] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -943,7 +717,7 @@ func (x *WatchRequest) String() string { func (*WatchRequest) ProtoMessage() {} func (x *WatchRequest) ProtoReflect() protoreflect.Message { - mi := &file_controlplane_v1_controlplane_proto_msgTypes[14] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[11] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -956,7 +730,7 @@ func (x *WatchRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use WatchRequest.ProtoReflect.Descriptor instead. func (*WatchRequest) Descriptor() ([]byte, []int) { - return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{14} + return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{11} } func (x *WatchRequest) GetKind() string { @@ -983,7 +757,7 @@ type WatchResponse struct { func (x *WatchResponse) Reset() { *x = WatchResponse{} - mi := &file_controlplane_v1_controlplane_proto_msgTypes[15] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[12] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -995,7 +769,7 @@ func (x *WatchResponse) String() string { func (*WatchResponse) ProtoMessage() {} func (x *WatchResponse) ProtoReflect() protoreflect.Message { - mi := &file_controlplane_v1_controlplane_proto_msgTypes[15] + mi := &file_controlplane_v1_controlplane_proto_msgTypes[12] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -1008,7 +782,7 @@ func (x *WatchResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use WatchResponse.ProtoReflect.Descriptor instead. func (*WatchResponse) Descriptor() ([]byte, []int) { - return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{15} + return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{12} } func (x *WatchResponse) GetResource() *Resource { @@ -1069,28 +843,7 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" + "\x04kind\x18\x01 \x01(\tR\x04kind\x12\x14\n" + "\x05limit\x18\x02 \x01(\x05R\x05limitJ\x04\b\x03\x10\x04R\x06org_id\"J\n" + "\x0fHistoryResponse\x127\n" + - "\trevisions\x18\x01 \x03(\v2\x19.controlplane.v1.RevisionR\trevisions\"\xc6\x02\n" + - "\n" + - "AuditEntry\x12\x0e\n" + - "\x02id\x18\x01 \x01(\x03R\x02id\x12\x14\n" + - "\x05table\x18\x02 \x01(\tR\x05table\x12\x1d\n" + - "\n" + - "record_key\x18\x03 \x01(\tR\trecordKey\x12\x14\n" + - "\x05field\x18\x04 \x01(\tR\x05field\x12$\n" + - "\x0eold_value_json\x18\x05 \x01(\fR\foldValueJson\x12$\n" + - "\x0enew_value_json\x18\x06 \x01(\fR\fnewValueJson\x12\x18\n" + - "\aversion\x18\a \x01(\x03R\aversion\x12\x14\n" + - "\x05actor\x18\b \x01(\tR\x05actor\x12\x16\n" + - "\x06source\x18\t \x01(\tR\x06source\x12\x1d\n" + - "\n" + - "request_id\x18\n" + - " \x01(\tR\trequestId\x12*\n" + - "\x02at\x18\v \x01(\v2\x1a.google.protobuf.TimestampR\x02at\"Z\n" + - "\fAuditRequest\x12\x14\n" + - "\x05kinds\x18\x04 \x03(\tR\x05kinds\x12\x14\n" + - "\x05limit\x18\x03 \x01(\x05R\x05limitJ\x04\b\x01\x10\x02J\x04\b\x02\x10\x03R\x05tableR\vrecord_keys\"F\n" + - "\rAuditResponse\x125\n" + - "\aentries\x18\x01 \x03(\v2\x1b.controlplane.v1.AuditEntryR\aentries\"\xe3\x01\n" + + "\trevisions\x18\x01 \x03(\v2\x19.controlplane.v1.RevisionR\trevisions\"\xe3\x01\n" + "\x0eCommandRequest\x12\x12\n" + "\x04kind\x18\x01 \x01(\tR\x04kind\x12\x18\n" + "\acommand\x18\x02 \x01(\tR\acommand\x12\x1d\n" + @@ -1108,12 +861,11 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" + "\rafter_version\x18\x02 \x01(\x03R\fafterVersionJ\x04\b\x03\x10\x04R\x06org_id\"\\\n" + "\rWatchResponse\x125\n" + "\bresource\x18\x01 \x01(\v2\x19.controlplane.v1.ResourceR\bresource\x12\x14\n" + - "\x05error\x18\x02 \x01(\tR\x05error2\xd9\x03\n" + + "\x05error\x18\x02 \x01(\tR\x05error2\x91\x03\n" + "\x13ControlPlaneService\x12L\n" + "\aCatalog\x12\x1f.controlplane.v1.CatalogRequest\x1a .controlplane.v1.CatalogResponse\x12F\n" + "\x05Query\x12\x1d.controlplane.v1.QueryRequest\x1a\x1e.controlplane.v1.QueryResponse\x12L\n" + - "\aHistory\x12\x1f.controlplane.v1.HistoryRequest\x1a .controlplane.v1.HistoryResponse\x12F\n" + - "\x05Audit\x12\x1d.controlplane.v1.AuditRequest\x1a\x1e.controlplane.v1.AuditResponse\x12L\n" + + "\aHistory\x12\x1f.controlplane.v1.HistoryRequest\x1a .controlplane.v1.HistoryResponse\x12L\n" + "\aCommand\x12\x1f.controlplane.v1.CommandRequest\x1a .controlplane.v1.CommandResponse\x12H\n" + "\x05Watch\x12\x1d.controlplane.v1.WatchRequest\x1a\x1e.controlplane.v1.WatchResponse0\x01B\xdc\x01\n" + "\x13com.controlplane.v1B\x11ControlplaneProtoP\x01ZUgithub.com/samcharles93/archie-core/internal/contracts/controlplane/v1;controlplanev1\xa2\x02\x03CXX\xaa\x02\x0fControlplane.V1\xca\x02\x0fControlplane\\V1\xe2\x02\x1bControlplane\\V1\\GPBMetadata\xea\x02\x10Controlplane::V1b\x06proto3" @@ -1130,7 +882,7 @@ func file_controlplane_v1_controlplane_proto_rawDescGZIP() []byte { return file_controlplane_v1_controlplane_proto_rawDescData } -var file_controlplane_v1_controlplane_proto_msgTypes = make([]protoimpl.MessageInfo, 16) +var file_controlplane_v1_controlplane_proto_msgTypes = make([]protoimpl.MessageInfo, 13) var file_controlplane_v1_controlplane_proto_goTypes = []any{ (*ResourceDescriptor)(nil), // 0: controlplane.v1.ResourceDescriptor (*CatalogRequest)(nil), // 1: controlplane.v1.CatalogRequest @@ -1141,42 +893,35 @@ var file_controlplane_v1_controlplane_proto_goTypes = []any{ (*Revision)(nil), // 6: controlplane.v1.Revision (*HistoryRequest)(nil), // 7: controlplane.v1.HistoryRequest (*HistoryResponse)(nil), // 8: controlplane.v1.HistoryResponse - (*AuditEntry)(nil), // 9: controlplane.v1.AuditEntry - (*AuditRequest)(nil), // 10: controlplane.v1.AuditRequest - (*AuditResponse)(nil), // 11: controlplane.v1.AuditResponse - (*CommandRequest)(nil), // 12: controlplane.v1.CommandRequest - (*CommandResponse)(nil), // 13: controlplane.v1.CommandResponse - (*WatchRequest)(nil), // 14: controlplane.v1.WatchRequest - (*WatchResponse)(nil), // 15: controlplane.v1.WatchResponse - (*timestamppb.Timestamp)(nil), // 16: google.protobuf.Timestamp + (*CommandRequest)(nil), // 9: controlplane.v1.CommandRequest + (*CommandResponse)(nil), // 10: controlplane.v1.CommandResponse + (*WatchRequest)(nil), // 11: controlplane.v1.WatchRequest + (*WatchResponse)(nil), // 12: controlplane.v1.WatchResponse + (*timestamppb.Timestamp)(nil), // 13: google.protobuf.Timestamp } var file_controlplane_v1_controlplane_proto_depIdxs = []int32{ 0, // 0: controlplane.v1.CatalogResponse.resources:type_name -> controlplane.v1.ResourceDescriptor - 16, // 1: controlplane.v1.Resource.updated_at:type_name -> google.protobuf.Timestamp + 13, // 1: controlplane.v1.Resource.updated_at:type_name -> google.protobuf.Timestamp 3, // 2: controlplane.v1.QueryResponse.resource:type_name -> controlplane.v1.Resource - 16, // 3: controlplane.v1.Revision.at:type_name -> google.protobuf.Timestamp + 13, // 3: controlplane.v1.Revision.at:type_name -> google.protobuf.Timestamp 6, // 4: controlplane.v1.HistoryResponse.revisions:type_name -> controlplane.v1.Revision - 16, // 5: controlplane.v1.AuditEntry.at:type_name -> google.protobuf.Timestamp - 9, // 6: controlplane.v1.AuditResponse.entries:type_name -> controlplane.v1.AuditEntry - 3, // 7: controlplane.v1.CommandResponse.resource:type_name -> controlplane.v1.Resource - 3, // 8: controlplane.v1.WatchResponse.resource:type_name -> controlplane.v1.Resource - 1, // 9: controlplane.v1.ControlPlaneService.Catalog:input_type -> controlplane.v1.CatalogRequest - 4, // 10: controlplane.v1.ControlPlaneService.Query:input_type -> controlplane.v1.QueryRequest - 7, // 11: controlplane.v1.ControlPlaneService.History:input_type -> controlplane.v1.HistoryRequest - 10, // 12: controlplane.v1.ControlPlaneService.Audit:input_type -> controlplane.v1.AuditRequest - 12, // 13: controlplane.v1.ControlPlaneService.Command:input_type -> controlplane.v1.CommandRequest - 14, // 14: controlplane.v1.ControlPlaneService.Watch:input_type -> controlplane.v1.WatchRequest - 2, // 15: controlplane.v1.ControlPlaneService.Catalog:output_type -> controlplane.v1.CatalogResponse - 5, // 16: controlplane.v1.ControlPlaneService.Query:output_type -> controlplane.v1.QueryResponse - 8, // 17: controlplane.v1.ControlPlaneService.History:output_type -> controlplane.v1.HistoryResponse - 11, // 18: controlplane.v1.ControlPlaneService.Audit:output_type -> controlplane.v1.AuditResponse - 13, // 19: controlplane.v1.ControlPlaneService.Command:output_type -> controlplane.v1.CommandResponse - 15, // 20: controlplane.v1.ControlPlaneService.Watch:output_type -> controlplane.v1.WatchResponse - 15, // [15:21] is the sub-list for method output_type - 9, // [9:15] is the sub-list for method input_type - 9, // [9:9] is the sub-list for extension type_name - 9, // [9:9] is the sub-list for extension extendee - 0, // [0:9] is the sub-list for field type_name + 3, // 5: controlplane.v1.CommandResponse.resource:type_name -> controlplane.v1.Resource + 3, // 6: controlplane.v1.WatchResponse.resource:type_name -> controlplane.v1.Resource + 1, // 7: controlplane.v1.ControlPlaneService.Catalog:input_type -> controlplane.v1.CatalogRequest + 4, // 8: controlplane.v1.ControlPlaneService.Query:input_type -> controlplane.v1.QueryRequest + 7, // 9: controlplane.v1.ControlPlaneService.History:input_type -> controlplane.v1.HistoryRequest + 9, // 10: controlplane.v1.ControlPlaneService.Command:input_type -> controlplane.v1.CommandRequest + 11, // 11: controlplane.v1.ControlPlaneService.Watch:input_type -> controlplane.v1.WatchRequest + 2, // 12: controlplane.v1.ControlPlaneService.Catalog:output_type -> controlplane.v1.CatalogResponse + 5, // 13: controlplane.v1.ControlPlaneService.Query:output_type -> controlplane.v1.QueryResponse + 8, // 14: controlplane.v1.ControlPlaneService.History:output_type -> controlplane.v1.HistoryResponse + 10, // 15: controlplane.v1.ControlPlaneService.Command:output_type -> controlplane.v1.CommandResponse + 12, // 16: controlplane.v1.ControlPlaneService.Watch:output_type -> controlplane.v1.WatchResponse + 12, // [12:17] is the sub-list for method output_type + 7, // [7:12] is the sub-list for method input_type + 7, // [7:7] is the sub-list for extension type_name + 7, // [7:7] is the sub-list for extension extendee + 0, // [0:7] is the sub-list for field type_name } func init() { file_controlplane_v1_controlplane_proto_init() } @@ -1190,7 +935,7 @@ func file_controlplane_v1_controlplane_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_controlplane_v1_controlplane_proto_rawDesc), len(file_controlplane_v1_controlplane_proto_rawDesc)), NumEnums: 0, - NumMessages: 16, + NumMessages: 13, NumExtensions: 0, NumServices: 1, }, diff --git a/internal/contracts/controlplane/v1/controlplane_grpc.pb.go b/internal/contracts/controlplane/v1/controlplane_grpc.pb.go index 09513fa51..4bd7fa05a 100644 --- a/internal/contracts/controlplane/v1/controlplane_grpc.pb.go +++ b/internal/contracts/controlplane/v1/controlplane_grpc.pb.go @@ -22,7 +22,6 @@ const ( ControlPlaneService_Catalog_FullMethodName = "/controlplane.v1.ControlPlaneService/Catalog" ControlPlaneService_Query_FullMethodName = "/controlplane.v1.ControlPlaneService/Query" ControlPlaneService_History_FullMethodName = "/controlplane.v1.ControlPlaneService/History" - ControlPlaneService_Audit_FullMethodName = "/controlplane.v1.ControlPlaneService/Audit" ControlPlaneService_Command_FullMethodName = "/controlplane.v1.ControlPlaneService/Command" ControlPlaneService_Watch_FullMethodName = "/controlplane.v1.ControlPlaneService/Watch" ) @@ -34,7 +33,6 @@ type ControlPlaneServiceClient interface { Catalog(ctx context.Context, in *CatalogRequest, opts ...grpc.CallOption) (*CatalogResponse, error) Query(ctx context.Context, in *QueryRequest, opts ...grpc.CallOption) (*QueryResponse, error) History(ctx context.Context, in *HistoryRequest, opts ...grpc.CallOption) (*HistoryResponse, error) - Audit(ctx context.Context, in *AuditRequest, opts ...grpc.CallOption) (*AuditResponse, error) Command(ctx context.Context, in *CommandRequest, opts ...grpc.CallOption) (*CommandResponse, error) Watch(ctx context.Context, in *WatchRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[WatchResponse], error) } @@ -77,16 +75,6 @@ func (c *controlPlaneServiceClient) History(ctx context.Context, in *HistoryRequ return out, nil } -func (c *controlPlaneServiceClient) Audit(ctx context.Context, in *AuditRequest, opts ...grpc.CallOption) (*AuditResponse, error) { - cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) - out := new(AuditResponse) - err := c.cc.Invoke(ctx, ControlPlaneService_Audit_FullMethodName, in, out, cOpts...) - if err != nil { - return nil, err - } - return out, nil -} - func (c *controlPlaneServiceClient) Command(ctx context.Context, in *CommandRequest, opts ...grpc.CallOption) (*CommandResponse, error) { cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) out := new(CommandResponse) @@ -123,7 +111,6 @@ type ControlPlaneServiceServer interface { Catalog(context.Context, *CatalogRequest) (*CatalogResponse, error) Query(context.Context, *QueryRequest) (*QueryResponse, error) History(context.Context, *HistoryRequest) (*HistoryResponse, error) - Audit(context.Context, *AuditRequest) (*AuditResponse, error) Command(context.Context, *CommandRequest) (*CommandResponse, error) Watch(*WatchRequest, grpc.ServerStreamingServer[WatchResponse]) error mustEmbedUnimplementedControlPlaneServiceServer() @@ -145,9 +132,6 @@ func (UnimplementedControlPlaneServiceServer) Query(context.Context, *QueryReque func (UnimplementedControlPlaneServiceServer) History(context.Context, *HistoryRequest) (*HistoryResponse, error) { return nil, status.Error(codes.Unimplemented, "method History not implemented") } -func (UnimplementedControlPlaneServiceServer) Audit(context.Context, *AuditRequest) (*AuditResponse, error) { - return nil, status.Error(codes.Unimplemented, "method Audit not implemented") -} func (UnimplementedControlPlaneServiceServer) Command(context.Context, *CommandRequest) (*CommandResponse, error) { return nil, status.Error(codes.Unimplemented, "method Command not implemented") } @@ -229,24 +213,6 @@ func _ControlPlaneService_History_Handler(srv interface{}, ctx context.Context, return interceptor(ctx, in, info, handler) } -func _ControlPlaneService_Audit_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(AuditRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(ControlPlaneServiceServer).Audit(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: ControlPlaneService_Audit_FullMethodName, - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(ControlPlaneServiceServer).Audit(ctx, req.(*AuditRequest)) - } - return interceptor(ctx, in, info, handler) -} - func _ControlPlaneService_Command_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(CommandRequest) if err := dec(in); err != nil { @@ -295,10 +261,6 @@ var ControlPlaneService_ServiceDesc = grpc.ServiceDesc{ MethodName: "History", Handler: _ControlPlaneService_History_Handler, }, - { - MethodName: "Audit", - Handler: _ControlPlaneService_Audit_Handler, - }, { MethodName: "Command", Handler: _ControlPlaneService_Command_Handler, diff --git a/internal/domain/access/access.go b/internal/domain/access/access.go index 4583f8f45..b3852f146 100644 --- a/internal/domain/access/access.go +++ b/internal/domain/access/access.go @@ -58,6 +58,10 @@ const ( ActionManageMembers Action = "manage_members" ActionManageIdentities Action = "manage_identities" ActionManagePolicies Action = "manage_policies" + // ActionReadAudit reads the instance audit feed. It is separate from + // ActionRead because the feed spans settings, access, identity and runtime + // trails: a viewer's broad read must not open the whole instance's history. + ActionReadAudit Action = "read_audit" ) // Actions is every action, in vocabulary order. The engine and the shipped @@ -67,6 +71,7 @@ func Actions() []Action { ActionRead, ActionCreate, ActionUpdate, ActionDelete, ActionApprove, ActionRun, ActionDeliver, ActionReadLogs, ActionReadSecret, ActionManageMembers, ActionManageIdentities, ActionManagePolicies, + ActionReadAudit, } } @@ -98,6 +103,7 @@ const ( KindMember ResourceKind = "member" KindDashboard ResourceKind = "dashboard" KindEventIngest ResourceKind = "event" + KindAudit ResourceKind = "audit" ) // Policy is one stored Cedar policy. LevelInstance has no scope; LevelOrg sets diff --git a/internal/domain/audit/audit.go b/internal/domain/audit/audit.go index 48fc181fc..1236d2a98 100644 --- a/internal/domain/audit/audit.go +++ b/internal/domain/audit/audit.go @@ -80,6 +80,39 @@ func (o Outcome) Valid() bool { return false } +// Categories is the feed's category vocabulary. The dashboard renders one +// filter value per entry rather than inventing its own list. +func Categories() []string { + return []string{ + string(CategorySettings), string(CategoryAccess), string(CategoryIdentity), + string(CategoryRuntime), string(CategoryAutomation), + } +} + +// Severities is the feed's severity vocabulary. +func Severities() []string { + return []string{string(SeverityInfo), string(SeverityWarning), string(SeverityError)} +} + +// Outcomes is the feed's outcome vocabulary. +func Outcomes() []string { + return []string{string(OutcomeSuccess), string(OutcomeFailure), string(OutcomeDenied)} +} + +// TargetKinds is the target-kind vocabulary the dashboard offers as a filter. +// The read model derives target_kind from each source, so the values are the +// access resource kinds and audited table names it emits. +func TargetKinds() []string { + return []string{ + "resource", "policy", "identity", "task", "workflow", + "org", "workspace", "source", "event_type", "mapping", "binding", + "capture", "secret", "member", "dashboard", "event", + "orgs", "workspaces", "memberships", "org_agents", + "mappings", "bindings", "event_types", "sources", + "installed_packages", "harness_secrets", + } +} + // 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 { diff --git a/internal/domain/storecontract/storecontract.go b/internal/domain/storecontract/storecontract.go index 46982b7e0..184bde75e 100644 --- a/internal/domain/storecontract/storecontract.go +++ b/internal/domain/storecontract/storecontract.go @@ -571,10 +571,6 @@ type Resource struct { At time.Time } -// AuditTableResources is the sys_audit table name for control-plane resources, -// keyed by ResourceAuditKey. -const AuditTableResources = "resources" - // DefaultOrgID is the org every resource written before orgs existed belongs // to, and the org a request that names none acts in. const DefaultOrgID = "org-sys" @@ -589,23 +585,6 @@ func ResourceAuditKey(orgID, kind string) string { return orgID + "/" + kind } -// AuditEntry is one changed field of one record, as sys_audit holds it. Any -// store table may write entries; Table and RecordKey locate the record. -// OldValue and NewValue are JSON; an absent side is empty. -type AuditEntry struct { - ID int64 - Table string - RecordKey string - Field string - OldValue []byte - NewValue []byte - Version int64 - Actor string - Source string - RequestID string - At time.Time -} - // ResourceWrite is a control-plane resource write request. ExpectedVersion is // the optimistic-concurrency guard: a write whose expectation does not match // the stored revision is refused with ErrResourceVersionConflict. diff --git a/internal/infrastructure/access/engine.go b/internal/infrastructure/access/engine.go index 00be64c9e..557c129d9 100644 --- a/internal/infrastructure/access/engine.go +++ b/internal/infrastructure/access/engine.go @@ -35,6 +35,7 @@ const schemaText = `namespace Archie { action manage_identities appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} }; action deliver appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} }; action manage_policies appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} }; + action read_audit appliesTo { principal: Identity, resource: Object, context: {signature: String, addr: ipaddr, time: datetime, run: String, step: String} }; }` // Problem is one stored policy the engine could not validate. It is what a diff --git a/internal/infrastructure/access/engine_test.go b/internal/infrastructure/access/engine_test.go index bbebe61a4..47fecda4b 100644 --- a/internal/infrastructure/access/engine_test.go +++ b/internal/infrastructure/access/engine_test.go @@ -46,6 +46,35 @@ func TestInvalidInstancePolicy(t *testing.T) { } } +// TestReadAuditRoleBoundary pins that the audit feed is an administrator's +// read: the dashboard's broad read grant (viewer and developer) must not open +// the instance's settings, access and identity history. +func TestReadAuditRoleBoundary(t *testing.T) { + const auditOrg = org.OrgID("acme") + engine := New(access.ShippedOrgPolicies(auditOrg)) + tests := []struct { + role org.Role + allowed bool + }{ + {org.RoleOwner, true}, + {org.RoleAdmin, true}, + {org.RoleDeveloper, false}, + {org.RoleViewer, false}, + } + for _, tt := range tests { + t.Run(string(tt.role), func(t *testing.T) { + p := access.Principal{ + IdentityID: "person", Org: auditOrg, + Memberships: []org.Membership{{IdentityID: "person", OrgID: auditOrg, Role: tt.role}}, + } + got := engine.Authorize(p, access.ActionReadAudit, access.Resource{Kind: access.KindAudit, Org: auditOrg}, access.Context{}) + if got.Allowed != tt.allowed { + t.Fatalf("allowed = %v, want %v (%+v)", got.Allowed, tt.allowed, got) + } + }) + } +} + // TestWorkspaceEnvironment pins that a policy can read the environment of // the workspace a resource is in: it validates on save and decides runs in // that workspace only. diff --git a/internal/infrastructure/postgres/postgresdb/audit.sql.go b/internal/infrastructure/postgres/postgresdb/audit.sql.go index 8edeb7558..1d9fe0753 100644 --- a/internal/infrastructure/postgres/postgresdb/audit.sql.go +++ b/internal/infrastructure/postgres/postgresdb/audit.sql.go @@ -10,66 +10,6 @@ import ( "time" ) -const auditForRecords = `-- name: AuditForRecords :many -SELECT id, at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id -FROM sys_audit -WHERE table_name = $1::text AND record_key = ANY($2::text[]) -ORDER BY id DESC -LIMIT $3::bigint -` - -type AuditForRecordsParams struct { - TableName string - RecordKeys []string - EntryLimit int64 -} - -type AuditForRecordsRow struct { - ID int64 - At time.Time - TableName string - RecordKey string - Field string - OldValue []byte - NewValue []byte - RecordVersion int64 - Actor string - Source string - RequestID string -} - -func (q *Queries) AuditForRecords(ctx context.Context, arg AuditForRecordsParams) ([]AuditForRecordsRow, error) { - rows, err := q.db.Query(ctx, auditForRecords, arg.TableName, arg.RecordKeys, arg.EntryLimit) - if err != nil { - return nil, err - } - defer rows.Close() - var items []AuditForRecordsRow - for rows.Next() { - var i AuditForRecordsRow - if err := rows.Scan( - &i.ID, - &i.At, - &i.TableName, - &i.RecordKey, - &i.Field, - &i.OldValue, - &i.NewValue, - &i.RecordVersion, - &i.Actor, - &i.Source, - &i.RequestID, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const insertAudit = `-- name: InsertAudit :exec INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id, org_id) VALUES (now(), $1::text, $2::text, $3::text, diff --git a/internal/infrastructure/postgres/queries/audit.sql b/internal/infrastructure/postgres/queries/audit.sql index a0c093dbf..ce4c54a72 100644 --- a/internal/infrastructure/postgres/queries/audit.sql +++ b/internal/infrastructure/postgres/queries/audit.sql @@ -24,7 +24,6 @@ INSERT INTO sys_audit (at, table_name, record_key, field, old_value, new_value, VALUES (now(), sqlc.arg(table_name)::text, sqlc.arg(record_key)::text, sqlc.arg(field)::text, sqlc.arg(old_value)::jsonb, sqlc.arg(new_value)::jsonb, sqlc.arg(record_version)::bigint, sqlc.arg(actor)::text, sqlc.arg(source)::text, sqlc.arg(request_id)::text, sqlc.arg(org_id)::text); - -- name: TryAuditPruneLock :one -- The retention sweep's transaction-scoped lock. A second sweep skips its -- tick rather than queuing deletes behind this one. @@ -41,10 +40,3 @@ WHERE id IN ( ORDER BY at LIMIT sqlc.arg(batch)::bigint ); - --- name: AuditForRecords :many -SELECT id, at, table_name, record_key, field, old_value, new_value, record_version, actor, source, request_id -FROM sys_audit -WHERE table_name = sqlc.arg(table_name)::text AND record_key = ANY(sqlc.arg(record_keys)::text[]) -ORDER BY id DESC -LIMIT sqlc.arg(entry_limit)::bigint; diff --git a/internal/infrastructure/postgres/resources.go b/internal/infrastructure/postgres/resources.go index 7617f5cd5..d8c3594f4 100644 --- a/internal/infrastructure/postgres/resources.go +++ b/internal/infrastructure/postgres/resources.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "math" "strings" "time" @@ -86,29 +85,6 @@ func checkResourceKey(orgID, kind string) error { return nil } -// Audit returns the field-level audit of the named records of one table, -// newest first. A limit of zero or less returns every entry. -func (s *Resources) Audit(ctx context.Context, table string, keys []string, limit int) ([]storecontract.AuditEntry, error) { - if limit <= 0 { - limit = math.MaxInt32 - } - rows, err := postgresdb.New(s.pool).AuditForRecords(ctx, postgresdb.AuditForRecordsParams{ - TableName: table, RecordKeys: keys, EntryLimit: int64(limit), - }) - if err != nil { - return nil, err - } - entries := make([]storecontract.AuditEntry, 0, len(rows)) - for _, row := range rows { - entries = append(entries, storecontract.AuditEntry{ - ID: row.ID, Table: row.TableName, RecordKey: row.RecordKey, Field: row.Field, - OldValue: row.OldValue, NewValue: row.NewValue, Version: row.RecordVersion, - Actor: row.Actor, Source: row.Source, RequestID: row.RequestID, At: row.At, - }) - } - return entries, nil -} - // PutResource applies one control-plane resource write, idempotent per // request ID. func (s *Resources) PutResource(ctx context.Context, write storecontract.ResourceWrite) (_ storecontract.Resource, retErr error) { diff --git a/internal/webui/api_audit.go b/internal/webui/api_audit.go index be427a285..cc55efa47 100644 --- a/internal/webui/api_audit.go +++ b/internal/webui/api_audit.go @@ -1,59 +1,654 @@ package webui import ( + "context" + "encoding/csv" "encoding/json" + "errors" + "fmt" + "io" + "maps" + "mime" "net/http" + "net/url" + "strconv" + "strings" "time" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + 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/ratelimit" ) -// auditEntryView is one changed field of one record. An absent side of the -// change is JSON null. -type auditEntryView struct { - ID int64 `json:"id"` - Table string `json:"table"` - RecordKey string `json:"record_key"` - Field string `json:"field"` - OldValue json.RawMessage `json:"old_value"` - NewValue json.RawMessage `json:"new_value"` - Version int64 `json:"version"` - Actor string `json:"actor"` - ActorName string `json:"actor_name"` - Source string `json:"source"` - At *time.Time `json:"at,omitempty"` +// auditExportPageSize is the largest page the feed serves. An export walks +// pages this size. +const auditExportPageSize = audit.MaxLimit + +// The export budget: six exports an hour per identity, one at a time. The +// caller's explicit range bounds the work, the deadline bounds the request, +// and the page cap bounds the stream even if the range is dense. +const ( + auditExportPerHour = 6 + auditExportWindow = time.Hour + auditExportMaxRange = 366 * 24 * time.Hour + auditExportDeadline = 5 * time.Minute + auditExportMaxPages = 2000 +) + +var errAuditExportTooLarge = errors.New("audit export exceeded its page cap") + +type auditActorView struct { + ID string `json:"id"` + Name string `json:"name"` + Kind string `json:"kind"` + PrincipalID string `json:"principal_id"` } -// handleAudit is GET /api/control-plane/audit?kind=K1&kind=K2: the -// field-level audit of the named resources in the caller's org, newest -// first. A page passes the kinds it shows, so its audit list is scoped to -// that page. +type auditTargetView struct { + Kind string `json:"kind"` + ID string `json:"id"` + Label string `json:"label"` + Href string `json:"href,omitempty"` +} + +type auditFieldView struct { + Field string `json:"field"` + Old json.RawMessage `json:"old"` + New json.RawMessage `json:"new"` +} + +type auditDetailView struct { + Fields []auditFieldView `json:"fields"` + Data json.RawMessage `json:"data"` + Version int64 `json:"version"` + RequestID string `json:"request_id"` + Source string `json:"source"` + Table string `json:"table"` +} + +type auditRecordView struct { + ID string `json:"id"` + At time.Time `json:"at"` + Category string `json:"category"` + Action string `json:"action"` + Kind string `json:"kind"` + Severity string `json:"severity"` + Outcome string `json:"outcome"` + Actor auditActorView `json:"actor"` + Target auditTargetView `json:"target"` + Summary string `json:"summary"` + Detail auditDetailView `json:"detail"` +} + +// auditSettingsKind is one control-plane resource kind and the title the +// catalog gives it, so the dashboard labels a settings row without carrying +// its own copy of the vocabulary. +type auditSettingsKind struct { + Kind string `json:"kind"` + Title string `json:"title"` +} + +type auditFacetsView struct { + Categories []string `json:"categories"` + Severities []string `json:"severities"` + Outcomes []string `json:"outcomes"` + TargetKinds []string `json:"target_kinds"` + SettingsKinds []auditSettingsKind `json:"settings_kinds"` +} + +type auditResponse struct { + Records []auditRecordView `json:"records"` + NextCursor string `json:"next_cursor"` + HasMore bool `json:"has_more"` + // Facets is supplied on the first page only: it is the closed vocabulary + // the UI needs once, not a field to repeat on every page. + Facets *auditFacetsView `json:"facets,omitempty"` +} + +// handleAudit is GET /api/audit: one page of the org-scoped feed. The org and +// the reader's authority come from the authenticated principal in the request +// context; the query string never names an org. func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) { - if s.ControlPlane == nil { - http.Error(w, "control plane unavailable", http.StatusServiceUnavailable) + if s.Audit == nil { + http.Error(w, "audit feed unavailable", http.StatusServiceUnavailable) return } - query := r.URL.Query() - response, err := s.ControlPlane.Audit(r.Context(), &controlpb.AuditRequest{Kinds: query["kind"]}) + filter, cursor, limit, err := auditRequest(r) if err != nil { - writeControlPlaneError(w, err) + http.Error(w, err.Error(), http.StatusBadRequest) return } - entries := make([]auditEntryView, 0, len(response.Entries)) - actorName := s.actorNames(r.Context()) - for _, entry := range response.Entries { - view := auditEntryView{ - ID: entry.Id, Table: entry.Table, RecordKey: entry.RecordKey, Field: entry.Field, - OldValue: jsonOrNull(entry.OldValueJson), NewValue: jsonOrNull(entry.NewValueJson), - Version: entry.Version, Actor: entry.Actor, ActorName: actorName(entry.Actor), Source: entry.Source, - } - if entry.At != nil { - at := entry.At.AsTime() - view.At = &at - } - entries = append(entries, view) + if err := filter.Normalize(); err != nil { + http.Error(w, firstLine(err.Error()), http.StatusBadRequest) + return } - writeJSON(w, map[string]any{"entries": entries}) + page, err := s.Audit.ListAudit(r.Context(), filter, cursor, limit) + if err != nil { + writeAuditError(w, err) + return + } + labels, settings := s.auditLabels(r.Context()) + records := make([]auditRecordView, 0, len(page.Entries)) + for _, entry := range page.Entries { + records = append(records, auditRecord(entry, labels)) + } + response := auditResponse{Records: records, NextCursor: page.NextCursor, HasMore: page.HasMore} + if cursor.IsZero() { + response.Facets = &auditFacetsView{ + Categories: audit.Categories(), + Severities: audit.Severities(), + Outcomes: audit.Outcomes(), + TargetKinds: audit.TargetKinds(), + SettingsKinds: settings, + } + } + writeJSON(w, response) +} + +// handleAuditExport is GET /api/audit/export?format=csv|json with the feed's +// filters. It requires an explicit range of at most a year and streams rows +// page by page. +func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) { + if s.Audit == nil { + http.Error(w, "audit feed unavailable", http.StatusServiceUnavailable) + return + } + format, ok := auditExportFormat(r) + if !ok { + http.Error(w, "format must be csv or json", http.StatusBadRequest) + return + } + filter, _, _, err := auditRequest(r) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + if err := filter.Normalize(); err != nil { + http.Error(w, firstLine(err.Error()), http.StatusBadRequest) + return + } + if err := auditExportRange(filter); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + identity := auditExportIdentity(r.Context()) + // Take the guard before charging the budget: a request rejected because + // the same identity is already exporting must cost no allowance. + if !s.beginAuditExport(identity) { + http.Error(w, "an audit export is already running for this identity", http.StatusTooManyRequests) + return + } + defer s.endAuditExport(identity) + if !s.auditExportLimiter().Allow("audit-export", identity) { + http.Error(w, "too many audit exports; try again later", http.StatusTooManyRequests) + return + } + ctx, cancel := context.WithTimeout(r.Context(), auditExportDeadline) + defer cancel() + + // Fetch the first page before setting download headers: a failure here is + // still a normal error response, not a truncated file. + first, err := s.Audit.ListAudit(ctx, filter, audit.Cursor{}, auditExportPageSize) + if err != nil { + writeAuditError(w, err) + return + } + writeAuditExportHeaders(w, format) + + labels, _ := s.auditLabels(ctx) + flush := func() {} + if flusher, ok := w.(http.Flusher); ok { + flush = flusher.Flush + } + if format == "csv" { + err = s.exportAuditCSV(ctx, filter, first, labels, w, flush) + } else { + err = s.exportAuditJSON(ctx, filter, first, labels, w, flush) + } + if err != nil { + // Headers and status are already on the wire, so no error status is + // left to send. Abort the connection so the client sees a failed + // download rather than a silently short file. + s.logf("audit export failed", "format", format, "err", err) + panic(http.ErrAbortHandler) + } +} + +// auditExportFormat resolves the download format. An absent format means JSON. +func auditExportFormat(r *http.Request) (string, bool) { + format := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("format"))) + switch format { + case "": + return "json", true + case "csv", "json": + return format, true + } + return "", false +} + +// auditExportRange requires an explicit window of at most a year: an unbounded +// export would stream the instance's whole history. +func auditExportRange(filter audit.Filter) error { + if filter.From.IsZero() || filter.To.IsZero() || filter.To.Sub(filter.From) > auditExportMaxRange { + return errors.New("choose an export time range of 366 days or less") + } + return nil +} + +func writeAuditExportHeaders(w http.ResponseWriter, format string) { + filename := "audit-" + time.Now().UTC().Format("20060102-150405") + "." + format + contentType := "application/json" + if format == "csv" { + contentType = "text/csv; charset=utf-8" + } + w.Header().Set("Content-Type", contentType) + w.Header().Set("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": filename})) + w.Header().Set("Cache-Control", "no-store") + w.Header().Set("X-Content-Type-Options", "nosniff") +} + +func (s *Server) exportAuditCSV(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) error { + writer := csv.NewWriter(w) + if err := writer.Write(auditCSVHeader); err != nil { + return err + } + err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error { + return writer.Write(auditCSVRow(auditRecord(entry, labels))) + }, func() { + writer.Flush() + flush() + }) + writer.Flush() + if flushErr := writer.Error(); err == nil { + err = flushErr + } + return err +} + +func (s *Server) exportAuditJSON(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) error { + if _, err := io.WriteString(w, "["); err != nil { + return err + } + encoder := json.NewEncoder(w) + firstRecord := true + err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error { + if !firstRecord { + if _, err := io.WriteString(w, ","); err != nil { + return err + } + } + firstRecord = false + return encoder.Encode(auditRecord(entry, labels)) + }, flush) + if err != nil { + // Leave the array open: the handler aborts the connection, and a + // closed array would masquerade as a complete download. + return err + } + _, err = io.WriteString(w, "]\n") + return err +} + +// walkAudit streams every entry of the window in feed order. It starts from +// the already-fetched first page so the caller could answer an error before +// any byte of the download was written. +func (s *Server) walkAudit(ctx context.Context, filter audit.Filter, first audit.Page, emit func(audit.Entry) error, afterPage func()) error { + page := first + pages := 1 + for { + for _, entry := range page.Entries { + if err := emit(entry); err != nil { + return err + } + } + if afterPage != nil { + afterPage() + } + if !page.HasMore || page.NextCursor == "" { + return nil + } + if pages >= auditExportMaxPages { + return errAuditExportTooLarge + } + pages++ + cursor, ok := audit.ParseCursor(page.NextCursor) + if !ok { + return nil + } + next, err := s.Audit.ListAudit(ctx, filter, cursor, auditExportPageSize) + if err != nil { + return err + } + page = next + } +} + +// auditRequest parses the shared filter query. Multi-valued parameters accept +// both repeated keys and comma-separated values, so the dashboard can send +// either encoding. +func auditRequest(r *http.Request) (audit.Filter, audit.Cursor, int, error) { + query := r.URL.Query() + filter := audit.Filter{ + Categories: auditList(query, "categories"), + Actions: auditList(query, "actions"), + Kinds: auditList(query, "kind"), + Actor: query.Get("actor"), + TargetKinds: auditList(query, "target_kind"), + TargetIDs: auditList(query, "target_id"), + Severities: auditList(query, "severity"), + Outcomes: auditList(query, "outcome"), + Query: query.Get("q"), + } + from, err := auditTime(query.Get("from")) + if err != nil { + return audit.Filter{}, audit.Cursor{}, 0, fmt.Errorf("invalid from: %w", err) + } + to, err := auditTime(query.Get("to")) + if err != nil { + return audit.Filter{}, audit.Cursor{}, 0, fmt.Errorf("invalid to: %w", err) + } + filter.From, filter.To = from, to + cursor := audit.Cursor{} + if raw := strings.TrimSpace(query.Get("cursor")); raw != "" { + // A malformed cursor means newest, matching the feed's own rule. + if parsed, ok := audit.ParseCursor(raw); ok { + cursor = parsed + } + } + limit := 0 + if raw := strings.TrimSpace(query.Get("limit")); raw != "" { + parsed, err := strconv.Atoi(raw) + if err != nil || parsed < 0 { + return audit.Filter{}, audit.Cursor{}, 0, fmt.Errorf("invalid limit") + } + limit = parsed + } + return filter, cursor, limit, nil +} + +func auditList(query url.Values, key string) []string { + var out []string + for _, value := range query[key] { + for part := range strings.SplitSeq(value, ",") { + if part = strings.TrimSpace(part); part != "" { + out = append(out, part) + } + } + } + return out +} + +// auditTime accepts the RFC3339 the dashboard sends and the local-style value +// a datetime control produces, treated as UTC rather than guessing a zone. +func auditTime(value string) (time.Time, error) { + value = strings.TrimSpace(value) + if value == "" { + return time.Time{}, nil + } + for _, layout := range []string{time.RFC3339Nano, "2006-01-02T15:04:05", "2006-01-02T15:04", "2006-01-02"} { + if parsed, err := time.Parse(layout, value); err == nil { + return parsed.UTC(), nil + } + } + return time.Time{}, fmt.Errorf("want an RFC3339 time") +} + +// auditLabels resolves the display names one response needs. Actor names and +// catalog titles are fetched once; workflow names are loaded only when a row +// actually targets a workflow. +type auditLabels struct { + actorName func(string) string + titles map[string]string + workflowFn func() map[string]string + workflows map[string]string +} + +func (l *auditLabels) workflow(id string) string { + if l.workflows == nil { + l.workflows = map[string]string{} + if l.workflowFn != nil { + maps.Copy(l.workflows, l.workflowFn()) + } + } + return l.workflows[id] +} + +// auditLabels builds the label resolver and the settings-kind facet in one +// catalog read. A failed catalog read degrades labels to their ids; it never +// fails the feed. +func (s *Server) auditLabels(ctx context.Context) (*auditLabels, []auditSettingsKind) { + labels := &auditLabels{actorName: s.actorNames(ctx), titles: map[string]string{}} + settings := []auditSettingsKind{} + for _, resource := range s.auditCatalogResources(ctx) { + if resource.GetTitle() != "" { + labels.titles[resource.GetKind()] = resource.GetTitle() + } + // Domain-managed kinds are read through their own service, so they + // never appear as a stored settings document. + if resource.GetQueryService() == "" { + settings = append(settings, auditSettingsKind{Kind: resource.GetKind(), Title: resource.GetTitle()}) + } + } + labels.workflowFn = func() map[string]string { + definitions, err := s.workflowDefinitions(ctx) + if err != nil { + return nil + } + names := make(map[string]string, len(definitions)) + for _, definition := range definitions { + names[definition.ID] = definition.Name + } + return names + } + return labels, settings +} + +// auditCatalogResources reads the control-plane catalogue once. A missing +// control plane or a failed read leaves labels at their ids rather than +// failing the feed. +func (s *Server) auditCatalogResources(ctx context.Context) []*controlpb.ResourceDescriptor { + if s.ControlPlane == nil { + return nil + } + catalog, err := s.ControlPlane.Catalog(ctx, &controlpb.CatalogRequest{}) + if err != nil { + return nil + } + return catalog.GetResources() +} + +func auditRecord(entry audit.Entry, labels *auditLabels) auditRecordView { + fields := make([]auditFieldView, 0, len(entry.Fields)) + for _, field := range entry.Fields { + fields = append(fields, auditFieldView{Field: field.Name, Old: jsonOrNull(field.Old), New: jsonOrNull(field.New)}) + } + return auditRecordView{ + ID: entry.ID(), + At: entry.At, + Category: string(entry.Category), + Action: entry.Action, + Kind: entry.Kind, + Severity: string(entry.Severity), + Outcome: string(entry.Outcome), + Actor: auditActorView{ + ID: entry.Actor, Name: labels.actorName(entry.Actor), + Kind: entry.ActorKind, PrincipalID: entry.PrincipalID, + }, + Target: auditTarget(entry, labels), + Summary: entry.Summary(), + Detail: auditDetailView{ + Fields: fields, Data: jsonOrNull(entry.Data), Version: entry.RecordVersion, + RequestID: entry.RequestID, Source: entry.Source, Table: auditEntryTable(entry), + }, + } +} + +func auditTarget(entry audit.Entry, labels *auditLabels) auditTargetView { + view := auditTargetView{Kind: entry.TargetKind, ID: entry.TargetID} + switch entry.TargetKind { + case "resource": + // A settings row's id is the control-plane kind the catalog names. + view.Label = labels.titles[entry.TargetID] + case "task": + if entry.TargetID != "" { + view.Label = "#" + entry.TargetID + view.Href = "/tasks?id=" + url.QueryEscape(entry.TargetID) + } + case "identity": + view.Label = labels.actorName(entry.TargetID) + view.Href = "/org/agents" + case "workflow": + view.Label = labels.workflow(entry.TargetID) + view.Href = "/workflows" + } + if view.Label == "" { + view.Label = auditTargetLabel(entry.TargetKind, entry.TargetID) + } + return view +} + +// auditTargetLabel is the fallback for a row the dashboard has no route or +// title for: the kind and id it names. +func auditTargetLabel(kind, id string) string { + switch { + case kind == "" && id == "": + return "" + case kind == "": + return id + case id == "": + return kind + default: + return kind + "/" + id + } +} + +// auditEntryTable is the audited table a row came from, for the detail view. +// The read model does not carry it, so it is derived from the branch that +// produced the row; coverage rows already carry their table name as the +// target kind. +func auditEntryTable(entry audit.Entry) string { + switch { + case entry.Category == audit.CategoryIdentity: + return "identity_events" + case entry.Category == audit.CategoryRuntime, entry.Category == audit.CategoryAutomation: + return "events" + case entry.Kind == "access_denial": + return "access_denials" + case entry.TargetKind == "policy": + return "access_policies" + case entry.TargetKind == "" || entry.TargetKind == "resource": + return "resources" + default: + return entry.TargetKind + } +} + +// writeAuditError maps the feed's transport errors to the dashboard's status +// vocabulary. An unreadable feed is unavailable, never an empty page. +func writeAuditError(w http.ResponseWriter, err error) { + code := http.StatusInternalServerError + switch status.Code(err) { + case codes.InvalidArgument: + code = http.StatusBadRequest + case codes.PermissionDenied: + code = http.StatusForbidden + case codes.Unavailable, codes.DeadlineExceeded: + code = http.StatusServiceUnavailable + } + message := firstLine(status.Convert(err).Message()) + if message == "" { + message = http.StatusText(code) + } + http.Error(w, message, code) +} + +func firstLine(message string) string { + line, _, _ := strings.Cut(message, "\n") + return strings.TrimSpace(line) +} + +// auditExportIdentity is the rate-limit key: the signed-in person. A service +// call acting for no person falls back to its org so it still has a budget. +func auditExportIdentity(ctx context.Context) string { + principal, ok := access.PrincipalFromContext(ctx) + if !ok { + return "unknown" + } + if principal.IdentityID != "" { + return string(principal.IdentityID) + } + if principal.Org != "" { + return "org:" + string(principal.Org) + } + return "unknown" +} + +func (s *Server) auditExportLimiter() *ratelimit.Limiter { + s.auditExportOnce.Do(func() { + s.auditExportRate = ratelimit.New(auditExportWindow, auditExportPerHour) + }) + return s.auditExportRate +} + +// beginAuditExport admits one export per identity. The guard is per identity, +// not process-wide: one person's long export must not stop another person in +// another org from taking their own. +func (s *Server) beginAuditExport(identity string) bool { + s.auditExportMu.Lock() + defer s.auditExportMu.Unlock() + if s.auditExporting == nil { + s.auditExporting = make(map[string]bool) + } + if s.auditExporting[identity] { + return false + } + s.auditExporting[identity] = true + return true +} + +func (s *Server) endAuditExport(identity string) { + s.auditExportMu.Lock() + delete(s.auditExporting, identity) + s.auditExportMu.Unlock() +} + +var auditCSVHeader = []string{ + "id", "at", "category", "action", "kind", "severity", "outcome", + "actor_id", "actor_name", "actor_kind", "principal_id", + "target_kind", "target_id", "target_label", "summary", + "fields", "data", "version", "request_id", "source", "table", +} + +func auditCSVRow(view auditRecordView) []string { + fields, _ := json.Marshal(view.Detail.Fields) + return []string{ + csvCell(view.ID), csvCell(view.At.UTC().Format(time.RFC3339Nano)), + csvCell(view.Category), csvCell(view.Action), csvCell(view.Kind), + csvCell(view.Severity), csvCell(view.Outcome), + csvCell(view.Actor.ID), csvCell(view.Actor.Name), csvCell(view.Actor.Kind), csvCell(view.Actor.PrincipalID), + csvCell(view.Target.Kind), csvCell(view.Target.ID), csvCell(view.Target.Label), + csvCell(view.Summary), csvCell(string(fields)), csvCell(string(view.Detail.Data)), + csvCell(strconv.FormatInt(view.Detail.Version, 10)), + csvCell(view.Detail.RequestID), csvCell(view.Detail.Source), csvCell(view.Detail.Table), + } +} + +// csvCell neutralises a spreadsheet formula. A cell a spreadsheet would +// evaluate is prefixed with an apostrophe, which every common spreadsheet +// reads as text. Quoting and newlines are encoding/csv's job. +func csvCell(value string) string { + if value == "" { + return value + } + switch value[0] { + case '=', '+', '-', '@', '\t', '\r': + return "'" + value + } + return value } func jsonOrNull(value []byte) json.RawMessage { diff --git a/internal/webui/api_audit_test.go b/internal/webui/api_audit_test.go new file mode 100644 index 000000000..1be8bda95 --- /dev/null +++ b/internal/webui/api_audit_test.go @@ -0,0 +1,90 @@ +package webui + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + "github.com/samcharles93/archie-core/internal/domain/audit" +) + +type auditStub struct { + page audit.Page + err error +} + +func (s auditStub) ListAudit(context.Context, audit.Filter, audit.Cursor, int) (audit.Page, error) { + return s.page, s.err +} + +func (s auditStub) AuditHead(context.Context) (string, error) { return "", nil } + +// TestAuditCSVCellNeutralisesFormulas pins the export's spreadsheet boundary: +// a value a spreadsheet would evaluate is text, while ordinary values are +// unchanged. +func TestAuditCSVCellNeutralisesFormulas(t *testing.T) { + tests := []struct { + name string + in string + want string + }{ + {"equals", "=1+1", "'=1+1"}, + {"plus", "+cmd", "'+cmd"}, + {"minus", "-2", "'-2"}, + {"at", "@SUM(A1)", "'@SUM(A1)"}, + {"tab", "\tcmd", "'\tcmd"}, + {"carriage return", "\r=cmd", "'\r=cmd"}, + {"ordinary", "alice", "alice"}, + {"empty", "", ""}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := csvCell(tt.in); got != tt.want { + t.Fatalf("csvCell(%q) = %q, want %q", tt.in, got, tt.want) + } + }) + } +} + +// TestAuditErrorMapping pins how a failed feed read reaches the browser: bad +// input is the caller's fault, a denial is forbidden, and an unreadable feed +// is unavailable rather than an empty page. +func TestAuditErrorMapping(t *testing.T) { + tests := []struct { + name string + err error + want int + body string + }{ + {"invalid filter", status.Error(codes.InvalidArgument, "invalid audit filter: unknown category \"nope\"\nsecond line"), http.StatusBadRequest, "invalid audit filter: unknown category \"nope\""}, + {"denied", status.Error(codes.PermissionDenied, "forbidden"), http.StatusForbidden, "forbidden"}, + {"unavailable", status.Error(codes.Unavailable, "state store unavailable"), http.StatusServiceUnavailable, "state store unavailable"}, + {"internal", status.Error(codes.Internal, "boom"), http.StatusInternalServerError, "boom"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + srv := &Server{Audit: auditStub{err: tt.err}} + rec := httptest.NewRecorder() + srv.handleAudit(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/audit", nil)) + if rec.Code != tt.want { + t.Fatalf("status = %d, want %d", rec.Code, tt.want) + } + if got := strings.TrimSpace(rec.Body.String()); got != tt.body { + t.Fatalf("body = %q, want %q", got, tt.body) + } + }) + } +} + +func TestAuditUnavailableWithoutFeed(t *testing.T) { + rec := httptest.NewRecorder() + (&Server{}).handleAudit(rec, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/audit", nil)) + if rec.Code != http.StatusServiceUnavailable { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusServiceUnavailable) + } +} diff --git a/internal/webui/api_control_plane.go b/internal/webui/api_control_plane.go index fd5017996..13d48739d 100644 --- a/internal/webui/api_control_plane.go +++ b/internal/webui/api_control_plane.go @@ -85,6 +85,8 @@ func (s *Server) handleControlPlaneHistory(w http.ResponseWriter, r *http.Reques revisions := make([]controlPlaneRevision, 0, len(response.Revisions)) actorName := s.actorNames(r.Context()) for _, revision := range response.Revisions { + // The value is real, not redacted: the dashboard stages a revision and + // applies it as a replace, so a redacted value would restore the marker. view := controlPlaneRevision{ Version: revision.Version, Value: revision.ValueJson, Actor: revision.Actor, ActorName: actorName(revision.Actor), Source: revision.Source, RequestID: revision.RequestId, diff --git a/internal/webui/authorization_surfaces_test.go b/internal/webui/authorization_surfaces_test.go index 78ab76838..1b6c81330 100644 --- a/internal/webui/authorization_surfaces_test.go +++ b/internal/webui/authorization_surfaces_test.go @@ -34,6 +34,8 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) { {"DELETE", "/api/tokens/7"}, {"GET", "/api/access/policies"}, {"GET", "/api/alerts"}, + {"GET", "/api/audit"}, + {"GET", "/api/audit/export"}, {"GET", "/api/authentication"}, {"GET", "/api/bindings"}, {"GET", "/api/bindings/7"}, @@ -49,7 +51,6 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) { {"GET", "/api/chat/update"}, {"GET", "/api/config"}, {"GET", "/api/control-plane/apply-status"}, - {"GET", "/api/control-plane/audit"}, {"GET", "/api/control-plane/catalog"}, {"GET", "/api/control-plane/resources/sample"}, {"GET", "/api/control-plane/resources/sample/history"}, @@ -161,3 +162,46 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) { }) } } + +// TestAuditRoutesFollowTheReadAuditRole pins the behavioural boundary the +// instance-forbid matrix cannot see: with the shipped org policies, a viewer +// and a developer are refused the audit feed and its export, while an admin +// is allowed through. +func TestAuditRoutesFollowTheReadAuditRole(t *testing.T) { + routes := []struct{ name, path string }{ + {"feed", "/api/audit"}, + {"export", "/api/audit/export?format=json&from=2026-01-01T00:00:00Z&to=2026-01-02T00:00:00Z"}, + } + tests := []struct { + role org.Role + want int + }{ + {org.RoleViewer, http.StatusForbidden}, + {org.RoleDeveloper, http.StatusForbidden}, + {org.RoleAdmin, http.StatusOK}, + } + for _, tt := range tests { + for _, route := range routes { + t.Run(string(tt.role)+" "+route.name, func(t *testing.T) { + principal := access.Principal{ + IdentityID: "alice", Org: "acme", + Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}}, + } + srv := &Server{ + Audit: auditStub{}, + Accounts: accountBoundaryStore{}, + Access: infraaccess.New(access.ShippedOrgPolicies("acme")), + Principals: principalOf(principal), + } + req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+route.path, nil) + req.Header.Set("X-Archie-CSRF", "1") + req.AddCookie(&http.Cookie{Name: "archie_session", Value: "valid"}) + rec := httptest.NewRecorder() + srv.Handler().ServeHTTP(rec, req) + if rec.Code != tt.want { + t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String()) + } + }) + } + } +} diff --git a/internal/webui/authorize.go b/internal/webui/authorize.go index 8a7dbf547..21e2775ef 100644 --- a/internal/webui/authorize.go +++ b/internal/webui/authorize.go @@ -264,6 +264,11 @@ func (*notWiredError) Error() string { return "webui: principal assembly not wir func accessRequest(r *http.Request) (access.Action, access.ResourceKind, string) { action := actionOf(r.Method) path := r.URL.Path + if matchPrefix(path, "/api/audit") { + // The feed spans every trail, so reading it is its own action: the + // export route below the same prefix carries the same grant. + return access.ActionReadAudit, access.KindAudit, "" + } if a, kind, id, ok := routeOverride(action, path); ok { return a, kind, id } diff --git a/internal/webui/server.go b/internal/webui/server.go index da122163f..04fa11390 100644 --- a/internal/webui/server.go +++ b/internal/webui/server.go @@ -119,6 +119,13 @@ type Server struct { // 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 + // auditExportOnce initialises auditExportRate; auditExporting and its + // mutex admit one export per identity. An export is bounded by its + // explicit range, a five-minute deadline and a page cap (see api_audit.go). + auditExportOnce sync.Once + auditExportRate *ratelimit.Limiter + auditExportMu sync.Mutex + auditExporting map[string]bool // 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 @@ -277,6 +284,8 @@ func (s *Server) registerCoreRoutes(mux *http.ServeMux) { mux.HandleFunc("POST /api/channels/{id}/reload", instanceOnly(s.handleChannelReload)) mux.HandleFunc("GET /api/version", s.handleVersion) mux.HandleFunc("GET /api/capabilities", s.handleCapabilities) + mux.HandleFunc("GET /api/audit", s.handleAudit) + mux.HandleFunc("GET /api/audit/export", s.handleAuditExport) mux.HandleFunc("GET /api/alerts", s.handleAlerts) mux.HandleFunc("POST /api/alerts/{id}/dismiss", s.handleAlertDismiss) } @@ -359,7 +368,6 @@ func (s *Server) registerConfigAndLogRoutes(mux *http.ServeMux) { mux.HandleFunc("GET /api/control-plane/resources/{kind}", s.handleControlPlaneQuery) mux.HandleFunc("GET /api/control-plane/resources/{kind}/history", s.handleControlPlaneHistory) mux.HandleFunc("POST /api/control-plane/resources/{kind}/commands/{command}", s.handleControlPlaneCommand) - mux.HandleFunc("GET /api/control-plane/audit", s.handleAudit) mux.HandleFunc("GET /api/identities", instanceOnly(s.handleIdentitiesList)) mux.HandleFunc("GET /api/orgs", s.handleOrgsList) mux.HandleFunc("POST /api/orgs", s.handleOrgCreate) diff --git a/internal/webui/sse.go b/internal/webui/sse.go index 8163913b7..d7053b24d 100644 --- a/internal/webui/sse.go +++ b/internal/webui/sse.go @@ -6,6 +6,7 @@ import ( "encoding/json" "log/slog" "net/http" + "time" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/events" @@ -62,7 +63,50 @@ func (s *Server) handleSSE(w http.ResponseWriter, r *http.Request) { if wantLogs && !stream.sendLogSnapshot(s.LogFeed) { return } - stream.drain(r.Context(), conn, stale, logs) + // The audit head is computed per connection with this viewer's principal, + // so it covers exactly the rows that viewer may read. It is not a hub + // topic: a shared token would be one org's activity for every viewer. + var auditHead <-chan liveUpdate + if s.Audit != nil { + updates := make(chan liveUpdate) + go s.pollAuditHead(r.Context(), updates) + auditHead = updates + } + stream.drain(r.Context(), conn, stale, logs, auditHead) +} + +// pollAuditHead announces the feed's change token on a connection's stream. +// It runs on the authenticated request's context, which carries the principal +// the feed scopes by. The topic name and payload shape are unchanged. +func (s *Server) pollAuditHead(ctx context.Context, out chan<- liveUpdate) { + ticker := time.NewTicker(2 * time.Second) + defer ticker.Stop() + defer close(out) + send := func() bool { + token, err := s.Audit.AuditHead(ctx) + if err != nil { + return true + } + select { + case out <- liveUpdate{topic: "audit-head", data: map[string]string{"token": token}}: + return true + case <-ctx.Done(): + return false + } + } + if !send() { + return + } + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + if !send() { + return + } + } + } } type streamCursors struct { @@ -229,7 +273,7 @@ func (s *sseStream) sendPage(backlog []events.Event, target string) (reachedTarg } // drain relays broadcasts after catch-up, filling any persisted gap first. -func (s *sseStream) drain(ctx context.Context, conn <-chan liveUpdate, stale <-chan struct{}, logs <-chan logging.Entry) { +func (s *sseStream) drain(ctx context.Context, conn <-chan liveUpdate, stale <-chan struct{}, logs <-chan logging.Entry, auditHead <-chan liveUpdate) { for { select { case <-ctx.Done(): @@ -244,6 +288,16 @@ func (s *sseStream) drain(ctx context.Context, conn <-chan liveUpdate, stale <-c if !s.sendLog(entry) { return } + case update, ok := <-auditHead: + if !ok { + auditHead = nil + continue + } + // Already scoped to this viewer by the context it was computed + // with, so it does not go through the instance-only gate. + if !s.writeLive(update, "") { + return + } case update := <-conn: if update.topic == "tasks" { e, ok := update.data.(events.Event) diff --git a/proto/buf.yaml b/proto/buf.yaml index 35b4d3fc3..c572960d3 100644 --- a/proto/buf.yaml +++ b/proto/buf.yaml @@ -34,15 +34,27 @@ breaking: # Personas were removed: ChatService.SetPersona, its messages and the # snapshot's persona fields (numbers and names reserved). The gateway's only # clients ship from this repository. + # + # The control plane's field-level Audit RPC and its AuditEntry, AuditRequest + # and AuditResponse messages were removed in favour of the unified feed in + # state/v1/audit.proto. The State Store and every client ship together, so + # this is a deliberate break with no compatibility window. ignore_only: FIELD_SAME_TYPE: - state/v1 RPC_NO_DELETE: - state/v1 + - controlplane/v1/controlplane.proto - gateway/v1/chat.proto MESSAGE_NO_DELETE: - state/v1 + - controlplane/v1/controlplane.proto - gateway/v1/chat.proto + # breaking.use is PACKAGE, so a deleted message is reported by the + # package-scoped form of the rule; MESSAGE_NO_DELETE itself only covers + # CSR and FILE scopes. + PACKAGE_MESSAGE_NO_DELETE: + - controlplane/v1/controlplane.proto FIELD_NO_DELETE: - state/v1 - controlplane/v1/controlplane.proto diff --git a/proto/controlplane/v1/controlplane.proto b/proto/controlplane/v1/controlplane.proto index 5bed5a9ab..930e0f6aa 100644 --- a/proto/controlplane/v1/controlplane.proto +++ b/proto/controlplane/v1/controlplane.proto @@ -8,7 +8,6 @@ service ControlPlaneService { rpc Catalog(CatalogRequest) returns (CatalogResponse); rpc Query(QueryRequest) returns (QueryResponse); rpc History(HistoryRequest) returns (HistoryResponse); - rpc Audit(AuditRequest) returns (AuditResponse); rpc Command(CommandRequest) returns (CommandResponse); rpc Watch(WatchRequest) returns (stream WatchResponse); } @@ -67,32 +66,11 @@ message HistoryRequest { } message HistoryResponse { repeated Revision revisions = 1; } -// AuditEntry is one changed field of one record. Old and new values are JSON; -// an absent side is empty. -message AuditEntry { - int64 id = 1; - string table = 2; - string record_key = 3; - string field = 4; - bytes old_value_json = 5; - bytes new_value_json = 6; - int64 version = 7; - string actor = 8; - string source = 9; - string request_id = 10; - google.protobuf.Timestamp at = 11; -} -// AuditRequest names resource kinds; the trail returned is theirs in the -// caller's org, so no request can name another org's records. -message AuditRequest { - reserved 1, 2; - reserved "table", "record_keys"; - repeated string kinds = 4; - // Limit caps the entries returned, newest first. Zero means the server's - // own default. - int32 limit = 3; -} -message AuditResponse { repeated AuditEntry entries = 1; } +// Removed: the field-level Audit RPC and its AuditEntry, AuditRequest and +// AuditResponse messages. The unified org-scoped feed in state/v1/audit.proto +// supersedes them. The State Store and every client ship from this repository +// together, so this is a deliberate break with no compatibility window; the +// numbers stay unassigned rather than being reused for another meaning. message CommandRequest { string kind = 1;