Skip to content

feat: implement audit log middleware for MCP servers - #551

Merged
JAORMX merged 3 commits into
mainfrom
feat/audit-log-middleware
Jun 3, 2025
Merged

JAORMX merged 3 commits into
mainfrom
feat/audit-log-middleware

Conversation

@JAORMX

@JAORMX JAORMX commented Jun 2, 2025

Copy link
Copy Markdown
Collaborator

Summary

Implements comprehensive audit logging middleware for MCP servers to provide NIST SP 800-53 compliant audit trails.

Changes

📦 New Audit Package (pkg/audit/)

  • event.go: NIST SP 800-53 compliant audit event structures (adapted from auditevent library)
  • mcp_events.go: MCP protocol-specific event types and constants
  • config.go: Flexible configuration with event filtering capabilities
  • auditor.go: HTTP middleware implementation with comprehensive audit logging

🔧 Integration

  • pkg/runner/config.go: Added audit configuration fields to RunConfig
  • pkg/runner/runner.go: Integrated audit middleware into middleware chain (Auth → Authz → Audit → Handler)

✅ Key Features

NIST SP 800-53 Compliance:

  • ✅ Event type classification (AU-3)
  • ✅ UTC timestamps (AU-8)
  • ✅ Source identification with IP extraction
  • ✅ Action outcomes (success/failure/denied/error)
  • ✅ Subject identification from JWT claims
  • ✅ Component identification (configurable)
  • ✅ Target resource information

Flexible Configuration:

  • ✅ Enable/disable audit logging
  • ✅ Configurable component names
  • ✅ Event type filtering (include/exclude lists)
  • ✅ Optional request/response data capture
  • ✅ Configurable data size limits
  • ✅ Privacy-conscious defaults

MCP Protocol Support:

  • ✅ 12 MCP-specific event types (initialize, tool_call, resource_read, etc.)
  • ✅ Transport detection (SSE vs HTTP)
  • ✅ Automatic event classification
  • ✅ Structured metadata capture

🧪 Testing

  • 45+ comprehensive tests covering all functionality
  • 100% test pass rate across the entire codebase
  • Linting compliance with all issues resolved
  • Build verification successful

📋 Configuration Example

{
  "enabled": true,
  "component": "my-mcp-server",
  "event_types": ["mcp_tool_call", "mcp_resource_read"],
  "exclude_event_types": ["mcp_ping"],
  "include_request_data": false,
  "include_response_data": false,
  "max_data_size": 2048
}

📊 Sample Audit Log Output

{
  "metadata": {
    "auditId": "uuid-here",
    "extra": {
      "duration_ms": 45,
      "transport": "sse",
      "response_size_bytes": 1024
    }
  },
  "type": "mcp_tool_call",
  "loggedAt": "2025-06-02T14:30:00Z",
  "source": {
    "type": "network",
    "value": "192.168.1.100",
    "extra": {
      "user_agent": "Claude/1.0",
      "request_id": "req-123"
    }
  },
  "outcome": "success",
  "subjects": {
    "user": "john.doe",
    "user_id": "sub123"
  },
  "component": "my-mcp-server",
  "target": {
    "endpoint": "/messages",
    "method": "POST",
    "type": "tool"
  }
}

Known Issues

⚠️ Logger Concern: Currently uses the shared application logger for audit events. This should be improved with a dedicated audit logger for better separation and compliance. See issue #550 for details.

Testing

# Run audit package tests
go test ./pkg/audit/... -v

# Run full test suite
go-task test

# Build verification
go-task build

Closes #204

@JAORMX
JAORMX force-pushed the feat/audit-log-middleware branch 2 times, most recently from 3fb9ca0 to b4a5330 Compare June 2, 2025 13:26
@ChrisJBurns

Copy link
Copy Markdown
Collaborator

@JAORMX Do we know if this works out of the box for the Kubernetes Operator?

@JAORMX

JAORMX commented Jun 2, 2025

Copy link
Copy Markdown
Collaborator Author

@JAORMX Do we know if this works out of the box for the Kubernetes Operator?

It should, but it needs to be explicitly enabled. I'm not happy about the logs being shared with the standard logger. We need to come up with something better.

I normally have a dedicated container for these logs, but we can brainstorm a little more about this.

Comment thread pkg/audit/event.go Resolved
Comment thread pkg/audit/auditor.go Resolved
Comment thread pkg/audit/config.go Resolved
jhrozek
jhrozek previously approved these changes Jun 2, 2025

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really familiar with thv code, but the audit part seems quite solid to me (the first thing I went looking after was a timestamp in UTC :-))

JAORMX added 3 commits June 3, 2025 14:27
- Add comprehensive audit logging package with NIST SP 800-53 compliance
- Implement HTTP middleware for capturing audit events
- Support MCP-specific event types and metadata
- Add flexible configuration with event filtering
- Integrate audit middleware into runner middleware chain
- Include extensive test coverage (45+ tests)
- Support privacy-conscious request/response data capture
- Add structured JSON output for log aggregation systems

Closes #204
- Reference issue #552 for implementing actual MCP method extraction
- Current implementation is a stub that returns empty string
- Need to parse JSON-RPC messages to get accurate event types
- Add EventTypeMCPRequest and EventTypeHTTPRequest constants
- Update validation function to include fallback event types
- Replace hardcoded strings with constants in auditor.go
- Addresses PR feedback from jhrozek in #551

Fixes the validation issue where mcp_request and http_request
event types were not included in the valid event types list,
even though they can be emitted by the middleware.
@JAORMX
JAORMX force-pushed the feat/audit-log-middleware branch from b4a5330 to 559ef9c Compare June 3, 2025 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit log middleware

3 participants