|
| 1 | +# audit-event.sh |
| 2 | + |
| 3 | +## Purpose |
| 4 | +Emit structured JSON audit events for operational actions, outcomes, and metadata. |
| 5 | + |
| 6 | +## Location |
| 7 | +`shared/safety/audit-event.sh` |
| 8 | + |
| 9 | +## Preconditions |
| 10 | +- Required tools: `bash`, `date` |
| 11 | +- Required permissions: write permissions for `--output` target file (if used) |
| 12 | +- Required environment variables: optional `AUDIT_ACTOR` |
| 13 | + |
| 14 | +## Arguments |
| 15 | +| Flag | Required | Default | Description | |
| 16 | +|------|----------|---------|-------------| |
| 17 | +| `--action TEXT` | Yes | N/A | Action identifier | |
| 18 | +| `--actor TEXT` | No | `AUDIT_ACTOR` or `USER` | Acting principal | |
| 19 | +| `--target TEXT` | No | empty | Target resource id | |
| 20 | +| `--status VALUE` | No | `info` | `info\|success\|failure\|warning` | |
| 21 | +| `--message TEXT` | No | empty | Human-readable message | |
| 22 | +| `--event-id ID` | No | generated id | Event correlation id | |
| 23 | +| `--source TEXT` | No | script basename | Originating component | |
| 24 | +| `--meta KEY=VALUE` | No | none | Metadata pair (repeatable, last key wins) | |
| 25 | +| `--timestamp-format F` | No | `%Y-%m-%dT%H:%M:%S%z` | Timestamp format | |
| 26 | +| `--output FILE` | No | stdout | Append event to file | |
| 27 | +| `--pretty` | No | `false` | Multi-line JSON output | |
| 28 | + |
| 29 | +## Scenarios |
| 30 | +- Happy path: emit audit JSON to stdout for stream ingestion. |
| 31 | +- Common operational path: append JSON lines to audit file. |
| 32 | +- Failure path: missing action, invalid status, or invalid meta key. |
| 33 | +- Recovery/rollback path: correct schema fields and rerun emit step. |
| 34 | + |
| 35 | +## Usage |
| 36 | +```bash |
| 37 | +shared/safety/audit-event.sh --action deploy.start --status info --target service/api |
| 38 | +shared/safety/audit-event.sh --action deploy.finish --status success --meta version=1.4.2 --meta env=prod |
| 39 | +shared/safety/audit-event.sh --action db.backup --status failure --message "snapshot timeout" --output /var/log/devops-audit.log |
| 40 | +``` |
| 41 | + |
| 42 | +## Behavior |
| 43 | +- Main execution flow: |
| 44 | + - validate required fields and enums |
| 45 | + - generate timestamp/event id when absent |
| 46 | + - build escaped JSON payload |
| 47 | + - emit to stdout or append to output file |
| 48 | +- Idempotency notes: each execution emits a new event (non-idempotent by design). |
| 49 | +- Side effects: appends to audit file when `--output` is used. |
| 50 | + |
| 51 | +## Output |
| 52 | +- Standard output format: JSON object (compact by default, pretty with `--pretty`). |
| 53 | +- Exit codes: |
| 54 | + - `0` event emitted successfully |
| 55 | + - `2` validation/usage error |
| 56 | + |
| 57 | +## Failure Modes |
| 58 | +- Common errors and likely causes: |
| 59 | + - missing `--action` |
| 60 | + - invalid `--status` |
| 61 | + - malformed `--meta` pair or invalid metadata key |
| 62 | + - output directory does not exist |
| 63 | +- Recovery and rollback steps: |
| 64 | + - correct required fields and enums |
| 65 | + - ensure output path parent exists and is writable |
| 66 | + - validate metadata format before emission |
| 67 | + |
| 68 | +## Security Notes |
| 69 | +- Secret handling: avoid including credentials/tokens in message or metadata. |
| 70 | +- Least-privilege requirements: restrict write access to audit output path. |
| 71 | +- Audit/logging expectations: designed for SIEM/log pipeline ingestion. |
| 72 | + |
| 73 | +## Testing |
| 74 | +- Unit tests: |
| 75 | + - status enum and metadata validation |
| 76 | + - JSON escaping logic |
| 77 | +- Integration tests: |
| 78 | + - append behavior to output files |
| 79 | +- Manual verification: |
| 80 | + - emit success/failure events and validate JSON schema |
0 commit comments