mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-07 16:41:36 +00:00
1d6c7a0552
Closes Audit-2026-04-25 H-008 (High), M-017 (Medium), M-022 (Medium).
Hardens audit-trail tamper-resistance + minimizes PII leakage in one
cohesive change, with both controls applying automatically and no
operator action required at install time.
What changed
- internal/service/audit_redact.go (NEW) — RedactDetailsForAudit:
* credentialKeys deny-list (api_key, password, *_pem, eab_secret, ...)
* piiKeys deny-list (email, phone, ssn, name, address, ip_address, ...)
* case-insensitive key match; recurses into nested maps + arrays
* mutation-free; surfaces redacted_keys array for operator visibility
* nil/empty input → nil out (preserves pre-Bundle-6 behaviour)
- internal/service/audit.go — RecordEvent now routes details through
RedactDetailsForAudit BEFORE marshaling. No call-site changes required.
- internal/service/audit_redact_test.go (NEW) — full coverage:
* credential keys (~30 entries)
* PII keys (~20 entries)
* nested maps + arrays
* case-insensitivity
* mutation-free invariant
* JSON round-trip (catches type-assertion regressions)
* scalar pass-through (no panic on int/bool/nil)
- migrations/000018_audit_events_worm.up.sql (NEW) — DB-level WORM:
* BEFORE UPDATE OR DELETE trigger raises check_violation with
diagnostic citing the rationale + compliance-superuser hint
* REVOKE UPDATE,DELETE ON audit_events FROM certctl (defence-in-depth)
* REVOKE wrapped in pg_roles existence check so test fixtures
without the certctl role stay idempotent
- migrations/000018_audit_events_worm.down.sql (NEW) — clean teardown
for dev resets; not for production use.
- internal/repository/postgres/audit_worm_test.go (NEW, testcontainers,
-short gated) — INSERT succeeds; UPDATE + DELETE fail with
check_violation; second INSERT after blocked modification still
succeeds (no trigger-state corruption).
- docs/compliance.md — new section "Audit-Trail Integrity & Privacy
(Bundle 6)" with verification psql snippet, compliance-superuser
pattern (NOT auto-created), redactor before/after example, and a
maintenance note for adding new credential keys.
Compliance mapping
- H-008 (CWE-532 Insertion of Sensitive Information into Log File)
- M-017 (HIPAA Technical Safeguards §164.312(b) — audit controls)
- M-022 (GDPR Art. 32 — data minimization)
Threat model: TB-3 (audit log tampering), TB-1 (operator/orchestrator).
Verification
- go vet ./... → clean
- go build ./... → clean
- go test -short -count=1 ./... → all packages pass
- go test -count=1 -run TestRedactDetailsForAudit ./internal/service/...
→ all pass
- (testcontainers, gated by -short) audit_worm_test.go pins WORM contract
- npx tsc --noEmit (web) → clean (no frontend changes)
- python3 yaml.safe_load(api/openapi.yaml) → 89 paths
Backward compatibility
- Trigger applies forward only — existing rows unchanged.
- nil/empty details from RecordEvent callers → nil out (preserves prior
behaviour for the many existing call sites that pass nil).
- Compliance superusers (provisioned out-of-band) bypass the trigger.
Bundle 6 of the 2026-04-25 comprehensive audit.
177 lines
5.9 KiB
Go
177 lines
5.9 KiB
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Bundle-6 / Audit H-008 + M-022 / CWE-532 (Insertion of Sensitive Information into Log File):
|
|
//
|
|
// Audit events flow into the audit_events.details JSONB column. Pre-Bundle-6,
|
|
// the middleware stored only `body_hash` (sha256 truncated) — no raw body —
|
|
// but service-layer call sites pass arbitrary map[string]interface{} details
|
|
// at every RecordEvent invocation. A future call site that accidentally
|
|
// includes a credential key (api_key, password, ACME EAB secret, etc.) or
|
|
// a PII key (email, phone, SSN, etc.) would persist plaintext into the
|
|
// append-only audit table.
|
|
//
|
|
// This file is the chokepoint that scrubs every details map BEFORE
|
|
// AuditService.RecordEvent marshals it. Two deny-lists:
|
|
//
|
|
// credentialKeys — value replaced with "[REDACTED:CREDENTIAL]"
|
|
// piiKeys — value replaced with "[REDACTED:PII]"
|
|
//
|
|
// The redacted entry surfaces in `details.redacted_keys` so operators can
|
|
// audit the redactor itself during a compliance review (GDPR Art. 30
|
|
// records-of-processing requires this transparency).
|
|
//
|
|
// Match semantics:
|
|
// - case-insensitive
|
|
// - structural: walks nested maps and arrays
|
|
// - exact key match (substring would over-redact — e.g. "tokenized_data")
|
|
//
|
|
// Compliance mapping:
|
|
// - GDPR Art. 32 (data minimization) — M-022
|
|
// - HIPAA §164.312(b) (audit controls) — paired with WORM trigger
|
|
// - PCI-DSS 4.0 Req 3 (protect stored PII) — paired with M-018 (deferred)
|
|
|
|
// credentialKeys are field names whose values must never appear in the
|
|
// audit log. Match is case-insensitive. Add new entries when a new
|
|
// credential-bearing field is introduced anywhere in the codebase.
|
|
var credentialKeys = map[string]bool{
|
|
"api_key": true,
|
|
"apikey": true,
|
|
"password": true,
|
|
"passphrase": true,
|
|
"secret": true,
|
|
"client_secret": true,
|
|
"token": true,
|
|
"access_token": true,
|
|
"refresh_token": true,
|
|
"bootstrap_token": true,
|
|
"credential": true,
|
|
"credentials": true,
|
|
"private_key": true,
|
|
"privatekey": true,
|
|
"private_key_pem": true,
|
|
"key_pem": true,
|
|
"cert_pem": true,
|
|
"chain_pem": true,
|
|
"full_pem": true,
|
|
"eab_secret": true,
|
|
"eab_kid": true,
|
|
"acme_account_key": true,
|
|
"hmac": true,
|
|
"hmac_key": true,
|
|
"signature": true,
|
|
"auth": true,
|
|
"authorization": true,
|
|
"bearer": true,
|
|
}
|
|
|
|
// piiKeys are field names that may carry personal data. Redacted by
|
|
// default; per-route opt-in retention is a future enhancement (post-Bundle-6).
|
|
// Note `ip_address` is debatable — useful for forensics but flagged by
|
|
// GDPR Art. 32 — defaulting to redact, operators can audit + adjust.
|
|
var piiKeys = map[string]bool{
|
|
"email": true,
|
|
"email_address": true,
|
|
"phone": true,
|
|
"phone_number": true,
|
|
"telephone": true,
|
|
"ssn": true,
|
|
"social_security": true,
|
|
"dob": true,
|
|
"date_of_birth": true,
|
|
"name": true,
|
|
"full_name": true,
|
|
"first_name": true,
|
|
"last_name": true,
|
|
"surname": true,
|
|
"address": true,
|
|
"street": true,
|
|
"street_address": true,
|
|
"city": true,
|
|
"postal_code": true,
|
|
"zip": true,
|
|
"zipcode": true,
|
|
"ip": true,
|
|
"ip_address": true,
|
|
}
|
|
|
|
// RedactDetailsForAudit walks a details map and returns a NEW map with
|
|
// credential + PII values scrubbed. The original map is NOT mutated (so
|
|
// service-layer code that reuses the map for other purposes is safe).
|
|
//
|
|
// The returned map is the original shape PLUS a `redacted_keys` array
|
|
// listing every key path that was scrubbed. The array surfaces redaction
|
|
// footprint to operators without exposing values.
|
|
//
|
|
// nil-in / empty-in returns nil so callers can pass through to
|
|
// json.Marshal which renders "null" — matches pre-Bundle-6 behaviour
|
|
// for nil-details RecordEvent calls.
|
|
func RedactDetailsForAudit(details map[string]interface{}) map[string]interface{} {
|
|
if len(details) == 0 {
|
|
return nil
|
|
}
|
|
|
|
out := make(map[string]interface{}, len(details)+1)
|
|
var redactedKeys []string
|
|
|
|
for k, v := range details {
|
|
lower := strings.ToLower(k)
|
|
switch {
|
|
case credentialKeys[lower]:
|
|
out[k] = "[REDACTED:CREDENTIAL]"
|
|
redactedKeys = append(redactedKeys, k)
|
|
case piiKeys[lower]:
|
|
out[k] = "[REDACTED:PII]"
|
|
redactedKeys = append(redactedKeys, k)
|
|
default:
|
|
// Recurse into nested maps + arrays so deeply-nested credentials
|
|
// don't bypass the redactor. Primitives pass through unchanged.
|
|
out[k] = redactValue(v, &redactedKeys, k)
|
|
}
|
|
}
|
|
|
|
if len(redactedKeys) > 0 {
|
|
// Surface the redaction footprint. If the caller accidentally
|
|
// passed `redacted_keys` themselves, prefer ours — the redactor's
|
|
// view of what was scrubbed is the load-bearing audit signal.
|
|
out["redacted_keys"] = redactedKeys
|
|
}
|
|
return out
|
|
}
|
|
|
|
// redactValue is the recursive arm of RedactDetailsForAudit. It walks
|
|
// arbitrary JSON-shaped values (map / slice / scalar) and returns a value
|
|
// with credential + PII keys scrubbed. Mutation-free.
|
|
func redactValue(v interface{}, redactedKeys *[]string, parentKey string) interface{} {
|
|
switch typed := v.(type) {
|
|
case map[string]interface{}:
|
|
nested := make(map[string]interface{}, len(typed))
|
|
for k, vv := range typed {
|
|
lower := strings.ToLower(k)
|
|
switch {
|
|
case credentialKeys[lower]:
|
|
nested[k] = "[REDACTED:CREDENTIAL]"
|
|
*redactedKeys = append(*redactedKeys, parentKey+"."+k)
|
|
case piiKeys[lower]:
|
|
nested[k] = "[REDACTED:PII]"
|
|
*redactedKeys = append(*redactedKeys, parentKey+"."+k)
|
|
default:
|
|
nested[k] = redactValue(vv, redactedKeys, parentKey+"."+k)
|
|
}
|
|
}
|
|
return nested
|
|
case []interface{}:
|
|
nested := make([]interface{}, len(typed))
|
|
for i, item := range typed {
|
|
nested[i] = redactValue(item, redactedKeys, parentKey)
|
|
}
|
|
return nested
|
|
default:
|
|
// scalar (string, number, bool, nil) — pass through unchanged.
|
|
return typed
|
|
}
|
|
}
|