fix(controlplane): change history names kinds and reads only the caller's org

This commit is contained in:
sam committed 2026-10-04 17:25:47 +11:00
1 parent 714ec99a49
commit 2501e1301c
9 files changed
+112 -33

No files matched your search

+61
View File
@@ -0,0 +1,61 @@
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)
}
})
}
}
+21 -4
View File
@@ -4,6 +4,7 @@ package controlplane
import (
"context"
"errors"
"strings"
"time"
"google.golang.org/grpc/codes"
@@ -115,22 +116,38 @@ func (s *Server) History(ctx context.Context, request *pb.HistoryRequest) (*pb.H
// 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) {
if request.GetTable() == "" || len(request.GetRecordKeys()) == 0 {
return nil, status.Error(codes.InvalidArgument, "table and record keys are required")
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, request.GetTable(), request.GetRecordKeys(), limit)
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: entry.RecordKey, Field: entry.Field,
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),
})
@@ -699,10 +699,11 @@ func (x *AuditEntry) GetAt() *timestamppb.Timestamp {
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"`
Table string `protobuf:"bytes,1,opt,name=table,proto3" json:"table,omitempty"`
RecordKeys []string `protobuf:"bytes,2,rep,name=record_keys,json=recordKeys,proto3" json:"record_keys,omitempty"`
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"`
@@ -740,16 +741,9 @@ func (*AuditRequest) Descriptor() ([]byte, []int) {
return file_controlplane_v1_controlplane_proto_rawDescGZIP(), []int{10}
}
func (x *AuditRequest) GetTable() string {
func (x *AuditRequest) GetKinds() []string {
if x != nil {
return x.Table
}
return ""
}
func (x *AuditRequest) GetRecordKeys() []string {
if x != nil {
return x.RecordKeys
return x.Kinds
}
return nil
}
@@ -1125,12 +1119,10 @@ const file_controlplane_v1_controlplane_proto_rawDesc = "" +
"\n" +
"request_id\x18\n" +
" \x01(\tR\trequestId\x12*\n" +
"\x02at\x18\v \x01(\v2\x1a.google.protobuf.TimestampR\x02at\"[\n" +
"\x02at\x18\v \x01(\v2\x1a.google.protobuf.TimestampR\x02at\"Z\n" +
"\fAuditRequest\x12\x14\n" +
"\x05table\x18\x01 \x01(\tR\x05table\x12\x1f\n" +
"\vrecord_keys\x18\x02 \x03(\tR\n" +
"recordKeys\x12\x14\n" +
"\x05limit\x18\x03 \x01(\x05R\x05limit\"F\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\"\xec\x01\n" +
"\x0eCommandRequest\x12\x12\n" +
+5 -4
View File
@@ -23,16 +23,17 @@ type auditEntryView struct {
At *time.Time `json:"at,omitempty"`
}
// handleAudit is GET /api/control-plane/audit?table=T&key=K1&key=K2: the
// field-level audit of the named records of one table, newest first. A page
// passes the records it shows, so its audit list is scoped to that page.
// 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.
func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) {
if s.ControlPlane == nil {
http.Error(w, "control plane unavailable", http.StatusServiceUnavailable)
return
}
query := r.URL.Query()
response, err := s.ControlPlane.Audit(r.Context(), &controlpb.AuditRequest{Table: query.Get("table"), RecordKeys: query["key"]})
response, err := s.ControlPlane.Audit(r.Context(), &controlpb.AuditRequest{Kinds: query["kind"]})
if err != nil {
writeControlPlaneError(w, err)
return