Skip to content

feat: add API key metadata to audit logs#19996

Closed
ThomasK33 wants to merge 1 commit into
thomask33/09-26-add_detailed_scope_auth_metricsfrom
thomask33/09-28-add_api_key_audit_metadata
Closed

feat: add API key metadata to audit logs#19996
ThomasK33 wants to merge 1 commit into
thomask33/09-26-add_detailed_scope_auth_metricsfrom
thomask33/09-28-add_api_key_audit_metadata

Conversation

@ThomasK33
Copy link
Copy Markdown
Member

Enhanced API Key Audit Logging

This PR improves audit logging for API keys by:

  1. Adding detailed API key information to audit logs, including:

    • Scopes
    • Allow lists
    • Effective permissions
  2. Including API key metadata in all audit logs when an API key is used for authentication

  3. Adding tests to verify the new audit fields are properly populated

  4. Updating documentation to reflect that API key scopes and allow lists are now tracked in audit logs

These changes provide administrators with better visibility into API key usage and permissions, making it easier to track and audit API key activities.

Copy link
Copy Markdown
Member Author

ThomasK33 commented Sep 28, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@ThomasK33 ThomasK33 linked an issue Sep 28, 2025 that may be closed by this pull request
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from b00259f to e9a1439 Compare September 28, 2025 21:33
@ThomasK33 ThomasK33 marked this pull request as ready for review September 29, 2025 07:41
@ThomasK33 ThomasK33 requested review from Emyrk and johnstcn September 29, 2025 07:41
Comment thread coderd/apikey.go
Comment thread coderd/audit/apikey_fields.go Outdated
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from 8b7a31c to 7725526 Compare September 29, 2025 08:25
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from e9a1439 to 37565ce Compare September 29, 2025 08:25
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from 7725526 to e2539b7 Compare September 29, 2025 09:43
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch 2 times, most recently from d46b7f1 to c9891d7 Compare September 29, 2025 09:46
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from e2539b7 to 3bf5bf9 Compare September 29, 2025 09:46
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from c9891d7 to a1ba3a5 Compare September 29, 2025 09:54
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from 3bf5bf9 to aeb80bd Compare September 29, 2025 11:19
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from 1060d38 to 403b866 Compare October 2, 2025 17:37
Comment thread coderd/audit/request.go
Comment on lines +406 to +409
if key, ok := httpmw.APIKeyOptional(p.Request); ok {
fields := APIKeyFields(logCtx, p.Log, key)
additionalFieldsRaw = mergeAdditionalFields(logCtx, p.Log, additionalFieldsRaw, fields)
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this is a lot of extra data to staple to every audit log?

If we are trying to debug rbac failures, the raw input is logged on authz failures. Do we need to have scope metadata on all audit log entries?

@ThomasK33 ThomasK33 marked this pull request as draft October 3, 2025 16:55
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from d817f31 to b076a30 Compare October 3, 2025 17:59
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from 403b866 to 5e40a34 Compare October 3, 2025 17:59
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from b076a30 to b020309 Compare October 6, 2025 09:42
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from 5e40a34 to 4280771 Compare October 6, 2025 09:42
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from b020309 to 37cd2d3 Compare October 6, 2025 10:11
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from 4280771 to 384a406 Compare October 6, 2025 10:11
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from 37cd2d3 to 807fafe Compare October 6, 2025 10:48
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch 2 times, most recently from 154d4a1 to cafac8d Compare October 6, 2025 11:24
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch 2 times, most recently from 7b72854 to 5a425af Compare October 6, 2025 11:57
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from cafac8d to 393492a Compare October 6, 2025 11:57
@Emyrk Emyrk self-assigned this Oct 6, 2025
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from 5a425af to e408ecf Compare October 6, 2025 21:16
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from 393492a to 2c9a4c1 Compare October 6, 2025 21:16
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from e408ecf to ab2a24f Compare October 6, 2025 21:40
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch 2 times, most recently from 7915a16 to e153689 Compare October 7, 2025 16:38
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch 2 times, most recently from 802b588 to cb61426 Compare October 9, 2025 12:55
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from e153689 to 48d0e45 Compare October 9, 2025 12:56
For any action authenticated via an API key, the audit log now includes
metadata about the key used for the request. This provides visibility
into the permissions used to perform an action.

The metadata is stored in the `request_api_key` field within the
`additional_fields` payload and includes the key's ID, name, scopes,
allow list, and its effective/expanded scope.

Additionally, when an API key is the subject of a create, update, or
delete action, its own metadata is now stored in the `api_key` field
to provide a more complete record of the change.
@ThomasK33 ThomasK33 force-pushed the thomask33/09-28-add_api_key_audit_metadata branch from 48d0e45 to 0f2c153 Compare October 9, 2025 13:06
@ThomasK33 ThomasK33 force-pushed the thomask33/09-26-add_detailed_scope_auth_metrics branch from cb61426 to 06d4957 Compare October 9, 2025 13:06
@ThomasK33 ThomasK33 closed this Oct 9, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 9, 2025
@ThomasK33 ThomasK33 deleted the thomask33/09-28-add_api_key_audit_metadata branch October 9, 2025 15:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit logging: capture scopes/allow-list on key create/update/use

3 participants