Security & Privacy

📖 8 min read 📄 Part 9 of 10

Key-Value Store - Security and Privacy

Authentication and Authorization

Authentication Mechanisms

1. API Key Authentication:
   - Static API keys per client
   - Stored in secure key management service
   - Rotated every 90 days
   - Rate limited per key

   Example:
     GET /api/v1/keys/user:123
     Authorization: Bearer sk_live_abc123xyz789

2. OAuth 2.0:
   - Token-based authentication
   - Short-lived access tokens (1 hour)
   - Refresh tokens for renewal
   - Scope-based permissions

   Flow:
     Client → Auth Server: Request token
     Auth Server → Client: Access token
     Client → KV Store: Request with token
     KV Store → Auth Server: Validate token
     KV Store → Client: Response

3. Mutual TLS (mTLS):
   - Certificate-based authentication
   - Both client and server verify certificates
   - Strong cryptographic identity
   - No password management

4. IAM Role-Based:
   - Cloud provider IAM integration
   - Temporary credentials
   - Fine-grained permissions
   - Audit trail

Authorization Models

Role-Based Access Control (RBAC):
  Roles:
    - Admin: Full access to all operations
    - Writer: Read and write access
    - Reader: Read-only access
    - Auditor: Read metadata only

  Example:
    User: john@example.com
    Role: Writer
    Permissions:
      - GET /keys/*
      - PUT /keys/*
      - DELETE /keys/*
      - ❌ POST /admin/*

Attribute-Based Access Control (ABAC):
  Policies:
    - Allow if user.department == resource.department
    - Allow if user.clearance >= resource.classification
    - Allow if time.hour >= 9 AND time.hour <= 17

  Example:
    User: {department: "engineering", clearance: "secret"}
    Resource: {department: "engineering", classification: "confidential"}
    Result: ALLOW (clearance >= classification)

Key-Level Permissions:
  - Per-key access control lists (ACLs)
  - Owner, group, and public permissions
  - Unix-style permissions (rwx)

  Example:
    Key: "user:123:profile"
    Owner: user:123 (rwx)
    Group: admins (r-x)
    Public: (---)

Data Encryption

Encryption at Rest

Storage Encryption:
  - AES-256 encryption for all data
  - Encrypted SSTables on disk
  - Encrypted WAL files
  - Encrypted backups

Key Management:
  - Master key stored in HSM or KMS
  - Data encryption keys (DEKs) per SSTable
  - Key rotation every 90 days
  - Envelope encryption pattern

Encryption Flow:
  1. Generate DEK for new SSTable
  2. Encrypt SSTable with DEK
  3. Encrypt DEK with master key
  4. Store encrypted DEK with SSTable
  5. On read: Decrypt DEK, decrypt data

Example:
  Master Key (in KMS): mk_abc123
  Data Encryption Key: dek_xyz789
  
  Write:
    plaintext → AES-256(plaintext, dek_xyz789) → ciphertext
    dek_xyz789 → AES-256(dek_xyz789, mk_abc123) → encrypted_dek
    Store: ciphertext + encrypted_dek
  
  Read:
    encrypted_dek → AES-256-decrypt(encrypted_dek, mk_abc123) → dek_xyz789
    ciphertext → AES-256-decrypt(ciphertext, dek_xyz789) → plaintext

Encryption in Transit

TLS 1.3 Configuration:
  - Minimum TLS 1.2, prefer TLS 1.3
  - Strong cipher suites only
  - Perfect forward secrecy (PFS)
  - Certificate pinning for clients

Cipher Suites:
  - TLS_AES_256_GCM_SHA384
  - TLS_CHACHA20_POLY1305_SHA256
  - TLS_AES_128_GCM_SHA256

Certificate Management:
  - Automated certificate rotation
  - Let's Encrypt or internal CA
  - Certificate expiry monitoring
  - Revocation checking (OCSP)

Inter-Node Communication:
  - Mutual TLS (mTLS) required
  - Certificate-based authentication
  - Encrypted replication traffic
  - Encrypted gossip protocol

Client-Side Encryption

Application-Level Encryption:
  - Encrypt data before sending to store
  - Store only ciphertext
  - Decrypt after retrieval
  - Zero-knowledge architecture

Example:
  Client Side:
    plaintext = "sensitive data"
    key = client_encryption_key
    ciphertext = AES-256(plaintext, key)
    
    PUT /keys/user:123
    Body: ciphertext
  
  Server Side:
    - Stores ciphertext (can't decrypt)
    - No access to encryption key
    - Zero knowledge of plaintext

Benefits:
  ✅ Server can't read data
  ✅ Protection against server compromise
  ✅ Compliance with regulations
  
Challenges:
  ❌ No server-side operations (search, filter)
  ❌ Key management complexity
  ❌ Performance overhead

Access Control and Auditing

Audit Logging

Audit Log Schema:
  {
    "timestamp": "2024-01-08T12:00:00Z",
    "request_id": "req_abc123",
    "user_id": "user:123",
    "client_ip": "192.168.1.100",
    "operation": "GET",
    "resource": "/keys/user:456",
    "status": "SUCCESS",
    "latency_ms": 5,
    "user_agent": "KV-Client/1.0",
    "auth_method": "API_KEY",
    "data_accessed": {
      "key": "user:456",
      "size_bytes": 1024
    }
  }

Logged Events:
  - All read operations
  - All write operations
  - All delete operations
  - Authentication attempts
  - Authorization failures
  - Admin operations
  - Configuration changes

Storage:
  - Immutable append-only log
  - Stored in separate audit database
  - Retained for 7 years (compliance)
  - Encrypted at rest
  - Tamper-proof (cryptographic hashing)

Access Patterns Monitoring

Anomaly Detection:
  - Unusual access patterns
  - High-volume requests
  - Off-hours access
  - Geographic anomalies
  - Failed authentication attempts

Alerts:
  - >100 failed auth attempts in 5 minutes
  - Access from new geographic location
  - Bulk data export (>10K keys)
  - Admin operations outside business hours
  - Privilege escalation attempts

Example Alert:
  Alert: Suspicious Access Pattern
  User: john@example.com
  Pattern: 10,000 keys accessed in 1 minute
  Location: New York (usual: San Francisco)
  Time: 2:00 AM (unusual)
  Action: Temporarily suspend account, notify security team

Data Privacy and Compliance

GDPR Compliance

Right to Access:
  - API endpoint to retrieve all user data
  - Export in machine-readable format (JSON)
  - Include metadata and audit logs

  Example:
    GET /api/v1/privacy/export?user_id=user:123
    Response: {
      "user_data": {...},
      "metadata": {...},
      "audit_logs": [...]
    }

Right to Erasure (Right to be Forgotten):
  - API endpoint to delete all user data
  - Cascade delete across all replicas
  - Remove from backups (or anonymize)
  - Audit trail of deletion

  Example:
    DELETE /api/v1/privacy/erase?user_id=user:123
    Process:
      1. Identify all keys for user
      2. Delete from all replicas
      3. Mark for deletion in backups
      4. Log deletion event
      5. Confirm completion

Right to Rectification:
  - API endpoint to update user data
  - Propagate updates to all replicas
  - Audit trail of changes

Data Minimization:
  - Store only necessary data
  - Automatic data expiration (TTL)
  - Anonymize old data
  - Aggregate instead of raw data

Data Residency

Geographic Data Placement:
  - Store EU user data in EU region
  - Store US user data in US region
  - Prevent cross-border replication

Implementation:
  - Partition by user location
  - Region-specific clusters
  - Routing based on user location
  - Compliance metadata per key

Example:
  User: john@example.com (EU resident)
  Data: Stored in eu-west-1 cluster
  Replication: Only within EU region
  Compliance: GDPR, EU data residency laws

Enforcement:
  - Metadata tag: {"region": "EU", "residency": "required"}
  - Replication policy: EU-only
  - Access control: Block non-EU access
  - Audit: Log all access attempts

PII Handling

PII Detection:
  - Automatic detection of PII fields
  - Email addresses, phone numbers, SSN
  - Credit card numbers, addresses
  - Machine learning-based detection

PII Protection:
  - Encrypt PII fields separately
  - Tokenization for sensitive data
  - Masking in logs and monitoring
  - Access control for PII

Example:
  Key: "user:123:profile"
  Value: {
    "name": "John Doe",           // PII
    "email": "john@example.com",  // PII
    "age": 30,                    // Not PII
    "preferences": {...}          // Not PII
  }

  Storage:
    "name": encrypt("John Doe")
    "email": tokenize("john@example.com") → "token_abc123"
    "age": 30
    "preferences": {...}

  Logs:
    "Accessed user:123:profile" (no PII in logs)
    "Email: j***@example.com" (masked)

Network Security

DDoS Protection

Rate Limiting:
  - Per-client rate limits
  - Per-IP rate limits
  - Adaptive rate limiting
  - Token bucket algorithm

  Example:
    Client: api_key_abc123
    Limit: 10,000 requests/minute
    Burst: 1,000 requests/second
    
    If exceeded:
      - Return 429 Too Many Requests
      - Exponential backoff required
      - Temporary ban after repeated violations

Traffic Filtering:
  - Geo-blocking for suspicious regions
  - IP blacklisting
  - User-agent filtering
  - Request size limits

Load Balancing:
  - Distribute traffic across nodes
  - Health checks and failover
  - Connection limits per node
  - Circuit breakers

Firewall and Network Segmentation

Network Architecture:
  ┌─────────────────────────────────────────┐
  │         Public Internet                 │
  └─────────────────┬───────────────────────┘
                    │
  ┌─────────────────▼───────────────────────┐
  │      Load Balancer (WAF enabled)        │
  └─────────────────┬───────────────────────┘
                    │
  ┌─────────────────▼───────────────────────┐
  │      DMZ (API Gateway)                  │
  │      - TLS termination                  │
  │      - Authentication                   │
  │      - Rate limiting                    │
  └─────────────────┬───────────────────────┘
                    │
  ┌─────────────────▼───────────────────────┐
  │      Private Network (KV Store Nodes)   │
  │      - No public internet access        │
  │      - mTLS for inter-node comm         │
  │      - Encrypted replication            │
  └─────────────────────────────────────────┘

Firewall Rules:
  - Allow: HTTPS (443) from internet to load balancer
  - Allow: Internal traffic between KV nodes
  - Deny: All other traffic
  - Log: All denied connections

Incident Response

Security Incident Handling

Incident Response Plan:

1. Detection:
   - Automated alerts
   - Anomaly detection
   - User reports
   - Security scanning

2. Containment:
   - Isolate affected nodes
   - Revoke compromised credentials
   - Block malicious IPs
   - Enable additional logging

3. Investigation:
   - Analyze audit logs
   - Identify scope of breach
   - Determine root cause
   - Collect evidence

4. Remediation:
   - Patch vulnerabilities
   - Rotate credentials
   - Update firewall rules
   - Restore from backups if needed

5. Recovery:
   - Bring systems back online
   - Verify security posture
   - Monitor for recurrence
   - Update documentation

6. Post-Incident:
   - Incident report
   - Lessons learned
   - Update procedures
   - Notify affected users (if required)

Backup and Disaster Recovery

Backup Strategy:
  - Incremental backups every hour
  - Full backups daily
  - Encrypted backups
  - Cross-region replication
  - Immutable backups (WORM)

Recovery Procedures:
  - Point-in-time recovery
  - Automated recovery testing
  - RTO: <1 hour
  - RPO: <5 minutes

Security Considerations:
  - Encrypted backups
  - Access control for backups
  - Audit trail for backup access
  - Regular recovery drills

This comprehensive security and privacy guide ensures the distributed key-value store meets enterprise security requirements and regulatory compliance standards.