Repository navigation
feat: add optional metadata-only call audit logging - #980
roonakyadav wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces structured call audit logging (JSONL) for tool invocations across mesh nodes and routers, capturing metadata while strictly excluding payloads and secrets. It adds a new internal/audit package, configures CLI and environment variables for the log path, and integrates logging into MCP tool calls, pass-through services, and router ingress. Feedback on the changes highlights a security-critical requirement to canonicalize raw peer IDs at the boundary using peer.Decode in ingressCallerPeerID, and suggests leveraging the audit.ClassifyJSONRPCError helper in MCPService to improve consistency and maintainability.
| func ingressCallerPeerID(claims *identity.VerifiedBiscuitClaims) string { | ||
| if claims == nil { | ||
| return "" | ||
| } | ||
| if claims.NodePeerID != "" { | ||
| return claims.NodePeerID | ||
| } | ||
| if claims.ActorNodePeerID != "" { | ||
| return claims.ActorNodePeerID | ||
| } | ||
| if claims.ClientPeerID != "" { | ||
| return claims.ClientPeerID | ||
| } | ||
| return claims.Principal() | ||
| } |
There was a problem hiding this comment.
Section 1: Peer IDs must be canonicalized at the boundary
The peer ID strings extracted from the verified Biscuit claims (claims.NodePeerID, claims.ActorNodePeerID, claims.ClientPeerID, or claims.Principal()) are raw, untrusted wire inputs and may not be in the canonical base58 multihash form. Storing or logging non-canonical peer IDs violates the canonicalization boundary rule and can cause bans, admission, or correlation checks to silently miss.
Please decode the raw peer ID string using peer.Decode to canonicalize it before returning.
func ingressCallerPeerID(claims *identity.VerifiedBiscuitClaims) string {
if claims == nil {
return ""
}
var raw string
if claims.NodePeerID != "" {
raw = claims.NodePeerID
} else if claims.ActorNodePeerID != "" {
raw = claims.ActorNodePeerID
} else if claims.ClientPeerID != "" {
raw = claims.ClientPeerID
} else {
raw = claims.Principal()
}
if pID, err := peer.Decode(raw); err == nil {
return pID.String()
}
return raw
}References
- Peer IDs must be canonicalized at the boundary. A raw string copied off the wire and used as a key or stored in a record is a different key for the same peer, and a ban, admission or dedup check silently misses. (link)
| outcome := audit.OutcomeUnauthorized | ||
| var rpcErr *jsonrpc.Error | ||
| if errors.As(errResp.Error, &rpcErr) && rpcErr.Code == jsonrpc.CodeInvalidParams { | ||
| outcome = audit.OutcomeInvalidParams | ||
| } |
There was a problem hiding this comment.
Instead of hardcoding the check for jsonrpc.CodeInvalidParams, leverage the audit.ClassifyJSONRPCError helper function to map the JSON-RPC error code to its standardized outcome class. This improves consistency and maintainability.
| outcome := audit.OutcomeUnauthorized | |
| var rpcErr *jsonrpc.Error | |
| if errors.As(errResp.Error, &rpcErr) && rpcErr.Code == jsonrpc.CodeInvalidParams { | |
| outcome = audit.OutcomeInvalidParams | |
| } | |
| outcome := audit.OutcomeUnauthorized | |
| var rpcErr *jsonrpc.Error | |
| if errors.As(errResp.Error, &rpcErr) { | |
| outcome = audit.ClassifyJSONRPCError(rpcErr.Code) | |
| } |
Summary
Adds an optional, operator-configurable tool call audit logging mechanism on nodes and edge routers, capturing tool call metadata as structured JSONL while strictly excluding arguments, prompts, results, response bodies, and credentials.
Closes #974
Architecture & Call Boundaries
Tool calls in Agent Mesh occur across distinct boundaries. Call audit logging is implemented at the critical call boundaries where identity, authorization, and byte transfer are verified:
Provider Node Inbound libp2p MCP Streams (
internal/node/mcp_service.go):tools/callrequests over the mesh protocol (/mcp/1.0.0libp2p streams).RequestContext.CallerLabelsfrom Biscuit query facts), never arbitrary caller headers.Caller Node Outbound Tool Invocations (
internal/node/mcp.go):CallMCPToolinvocations made by the node to remote mesh peers.Router Edge HTTP Ingress (
internal/router/ingress.go):/ingress/edge gateway.OutcomeUnauthorized).OutcomeOK,OutcomeForbidden,OutcomeBadGateway,OutcomeTimeout, etc.).VerifiedBiscuitClaims.Labels).Deliberate Scope Limitations & Zero-Trust Encrypted Relays
Captured Metadata (Metadata Only)
Each audit record is emitted as a single, atomic JSONL line with the following fields:
timestamp: UTC instant formatted in RFC 3339 nano (2026-11-28T...Z).caller_peer_id: Authenticated libp2p peer ID of the caller (or verified Biscuit principal).caller_labels: Control-plane attested key-value label facts (map[string]string), omitted if none.provider_peer_id: Peer ID of the serving node.service_name: Fully qualified or registered service name (e.g.,calc,mcp://calc).tool_name: Invoked MCP tool name.outcome: Safe, standardized outcome classification (ok,unauthorized,invalid_params,not_found,timeout,cancelled,rate_limited,tool_error,backend_error,bad_gateway,forbidden,call_failed,labels_not_satisfied,provider_not_granted).latency_ms: Measured round-trip latency in milliseconds (float64).bytes_in: Count of request payload bytes transferred into the boundary.bytes_out: Count of response payload bytes emitted from the boundary.Privacy & Zero-Leak Guarantees
OutcomeUnauthorized,OutcomeBackendError,OutcomeToolError, etc.) to prevent data leaks.Operator Configuration
Disabled by default (zero overhead and zero allocations when disabled):
agentmesh-node,agentmesh-router):--call-audit-log-path=/var/log/agentmesh/call-audit.jsonl # Or output to standard streams: --call-audit-log-path=stdout --call-audit-log-path=stderrexport AGENTMESH_CALL_AUDIT_LOG_PATH=/var/log/agentmesh/call-audit.jsonlagentmesh-node.yaml):Reliability & Concurrency Guarantees
Testing
Added comprehensive unit and integration tests covering:
internal/audit/audit_test.go:errWriter).internal/node/mcp_audit_test.go:OutcomeUnauthorized).-race).CallMCPToolerror classification and payload exclusion.internal/router/ingress_audit_test.go: