Audit Logging in Kong Gateway

Kong Gateway provides a granular logging facility through its Admin API. This allows cluster administrators to keep detailed track of changes made to the cluster configuration throughout its lifetime, aiding in compliance efforts and providing valuable data points during forensic investigations. Generated audit log trails are Workspace and RBAC-aware, providing Kong operators a deep and wide look into changes happening within the cluster.

Enable audit logging

Audit logging is disabled by default. Configure it through Kong Gateway configuration in kong.conf:

  1. audit_log = on # audit logging is enabled
  2. audit_log = off # audit logging is disabled

or via environment variables:

  1. export KONG_AUDIT_LOG=on
  2. export KONG_AUDIT_LOG=off

As with other Kong configurations, changes take effect on kong reload or kong restart.

Request audits

Generating and viewing audit logs

Audit logging provides granular details of each HTTP request that was handled by Kong’s Admin API. Audit log data is written to Kong’s database. As a result, request audit logs are available via the Admin API (in addition to via direct database query).

For example, consider a query to the Admin API to the /status endpoint:

  1. curl -i -X GET http://localhost:8001/status

You get the following response:

  1. HTTP/1.1 200 OK
  2. Access-Control-Allow-Origin: *
  3. Connection: keep-alive
  4. Content-Type: application/json; charset=utf-8
  5. Date: Tue, 13 Nov 2018 17:32:47 GMT
  6. Server: kong/ kong/3.4.0.0-enterprise-edition
  7. Transfer-Encoding: chunked
  8. X-Kong-Admin-Request-ID: ZuUfPfnxNn7D2OTU6Xi4zCnQkavzMUNM
  9. {
  10. "database": {
  11. "reachable": true
  12. },
  13. "memory": {
  14. "lua_shared_dicts": {
  15. ...
  16. },
  17. "workers_lua_vms": [
  18. ...
  19. ]
  20. }
  21. "server": {
  22. "connections_accepted": 1,
  23. "connections_active": 1,
  24. "connections_handled": 1,
  25. "connections_reading": 0,
  26. "connections_waiting": 0,
  27. "connections_writing": 1,
  28. "total_requests": 1
  29. }
  30. }

The above interaction with the Admin API generates a correlating entry in the audit log table. Querying the audit log via the Admin API returns the details of the previous interaction:

  1. curl -i -X GET http://localhost:8001/audit/requests
  1. HTTP/1.1 200 OK
  2. Access-Control-Allow-Origin: *
  3. Connection: keep-alive
  4. Content-Type: application/json; charset=utf-8
  5. Date: Tue, 13 Nov 2018 17:35:24 GMT
  6. Server: kong/3.4.0.0-enterprise-edition
  7. Transfer-Encoding: chunked
  8. X-Kong-Admin-Request-ID: VXgMG1Y3rZKbjrzVYlSdLNPw8asVwhET
  9. {
  10. "data": [
  11. {
  12. "client_ip": "127.0.0.1",
  13. "method": "GET",
  14. "path": "/status",
  15. "payload": null,
  16. "rbac_user_id": null,
  17. "rbac_user_name": null,
  18. "removed_from_payload": null,
  19. "request_id": "OjOcUBvt6q6XJlX3dd6BSpy1uUkTyctC",
  20. "request_source": null,
  21. "request_timestamp": 1676424547,
  22. "signature": null,
  23. "status": 200,
  24. "ttl": 2591997,
  25. "workspace": "1065b6d6-219f-4002-b3e9-334fc3eff46c"
  26. }
  27. ],
  28. "total": 1
  29. }

Note the value of the request_id field. This is tied to the X-Kong-Admin-Request-ID response header received in the first transaction. This allows close association of client requests and audit log records within the Kong cluster.

Because every audit log entry is made available via Kong’s Admin API, it is possible to transport audit log entries into existing logging warehouses, SIEM solutions, or other remote services for duplication and inspection.

Workspaces and RBAC

Audit log entries are written with an awareness of the requested workspace, and the RBAC user (if present). When RBAC is enforced, the RBAC user’s UUID will be written to the rbac_user_id field in the audit log entry, and the username will be written to the rbac_user_name field:

  1. {
  2. "data": [
  3. {
  4. "client_ip": "127.0.0.1",
  5. "method": "GET",
  6. "path": "/status",
  7. "payload": null,
  8. "rbac_user_id": "2e959b45-0053-41cc-9c2c-5458d0964331",
  9. "rbac_user_name": "admin",
  10. "request_id": "QUtUa3RMbRLxomqcL68ilOjjl68h56xr",
  11. "request_source": "kong-manager",
  12. "request_timestamp": 1581617463,
  13. "signature": null,
  14. "status": 200,
  15. "ttl": 2591995,
  16. "workspace": "0da4afe7-44ad-4e81-a953-5d2923ce68ae"
  17. }
  18. ],
  19. "total": 1
  20. }

Note the presence of the workspace field. This is the UUID of the workspace with which the request is associated.

Kong Manager authentication

You can track login and logout events for the Kong Manager through the path, method, and request_source audit log fields.

For example, review the following audit log entry:

  1. {
  2. "data": [
  3. {
  4. "client_ip": "127.0.0.1",
  5. "method": "GET",
  6. "path": "/auth",
  7. "payload": null,
  8. "rbac_user_id": "2e959b45-0053-41cc-9c2c-5458d0964331",
  9. "rbac_user_name": "admin",
  10. "request_id": "QUtUa3RMbRLxomqcL68ilOjjl68h56xr",
  11. "request_source": "kong-manager",
  12. "request_timestamp": 1581617463,
  13. "signature": null,
  14. "status": 200,
  15. "ttl": 2591995,
  16. "workspace": "0da4afe7-44ad-4e81-a953-5d2923ce68ae"
  17. }
  18. ],
  19. "total": 1
  20. }

The request_source field tells you that the action occurred in Kong Manager.

The method and path fields correspond either to a login or logout event:

  • Login event: "method": "GET", "path": "/auth"
  • Logout event: "method": "DELETE", "path": "/auth?session_logout=true"

Limiting audit log generation

You may want to ignore audit log generation for certain Admin API requests, such as requests to the /status endpoint for health checking, or to ignore requests to a specific path prefix, for example, a given workspace.

Use the audit_log_ignore_methods and audit_log_ignore_paths configuration options:

  1. audit_log_ignore_methods = GET,OPTIONS
  2. # do not generate an audit log entry for GET or OPTIONS HTTP requests
  3. audit_log_ignore_paths = /foo,/status,^/services,/routes$,/one/.+/two,/upstreams/
  4. # do not generate an audit log entry for requests that match the above regular expressions

The values of audit_log_ignore_paths are matched via a Perl-compatible regular expression.

For example, when audit_log_ignore_paths = /foo,/status,^/services,/routes$,/one/.+/two,/upstreams/, the following request paths do not generate an audit log entry in the database:

  • /status
  • /status/
  • /foo
  • /foo/
  • /services
  • /services/example/
  • /one/services/two
  • /one/test/two
  • /routes
  • /plugins/routes
  • /one/routes/two
  • /upstreams/
  • bad400request

The following request paths generate an audit log entry in the database:

  • /example/services
  • /routes/plugins
  • /one/two
  • /routes/
  • /upstreams

Database audits

Generating and viewing audit logs

In addition to Admin API request data, Kong can generate granular audit log entries for all insertions, updates, and deletions to the cluster database. Database update audit logs are also associated with Admin API request unique IDs. Consider the following request to create a consumer:

  1. curl -i -X POST http://localhost:8001/consumers username=bob

Response:

  1. HTTP/1.1 201 Created
  2. Access-Control-Allow-Origin: *
  3. Connection: keep-alive
  4. Content-Type: application/json; charset=utf-8
  5. Date: Tue, 13 Nov 2018 17:50:18 GMT
  6. Server: kong/3.4.0.0-enterprise-edition
  7. Transfer-Encoding: chunked
  8. X-Kong-Admin-Request-ID: 59fpTWlpUtHJ0qnAWBzQRHRDv7i5DwK2
  9. {
  10. "created_at": 1542131418000,
  11. "id": "16787ed7-d805-434a-9cec-5e5a3e5c9e4f",
  12. "type": 0,
  13. "username": "bob"
  14. }

As seen before, a request audit log is generated with details about the request. Note the presence of the payload field, recorded when the request body is present:

  1. curl -i -X GET http://localhost:8001/audit/requests
  1. HTTP/1.1 200 OK
  2. Access-Control-Allow-Origin: *
  3. Connection: keep-alive
  4. Content-Type: application/json; charset=utf-8
  5. Date: Tue, 13 Nov 2018 17:52:41 GMT
  6. Server: kong/3.4.0.0-dev-enterprise-edition
  7. Transfer-Encoding: chunked
  8. X-Kong-Admin-Request-ID: SpPaxLTkDNndzKaYiWuZl3xrxDUIiGRR
  9. {
  10. "data": [
  11. {
  12. "client_ip": "127.0.0.1",
  13. "method": "POST",
  14. "path": "/consumers",
  15. "payload": "{\"username\": \"bob\"}",
  16. "request_id": "59fpTWlpUtHJ0qnAWBzQRHRDv7i5DwK2",
  17. "request_timestamp": 1581617463,
  18. "signature": null,
  19. "status": 201,
  20. "ttl": 2591995,
  21. "workspace": "fd51ce6e-59c0-4b6b-b991-aa708a9ff4d2"
  22. }
  23. ],
  24. "total": 1
  25. }

Additionally, audit logs are generated to track the creation of the database entity:

  1. curl -i -X GET http://localhost:8001/audit/objects

Response:

  1. HTTP/1.1 200 OK
  2. Access-Control-Allow-Origin: *
  3. Connection: keep-alive
  4. Content-Type: application/json; charset=utf-8
  5. Date: Tue, 13 Nov 2018 17:53:27 GMT
  6. Server: kong/3.4.0.0-dev-enterprise-edition
  7. Transfer-Encoding: chunked
  8. X-Kong-Admin-Request-ID: ZKra3QT0d3eJKl96jOUXYueLumo0ck8c
  9. {
  10. "data": [
  11. {
  12. "dao_name": "consumers",
  13. "entity": "{\"created_at\":1542131418000,\"id\":\"16787ed7-d805-434a-9cec-5e5a3e5c9e4f\",\"username\":\"bob\",\"type\":0}",
  14. "entity_key": "16787ed7-d805-434a-9cec-5e5a3e5c9e4f",
  15. "expire": 1544723418009,
  16. "id": "7ebabee7-2b09-445d-bc1f-2092c4ddc4be",
  17. "operation": "create",
  18. "request_id": "59fpTWlpUtHJ0qnAWBzQRHRDv7i5DwK2",
  19. "request_timestamp": 1581617463,
  20. },
  21. ],
  22. "total": 1
  23. }

Object audit entries contain information about the entity updated, including the entity body itself, its database primary key, and the type of operation performed (create, update, or delete). Note also the associated request_id field.

Limiting audit log generation

As with request audit logs, you may want to skip generation of audit logs for certain database tables. This is configurable via the audit_log_ignore_tables Kong config option:

  1. audit_log_ignore_tables = consumers
  2. # do not generate database audit logs for changes to the consumers table

Audit log retention

Audit log records are kept in the database for a duration defined by the audit_log_record_ttl Kong configuration property. Records in the database older than audit_log_record_ttl seconds are automatically purged.

PostgreSQL purges records via the stored procedure that is executed on insert into the record database.

Therefore, request audit records may exist in the database longer than the configured TTL, if no new records are inserted to the audit table following the expiration timestamp.

Digital signatures

To provide non-repudiation, audit logs may be signed with a private RSA key. When enabled, a lexically sorted representation of each audit log entry is signed by the defined private key; the signature is stored in an additional field within the record itself. The public key should be stored elsewhere and can be used later to validate the signature of the record.

Setting up log signing

  1. Generate a private key via the openssl tool:

    1. openssl genrsa -out private.pem 2048
  2. Extract the public key for future audit verification:

    1. openssl rsa -in private.pem -outform PEM -pubout -out public.pem
  3. Configure Kong to sign audit log records:

    1. audit_log_signing_key = /path/to/private.pem
  4. Audit log entries will now contain the field signature:

    1. {
    2. "client_ip": "127.0.0.1",
    3. "method": "GET",
    4. "path": "/status",
    5. "payload": null,
    6. "rbac_user_id": "2e959b45-0053-41cc-9c2c-5458d0964331",
    7. "rbac_user_name": null,
    8. "request_id": "Ka2GeB13RkRIbMwBHw0xqe2EEfY0uZG0",
    9. "request_source": null,
    10. "request_timestamp": 1581617463,
    11. "signature": "l2LWYaRIHfXglFa5ehFc2j9ijfERazxisKVtJnYa+QUz2ckcytxfOLuA4VKEWHgY7cCLdn5C7uRJzE6es5V2SoOV59NOpskkr5lTt9kzao64UEw5UNOdeZYZKwyhG9Ge7IsxTK6haW0iG3a9dHqlKlwvnHZTbFM8TUV/umg8sJ1QJ/5ivXecbyHYtD5luKAI6oEgIdZPtQexRkwxlzvfR8lzeC/dDc2slSrjWRbBxNFlgfRKhDdVzVzgu8pEucgKggu67PKLkJ+bQEkxX1+Yg3czIpJyC3t6cgoggb0UNtBq1uUpswe0wdueKh6G5Gzz6XrmOjlv7zSz4gtVyEHZgg==",
    12. "status": 200,
    13. "ttl": 2591995,
    14. "workspace": "fd51ce6e-59c0-4b6b-b991-aa708a9ff4d2"
    15. }

Validating signatures

To verify record signatures, use the openssl utility, or other cryptographic tools that are capable of validating RSA digital signatures.

Signatures are generated using a 256-bit SHA digest. The following example demonstrates how to verify the audit log record shown above.

  1. First, store the record signature on disk after stripping the Base64 encoding:

    1. cat <<EOF | base64 -d > record_signature
    2. > l2LWYaRIHfXglFa5ehFc2j9ijfERazxisKVtJnYa+QUz2ckcytxfOLuA4VKEWHgY7cCLdn5C7uRJzE6es5V2SoOV59NOpskkr5lTt9kzao64UEw5UNOdeZYZKwyhG9Ge7IsxTK6haW0iG3a9dHqlKlwvnHZTbFM8TUV/umg8sJ1QJ/5ivXecbyHYtD5luKAI6oEgIdZPtQexRkwxlzvfR8lzeC/dDc2slSrjWRbBxNFlgfRKhDdVzVzgu8pEucgKggu67PKLkJ+bQEkxX1+Yg3czIpJyC3t6cgoggb0UNtBq1uUpswe0wdueKh6G5Gzz6XrmOjlv7zSz4gtVyEHZgg==
    3. > EOF
  2. Next, the audit record must be transformed into its canonical format used for signature generation. This transformation requires serializing the record into a string format that can be verified. The format is a lexically-sorted, pipe-delimited string of each audit log record part, without the signature, ttl, or expire fields. The following is a canonical implementation written in Lua:

    1. local cjson = require "cjson"
    2. local pl_sort = require "pl.tablex".sort
    3. local function serialize(data)
    4. local p = {}
    5. data.signature = nil
    6. data.expire = nil
    7. data.ttl = nil
    8. for k, v in pl_sort(data) do
    9. if type(v) == "table" then
    10. p[#p + 1] = serialize(v)
    11. elseif v ~= cjson.null then
    12. p[#p + 1] = v
    13. end
    14. end
    15. return p
    16. end
    17. table.concat(serialize(data), "|")

    For example, the canonical format of the audit record above is:

    1. cat canonical_record.txt
    2. 127.0.0.1|1544724298663|GET|/status|Ka2GeB13RkRIbMwBHw0xqe2EEfY0uZG0|1542132298664|200|fd51ce6e-59c0-4b6b-b991-aa708a9ff4d2

    Ensure that the contents of the canonical record file on disk match the expected canonical record format exactly. The presence of any additional bytes, such as a trailing newline \n, will cause a validation failure in the next step.

  3. Once these two elements are in place, the signature can be verified:

    1. openssl dgst -sha256 -verify public.pem -signature record_signature canonical_record.txt
    2. Verified OK

More information

  • For Kong Gateway kong.conf options, see the Data & Admin Audit section of the Configuration Property Reference.
  • For the /audit API reference, see Audit Logs.