Audit access token rotation

What does this MR do and why?

Closes #438762 Part 1 of #634410 (rotation audit events)

Rotating an access token leaves no audit event. PersonalAccessTokens::RotateService revokes the old token with token.revoke! and creates the new one with personal_access_tokens.create, so the audited create and revoke services never run. GroupAccessTokens::RotateService and ProjectAccessTokens::RotateService inherit that behaviour.

A rotation mints a new credential and revokes the old one, so it needs the same visibility as a creation. When a token is rotated unexpectedly, for example by someone other than the application that relies on it, owners can't see who rotated it, when, or from where without asking Support to search application logs.

This MR adds three audit event types, recorded by the EE override of PersonalAccessTokens::RotateService#execute after a successful rotation:

Event Scope Target
personal_access_token_rotated User (the author, like personal_access_token_created and _revoked) Token owner
group_access_token_rotated Group New token
project_access_token_rotated Project New token

The group and project rotate services inherit execute, so a single override covers all three. The event type comes from the resource the rotate service was given.

Entry points covered

  • POST /personal_access_tokens/:id/rotate and POST /personal_access_tokens/self/rotate
  • POST /groups/:id/access_tokens/:token_id/rotate and POST /projects/:id/access_tokens/:token_id/rotate
  • POST /groups/:id/access_tokens/self/rotate and POST /projects/:id/access_tokens/self/rotate. These call the service without a resource, so the event type and scope come from the bot user's group or project.
  • Service account token rotation through the API
  • The personalAccessTokenRotate GraphQL mutation
  • The group and project access token settings pages
  • The admin impersonation token page

Event details

{
  "event_name": "project_access_token_rotated",
  "custom_message": "Rotated project access token: revoked token with id 101, created token with id 102",
  "target_details": "project_123_bot",
  "revoked_token_id": 101,
  "created_token_id": 102,
  "token_name": "deploy",
  "scopes": ["read_repository"],
  "expires_at": "2026-12-02"
}

The source IP is added by the audit framework. Impersonation tokens also carry "impersonation": true.

Failed rotations

A failed rotation is audited the same way as a failed creation or revocation:

Token Event Message
Personal personal_access_token_rotated, like personal_access_token_created and _revoked Attempted to rotate personal access token with id 101 but failed with message: …
Group group_access_token_rotation_failed (new), like group_access_token_creation_failed Attempted to rotate group access token with id 101 but failed with message: …
Project project_access_token_rotation_failed (new), like project_access_token_creation_failed Attempted to rotate project access token with id 101 but failed with message: …

The details carry token_id instead of the revoked and created token IDs.

Not in this MR

These are the remaining parts of #634410:

  • Routing impersonation token revocation through the audited revoke service
  • Recording the user agent on token created, revoked and rotated events. Doing that consistently needs changes to every caller, so it's better as its own MR.

How to set up and validate locally

  1. With an Ultimate license, rotate a personal access token from User settings > Access tokens, a project access token from Settings > Access tokens, and a group access token through the API.
  2. Check the audit events for the user, project and group, or log/audit_json.log. Each rotation produces one *_access_token_rotated event with both token IDs.
  3. Try to rotate a revoked token through the API. An "Attempted to rotate" event is recorded (*_rotation_failed for group and project access tokens).

Tests

ee/spec/services/personal_access_tokens/rotate_service_audit_log_spec.rb:

  • each of the personal, group and project rotate services records its event with the right scope, message and details
  • the audit event is saved
  • a failed rotation (revoked token) records an "Attempted to rotate" event, using *_rotation_failed for group and project access tokens, without the rotation token IDs
  • personal access token events target the token owner
  • impersonation tokens are flagged
  • self-rotation of a group or project access token records the group or project event

ee/spec/requests/api/resource_access_tokens/self_rotation_spec.rb: both self/rotate endpoints record the group or project event, not personal_access_token_rotated

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist.

Context

This is part of the Security Logging team's work to close high-priority gaps in gitlab-org&736 (Comprehensive audit log), so customers can answer the same incident-response questions from audit events that GitLab answers internally from Rails logs.

🤖 Generated with Claude Code

Edited by Daryn Wilkinson

Merge request reports

Loading
Loading