|
| 1 | +Kong Request Log Bridge |
| 2 | +==== |
| 3 | +Transform Kong request logs and forward them to Elasticsearch. Redact request logs for improved privacy and security, and index them directly into Elasticsearch, without the need for complex and heavyweight tools like Logstash. |
| 4 | + |
| 5 | +[Source Code](https://github.com/braedon/kong-log-bridge) | [Docker Image](https://hub.docker.com/r/braedon/kong-log-bridge) |
| 6 | + |
| 7 | +# Usage |
| 8 | +The service is distributed as a docker image. Released versions can be found on Docker Hub (note that no `latest` version is provided): |
| 9 | + |
| 10 | +```bash |
| 11 | +> sudo docker pull braedon/kong-log-bridge:<version> |
| 12 | +``` |
| 13 | + |
| 14 | +The docker image exposes a REST API on port `8080`. It is configured by passing options after the image name: |
| 15 | +```bash |
| 16 | +> sudo docker run --rm --name kong-log-bridge \ |
| 17 | + -p <host port>:8080 \ |
| 18 | + braedon/kong-log-bridge:<version> \ |
| 19 | + -e <elasticsearch node> \ |
| 20 | + --convert-ts \ |
| 21 | + --hash-ip \ |
| 22 | + --hash-auth \ |
| 23 | + --hash-cookie |
| 24 | +``` |
| 25 | +Run with the `-h` flag to see details on all the available options. |
| 26 | + |
| 27 | +Note that all options can be set via environment variables. The environment variable names are prefixed with `KONG_LOG_BRIDGE_OPT`, e.g. `KONG_LOG_BRIDGE_OPT_CONVERT_TS=true` is equivalent to `--convert-ts`. CLI options take precedence over environment variables. |
| 28 | + |
| 29 | +## Input |
| 30 | +Kong JSON request logs can be `POST`ed to the `/logs` endpoint. This is designed for logs to be sent by the [Kong HTTP Log plugin](https://docs.konghq.com/hub/kong-inc/http-log/). See the Kong documentation for details on how to enable and configure the plugin. |
| 31 | + |
| 32 | +This is currently the only supported input method, but more may be added in the future. |
| 33 | + |
| 34 | +## Transformation |
| 35 | +Request logs are passed through unchanged by default, but you probably want to enable at least one transformation. |
| 36 | + |
| 37 | +### Timestamp Conversion `--convert-ts` |
| 38 | +Kong request logs include a number of UNIX timestamps (some in milliseconds rather than seconds). These are not human readable, and require explicit mappings to be used in Elasticsearch. Enabling this option will convert these timestamps to [RFC3339 date-time strings](https://www.ietf.org/rfc/rfc3339.txt) for readability and automatic Elasticsearch mapping. |
| 39 | + |
| 40 | +Fields converted: |
| 41 | +``` |
| 42 | + - service.created_at |
| 43 | + - service.updated_at |
| 44 | + - route.created_at |
| 45 | + - route.updated_at |
| 46 | + - started_at |
| 47 | + - tries[].balancer_start |
| 48 | +``` |
| 49 | + |
| 50 | +### Client IP Hashing `--hash-ip` |
| 51 | +This option enables hashing the `client_ip` field to avoid storing sensitive user IP addresses. |
| 52 | + |
| 53 | +### Authorization Hashing `--hash-auth` |
| 54 | +This option enables hashing the `credentials` part of the [`Authorization` request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization) (`request.headers.authorization` field) to avoid storing credentials/tokens. |
| 55 | + |
| 56 | +``` |
| 57 | +Authorization: Bearer some_secret_token -> Bearer 7ftgstREEBqhHrQNgj6MVA |
| 58 | +``` |
| 59 | + |
| 60 | +### Cookie Hashing `--hash-cookie` |
| 61 | +This option enables hashing the `value` part of the [`Cookie` request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie) (`request.headers.cookie` field) and [`Set-Cookie` response header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) (`response.headers.set-cookie` field) to avoid storing sensitive cookies. |
| 62 | + |
| 63 | +``` |
| 64 | +Cookie: some_cookie=some_session -> some_cookie=q1EXmTUdD0Bvm8_jHrQizw |
| 65 | +Set-Cookie: some_cookie=some_session; Secure; HttpOnly; SameSite=Lax -> some_cookie=q1EXmTUdD0Bvm8_jHrQizw; Secure; HttpOnly; SameSite=Lax |
| 66 | +``` |
| 67 | + |
| 68 | +### Field Hashing and Nulling `--hash-path`/`--null-path` |
| 69 | +Arbitrary request log fields can be hashed or converted to null by specifying their path with these options. Provide the desired option multiple times to specify multiple paths. |
| 70 | + |
| 71 | +Paths describe how to traverse the JSON structure of the request logs to find a field. They consist of a hierarchy of object fields to traverse from the root JSON object, separated by periods (`.`). The `[]` suffix on a field indicates its value is an array, and should be iterated. |
| 72 | + |
| 73 | +e.g. `--hash-path tries[].ip` will hash the `ip` of every upstream "try" in the `tries` array. |
| 74 | + |
| 75 | +Paths don't need to end at specific value - they can specify an entire object or array. |
| 76 | + |
| 77 | +e.g. `--null-path request.headers` will convert the entire `request.headers` object to null, effectively removing it from the log. |
| 78 | + |
| 79 | +If a path doesn't match any field in a given request log it will be ignored. |
| 80 | + |
| 81 | +## Output |
| 82 | +Transformed logs are indexed in Elasticsearch. |
| 83 | + |
| 84 | +This is currently the only supported output method, but more may be added in the future. |
| 85 | + |
| 86 | +### Elasticsearch Nodes `-e`/`--es-node` (required) |
| 87 | +The address of at least one Elasticsearch node must be provided via this option. The port should be included if non-standard (`9200`). Provide the option multiple times to specify multiple nodes in a cluster. |
| 88 | + |
| 89 | +### Elasticsearch Index `-es-index` |
| 90 | +The Elasticsearch index to send logs to. [Elasticsearch index date math](https://www.elastic.co/guide/en/elasticsearch/reference/current/date-math-index-names.html) can be used. Defaults to `<kong-requests-{now/d}>`. |
| 91 | + |
| 92 | +### Elasticsearch Security |
| 93 | +A number of options exist to support Elasticsearch server and client SSL, and basic authentication. See the `-h` output for details. |
| 94 | + |
| 95 | +# Development |
| 96 | +To run directly from the git repo, run the following in the root project directory: |
| 97 | +```bash |
| 98 | +> pip3 install -r requirements.txt |
| 99 | +> python3 main.py [OPTIONS] |
| 100 | +``` |
| 101 | +To run tests (as usual, from the root project directory), use: |
| 102 | +```bash |
| 103 | +> python3 -m unittest |
| 104 | +``` |
| 105 | +Note that these tests currently only cover the log transformation functionality - there are no automated system tests as of yet. |
| 106 | + |
| 107 | +To build a docker image directly from the git repo, run the following in the root project directory: |
| 108 | +```bash |
| 109 | +> sudo docker build -t <your repository name and tag> . |
| 110 | +``` |
| 111 | + |
| 112 | +To develop in a docker container, first build the image, and then run the following in the root project directory: |
| 113 | +```bash |
| 114 | +> sudo docker run --rm -it --name kong-log-bridge --entrypoint bash -v $(pwd):/app <your repository name and tag> |
| 115 | +``` |
| 116 | +This will mount all the files inside the container, so editing tests or application code will be synced live. You can run the tests with `python -m unittest`. |
| 117 | + |
| 118 | +Send me a PR if you have a change you want to contribute! |
0 commit comments