|
| 1 | +<!-- SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company |
| 2 | +SPDX-License-Identifier: Apache-2.0 |
| 3 | +--> |
| 4 | + |
| 5 | +# audittools |
| 6 | + |
| 7 | +`audittools` provides a standard interface for generating and sending CADF (Cloud Auditing Data Federation) audit events to a RabbitMQ message broker. |
| 8 | + |
| 9 | +## Certification Requirements (PCI DSS, SOC 2, and more) |
| 10 | + |
| 11 | +As a cloud provider subject to strict audits (including PCI DSS and more), we must ensure the **completeness** and **integrity** of audit logs while maintaining service **availability**. |
| 12 | + |
| 13 | +### Standard Production Configuration |
| 14 | + |
| 15 | +**You MUST configure a Backing Store with Persistent Storage (PVC).** |
| 16 | + |
| 17 | +* **Configuration**: Set `BackingStorePath` to a mount point backed by a PVC. |
| 18 | +* **Requirement**: This ensures that audit events are preserved even in double-failure scenarios (RabbitMQ outage + Pod crash/reschedule). |
| 19 | +* **Compliance**: Satisfies requirements for guaranteed event delivery and audit trail completeness. |
| 20 | + |
| 21 | +### Non-Compliant Configurations |
| 22 | + |
| 23 | +The following configurations are available for development or specific edge cases but are **NOT** recommended for production services subject to audit: |
| 24 | + |
| 25 | +1. **Ephemeral Storage (emptyDir)**: |
| 26 | + * *Risk*: Data loss if the Pod is rescheduled during a RabbitMQ outage. |
| 27 | + * *Status*: **Development / Testing Only**. |
| 28 | + |
| 29 | +2. **No Backing Store**: |
| 30 | + * *Behavior*: The service will **block** (stop processing requests) if the RabbitMQ broker is down and the memory buffer fills up. |
| 31 | + * *Risk*: Service downtime (Violation of Availability targets). |
| 32 | + * *Status*: **Not Recommended**. Only acceptable if service downtime is preferred over *any* storage complexity. |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +### Basic Setup |
| 37 | + |
| 38 | +To use `audittools`, you typically initialize an `Auditor` with your RabbitMQ connection details. |
| 39 | + |
| 40 | +```go |
| 41 | +import "github.com/sapcc/go-bits/audittools" |
| 42 | + |
| 43 | +func main() { |
| 44 | + // ... |
| 45 | + auditor, err := audittools.NewAuditor(audittools.AuditorOpts{ |
| 46 | + EnvPrefix: "MYSERVICE_AUDIT", // Configures env vars like MYSERVICE_AUDIT_RABBITMQ_URL |
| 47 | + }) |
| 48 | + if err != nil { |
| 49 | + log.Fatal(err) |
| 50 | + } |
| 51 | + // ... |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +### Sending Events |
| 56 | + |
| 57 | +```go |
| 58 | +event := cadf.Event{ |
| 59 | + // ... fill in event details ... |
| 60 | +} |
| 61 | +auditor.Record(event) |
| 62 | +``` |
| 63 | + |
| 64 | +## Disk-Based Buffering |
| 65 | + |
| 66 | +`audittools` includes buffering to ensure audit events are not lost if the RabbitMQ broker becomes unavailable. Events are temporarily written to disk and replayed once the connection is restored. |
| 67 | + |
| 68 | +### Configuration |
| 69 | + |
| 70 | +Buffering is enabled by providing a `BackingStorePath`. |
| 71 | + |
| 72 | +```go |
| 73 | +auditor, err := audittools.NewAuditor(audittools.AuditorOpts{ |
| 74 | + EnvPrefix: "MYSERVICE_AUDIT", |
| 75 | + BackingStorePath: "/var/lib/myservice/audit-buffer", |
| 76 | +}) |
| 77 | +``` |
| 78 | + |
| 79 | +Or via environment variables: |
| 80 | + |
| 81 | +* `MYSERVICE_AUDIT_BACKING_STORE_PATH`: Directory to store buffered events. |
| 82 | +* `MYSERVICE_AUDIT_BACKING_STORE_MAX_TOTAL_SIZE`: (Optional) Max total size of the buffer in bytes. |
| 83 | + |
| 84 | +### Kubernetes Deployment |
| 85 | + |
| 86 | +If running in Kubernetes, you have two main options for the backing store: |
| 87 | + |
| 88 | +1. **Persistent Storage (PVC) - Recommended for Audit Compliance**: |
| 89 | + * Mount a Persistent Volume Claim (PVC) at the `BackingStorePath`. |
| 90 | + * **Pros**: Data survives Pod deletion, rescheduling, and rolling updates. |
| 91 | + * **Cons**: Adds complexity (volume management, access modes). |
| 92 | + * **Use Case**: **Required** for strict audit compliance to ensure no data is lost even if the service instance fails during a broker outage. |
| 93 | + |
| 94 | +2. **Ephemeral Storage (emptyDir)**: |
| 95 | + * Mount an `emptyDir` volume at the `BackingStorePath`. |
| 96 | + * **Pros**: Simple, fast, no persistent volume management. |
| 97 | + * **Cons**: Data is lost if the *Pod* is deleted or rescheduled. However, it survives container restarts within the same Pod. |
| 98 | + * **Use Case**: Suitable for non-critical environments or where occasional data loss during complex failure scenarios (simultaneous broker outage + pod rescheduling) is acceptable. |
| 99 | + |
| 100 | +### Behavior |
| 101 | + |
| 102 | +The system transitions through the following states to ensure zero data loss: |
| 103 | + |
| 104 | +1. **Normal Operation**: Events are sent directly to RabbitMQ. |
| 105 | +2. **RabbitMQ Outage**: Events are written to the disk backing store. The application continues without blocking. |
| 106 | +3. **Disk Full**: If the backing store reaches `BackingStoreMaxTotalSize`, writes fail. Events are buffered in memory (up to 20). |
| 107 | +4. **Fail-Closed**: If the memory buffer fills up, `auditor.Record()` **blocks**. This pauses the application to prevent data loss. |
| 108 | +5. **Recovery**: A background routine continuously drains the backing store to RabbitMQ once it becomes available. New events are persisted to disk during draining to prevent blocking. |
| 109 | + |
| 110 | +**Additional Details**: |
| 111 | + |
| 112 | +* **Security**: The directory is created with `0700` permissions, and files with `0600`, ensuring only the service user can access the sensitive audit data. |
| 113 | +* **Capacity**: If `BackingStoreMaxTotalSize` is configured, the store will reject new writes when full. The limit is approximate and may be exceeded by up to one event's size (typically a few KB) due to the check-then-write sequence. Set the limit with appropriate headroom for your filesystem. |
| 114 | +* **Corrupted Event Handling**: |
| 115 | + * Corrupted events encountered during reads are written to dead-letter files (`audit-events-deadletter-*.jsonl`) |
| 116 | + * Dead-letter files contain metadata (timestamp, source file) and the raw corrupted data for investigation |
| 117 | + * The `corrupted_event` metric is incremented for monitoring |
| 118 | + * Source files are deleted after processing, even if all events were corrupted (after moving to dead-letter) |
| 119 | + * Dead-letter files should be monitored and investigated to identify data corruption issues |
| 120 | + |
| 121 | +### Metrics |
| 122 | + |
| 123 | +The backing store exports the following Prometheus metrics: |
| 124 | + |
| 125 | +* `audittools_backing_store_writes_total`: Total number of audit events written to disk. |
| 126 | +* `audittools_backing_store_reads_total`: Total number of audit events read from disk. |
| 127 | +* `audittools_backing_store_errors_total`: Total number of errors, labeled by operation: |
| 128 | + * `write_stat`: Failed to stat file during rotation check |
| 129 | + * `write_full`: Backing store is full (exceeds `MaxTotalSize`) |
| 130 | + * `write_open`: Failed to open backing store file for writing |
| 131 | + * `write_marshal`: Failed to marshal event to JSON |
| 132 | + * `write_io`: Failed to write event to disk |
| 133 | + * `write_sync`: Failed to sync (flush) event to disk |
| 134 | + * `write_close`: Failed to close backing store file |
| 135 | + * `read_open`: Failed to open backing store file for reading |
| 136 | + * `read_scan`: Failed to scan backing store file |
| 137 | + * `corrupted_event`: Encountered corrupted event during read (written to dead-letter) |
| 138 | + * `deadletter_write`: Successfully wrote corrupted event to dead-letter file |
| 139 | + * `deadletter_write_failed`: Failed to write corrupted event to dead-letter file |
| 140 | + * `commit_remove`: Failed to remove file after successful processing |
| 141 | +* `audittools_backing_store_size_bytes`: Current total size of the backing store in bytes. |
| 142 | +* `audittools_backing_store_files_count`: Current number of files in the backing store. |
0 commit comments