BE-019 — Implement Audit Logging & Compliance API - #317
Merged
Conversation
- Enhanced AuditLog entity with severity, category, eventId, source, requestId, retentionUntil fields and 25+ new action types - Added AuditQueueService for async/batch writes via BullMQ - Added ComplianceService for 7 report types with CSV/JSON export - Added SecurityMonitoringService (brute-force, escalation, API abuse) - Added AuditMetricsService with Prometheus counters/histograms/gauges - Added standardized API response types (AuditResponse, AuditPaginatedResponse) - Added DTOs with validation for queries, exports, and reports - Added AuditLogProcessor for queue-based async write processing - Enhanced AuditTrailService with batch, async, comprehensive filtering - Updated AuditController with 15+ new endpoints (export, reports, security, metrics, event lookup, correlation tracing) - Added audit config for retention, archival, batch size settings - 51 tests passing across 6 test suites (audit-trail, compliance, security-monitoring, audit-metrics, audit-queue, audit-retention)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #290
📚 Overview
Implements a production-ready Audit Logging & Compliance API providing immutable, searchable, and tamper-evident records of all significant activities across the TruthBounty platform.
✅ Acceptance Criteria
🧩 What Was Implemented
1. Audit Event Recording
2. Enhanced Audit Log Storage
eventId(unique event identifier),severity(LOW/MEDIUM/HIGH/CRITICAL),category(12 categories),source(service origin),requestId,retentionUntil(legal hold)3. Search & Filtering
4. Compliance Reporting
5. Security Monitoring
6. Data Retention
AUDIT_LOG_RETENTION_DAYSenv var (default 365)retentionUntil)7. Monitoring Metrics (Prometheus)
audit_events_total,audit_events_by_action_total,audit_events_by_severity_total,audit_events_by_category_totalaudit_write_duration_seconds,audit_search_duration_secondsaudit_storage_records_total,audit_oldest_record_age_daysaudit_failed_writes_total,audit_export_operations_total,audit_retention_operations_total8. Performance
logAsync)logBatch)9. API Standardisation
AuditResponse<T>/AuditPaginatedResponse<T>shapes10. Future Extensibility
sourcefield for cross-service auditingeventIdfor blockchain correlationrequestId/correlationIdfor distributed tracing📁 Files Changed (20 files)
Modified (6)
src/audit/audit.module.ts- Register queue, all new services, processorsrc/audit/controllers/audit-log.controller.ts- 15+ new endpointssrc/audit/entities/audit-log.entity.ts- Extended enums, new fields, indexessrc/audit/services/audit-trail.service.ts- Batch, async, enhanced querysrc/audit/services/index.ts- Export all servicessrc/audit/services/audit-trail.service.spec.ts- Comprehensive test updateCreated (14)
src/audit/config/audit.config.ts+index.tssrc/audit/dto/audit-query.dto.tssrc/audit/interfaces/audit-response.interface.ts+index.tssrc/audit/processors/audit-log.processor.tssrc/audit/services/compliance.service.ts+.spec.tssrc/audit/services/security-monitoring.service.ts+.spec.tssrc/audit/services/audit-metrics.service.ts+.spec.tssrc/audit/services/audit-queue.service.ts+.spec.ts🧪 Tests
51 tests passing across 6 test suites (0 failures):
audit-trail.service.spec.ts- 14 tests (log, batch, async query, filters, storage stats, retention, IP security)compliance.service.spec.ts- 9 tests (all report types, CSV/JSON export, daily/category summaries)security-monitoring.service.spec.ts- 8 tests (failed logins, escalation, API abuse, security events)audit-metrics.service.spec.ts- 7 tests (counters, histograms, storage gauges, getMetrics)audit-queue.service.spec.ts- 5 tests (enqueue, batch, error handling, queue stats)audit-retention.service.spec.ts- 2 tests (existing, preserved)📋 New API Endpoints
🔗 Dependencies