Security & Compliance: Scenario-Based Questions
31. How do you implement audit logging in production systems for security and compliance?
Audit logs capture who did what, when, and where โ providing traceability for sensitive actions in production environments. They're essential for incident response, regulatory compliance, and internal governance.
๐ What to Audit
- Authentication Events: Logins, logouts, failed attempts, MFA.
- Authorization Changes: Role or permission updates.
- Data Access: Reads, writes, exports, and deletes of sensitive or regulated data.
- Configuration Changes: System settings, infrastructure updates, deployment events.
โ๏ธ Technical Implementation
- Use a centralized logger with timestamped, structured (JSON) logs.
- Include user ID, IP address, action type, resource ID, and request metadata.
- Implement middleware or interceptors in APIs to automatically log access and changes.
- Ensure logs are immutable and tamper-resistant (e.g., write-once storage, hash chaining).
๐ Security Considerations
- Mask or exclude PII and secrets from logs (e.g., passwords, tokens).
- Encrypt logs at rest and in transit.
- Restrict access to audit logs via RBAC or IAM roles.
๐งช Compliance and Monitoring
- Integrate with SIEMs (e.g., Splunk, ELK, Datadog) for alerting and analysis.
- Set up retention policies to match regulations (e.g., 90 days, 1 year).
- Review logs during security audits and postmortems.
โ Best Practices
- Correlate logs with user identity and session context.
- Use trace IDs to link audit logs with request flows.
- Document what is logged and why in security policies.
๐ซ Common Mistakes
- Storing audit logs in the same system they audit โ creates conflict of interest.
- Logging too much or too little โ balancing signal vs noise is key.
- Not alerting on critical audit events (e.g., failed logins, privilege escalations).
๐ Real-World Insight
Compliance standards like SOC 2, HIPAA, and GDPR require detailed auditing. Teams that treat audit logging as a first-class concern gain not just regulatory approval, but stronger security posture and incident response readiness.