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:
- gitlab-shell sends only username (from
gl_usernamein the/allowedresponse) to the audit
endpoint - For deploy keys without a real user,
gl_usernameis the Ghost User's username - Rails creates an actor via
GitAccessActor.from_params({username: "ghost"}) - This finds the Ghost User but sets
actor.key = nil - The access check uses
actor.key_or_userwhich returns the Ghost User - 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)
...
endWith key_id sent:
- Rails finds the DeployKey directly via
Key.auth.find_by_id(key_id) - Sets
actor.key= DeployKey actor.key_or_userreturns the DeployKey- 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_idto 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_VERSIONto the new version - Remove deploy key guard in
Gitlab::GitAuditEvent#enabled? - Add
key_idparam documentation to shellhorse endpoint
Local Validation Steps
Prerequisites
-
Build gitlab-shell with the changes after checking out this branch:
cd <gdk>/gitlab-shell
make build -
Restart sshd to use the new binary:
gdk restart sshd -
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):
Inee/lib/gitlab/git_audit_event.rb, remove line 19:
return false unless author.is_a?(DeployToken) || user&.human? -
Restart Rails:
gdk restart rails -
Enable the feature flag for your test project in rails c:
Feature.enable(:log_git_streaming_audit_events)
Setup
- Generate a test SSH key:
ssh-keygen -t ed25519 -f ~/.ssh/deploy_key_test -N '' -C 'deploy-key-test' - Add the deploy key to your project:
project -> settings -> repository -> deploy keys - 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
- 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 - Check the API log for the audit event:
grep "git_audit_event" <gdk>/gitlab/log/api_json.log | tail -1 | jq '{time, status, params}' - Check webhook.site for the streamed audit event.
Outcome
Related issues
- Issue: #833 (closed)
- Epic: gitlab-org#20506 (closed)
