fix(audit): harden export, scope history and derive feed targets

This commit is contained in:
sam committed 2026-10-25 22:39:22 +11:00
1 parent c04a18d387
commit 6a69fb760a
16 files changed
+1460 -170

No files matched your search

+15 -2
View File
@@ -70,12 +70,25 @@ func NewServer(resources ResourceStore, steps *workflow.Manager) (*Server, error
return &Server{store: resources, definitions: byKind, ordered: definitions, steps: registry}, nil
}
func (s *Server) Catalog(context.Context, *pb.CatalogRequest) (*pb.CatalogResponse, error) {
// Catalog advertises the resources the caller's org can use. A definition is
// instance-only when the runtime layers it from the system org's copy alone,
// so another org would be offered a control whose write is refused; hide it
// there. Query and Command still enforce the same rule server-side.
func (s *Server) Catalog(ctx context.Context, _ *pb.CatalogRequest) (*pb.CatalogResponse, error) {
instanceOrg := requestOrg(ctx) == storecontract.DefaultOrgID
descriptors := make([]*pb.ResourceDescriptor, 0, len(s.ordered)+len(domainManagedDescriptors()))
for _, definition := range s.ordered {
if instanceOnlyKinds[definition.Kind] && !instanceOrg {
continue
}
descriptors = append(descriptors, definition.Descriptor())
}
descriptors = append(descriptors, domainManagedDescriptors()...)
for _, descriptor := range domainManagedDescriptors() {
if instanceOnlyKinds[descriptor.GetKind()] && !instanceOrg {
continue
}
descriptors = append(descriptors, descriptor)
}
return &pb.CatalogResponse{Resources: descriptors}, nil
}
@@ -4,6 +4,8 @@ import (
"errors"
"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"
)
@@ -26,3 +28,41 @@ func TestInstanceOnlyKindsRefuseOtherOrgs(t *testing.T) {
})
}
}
// TestCatalogHidesInstanceOnlyKinds pins what the catalog advertises: the
// system org sees an instance setting, another org does not (its write would
// be refused), and an org-scoped kind stays visible to both.
func TestCatalogHidesInstanceOnlyKinds(t *testing.T) {
server := &Server{ordered: []Definition{
{Kind: AuditRetentionKind, Title: "Audit retention"},
{Kind: SoulKind, Title: "Soul"},
}}
tests := []struct {
name string
org org.OrgID
wantRetention bool
wantOrgResource bool
}{
{"system org sees the instance setting", org.DefaultOrgID, true, true},
{"another org does not", "acme", false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := org.WithOrg(t.Context(), tt.org)
catalog, err := server.Catalog(ctx, &pb.CatalogRequest{})
if err != nil {
t.Fatalf("Catalog() error = %v", err)
}
visible := make(map[string]bool, len(catalog.GetResources()))
for _, descriptor := range catalog.GetResources() {
visible[descriptor.GetKind()] = true
}
if got := visible[AuditRetentionKind]; got != tt.wantRetention {
t.Fatalf("%s visible = %v, want %v", AuditRetentionKind, got, tt.wantRetention)
}
if got := visible[SoulKind]; got != tt.wantOrgResource {
t.Fatalf("%s visible = %v, want %v", SoulKind, got, tt.wantOrgResource)
}
})
}
}
+234 -27
View File
@@ -617,6 +617,192 @@ func (x *GetAuditHeadResponse) GetToken() string {
return ""
}
type CountAuditRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Filter *AuditFilter `protobuf:"bytes,1,opt,name=filter,proto3" json:"filter,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CountAuditRequest) Reset() {
*x = CountAuditRequest{}
mi := &file_state_v1_audit_proto_msgTypes[7]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *CountAuditRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CountAuditRequest) ProtoMessage() {}
func (x *CountAuditRequest) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[7]
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 CountAuditRequest.ProtoReflect.Descriptor instead.
func (*CountAuditRequest) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{7}
}
func (x *CountAuditRequest) GetFilter() *AuditFilter {
if x != nil {
return x.Filter
}
return nil
}
type CountAuditResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
Count int64 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CountAuditResponse) Reset() {
*x = CountAuditResponse{}
mi := &file_state_v1_audit_proto_msgTypes[8]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *CountAuditResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CountAuditResponse) ProtoMessage() {}
func (x *CountAuditResponse) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_proto_msgTypes[8]
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 CountAuditResponse.ProtoReflect.Descriptor instead.
func (*CountAuditResponse) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{8}
}
func (x *CountAuditResponse) GetCount() int64 {
if x != nil {
return x.Count
}
return 0
}
type RecordAuditExportRequest struct {
state protoimpl.MessageState `protogen:"open.v1"`
Format string `protobuf:"bytes,1,opt,name=format,proto3" json:"format,omitempty"`
// filters_hash is the normalised filter's fingerprint, so an operator can
// tell one exported view from another without storing the filter itself.
FiltersHash string `protobuf:"bytes,2,opt,name=filters_hash,json=filtersHash,proto3" json:"filters_hash,omitempty"`
Rows int64 `protobuf:"varint,3,opt,name=rows,proto3" json:"rows,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *RecordAuditExportRequest) Reset() {
*x = RecordAuditExportRequest{}
mi := &file_state_v1_audit_proto_msgTypes[9]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *RecordAuditExportRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*RecordAuditExportRequest) ProtoMessage() {}
func (x *RecordAuditExportRequest) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_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 RecordAuditExportRequest.ProtoReflect.Descriptor instead.
func (*RecordAuditExportRequest) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{9}
}
func (x *RecordAuditExportRequest) GetFormat() string {
if x != nil {
return x.Format
}
return ""
}
func (x *RecordAuditExportRequest) GetFiltersHash() string {
if x != nil {
return x.FiltersHash
}
return ""
}
func (x *RecordAuditExportRequest) GetRows() int64 {
if x != nil {
return x.Rows
}
return 0
}
type RecordAuditExportResponse struct {
state protoimpl.MessageState `protogen:"open.v1"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *RecordAuditExportResponse) Reset() {
*x = RecordAuditExportResponse{}
mi := &file_state_v1_audit_proto_msgTypes[10]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *RecordAuditExportResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*RecordAuditExportResponse) ProtoMessage() {}
func (x *RecordAuditExportResponse) ProtoReflect() protoreflect.Message {
mi := &file_state_v1_audit_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 RecordAuditExportResponse.ProtoReflect.Descriptor instead.
func (*RecordAuditExportResponse) Descriptor() ([]byte, []int) {
return file_state_v1_audit_proto_rawDescGZIP(), []int{10}
}
var File_state_v1_audit_proto protoreflect.FileDescriptor
const file_state_v1_audit_proto_rawDesc = "" +
@@ -682,10 +868,22 @@ const file_state_v1_audit_proto_rawDesc = "" +
"\bhas_more\x18\x03 \x01(\bR\ahasMore\"\x15\n" +
"\x13GetAuditHeadRequest\",\n" +
"\x14GetAuditHeadResponse\x12\x14\n" +
"\x05token\x18\x01 \x01(\tR\x05token2\xa3\x01\n" +
"\x05token\x18\x01 \x01(\tR\x05token\"B\n" +
"\x11CountAuditRequest\x12-\n" +
"\x06filter\x18\x01 \x01(\v2\x15.state.v1.AuditFilterR\x06filter\"*\n" +
"\x12CountAuditResponse\x12\x14\n" +
"\x05count\x18\x01 \x01(\x03R\x05count\"i\n" +
"\x18RecordAuditExportRequest\x12\x16\n" +
"\x06format\x18\x01 \x01(\tR\x06format\x12!\n" +
"\ffilters_hash\x18\x02 \x01(\tR\vfiltersHash\x12\x12\n" +
"\x04rows\x18\x03 \x01(\x03R\x04rows\"\x1b\n" +
"\x19RecordAuditExportResponse2\xca\x02\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" +
"\fGetAuditHead\x12\x1d.state.v1.GetAuditHeadRequest\x1a\x1e.state.v1.GetAuditHeadResponse\x12G\n" +
"\n" +
"CountAudit\x12\x1b.state.v1.CountAuditRequest\x1a\x1c.state.v1.CountAuditResponse\x12\\\n" +
"\x11RecordAuditExport\x12\".state.v1.RecordAuditExportRequest\x1a#.state.v1.RecordAuditExportResponseB\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"
@@ -701,33 +899,42 @@ func file_state_v1_audit_proto_rawDescGZIP() []byte {
return file_state_v1_audit_proto_rawDescData
}
var file_state_v1_audit_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
var file_state_v1_audit_proto_msgTypes = make([]protoimpl.MessageInfo, 11)
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
(*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
(*CountAuditRequest)(nil), // 7: state.v1.CountAuditRequest
(*CountAuditResponse)(nil), // 8: state.v1.CountAuditResponse
(*RecordAuditExportRequest)(nil), // 9: state.v1.RecordAuditExportRequest
(*RecordAuditExportResponse)(nil), // 10: state.v1.RecordAuditExportResponse
(*timestamppb.Timestamp)(nil), // 11: 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
11, // 0: state.v1.AuditFilter.from:type_name -> google.protobuf.Timestamp
11, // 1: state.v1.AuditFilter.to:type_name -> google.protobuf.Timestamp
0, // 2: state.v1.ListAuditRequest.filter:type_name -> state.v1.AuditFilter
11, // 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
0, // 6: state.v1.CountAuditRequest.filter:type_name -> state.v1.AuditFilter
1, // 7: state.v1.AuditService.ListAudit:input_type -> state.v1.ListAuditRequest
5, // 8: state.v1.AuditService.GetAuditHead:input_type -> state.v1.GetAuditHeadRequest
7, // 9: state.v1.AuditService.CountAudit:input_type -> state.v1.CountAuditRequest
9, // 10: state.v1.AuditService.RecordAuditExport:input_type -> state.v1.RecordAuditExportRequest
4, // 11: state.v1.AuditService.ListAudit:output_type -> state.v1.ListAuditResponse
6, // 12: state.v1.AuditService.GetAuditHead:output_type -> state.v1.GetAuditHeadResponse
8, // 13: state.v1.AuditService.CountAudit:output_type -> state.v1.CountAuditResponse
10, // 14: state.v1.AuditService.RecordAuditExport:output_type -> state.v1.RecordAuditExportResponse
11, // [11:15] is the sub-list for method output_type
7, // [7:11] 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_state_v1_audit_proto_init() }
@@ -741,7 +948,7 @@ func file_state_v1_audit_proto_init() {
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,
NumMessages: 11,
NumExtensions: 0,
NumServices: 1,
},
+90 -2
View File
@@ -19,8 +19,10 @@ import (
const _ = grpc.SupportPackageIsVersion9
const (
AuditService_ListAudit_FullMethodName = "/state.v1.AuditService/ListAudit"
AuditService_GetAuditHead_FullMethodName = "/state.v1.AuditService/GetAuditHead"
AuditService_ListAudit_FullMethodName = "/state.v1.AuditService/ListAudit"
AuditService_GetAuditHead_FullMethodName = "/state.v1.AuditService/GetAuditHead"
AuditService_CountAudit_FullMethodName = "/state.v1.AuditService/CountAudit"
AuditService_RecordAuditExport_FullMethodName = "/state.v1.AuditService/RecordAuditExport"
)
// AuditServiceClient is the client API for AuditService service.
@@ -38,6 +40,14 @@ type AuditServiceClient interface {
// 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)
// CountAudit returns how many entries the filter matches, without a cursor
// or a page bound. The export preflights it so an oversized download is
// refused before any row is streamed.
CountAudit(ctx context.Context, in *CountAuditRequest, opts ...grpc.CallOption) (*CountAuditResponse, error)
// RecordAuditExport appends the audit row of one completed export: its
// format, the filter fingerprint and the row count, attributed to the
// caller. The State Store writes it; the dashboard never does.
RecordAuditExport(ctx context.Context, in *RecordAuditExportRequest, opts ...grpc.CallOption) (*RecordAuditExportResponse, error)
}
type auditServiceClient struct {
@@ -68,6 +78,26 @@ func (c *auditServiceClient) GetAuditHead(ctx context.Context, in *GetAuditHeadR
return out, nil
}
func (c *auditServiceClient) CountAudit(ctx context.Context, in *CountAuditRequest, opts ...grpc.CallOption) (*CountAuditResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(CountAuditResponse)
err := c.cc.Invoke(ctx, AuditService_CountAudit_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *auditServiceClient) RecordAuditExport(ctx context.Context, in *RecordAuditExportRequest, opts ...grpc.CallOption) (*RecordAuditExportResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(RecordAuditExportResponse)
err := c.cc.Invoke(ctx, AuditService_RecordAuditExport_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.
@@ -83,6 +113,14 @@ type AuditServiceServer interface {
// 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)
// CountAudit returns how many entries the filter matches, without a cursor
// or a page bound. The export preflights it so an oversized download is
// refused before any row is streamed.
CountAudit(context.Context, *CountAuditRequest) (*CountAuditResponse, error)
// RecordAuditExport appends the audit row of one completed export: its
// format, the filter fingerprint and the row count, attributed to the
// caller. The State Store writes it; the dashboard never does.
RecordAuditExport(context.Context, *RecordAuditExportRequest) (*RecordAuditExportResponse, error)
mustEmbedUnimplementedAuditServiceServer()
}
@@ -99,6 +137,12 @@ func (UnimplementedAuditServiceServer) ListAudit(context.Context, *ListAuditRequ
func (UnimplementedAuditServiceServer) GetAuditHead(context.Context, *GetAuditHeadRequest) (*GetAuditHeadResponse, error) {
return nil, status.Error(codes.Unimplemented, "method GetAuditHead not implemented")
}
func (UnimplementedAuditServiceServer) CountAudit(context.Context, *CountAuditRequest) (*CountAuditResponse, error) {
return nil, status.Error(codes.Unimplemented, "method CountAudit not implemented")
}
func (UnimplementedAuditServiceServer) RecordAuditExport(context.Context, *RecordAuditExportRequest) (*RecordAuditExportResponse, error) {
return nil, status.Error(codes.Unimplemented, "method RecordAuditExport not implemented")
}
func (UnimplementedAuditServiceServer) mustEmbedUnimplementedAuditServiceServer() {}
func (UnimplementedAuditServiceServer) testEmbeddedByValue() {}
@@ -156,6 +200,42 @@ func _AuditService_GetAuditHead_Handler(srv interface{}, ctx context.Context, de
return interceptor(ctx, in, info, handler)
}
func _AuditService_CountAudit_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(CountAuditRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(AuditServiceServer).CountAudit(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: AuditService_CountAudit_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(AuditServiceServer).CountAudit(ctx, req.(*CountAuditRequest))
}
return interceptor(ctx, in, info, handler)
}
func _AuditService_RecordAuditExport_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(RecordAuditExportRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(AuditServiceServer).RecordAuditExport(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: AuditService_RecordAuditExport_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(AuditServiceServer).RecordAuditExport(ctx, req.(*RecordAuditExportRequest))
}
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)
@@ -171,6 +251,14 @@ var AuditService_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetAuditHead",
Handler: _AuditService_GetAuditHead_Handler,
},
{
MethodName: "CountAudit",
Handler: _AuditService_CountAudit_Handler,
},
{
MethodName: "RecordAuditExport",
Handler: _AuditService_RecordAuditExport_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "state/v1/audit.proto",
+11 -1
View File
@@ -110,6 +110,10 @@ func TargetKinds() []string {
"orgs", "workspaces", "memberships", "org_agents",
"mappings", "bindings", "event_types", "sources",
"installed_packages", "harness_secrets",
// audit_retention is the retention sweep's coverage table, audit is
// the resource kind a denied feed read names, and audit_export is the
// row a completed export writes; all reach the feed as target kinds.
"audit_retention", "audit", "audit_export",
}
}
@@ -183,14 +187,20 @@ type Page struct {
HasMore bool
}
// Store is the audit feed's read surface. The org is derived from the request
// Store is the audit feed's 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)
// CountAudit returns how many entries the filter matches, without a
// cursor or a page bound.
CountAudit(ctx context.Context, filter Filter) (int64, 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)
// RecordAuditExport appends one audit row recording a completed export,
// attributed to the caller.
RecordAuditExport(ctx context.Context, format, filterHash string, rows int64) error
}
// instanceAdminKey carries the authenticated principal's instance-wide
@@ -100,6 +100,57 @@ func (s *Store) AuditHead(ctx context.Context) (string, error) {
row.SysAuditID, row.IdentityEventID, row.AccessDenialID, row.AccessDenialCount, row.EventID), nil
}
// CountAudit returns how many entries a filter matches, with the same org and
// instance scoping as a page. It counts change sets, not field rows, so the
// number is exactly what an export of that filter would stream.
func (s *Store) CountAudit(ctx context.Context, filter audit.Filter) (int64, error) {
if err := filter.Normalize(); err != nil {
return 0, err
}
orgID := org.DefaultOrgID
if scoped, ok := org.Scope(ctx); ok {
orgID = scoped
}
count, err := s.queries().CountAudit(ctx, postgresdb.CountAuditParams{
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),
})
if err != nil {
return 0, fmt.Errorf("count audit: %w", err)
}
return count, nil
}
// auditExportRecord is the value stored on a completed export's audit row.
type auditExportRecord struct {
Format string `json:"format"`
FiltersHash string `json:"filters_hash"`
Rows int64 `json:"rows"`
}
// RecordAuditExport appends the audit row of one completed export. The org,
// actor and source come from the caller's context, as every sys_audit write
// does.
func (s *Store) RecordAuditExport(ctx context.Context, format, filterHash string, rows int64) error {
orgID := org.DefaultOrgID
if scoped, ok := org.Scope(ctx); ok {
orgID = scoped
}
return insertAuditValues(ctx, s.queries(), "audit_export", "export", "export", nil,
auditExportRecord{Format: format, FiltersHash: filterHash, Rows: rows}, 0, string(orgID))
}
func auditTime(t time.Time) pgtype.Timestamptz {
if t.IsZero() {
return pgtype.Timestamptz{}
@@ -5,40 +5,29 @@
--
-- 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.
-- those created at or before it. Memberships and agent assignments are counted
-- together, matching the writer's union: an event is attributed only when
-- exactly one distinct 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. A row with no qualifying org stays NULL and is visible
-- only to an instance administrator, so rows written before and after the
-- upgrade agree.
-- +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
UPDATE identity_events ie SET org_id = (
SELECT min(b.org_id) FROM (
SELECT 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
UNION
SELECT 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;
) b
HAVING count(DISTINCT b.org_id) = 1
);
-- 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.
@@ -52,6 +52,363 @@ func (q *Queries) AuditHead(ctx context.Context, arg AuditHeadParams) (AuditHead
return i, err
}
const countAudit = `-- name: CountAudit :one
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,
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 = $11::text
AND (
COALESCE(cardinality($1::text[]), 0) = 0
OR (a.table_name = 'resources' AND 'settings' = ANY($1::text[]))
OR (a.table_name = 'access_policies' AND 'access' = ANY($1::text[]))
OR (a.table_name IN ('memberships', 'org_agents') AND 'access' = ANY($1::text[]))
OR (a.table_name IN ('bindings', 'mappings', 'event_types') AND 'automation' = ANY($1::text[]))
OR (
a.table_name NOT IN (
'resources', 'access_policies',
'memberships', 'org_agents',
'bindings', 'mappings', 'event_types'
)
AND 'settings' = ANY($1::text[])
)
)
AND ($12::timestamptz IS NULL OR a.at >= $12::timestamptz)
AND ($13::timestamptz IS NULL OR a.at < $13::timestamptz)
AND ($4::text IS NULL OR a.actor = $4::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 count(*)::bigint AS count
FROM (
SELECT
CASE t.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
CASE
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'reset'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.record_version > 0 THEN 'delete'
WHEN t.table_name = 'access_policies' AND t.all_old_null THEN 'create'
WHEN t.table_name = 'resources' AND t.record_version = 1 AND t.all_old_null THEN 'create'
ELSE 'update'
END AS action,
CASE t.table_name
WHEN 'resources' THEN t.target_id
ELSE 'access_policy'
END AS kind,
t.actor,
CASE t.table_name
WHEN 'resources' THEN 'resource'
ELSE 'policy'
END AS target_kind,
t.target_id,
CASE
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'warning'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.record_version > 0 THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
concat_ws(' ', t.record_key, t.actor, t.source, t.field_names, t.target_id) AS search_text
FROM (
SELECT
g.org_id, g.table_name, g.record_key, g.request_id,
g.record_version, g.at, g.actor, g.source,
g.all_old_null, g.all_new_null, g.is_reset,
g.field_count, g.whole_record_field, g.field_names,
CASE g.table_name
WHEN 'resources' THEN
CASE
WHEN left(g.record_key, length(g.org_id) + 1) = g.org_id || '/'
THEN substr(g.record_key, length(g.org_id) + 2)
WHEN position('/' IN g.record_key) > 0
THEN regexp_replace(g.record_key, '^.*/', '')
ELSE g.record_key
END
ELSE
CASE
WHEN left(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 2
) = split_part(g.record_key, '/', 1) || '/' || g.org_id || '/'
THEN ltrim(substr(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 3
), '/')
ELSE g.record_key
END
END::text AS target_id
FROM sys_changes g
WHERE g.table_name IN ('resources', 'access_policies')
) t
WHERE (COALESCE(cardinality($1::text[]), 0) = 0 OR category = ANY($1::text[]))
AND (COALESCE(cardinality($2::text[]), 0) = 0 OR action = ANY($2::text[]))
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR kind = ANY($3::text[]))
AND ($4::text IS NULL OR actor = $4::text)
AND (COALESCE(cardinality($5::text[]), 0) = 0 OR target_kind = ANY($5::text[]))
AND (COALESCE(cardinality($6::text[]), 0) = 0 OR target_id = ANY($6::text[]))
AND (COALESCE(cardinality($7::text[]), 0) = 0 OR severity = ANY($7::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR outcome = ANY($8::text[]))
AND ($9::text IS NULL OR position(lower($9::text) IN lower(search_text)) > 0)
UNION ALL
SELECT
'identity'::text AS category,
event_type AS action,
event_type AS kind,
actor_id AS actor,
'identity'::text AS target_kind,
identity_id::text AS target_id,
CASE WHEN event_type IN ('suspend', 'retire') THEN 'warning' ELSE 'info' END AS severity,
'success'::text AS outcome,
concat_ws(' ', identity_id, event_type, display_name, actor_id, source) AS search_text
FROM identity_events
WHERE event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND ($10::boolean
OR (org_id IS NOT NULL AND org_id = $11::text))
AND ($12::timestamptz IS NULL OR at >= $12::timestamptz)
AND ($13::timestamptz IS NULL OR at < $13::timestamptz)
AND (COALESCE(cardinality($1::text[]), 0) = 0 OR 'identity' = ANY($1::text[]))
AND (COALESCE(cardinality($2::text[]), 0) = 0 OR event_type = ANY($2::text[]))
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR event_type = ANY($3::text[]))
AND ($4::text IS NULL OR actor_id = $4::text)
AND (COALESCE(cardinality($5::text[]), 0) = 0 OR 'identity' = ANY($5::text[]))
AND (COALESCE(cardinality($6::text[]), 0) = 0 OR identity_id::text = ANY($6::text[]))
AND (
COALESCE(cardinality($7::text[]), 0) = 0
OR (CASE WHEN event_type IN ('suspend', 'retire') THEN 'warning' ELSE 'info' END) = ANY($7::text[])
)
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR 'success' = ANY($8::text[]))
AND ($9::text IS NULL OR position(lower($9::text) IN lower(concat_ws(' ', identity_id, event_type, display_name, actor_id, source))) > 0)
UNION ALL
SELECT
'access'::text AS category,
'deny'::text AS action,
'access_denial'::text AS kind,
principal AS actor,
resource_kind AS target_kind,
resource_id::text AS target_id,
'warning'::text AS severity,
'denied'::text AS outcome,
concat_ws(' ', principal, action, resource_kind, resource_id, level, array_to_string(policies, ' ')) AS search_text
FROM access_denials
WHERE org_id = $11::text
AND ($12::timestamptz IS NULL OR minute_at >= $12::timestamptz)
AND ($13::timestamptz IS NULL OR minute_at < $13::timestamptz)
AND (COALESCE(cardinality($1::text[]), 0) = 0 OR 'access' = ANY($1::text[]))
AND (COALESCE(cardinality($2::text[]), 0) = 0 OR 'deny' = ANY($2::text[]))
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR 'access_denial' = ANY($3::text[]))
AND ($4::text IS NULL OR principal = $4::text)
AND (COALESCE(cardinality($5::text[]), 0) = 0 OR resource_kind = ANY($5::text[]))
AND (COALESCE(cardinality($6::text[]), 0) = 0 OR resource_id::text = ANY($6::text[]))
AND (COALESCE(cardinality($7::text[]), 0) = 0 OR 'warning' = ANY($7::text[]))
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR 'denied' = ANY($8::text[]))
AND ($9::text IS NULL OR position(lower($9::text) IN lower(concat_ws(' ', principal, action, resource_kind, resource_id, level, array_to_string(policies, ' ')))) > 0)
UNION ALL
SELECT
CASE
WHEN 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,
kind AS action,
kind,
actor_id AS actor,
CASE WHEN task_id > 0 THEN 'task' ELSE '' END AS target_kind,
(CASE WHEN task_id > 0 THEN task_id::text ELSE '' END)::text AS target_id,
CASE
WHEN 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 kind = ANY(ARRAY[
'parked', 'task_retried', 'task_cancelled', 'task_stopped',
'alert', 'update_report'
]::text[]) THEN 'warning'
ELSE 'info'
END AS severity,
CASE
WHEN 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,
concat_ws(' ', kind, actor_id, actor_kind, repo, workflow, stage, task_id::text) AS search_text
FROM events
WHERE org_id = $11::text
AND ($12::timestamptz IS NULL OR at >= $12::timestamptz)
AND ($13::timestamptz IS NULL OR at < $13::timestamptz)
AND (
COALESCE(cardinality($1::text[]), 0) = 0
OR (CASE WHEN 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) = ANY($1::text[])
)
AND (COALESCE(cardinality($2::text[]), 0) = 0 OR kind = ANY($2::text[]))
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR kind = ANY($3::text[]))
AND ($4::text IS NULL OR actor_id = $4::text)
AND (
COALESCE(cardinality($5::text[]), 0) = 0
OR (CASE WHEN task_id > 0 THEN 'task' ELSE '' END) = ANY($5::text[])
)
AND (
COALESCE(cardinality($6::text[]), 0) = 0
OR (CASE WHEN task_id > 0 THEN task_id::text ELSE '' END) = ANY($6::text[])
)
AND (
COALESCE(cardinality($7::text[]), 0) = 0
OR (CASE
WHEN 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 kind = ANY(ARRAY[
'parked', 'task_retried', 'task_cancelled', 'task_stopped',
'alert', 'update_report'
]::text[]) THEN 'warning'
ELSE 'info'
END) = ANY($7::text[])
)
AND (
COALESCE(cardinality($8::text[]), 0) = 0
OR (CASE
WHEN 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) = ANY($8::text[])
)
AND ($9::text IS NULL OR position(lower($9::text) IN lower(concat_ws(' ', kind, actor_id, actor_kind, repo, workflow, stage, task_id::text))) > 0)
UNION ALL
SELECT
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,
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,
g.table_name AS kind,
g.actor,
g.table_name::text AS target_kind,
g.record_key::text AS target_id,
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,
concat_ws(' ', g.record_key, g.actor, g.source, g.field_names, g.table_name) AS search_text
FROM sys_changes g
WHERE g.table_name NOT IN ('resources', 'access_policies')
AND (
COALESCE(cardinality($1::text[]), 0) = 0
OR (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) = ANY($1::text[])
)
AND (
COALESCE(cardinality($2::text[]), 0) = 0
OR (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) = ANY($2::text[])
)
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR g.table_name = ANY($3::text[]))
AND ($4::text IS NULL OR g.actor = $4::text)
AND (COALESCE(cardinality($5::text[]), 0) = 0 OR g.table_name = ANY($5::text[]))
AND (COALESCE(cardinality($6::text[]), 0) = 0 OR g.record_key = ANY($6::text[]))
AND (
COALESCE(cardinality($7::text[]), 0) = 0
OR (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) = ANY($7::text[])
)
AND (COALESCE(cardinality($8::text[]), 0) = 0 OR 'success' = ANY($8::text[]))
AND ($9::text IS NULL OR position(lower($9::text) IN lower(concat_ws(' ', g.record_key, g.actor, g.source, g.field_names, g.table_name))) > 0)
) branches
`
type CountAuditParams struct {
Categories []string
Actions []string
Kinds []string
Actor pgtype.Text
TargetKinds []string
TargetIds []string
Severities []string
Outcomes []string
Q pgtype.Text
InstanceAdmin bool
OrgID string
FromAt pgtype.Timestamptz
ToAt pgtype.Timestamptz
}
// The export preflight: the same predicates and change-set grouping as
// ListAudit, without the cursor and page bound, so the number is exactly what
// an export of that filter would stream. Counting the merged change sets (not
// field rows) keeps the answer in the unit the feed pages in.
func (q *Queries) CountAudit(ctx context.Context, arg CountAuditParams) (int64, error) {
row := q.db.QueryRow(ctx, countAudit,
arg.Categories,
arg.Actions,
arg.Kinds,
arg.Actor,
arg.TargetKinds,
arg.TargetIds,
arg.Severities,
arg.Outcomes,
arg.Q,
arg.InstanceAdmin,
arg.OrgID,
arg.FromAt,
arg.ToAt,
)
var count int64
err := row.Scan(&count)
return count, err
}
const listAudit = `-- name: ListAudit :many
@@ -109,71 +466,89 @@ WITH sys_changes AS (
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,
t.org_id AS org_id,
CASE t.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
1 AS source_rank,
g.source_id,
g.at,
g.actor,
t.source_id,
t.at,
t.actor,
''::text AS actor_kind,
''::text AS principal_id,
CASE g.table_name
CASE t.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
t.target_id,
CASE t.table_name
WHEN 'resources' THEN t.target_id
ELSE 'access_policy'
END AS kind,
g.record_version,
g.request_id,
g.source,
g.fields,
t.record_version,
t.request_id,
t.source,
t.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'
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'reset'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.record_version > 0 THEN 'delete'
WHEN t.table_name = 'access_policies' AND t.all_old_null THEN 'create'
WHEN t.table_name = 'resources' AND t.record_version = 1 AND t.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'
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'warning'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.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,
concat_ws(' ', t.record_key, t.actor, t.source, t.field_names, t.target_id) 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)
WHEN t.table_name = 'access_policies'
AND split_part(t.record_key, '/', 1) IN ('workspace', 'object')
THEN split_part(t.target_id, '/', 1)
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')
FROM (
-- The target is derived from the row's org_id, never from a
-- split of the key: an org id may contain '/', and the org is
-- already a column here. resources: <org>/<kind> unless the key
-- is the bare kind. access_policies:
-- <level>/<org>/<workspace>/<objectKind>/<objectID>/<policyID>.
SELECT
g.org_id, g.table_name, g.record_key, g.request_id,
g.record_version, g.at, g.actor, g.source, g.source_id,
g.fields, g.all_old_null, g.all_new_null, g.is_reset,
g.field_count, g.whole_record_field, g.field_names,
CASE g.table_name
WHEN 'resources' THEN
CASE
WHEN left(g.record_key, length(g.org_id) + 1) = g.org_id || '/'
THEN substr(g.record_key, length(g.org_id) + 2)
-- A key written before the default org was renamed
-- still carries that dead id, not the row's org_id.
-- The kind is the last segment; never the org.
WHEN position('/' IN g.record_key) > 0
THEN regexp_replace(g.record_key, '^.*/', '')
ELSE g.record_key
END
ELSE
CASE
WHEN left(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 2
) = split_part(g.record_key, '/', 1) || '/' || g.org_id || '/'
THEN ltrim(substr(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 3
), '/')
ELSE g.record_key
END
END::text AS target_id
FROM sys_changes g
WHERE g.table_name IN ('resources', 'access_policies')
) t
) 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[]))
@@ -84,71 +84,89 @@ WITH sys_changes AS (
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,
t.org_id AS org_id,
CASE t.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
1 AS source_rank,
g.source_id,
g.at,
g.actor,
t.source_id,
t.at,
t.actor,
''::text AS actor_kind,
''::text AS principal_id,
CASE g.table_name
CASE t.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
t.target_id,
CASE t.table_name
WHEN 'resources' THEN t.target_id
ELSE 'access_policy'
END AS kind,
g.record_version,
g.request_id,
g.source,
g.fields,
t.record_version,
t.request_id,
t.source,
t.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'
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'reset'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.record_version > 0 THEN 'delete'
WHEN t.table_name = 'access_policies' AND t.all_old_null THEN 'create'
WHEN t.table_name = 'resources' AND t.record_version = 1 AND t.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'
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'warning'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.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,
concat_ws(' ', t.record_key, t.actor, t.source, t.field_names, t.target_id) 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)
WHEN t.table_name = 'access_policies'
AND split_part(t.record_key, '/', 1) IN ('workspace', 'object')
THEN split_part(t.target_id, '/', 1)
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')
FROM (
-- The target is derived from the row's org_id, never from a
-- split of the key: an org id may contain '/', and the org is
-- already a column here. resources: <org>/<kind> unless the key
-- is the bare kind. access_policies:
-- <level>/<org>/<workspace>/<objectKind>/<objectID>/<policyID>.
SELECT
g.org_id, g.table_name, g.record_key, g.request_id,
g.record_version, g.at, g.actor, g.source, g.source_id,
g.fields, g.all_old_null, g.all_new_null, g.is_reset,
g.field_count, g.whole_record_field, g.field_names,
CASE g.table_name
WHEN 'resources' THEN
CASE
WHEN left(g.record_key, length(g.org_id) + 1) = g.org_id || '/'
THEN substr(g.record_key, length(g.org_id) + 2)
-- A key written before the default org was renamed
-- still carries that dead id, not the row's org_id.
-- The kind is the last segment; never the org.
WHEN position('/' IN g.record_key) > 0
THEN regexp_replace(g.record_key, '^.*/', '')
ELSE g.record_key
END
ELSE
CASE
WHEN left(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 2
) = split_part(g.record_key, '/', 1) || '/' || g.org_id || '/'
THEN ltrim(substr(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 3
), '/')
ELSE g.record_key
END
END::text AS target_id
FROM sys_changes g
WHERE g.table_name IN ('resources', 'access_policies')
) t
) 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[]))
@@ -424,3 +442,322 @@ SELECT
(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;
-- name: CountAudit :one
-- The export preflight: the same predicates and change-set grouping as
-- ListAudit, without the cursor and page bound, so the number is exactly what
-- an export of that filter would stream. Counting the merged change sets (not
-- field rows) keeps the answer in the unit the feed pages in.
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,
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(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 count(*)::bigint AS count
FROM (
SELECT
CASE t.table_name WHEN 'access_policies' THEN 'access' ELSE 'settings' END AS category,
CASE
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'reset'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.record_version > 0 THEN 'delete'
WHEN t.table_name = 'access_policies' AND t.all_old_null THEN 'create'
WHEN t.table_name = 'resources' AND t.record_version = 1 AND t.all_old_null THEN 'create'
ELSE 'update'
END AS action,
CASE t.table_name
WHEN 'resources' THEN t.target_id
ELSE 'access_policy'
END AS kind,
t.actor,
CASE t.table_name
WHEN 'resources' THEN 'resource'
ELSE 'policy'
END AS target_kind,
t.target_id,
CASE
WHEN t.table_name = 'access_policies' AND t.is_reset THEN 'warning'
WHEN t.table_name = 'access_policies' AND t.all_new_null AND t.record_version > 0 THEN 'warning'
ELSE 'info'
END AS severity,
'success'::text AS outcome,
concat_ws(' ', t.record_key, t.actor, t.source, t.field_names, t.target_id) AS search_text
FROM (
SELECT
g.org_id, g.table_name, g.record_key, g.request_id,
g.record_version, g.at, g.actor, g.source,
g.all_old_null, g.all_new_null, g.is_reset,
g.field_count, g.whole_record_field, g.field_names,
CASE g.table_name
WHEN 'resources' THEN
CASE
WHEN left(g.record_key, length(g.org_id) + 1) = g.org_id || '/'
THEN substr(g.record_key, length(g.org_id) + 2)
WHEN position('/' IN g.record_key) > 0
THEN regexp_replace(g.record_key, '^.*/', '')
ELSE g.record_key
END
ELSE
CASE
WHEN left(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 2
) = split_part(g.record_key, '/', 1) || '/' || g.org_id || '/'
THEN ltrim(substr(
g.record_key,
length(split_part(g.record_key, '/', 1)) + length(g.org_id) + 3
), '/')
ELSE g.record_key
END
END::text AS target_id
FROM sys_changes g
WHERE g.table_name IN ('resources', 'access_policies')
) t
WHERE (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR category = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR action = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR target_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR target_id = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR severity = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR outcome = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(search_text)) > 0)
UNION ALL
SELECT
'identity'::text AS category,
event_type AS action,
event_type AS kind,
actor_id AS actor,
'identity'::text AS target_kind,
identity_id::text AS target_id,
CASE WHEN event_type IN ('suspend', 'retire') THEN 'warning' ELSE 'info' END AS severity,
'success'::text AS outcome,
concat_ws(' ', identity_id, event_type, display_name, actor_id, source) AS search_text
FROM identity_events
WHERE event_type IN ('create', 'rename', 'suspend', 'reactivate', 'retire')
AND (sqlc.arg(instance_admin)::boolean
OR (org_id IS NOT NULL AND org_id = sqlc.arg(org_id)::text))
AND (sqlc.narg(from_at)::timestamptz IS NULL OR at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR at < sqlc.narg(to_at)::timestamptz)
AND (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR 'identity' = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR event_type = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR event_type = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR actor_id = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR 'identity' = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR identity_id::text = ANY(sqlc.arg(target_ids)::text[]))
AND (
COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0
OR (CASE WHEN event_type IN ('suspend', 'retire') THEN 'warning' ELSE 'info' END) = ANY(sqlc.arg(severities)::text[])
)
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR 'success' = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(concat_ws(' ', identity_id, event_type, display_name, actor_id, source))) > 0)
UNION ALL
SELECT
'access'::text AS category,
'deny'::text AS action,
'access_denial'::text AS kind,
principal AS actor,
resource_kind AS target_kind,
resource_id::text AS target_id,
'warning'::text AS severity,
'denied'::text AS outcome,
concat_ws(' ', principal, action, resource_kind, resource_id, level, array_to_string(policies, ' ')) AS search_text
FROM access_denials
WHERE org_id = sqlc.arg(org_id)::text
AND (sqlc.narg(from_at)::timestamptz IS NULL OR minute_at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR minute_at < sqlc.narg(to_at)::timestamptz)
AND (COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0 OR 'access' = ANY(sqlc.arg(categories)::text[]))
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR 'deny' = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR 'access_denial' = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR principal = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR resource_kind = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR resource_id::text = ANY(sqlc.arg(target_ids)::text[]))
AND (COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0 OR 'warning' = ANY(sqlc.arg(severities)::text[]))
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR 'denied' = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(concat_ws(' ', principal, action, resource_kind, resource_id, level, array_to_string(policies, ' ')))) > 0)
UNION ALL
SELECT
CASE
WHEN 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,
kind AS action,
kind,
actor_id AS actor,
CASE WHEN task_id > 0 THEN 'task' ELSE '' END AS target_kind,
(CASE WHEN task_id > 0 THEN task_id::text ELSE '' END)::text AS target_id,
CASE
WHEN 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 kind = ANY(ARRAY[
'parked', 'task_retried', 'task_cancelled', 'task_stopped',
'alert', 'update_report'
]::text[]) THEN 'warning'
ELSE 'info'
END AS severity,
CASE
WHEN 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,
concat_ws(' ', kind, actor_id, actor_kind, repo, workflow, stage, task_id::text) AS search_text
FROM events
WHERE org_id = sqlc.arg(org_id)::text
AND (sqlc.narg(from_at)::timestamptz IS NULL OR at >= sqlc.narg(from_at)::timestamptz)
AND (sqlc.narg(to_at)::timestamptz IS NULL OR at < sqlc.narg(to_at)::timestamptz)
AND (
COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0
OR (CASE WHEN 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) = ANY(sqlc.arg(categories)::text[])
)
AND (COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0 OR kind = ANY(sqlc.arg(actions)::text[]))
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR kind = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR actor_id = sqlc.narg(actor)::text)
AND (
COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0
OR (CASE WHEN task_id > 0 THEN 'task' ELSE '' END) = ANY(sqlc.arg(target_kinds)::text[])
)
AND (
COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0
OR (CASE WHEN task_id > 0 THEN task_id::text ELSE '' END) = ANY(sqlc.arg(target_ids)::text[])
)
AND (
COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0
OR (CASE
WHEN 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 kind = ANY(ARRAY[
'parked', 'task_retried', 'task_cancelled', 'task_stopped',
'alert', 'update_report'
]::text[]) THEN 'warning'
ELSE 'info'
END) = ANY(sqlc.arg(severities)::text[])
)
AND (
COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0
OR (CASE
WHEN 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) = ANY(sqlc.arg(outcomes)::text[])
)
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(concat_ws(' ', kind, actor_id, actor_kind, repo, workflow, stage, task_id::text))) > 0)
UNION ALL
SELECT
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,
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,
g.table_name AS kind,
g.actor,
g.table_name::text AS target_kind,
g.record_key::text AS target_id,
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,
concat_ws(' ', g.record_key, g.actor, g.source, g.field_names, g.table_name) AS search_text
FROM sys_changes g
WHERE g.table_name NOT IN ('resources', 'access_policies')
AND (
COALESCE(cardinality(sqlc.arg(categories)::text[]), 0) = 0
OR (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) = ANY(sqlc.arg(categories)::text[])
)
AND (
COALESCE(cardinality(sqlc.arg(actions)::text[]), 0) = 0
OR (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) = ANY(sqlc.arg(actions)::text[])
)
AND (COALESCE(cardinality(sqlc.arg(kinds)::text[]), 0) = 0 OR g.table_name = ANY(sqlc.arg(kinds)::text[]))
AND (sqlc.narg(actor)::text IS NULL OR g.actor = sqlc.narg(actor)::text)
AND (COALESCE(cardinality(sqlc.arg(target_kinds)::text[]), 0) = 0 OR g.table_name = ANY(sqlc.arg(target_kinds)::text[]))
AND (COALESCE(cardinality(sqlc.arg(target_ids)::text[]), 0) = 0 OR g.record_key = ANY(sqlc.arg(target_ids)::text[]))
AND (
COALESCE(cardinality(sqlc.arg(severities)::text[]), 0) = 0
OR (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) = ANY(sqlc.arg(severities)::text[])
)
AND (COALESCE(cardinality(sqlc.arg(outcomes)::text[]), 0) = 0 OR 'success' = ANY(sqlc.arg(outcomes)::text[]))
AND (sqlc.narg(q)::text IS NULL OR position(lower(sqlc.narg(q)::text) IN lower(concat_ws(' ', g.record_key, g.actor, g.source, g.field_names, g.table_name))) > 0)
) branches;
+25
View File
@@ -203,3 +203,28 @@ func (s *auditServer) GetAuditHead(ctx context.Context, _ *pb.GetAuditHeadReques
}
return &pb.GetAuditHeadResponse{Token: token}, nil
}
func (s *auditServer) CountAudit(ctx context.Context, request *pb.CountAuditRequest) (*pb.CountAuditResponse, error) {
if s.deps.Audit == nil {
return nil, errAuditUnavailable
}
filter := auditFilterValue(request.GetFilter())
if err := filter.Normalize(); err != nil {
return nil, validationFailure(err)
}
count, err := s.deps.Audit.CountAudit(withInstanceAdmin(ctx), filter)
if err != nil {
return nil, s.logErr("CountAudit", err)
}
return &pb.CountAuditResponse{Count: count}, nil
}
func (s *auditServer) RecordAuditExport(ctx context.Context, request *pb.RecordAuditExportRequest) (*pb.RecordAuditExportResponse, error) {
if s.deps.Audit == nil {
return nil, errAuditUnavailable
}
if err := s.deps.Audit.RecordAuditExport(ctx, request.GetFormat(), request.GetFiltersHash(), request.GetRows()); err != nil {
return nil, s.logErr("RecordAuditExport", err)
}
return &pb.RecordAuditExportResponse{}, nil
}
@@ -37,3 +37,18 @@ func (c *Client) AuditHead(ctx context.Context) (string, error) {
}
return response.GetToken(), nil
}
func (c *Client) CountAudit(ctx context.Context, filter audit.Filter) (int64, error) {
response, err := c.audit.CountAudit(ctx, &pb.CountAuditRequest{Filter: auditFilterProto(filter)})
if err != nil {
return 0, unmapError(err)
}
return response.GetCount(), nil
}
func (c *Client) RecordAuditExport(ctx context.Context, format, filterHash string, rows int64) error {
_, err := c.audit.RecordAuditExport(ctx, &pb.RecordAuditExportRequest{
Format: format, FiltersHash: filterHash, Rows: rows,
})
return unmapError(err)
}
+62 -21
View File
@@ -37,9 +37,16 @@ const (
auditExportMaxRange = 366 * 24 * time.Hour
auditExportDeadline = 5 * time.Minute
auditExportMaxPages = 2000
// auditExportMaxRows bounds one download. The preflight refuses a larger
// match before any byte is written, so a client never gets a truncated
// file it cannot tell from a complete one.
auditExportMaxRows = 100_000
)
var errAuditExportTooLarge = errors.New("audit export exceeded its page cap")
var (
errAuditExportTooLarge = errors.New("audit export exceeded its page cap")
errAuditExportCursor = errors.New("audit export cursor unreadable")
)
type auditActorView struct {
ID string `json:"id"`
@@ -190,6 +197,12 @@ func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), auditExportDeadline)
defer cancel()
// Count before any header is written: a match over the cap is the
// caller's problem to narrow, not a download that ends early.
if !s.auditExportWithinBudget(ctx, w, filter) {
return
}
// 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)
@@ -204,10 +217,11 @@ func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
if flusher, ok := w.(http.Flusher); ok {
flush = flusher.Flush
}
var rows int64
if format == "csv" {
err = s.exportAuditCSV(ctx, filter, first, labels, w, flush)
rows, err = s.exportAuditCSV(ctx, filter, first, labels, w, flush)
} else {
err = s.exportAuditJSON(ctx, filter, first, labels, w, flush)
rows, 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
@@ -216,6 +230,31 @@ func (s *Server) handleAuditExport(w http.ResponseWriter, r *http.Request) {
s.logf("audit export failed", "format", format, "err", err)
panic(http.ErrAbortHandler)
}
// The file is complete; the trail row is bookkeeping, so a failed write
// is logged rather than turning a delivered export into a failed one.
if err := s.Audit.RecordAuditExport(r.Context(), format, filter.Hash(), rows); err != nil {
s.logf("record audit export", "format", format, "err", err)
}
}
// auditExportWithinBudget refuses an export whose filter matches more than the
// cap, answering before any header is written so the client never receives a
// truncated file it cannot tell from a complete one. A feed error is written
// with the same status mapping as a page read.
func (s *Server) auditExportWithinBudget(ctx context.Context, w http.ResponseWriter, filter audit.Filter) bool {
count, err := s.Audit.CountAudit(ctx, filter)
if err != nil {
writeAuditError(w, err)
return false
}
if count > auditExportMaxRows {
http.Error(w, fmt.Sprintf(
"%d rows match; narrow the time range to at most %d",
count, auditExportMaxRows,
), http.StatusBadRequest)
return false
}
return true
}
// auditExportFormat resolves the download format. An absent format means JSON.
@@ -262,12 +301,12 @@ func writeAuditExportHeaders(w http.ResponseWriter, format string) {
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 {
func (s *Server) exportAuditCSV(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) (int64, error) {
writer := csv.NewWriter(w)
if err := writer.Write(auditCSVHeader); err != nil {
return err
return 0, err
}
err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
rows, err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
return writer.Write(auditCSVRow(auditRecord(entry, labels)))
}, func() {
writer.Flush()
@@ -277,16 +316,16 @@ func (s *Server) exportAuditCSV(ctx context.Context, filter audit.Filter, first
if flushErr := writer.Error(); err == nil {
err = flushErr
}
return err
return rows, err
}
func (s *Server) exportAuditJSON(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) error {
func (s *Server) exportAuditJSON(ctx context.Context, filter audit.Filter, first audit.Page, labels *auditLabels, w io.Writer, flush func()) (int64, error) {
if _, err := io.WriteString(w, "["); err != nil {
return err
return 0, err
}
encoder := json.NewEncoder(w)
firstRecord := true
err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
rows, err := s.walkAudit(ctx, filter, first, func(entry audit.Entry) error {
if !firstRecord {
if _, err := io.WriteString(w, ","); err != nil {
return err
@@ -298,41 +337,43 @@ func (s *Server) exportAuditJSON(ctx context.Context, filter audit.Filter, first
if err != nil {
// Leave the array open: the handler aborts the connection, and a
// closed array would masquerade as a complete download.
return err
return rows, err
}
_, err = io.WriteString(w, "]\n")
return err
return rows, 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 {
// walkAudit streams every entry of the window in feed order and returns how
// many it emitted. 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()) (int64, error) {
page := first
pages := 1
var rows int64
for {
for _, entry := range page.Entries {
if err := emit(entry); err != nil {
return err
return rows, err
}
rows++
}
if afterPage != nil {
afterPage()
}
if !page.HasMore || page.NextCursor == "" {
return nil
return rows, nil
}
if pages >= auditExportMaxPages {
return errAuditExportTooLarge
return rows, errAuditExportTooLarge
}
pages++
cursor, ok := audit.ParseCursor(page.NextCursor)
if !ok {
return nil
return rows, errAuditExportCursor
}
next, err := s.Audit.ListAudit(ctx, filter, cursor, auditExportPageSize)
if err != nil {
return err
return rows, err
}
page = next
}
+29 -2
View File
@@ -14,16 +14,23 @@ import (
)
type auditStub struct {
page audit.Page
err error
page audit.Page
count int64
err error
}
func (s auditStub) ListAudit(context.Context, audit.Filter, audit.Cursor, int) (audit.Page, error) {
return s.page, s.err
}
func (s auditStub) CountAudit(context.Context, audit.Filter) (int64, error) {
return s.count, s.err
}
func (s auditStub) AuditHead(context.Context) (string, error) { return "", nil }
func (s auditStub) RecordAuditExport(context.Context, string, string, int64) error { return nil }
// TestAuditCSVCellNeutralisesFormulas pins the export's spreadsheet boundary:
// a value a spreadsheet would evaluate is text, while ordinary values are
// unchanged.
@@ -88,3 +95,23 @@ func TestAuditUnavailableWithoutFeed(t *testing.T) {
t.Fatalf("status = %d, want %d", rec.Code, http.StatusServiceUnavailable)
}
}
// TestAuditExportRefusesOverTheRowCap pins the export preflight: a filter
// matching more than the cap is refused with a readable error before any
// header or row is written, never a silently truncated file.
func TestAuditExportRefusesOverTheRowCap(t *testing.T) {
srv := &Server{Audit: auditStub{count: auditExportMaxRows + 1}}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
"/api/audit/export?format=json&from=2026-01-01T00:00:00Z&to=2026-01-02T00:00:00Z", nil)
rec := httptest.NewRecorder()
srv.handleAuditExport(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusBadRequest, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "narrow") {
t.Fatalf("body = %q, want an instruction to narrow the range", rec.Body.String())
}
if got := rec.Header().Get("Content-Disposition"); got != "" {
t.Fatalf("Content-Disposition = %q, want no download header", got)
}
}
@@ -163,6 +163,44 @@ func TestHTTPPolicyCannotBeBypassed(t *testing.T) {
}
}
// TestResourceHistoryNeedsPolicyManagement pins the raw-value history route:
// it serves unredacted revisions because restore re-applies the real value, so
// it needs policy management of the kind rather than the redacted feed's
// read_audit grant. A developer and a viewer are refused; an admin passes the
// gate and reaches the handler, which reports the unwired control plane.
func TestResourceHistoryNeedsPolicyManagement(t *testing.T) {
const path = "/api/control-plane/resources/audit-retention/history"
tests := []struct {
role org.Role
want int
}{
{org.RoleViewer, http.StatusForbidden},
{org.RoleDeveloper, http.StatusForbidden},
{org.RoleAdmin, http.StatusServiceUnavailable},
}
for _, tt := range tests {
t.Run(string(tt.role), func(t *testing.T) {
principal := access.Principal{
IdentityID: "alice", Org: "acme",
Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}},
}
srv := &Server{
Accounts: accountBoundaryStore{},
Access: infraaccess.New(access.ShippedOrgPolicies("acme")),
Principals: principalOf(principal),
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example"+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())
}
})
}
}
// 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
+13 -1
View File
@@ -312,7 +312,7 @@ func routeOverride(action access.Action, path string) (access.Action, access.Res
// is the same administration as editing a control-plane resource.
return actionManage(action), access.KindPolicy, segmentValue(path, 2), true
case matchPrefix(path, "/api/control-plane/resources"):
return actionManage(action), access.KindPolicy, segmentValue(path, 3), true
return controlPlaneResourceRoute(action, path)
case matchPrefix(path, "/api/chat"):
return chatRoute(action)
case matchSegment(path, "/api/sources/", "/secret"):
@@ -334,6 +334,18 @@ func routeOverride(action access.Action, path string) (access.Action, access.Res
return action, "", "", false
}
// controlPlaneResourceRoute maps a control-plane resource request. History
// returns the raw stored revisions, including secret references and policy
// text, because restore re-applies the real value: that is stronger than the
// redacted feed's read_audit grant, so it is policy management of that kind.
// Everything else falls back to the managed-resource action.
func controlPlaneResourceRoute(action access.Action, path string) (access.Action, access.ResourceKind, string, bool) {
if matchSegment(path, "/api/control-plane/resources/", "/history") {
return access.ActionManagePolicies, access.KindPolicy, segmentValue(path, 3), true
}
return actionManage(action), access.KindPolicy, segmentValue(path, 3), true
}
// bindingCommand maps a binding lifecycle route to its action: approving arms
// a binding, so it is approval; pausing and resuming change its state.
func bindingCommand(action access.Action, path string) (access.Action, bool) {
+22
View File
@@ -15,6 +15,14 @@ service AuditService {
// 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);
// CountAudit returns how many entries the filter matches, without a cursor
// or a page bound. The export preflights it so an oversized download is
// refused before any row is streamed.
rpc CountAudit(CountAuditRequest) returns (CountAuditResponse);
// RecordAuditExport appends the audit row of one completed export: its
// format, the filter fingerprint and the row count, attributed to the
// caller. The State Store writes it; the dashboard never does.
rpc RecordAuditExport(RecordAuditExportRequest) returns (RecordAuditExportResponse);
}
// AuditFilter selects the entries a page returns. An empty list places no
@@ -90,3 +98,17 @@ message ListAuditResponse {
message GetAuditHeadRequest {}
message GetAuditHeadResponse { string token = 1; }
message CountAuditRequest { AuditFilter filter = 1; }
message CountAuditResponse { int64 count = 1; }
message RecordAuditExportRequest {
string format = 1;
// filters_hash is the normalised filter's fingerprint, so an operator can
// tell one exported view from another without storing the filter itself.
string filters_hash = 2;
int64 rows = 3;
}
message RecordAuditExportResponse {}