Audit two-factor recovery code regeneration
What does this MR do and why?
Part of #377621 (2FA recovery events)
Regenerating two-factor recovery codes leaves no audit event. There are two ways to do it:
- Profile page: User settings > Account > Two-factor authentication > Regenerate recovery codes (
Profiles::TwoFactorAuthsController#codes) - SSH:
ssh git@gitlab.com 2fa_recovery_codes(POST /internal/two_factor_recovery_codes)
The SSH path matters for account takeover. Anyone holding a user's SSH key can mint a new set of recovery codes and then sign in past two-factor authentication. Today that leaves nothing in the audit log, while enabling and disabling 2FA (user_enable_two_factor, user_disable_two_factor) and failed OTP sign-ins are already audited.
This MR adds a user_two_factor_recovery_codes_regenerated audit event:
| Field | Value |
|---|---|
| Scope | The enterprise group for enterprise users, otherwise the user, matching user_enable_two_factor |
| Author and target | The user |
additional_details.source |
web or ssh |
| Message | Regenerated two-factor authentication recovery codes (ssh) |
Codes generated as part of enabling 2FA aren't audited separately, because user_enable_two_factor already covers that.
How it works
Users::TwoFactorRecoveryCodesAuditor(EE) records the event.- Web: the EE override of
Profiles::TwoFactorAuthsController#codescalls it after the codes are generated. - SSH: a new CE helper,
audit_two_factor_recovery_codes_regenerated, does nothing in CE. It's called after the codes are generated, and the EE helper override calls the auditor.
For the SSH path, the request comes from gitlab-shell, so the IP address recorded is gitlab-shell's rather than the user's.
Not in this MR
The rest of #377621 (2FA "updated", "auth request" and recovery success or failure events). Successful 2FA sign-ins are already audited as authenticated_with_two_factor, and failed ones as login_failed_with_otp_authentication.
How to set up and validate locally
- With an Ultimate license, enable 2FA for a user.
- Regenerate recovery codes from the profile page, then run
ssh -p 2222 git@gdk.test 2fa_recovery_codeswith that user's SSH key. - Check the user's audit events, or
log/audit_json.log. There's oneuser_two_factor_recovery_codes_regeneratedevent per regeneration, withsourceset toweborssh.
Tests
ee/spec/lib/users/two_factor_recovery_codes_auditor_spec.rb: event contents, the enterprise group scope, and that an unknown source raises an erroree/spec/controllers/ee/profiles/two_factor_auths_controller_spec.rb: regenerating from the profile page records the event; and an invalid password doesn'tee/spec/requests/api/internal/base_spec.rb: regenerating over SSH audits; a refused request (SSH keys disabled by the group) doesn't
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.