Files
sam 4b97ac5841 feat(audit): a connector call keeps the request core sent and the response it received
Resolve, target facts, invoke, lookup, deliver and parse record the protobuf wire messages as JSON on the context recorder the owning module supplies. Credentials are redacted and a body over 1 MiB keeps its prefix, full length and SHA-256. Judgement stores the resolve and target facts in the decision body and appends the executed invoke to it.
2026-11-16 12:06:05 +11:00

55 lines
1.7 KiB
Go

package connectors
import (
"context"
"google.golang.org/protobuf/proto"
"github.com/samcharles93/archie-core/internal/platform/auditjson"
)
// Call is one call core made to a connector or MCP server: the request it sent
// and the response it received, rendered as JSON. Credentials are removed and
// an oversized body is capped, so the record is safe to keep.
type Call struct {
Connector string `json:"connector"`
Method string `json:"method"`
Request auditjson.Payload `json:"request"`
Response auditjson.Payload `json:"response,omitzero"`
Error string `json:"error,omitzero"`
}
// Recorder keeps each call core makes to a connector. The module that owns the
// record supplies one on the context with WithRecorder; without one nothing is
// recorded.
type Recorder interface {
RecordConnectorCall(ctx context.Context, call Call)
}
type recorderKey struct{}
// WithRecorder makes every connector call made with ctx report to r.
func WithRecorder(ctx context.Context, r Recorder) context.Context {
return context.WithValue(ctx, recorderKey{}, r)
}
// record hands c to the recorder on ctx, if there is one.
func record(ctx context.Context, c Call) {
if r, ok := ctx.Value(recorderKey{}).(Recorder); ok && r != nil {
r.RecordConnectorCall(ctx, c)
}
}
// recordProto records one protobuf wire call. The response is rendered only
// when the call returned one; a transport error is recorded instead.
func recordProto(ctx context.Context, connector, method string, req, resp proto.Message, err error) {
c := Call{Connector: connector, Method: method, Request: auditjson.FromProto(req)}
switch {
case err != nil:
c.Error = err.Error()
case resp != nil:
c.Response = auditjson.FromProto(resp)
}
record(ctx, c)
}