Compliance Engineering
Framework Overview
| Framework |
Scope |
Key Requirements |
| SOC 2 |
Service organizations |
Security, availability, confidentiality, privacy, processing integrity |
| HIPAA |
Healthcare data (PHI) |
Encryption, access controls, audit logging, BAAs |
| GDPR |
EU personal data |
Consent, data minimization, right to erasure, DPIAs |
| PCI-DSS |
Payment card data |
Network segmentation, encryption, access controls, logging |
| FedRAMP |
US government cloud |
NIST 800-53 controls, continuous monitoring, authorization |
SOC 2 Controls in Code
Audit Logging
interface AuditEvent {
timestamp: string;
actor: { id: string; role: string; ip: string };
action: string;
resource: { type: string; id: string };
outcome: 'success' | 'failure';
metadata: Record<string, unknown>;
}
async function auditLog(event: AuditEvent): Promise<void> {
// Write-once, append-only storage (immutable)
await auditStore.append({
...event,
timestamp: new Date().toISOString(),
hash: computeChainHash(event), // tamper detection
});
}
Access Control
// RBAC with principle of least privilege
const permissions = {
admin: ['read', 'write', 'delete', 'manage_users'],
editor: ['read', 'write'],
viewer: ['read'],
} as const;
function authorize(user: User, action: string, resource: Resource): boolean {
const allowed = permissions[user.role];
if (!allowed?.includes(action)) {
auditLog({ action, outcome: 'failure', actor: user, resource });
return false;
}
return true;
}
HIPAA Technical Safeguards
- Encryption at rest: AES-256 for PHI storage, AWS KMS / GCP KMS for key management
- Encryption in transit: TLS 1.2+ mandatory, certificate pinning for mobile
- Access controls: Unique user IDs, automatic logoff, MFA required
- Audit controls: Log all PHI access, retain logs 6+ years, tamper-evident
- Data backup: Encrypted backups, tested restore procedures, geographic redundancy
GDPR Implementation
Consent Management
interface ConsentRecord {
userId: string;
purpose: string;
granted: boolean;
timestamp: string;
source: 'explicit' | 'legitimate_interest';
withdrawable: boolean;
}
// Data Subject Access Request (DSAR)
async function handleDSAR(userId: string, type: 'access' | 'erasure' | 'portability') {
switch (type) {
case 'access': return await exportUserData(userId); // JSON/CSV
case 'erasure': return await deleteUserData(userId); // Right to be forgotten
case 'portability': return await exportPortableData(userId); // Machine-readable
}
}
Data Minimization
- Collect only what's needed for the stated purpose
- Set retention policies with automatic deletion
- Pseudonymize where possible (replace PII with tokens)
- Anonymize for analytics (k-anonymity, differential privacy)
PCI-DSS Key Controls
- Never store CVV/CVC — ever, in any form
- Tokenize card numbers — use Stripe/Braintree tokens instead of raw PANs
- Network segmentation — isolate cardholder data environment (CDE)
- Quarterly vulnerability scans — ASV-approved external scans
- Penetration testing — annual at minimum, after significant changes
Compliance as Code
- Policy as code: Open Policy Agent (OPA), AWS Config Rules, Azure Policy
- Infrastructure compliance: Terraform Sentinel, Checkov, tfsec
- Runtime compliance: Falco for container monitoring, AWS GuardDuty
- Evidence collection: Automated screenshot/log collection for audit evidence
1---2name: compliance-engineering3description: SOC2, HIPAA, GDPR, PCI-DSS, FedRAMP compliance implementation in code. Audit logging, data encryption, access controls, privacy by design, and regulatory requirement mapping. Use when implementing compliance controls, preparing for audits, or building privacy-compliant systems.4---5
6# Compliance Engineering
7
8## Framework Overview
9
10| Framework | Scope | Key Requirements |
11|-----------|-------|-----------------|
12| **SOC 2** | Service organizations | Security, availability, confidentiality, privacy, processing integrity |
13| **HIPAA** | Healthcare data (PHI) | Encryption, access controls, audit logging, BAAs |
14| **GDPR** | EU personal data | Consent, data minimization, right to erasure, DPIAs |
15| **PCI-DSS** | Payment card data | Network segmentation, encryption, access controls, logging |
16| **FedRAMP** | US government cloud | NIST 800-53 controls, continuous monitoring, authorization |
17
18## SOC 2 Controls in Code
19
20### Audit Logging
21```typescript
22interface AuditEvent {
23 timestamp: string;
24 actor: { id: string; role: string; ip: string };
25 action: string;
26 resource: { type: string; id: string };
27 outcome: 'success' | 'failure';
28 metadata: Record<string, unknown>;
29}
30
31async function auditLog(event: AuditEvent): Promise<void> {
32 // Write-once, append-only storage (immutable)
33 await auditStore.append({
34 ...event,
35 timestamp: new Date().toISOString(),
36 hash: computeChainHash(event), // tamper detection
37 });
38}
39```
40
41### Access Control
42```typescript
43// RBAC with principle of least privilege
44const permissions = {
45 admin: ['read', 'write', 'delete', 'manage_users'],
46 editor: ['read', 'write'],
47 viewer: ['read'],
48} as const;
49
50function authorize(user: User, action: string, resource: Resource): boolean {
51 const allowed = permissions[user.role];
52 if (!allowed?.includes(action)) {
53 auditLog({ action, outcome: 'failure', actor: user, resource });
54 return false;
55 }
56 return true;
57}
58```
59
60## HIPAA Technical Safeguards
61
62- **Encryption at rest:** AES-256 for PHI storage, AWS KMS / GCP KMS for key management
63- **Encryption in transit:** TLS 1.2+ mandatory, certificate pinning for mobile
64- **Access controls:** Unique user IDs, automatic logoff, MFA required
65- **Audit controls:** Log all PHI access, retain logs 6+ years, tamper-evident
66- **Data backup:** Encrypted backups, tested restore procedures, geographic redundancy
67
68## GDPR Implementation
69
70### Consent Management
71```typescript
72interface ConsentRecord {
73 userId: string;
74 purpose: string;
75 granted: boolean;
76 timestamp: string;
77 source: 'explicit' | 'legitimate_interest';
78 withdrawable: boolean;
79}
80
81// Data Subject Access Request (DSAR)
82async function handleDSAR(userId: string, type: 'access' | 'erasure' | 'portability') {
83 switch (type) {
84 case 'access': return await exportUserData(userId); // JSON/CSV
85 case 'erasure': return await deleteUserData(userId); // Right to be forgotten
86 case 'portability': return await exportPortableData(userId); // Machine-readable
87 }
88}
89```
90
91### Data Minimization
92- Collect only what's needed for the stated purpose
93- Set retention policies with automatic deletion
94- Pseudonymize where possible (replace PII with tokens)
95- Anonymize for analytics (k-anonymity, differential privacy)
96
97## PCI-DSS Key Controls
98- **Never store CVV/CVC** — ever, in any form
99- **Tokenize card numbers** — use Stripe/Braintree tokens instead of raw PANs
100- **Network segmentation** — isolate cardholder data environment (CDE)
101- **Quarterly vulnerability scans** — ASV-approved external scans
102- **Penetration testing** — annual at minimum, after significant changes
103
104## Compliance as Code
105- **Policy as code:** Open Policy Agent (OPA), AWS Config Rules, Azure Policy
106- **Infrastructure compliance:** Terraform Sentinel, Checkov, tfsec
107- **Runtime compliance:** Falco for container monitoring, AWS GuardDuty
108- **Evidence collection:** Automated screenshot/log collection for audit evidence