Pass key_id to git_audit_event endpoint for deploy key auth

What does this MR do and why?

Fixes git streaming audit events failing with errors when a deploy key authenticates via SSH.

Problem

When the log_git_streaming_audit_events feature flag is enabled and a deploy key performs a git
operation via SSH, the /api/v4/internal/shellhorse/git_audit_event endpoint returns errors.

Root cause:

  1. gitlab-shell sends only username (from gl_username in the /allowed response) to the audit
    endpoint
  2. For deploy keys without a real user, gl_username is the Ghost User's username
  3. Rails creates an actor via GitAccessActor.from_params({username: "ghost"})
  4. This finds the Ghost User but sets actor.key = nil
  5. The access check uses actor.key_or_user which returns the Ghost User
  6. Ghost User has no project access → 404

Solution

Pass key_id (from gl_key_id in the /allowed response) to the git_audit_event endpoint.

Rails' from_params prioritizes key_id over username:

 def self.from_params(params)                                                                        
   if params[:key_id]                                                                                
     new(key: Key.auth.find_by_id(params[:key_id]))  # ← Used when key_id is present                 
   elsif params[:username]                                                                           
     new(user: UserFinder.new(params[:username]).find_by_username)                                   
   ...                                                                                               
 end

With key_id sent:

  1. Rails finds the DeployKey directly via Key.auth.find_by_id(key_id)
  2. Sets actor.key = DeployKey
  3. actor.key_or_user returns the DeployKey
  4. DeployKey has project access → access check passes ✓

Rollout Plan

This MR is part of a coordinated fix across gitlab-shell and GitLab Rails.

Step 1: gitlab-shell

  • Merge this MR (pass key_id to audit endpoint)
  • Merge !1412 (merged) (add logging for debugging)
  • Release new version (e.g., v14.48.0)

Step 2: GitLab Rails

After gitlab-shell release, a follow-up Rails MR will:

  • Bump GITLAB_SHELL_VERSION to the new version
  • Remove deploy key guard in Gitlab::GitAuditEvent#enabled?
  • Add key_id param documentation to shellhorse endpoint

Local Validation Steps

Prerequisites

  1. Build gitlab-shell with the changes after checking out this branch:
    cd <gdk>/gitlab-shell
    make build

  2. Restart sshd to use the new binary:
    gdk restart sshd

  3. In gitlab repo, the deploy key is currently blocked. Temporarily remove the deploy key guard in Rails (this will be removed in a follow-up MR):
    In ee/lib/gitlab/git_audit_event.rb, remove line 19:
    return false unless author.is_a?(DeployToken) || user&.human?

  4. Restart Rails:
    gdk restart rails

  5. Enable the feature flag for your test project in rails c:
    Feature.enable(:log_git_streaming_audit_events)

Setup

  1. Generate a test SSH key:
    ssh-keygen -t ed25519 -f ~/.ssh/deploy_key_test -N '' -C 'deploy-key-test'
  2. Add the deploy key to your project: project -> settings -> repository -> deploy keys
  3. Set up audit event streaming webhook:
    - Go to https://webhook.site and copy the unique URL
    - Navigate to your project's group: Secure > Audit events > Streams
    - Click Add streaming destination
    - Enter a name and paste the webhook.site URL
    - Save the destination

Test

  1. Clone the project using the deploy key:
    GIT_SSH_COMMAND='ssh -i ~/.ssh/deploy_key_test -o StrictHostKeyChecking=no' git clone ssh://git@127.0.0.1:2222/<namespace>/<project>.git /tmp/deploy-key-test
  2. Check the API log for the audit event:
    grep "git_audit_event" <gdk>/gitlab/log/api_json.log | tail -1 | jq '{time, status, params}'
  3. Check webhook.site for the streamed audit event.

Outcome

  1. You should see the audit event in the webbook Screenshot_2026-04-01_at_3.04.48_pm
Edited by Emma Park

Merge request reports

Loading
Loading