Admin API Audit Log

Kong Gateway provides a granular logging facility on 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.

Getting Started

Audit logging is disabled by default. It is configured via the Kong configuration (e.g. 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 back 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. vagrant@ubuntu-xenial:/kong$ http :8001/status
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Type: application/json; charset=utf-8
  6. Date: Tue, 13 Nov 2018 17:32:47 GMT
  7. Server: kong/0.34-enterprise-edition
  8. Transfer-Encoding: chunked
  9. X-Kong-Admin-Request-ID: ZuUfPfnxNn7D2OTU6Xi4zCnQkavzMUNM
  10. {
  11. "database": {
  12. "reachable": true
  13. },
  14. "server": {
  15. "connections_accepted": 1,
  16. "connections_active": 1,
  17. "connections_handled": 1,
  18. "connections_reading": 0,
  19. "connections_waiting": 0,
  20. "connections_writing": 1,
  21. "total_requests": 1
  22. }
  23. }

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

  1. http :8001/audit/requests
  2. HTTP/1.1 200 OK
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Type: application/json; charset=utf-8
  6. Date: Tue, 13 Nov 2018 17:35:24 GMT
  7. Server: kong/0.34-enterprise-edition
  8. Transfer-Encoding: chunked
  9. X-Kong-Admin-Request-ID: VXgMG1Y3rZKbjrzVYlSdLNPw8asVwhET
  10. {
  11. "data": [
  12. {
  13. "client_ip": "127.0.0.1",
  14. "method": "GET",
  15. "path": "/status",
  16. "payload": null,
  17. "request_id": "ZuUfPfnxNn7D2OTU6Xi4zCnQkavzMUNM",
  18. "request_timestamp": 1581617463,
  19. "signature": null,
  20. "status": 200,
  21. "ttl": 2591995,
  22. "workspace": "0da4afe7-44ad-4e81-a953-5d2923ce68ae"
  23. }
  24. ],
  25. "total": 1
  26. }

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:

  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. "request_id": "QUtUa3RMbRLxomqcL68ilOjjl68h56xr",
  10. "request_timestamp": 1581617463,
  11. "signature": null,
  12. "status": 200,
  13. "ttl": 2591995,
  14. "workspace": "0da4afe7-44ad-4e81-a953-5d2923ce68ae"
  15. }
  16. ],
  17. "total": 1
  18. }

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

Limiting Audit Log Generation

It may be desirable to ignore audit log generation for certain Admin API requests such as innocuous requests to the /status endpoint for healthchecking or to ignore requests for a given path prefix (e.g. a given Workspace). To this end, the audit_log_ignore_methods and audit_log_ignore_paths configuration options are presented:

  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 databse:

  • /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

Audit Log Retention

Request audit 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. In Cassandra databases, record deletion is handled automatically via the Cassandra TTL mechanism. In Postgres databases, records are purged via the stored procedure that is executed on insert into the record database. Thus, 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.

Database Audits

Generating and Viewing Audit Logs

In addition to Admin API request data, Kong will 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. http :8001/consumers username=bob
  2. HTTP/1.1 201 Created
  3. Access-Control-Allow-Origin: *
  4. Connection: keep-alive
  5. Content-Type: application/json; charset=utf-8
  6. Date: Tue, 13 Nov 2018 17:50:18 GMT
  7. Server: kong/0.34-enterprise-edition
  8. Transfer-Encoding: chunked
  9. X-Kong-Admin-Request-ID: 59fpTWlpUtHJ0qnAWBzQRHRDv7i5DwK2
  10. {
  11. "created_at": 1542131418000,
  12. "id": "16787ed7-d805-434a-9cec-5e5a3e5c9e4f",
  13. "type": 0,
  14. "username": "bob"
  15. }

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

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

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

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, it may be desirable 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

Database audit 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. In Cassandra databases, record deletion is handled automatically via the Cassandra TTL mechanism. In Postgres databases, records are purged via the stored procedure that is executed on insert into the record database. Thus, database 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

Generate a private key via the openssl tool:

  1. openssl genrsa -out private.pem 2048

Extract the public key for future audit verification:

  1. openssl rsa -in private.pem -outform PEM -pubout -out public.pem

Configure Kong to sign audit log records:

  1. audit_log_signing_key = /path/to/private.pem

Audit log entries will now contain a field signature:

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

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. 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

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 addditional bytes, such as a trailing newline `\n`, will cause a validation failure in the next step.

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

Reference

API Reference

List Request Audit Logs

Endpoint

/audit/requests

Response
  1. HTTP 200 OK
  1. {
  2. "data": [
  3. {
  4. "client_ip": "127.0.0.1",
  5. "method": "GET",
  6. "path": "/status",
  7. "payload": null,
  8. "request_id": "ZuUfPfnxNn7D2OTU6Xi4zCnQkavzMUNM",
  9. "request_timestamp": 1581617463,
  10. "signature": null,
  11. "status": 200,
  12. "ttl": 2591995,
  13. "workspace": "0da4afe7-44ad-4e81-a953-5d2923ce68ae"
  14. }
  15. ],
  16. "total": 1
  17. }

List Database Audit Logs

Endpoint

/audit/objects

Response
  1. HTTP 200 OK
  1. {
  2. "data": [
  3. {
  4. "dao_name": "consumers",
  5. "entity": "{\"created_at\":1542131418000,\"id\":\"16787ed7-d805-434a-9cec-5e5a3e5c9e4f\",\"username\":\"bob\",\"type\":0}",
  6. "entity_key": "16787ed7-d805-434a-9cec-5e5a3e5c9e4f",
  7. "expire": 1544723418009,
  8. "id": "7ebabee7-2b09-445d-bc1f-2092c4ddc4be",
  9. "operation": "create",
  10. "request_id": "59fpTWlpUtHJ0qnAWBzQRHRDv7i5DwK2"
  11. },
  12. ],
  13. "total": 1
  14. }

Configuration Reference

See the Data & Admin Audit section of the Configuration Property Reference.