|
1 | | -# vault-audit-filter |
2 | | - A lightweight tool designed to receive audit events from the vault and apply filtering using a rule-based engine |
| 1 | +[](https://goreportcard.com/report/github.com/ncode/vault-audit-filter) |
| 2 | +[](https://opensource.org/licenses/Apache-2.0) |
| 3 | +[](https://codecov.io/gh/ncode/vault-audit-filter) |
| 4 | + |
| 5 | +# Vault Audit Filter |
| 6 | + |
| 7 | +`vault-audit-filter` is a Go-based tool designed to filter and log HashiCorp Vault audit logs based on configurable rules. It provides fine-grained control over how Vault audit events are processed and categorized, allowing you to capture critical events while reducing noise from routine operations. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Configurable Rule-Based Filtering**: Define rules to match specific audit events, such as read, write, delete, or specific paths in Vault. |
| 12 | +- **Multiple Rule Groups**: Organize rules into groups and log them to separate files. |
| 13 | +- **Dynamic Logging**: Log audit events to specified files with log rotation and size limits. |
| 14 | +- **Supports Multiple Operations**: Filters common Vault operations, including KV operations, metadata updates, and deletion events. |
| 15 | +- **Performance-Oriented**: Built with `gnet` to handle high concurrency. |
| 16 | + |
| 17 | +## Table of Contents |
| 18 | + |
| 19 | +- [Getting Started](#getting-started) |
| 20 | +- [Installation](#installation) |
| 21 | +- [Configuration](#configuration) |
| 22 | +- [Usage](#usage) |
| 23 | +- [Testing](#testing) |
| 24 | +- [Contributing](#contributing) |
| 25 | +- [License](#license) |
| 26 | + |
| 27 | +## Getting Started |
| 28 | + |
| 29 | +These instructions will help you set up and run `vault-audit-filter` on your local machine. |
| 30 | + |
| 31 | +### Prerequisites |
| 32 | + |
| 33 | +- **Go**: Ensure you have Go 1.21 or later installed. You can download it here: <https://golang.org/dl/> |
| 34 | +- **Vault**: You should have HashiCorp Vault installed and configured. Instructions can be found here: <https://www.vaultproject.io/docs/install> |
| 35 | + |
| 36 | +### Installation |
| 37 | + |
| 38 | +Clone the repository: |
| 39 | + |
| 40 | + git clone https://github.com/ncode/vault-audit-filter.git |
| 41 | + cd vault-audit-filter |
| 42 | + |
| 43 | +### Build the Project |
| 44 | + |
| 45 | +To build the binary: |
| 46 | + |
| 47 | + go build -o vault-audit-filter . |
| 48 | + |
| 49 | +### Running the Application |
| 50 | + |
| 51 | +Once you have built the project, you can run the `vault-audit-filter` executable: |
| 52 | + |
| 53 | + ./vault-audit-filter --config config.yaml |
| 54 | + |
| 55 | +## Configuration |
| 56 | + |
| 57 | +`vault-audit-filter` uses a YAML-based configuration file that allows you to define rule groups, specify logging files, and configure Vault settings. |
| 58 | + |
| 59 | +### Sample Configuration (`config.yaml`) |
| 60 | + |
| 61 | + vault: |
| 62 | + address: "http://127.0.0.1:8200" |
| 63 | + token: "your-vault-token" |
| 64 | + audit_path: "/vault-audit-filter" |
| 65 | + audit_address: "127.0.0.1:1269" |
| 66 | + audit_description: "Vault Audit Filter Device" |
| 67 | + |
| 68 | + rule_groups: |
| 69 | + - name: "normal_operations" |
| 70 | + rules: |
| 71 | + - 'Request.Operation in ["read", "update"] && Request.Path startsWith "secret/data/" && Auth.PolicyResults.Allowed == true' |
| 72 | + log_file: |
| 73 | + file_path: "/var/log/vault_normal_operations.log" |
| 74 | + max_size: 100 # Max size in MB |
| 75 | + max_backups: 5 # Max number of backup files |
| 76 | + max_age: 30 # Max age in days |
| 77 | + compress: true # Compress rotated files |
| 78 | + |
| 79 | + - name: "critical_events" |
| 80 | + rules: |
| 81 | + - 'Request.Operation == "delete" && Auth.PolicyResults.Allowed == true' |
| 82 | + - 'Request.Path startsWith "secret/metadata/" && Auth.PolicyResults.Allowed == true' |
| 83 | + log_file: |
| 84 | + file_path: "/var/log/vault_critical_events.log" |
| 85 | + max_size: 100 |
| 86 | + max_backups: 5 |
| 87 | + max_age: 30 |
| 88 | + compress: true |
| 89 | + |
| 90 | +### Configuration Parameters |
| 91 | + |
| 92 | +- **Vault Settings**: |
| 93 | + - `vault.address`: The address of your Vault instance. |
| 94 | + - `vault.token`: Vault token for authentication. |
| 95 | + - `vault.audit_path`: The path for Vault's audit device. |
| 96 | + - `vault.audit_address`: The address for receiving audit logs. |
| 97 | + - `vault.audit_description`: Description for the Vault audit device. |
| 98 | + |
| 99 | +- **Rule Groups**: |
| 100 | + - `rule_groups.name`: The name of the rule group. |
| 101 | + - `rule_groups.rules`: A list of expressions using `expr` to define rules for audit log filtering. |
| 102 | + - `log_file.file_path`: The file path where matching logs will be written. |
| 103 | + - `log_file.max_size`: The maximum size of the log file in MB before rotation. |
| 104 | + - `log_file.max_backups`: The number of backup logs to keep. |
| 105 | + - `log_file.max_age`: The maximum number of days to retain logs. |
| 106 | + - `log_file.compress`: Whether to compress the old log files. |
| 107 | + |
| 108 | +### Rule Syntax |
| 109 | + |
| 110 | +Rules are written using the `expr` language, a simple and safe expression language for Go. Rules can be based on the following properties of audit logs: |
| 111 | + |
| 112 | +- `Request.Operation`: The type of operation (`read`, `update`, `delete`, etc.). |
| 113 | +- `Request.Path`: The Vault path being accessed. |
| 114 | +- `Auth.PolicyResults.Allowed`: Whether the operation was allowed. |
| 115 | + |
| 116 | +**Example Rule**: |
| 117 | + |
| 118 | + 'Request.Operation == "update" && Request.Path startsWith "secret/data/" && Auth.PolicyResults.Allowed == true' |
| 119 | + |
| 120 | +## Usage |
| 121 | + |
| 122 | +To run `vault-audit-filter` with your configuration file, use: |
| 123 | + |
| 124 | +```bash |
| 125 | +$ ./vault-audit-filter --config config.yaml |
| 126 | +``` |
| 127 | + |
| 128 | +### Command-Line Options |
| 129 | + |
| 130 | +- `--config`: Specify the path to the configuration file (default is `config.yaml`). |
| 131 | +- `--log-level`: Set the logging level (`debug`, `info`, `warn`, `error`). |
| 132 | + |
| 133 | +### Environment Variables |
| 134 | + |
| 135 | +You can also define environment variables to override configuration file values. For example: |
| 136 | + |
| 137 | +```bash |
| 138 | +$ export VAULT_ADDRESS="http://127.0.0.1:8200" |
| 139 | +$ export VAULT_TOKEN="your-vault-token" |
| 140 | +``` |
| 141 | + |
| 142 | +### Development |
| 143 | + |
| 144 | +For development purposes, you can use the provided Makefile located at `configs/development/Makefile` to build and run the project using Docker and Docker Compose. This is how I test my changes and have a playground of sorts. |
| 145 | + |
| 146 | +## Contributing |
| 147 | + |
| 148 | +We welcome contributions from the community! |
| 149 | +Before submitting a pull request, ensure that: |
| 150 | + |
| 151 | +- The code compiles without errors. |
| 152 | +- All tests pass. |
| 153 | +- Your changes are well-documented. |
| 154 | + |
| 155 | +## License |
| 156 | + |
| 157 | +This project is licensed under the Apache License, Version 2.0. See the `LICENSE` file for details. |
0 commit comments