From 36bdf3dc9ac290058b73ab54abeb9ba84b211b29 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 24 Dec 2017 09:03:58 -0800 Subject: [PATCH] Gracefully handle orphaned write deploy keys in /internal/post_receive On GitLab.com, there are write deploy keys with no associated users. Pushes with these deploy keys end with an Error 500 since we attempt to look up redirect message. If there is no user, don't attempt to display a redirect message. Closes #41466 --- .../unreleased/sh-handle-orphaned-deploy-keys.yml | 5 +++++ lib/api/internal.rb | 9 ++++++--- spec/requests/api/internal_spec.rb | 10 ++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 changelogs/unreleased/sh-handle-orphaned-deploy-keys.yml diff --git a/changelogs/unreleased/sh-handle-orphaned-deploy-keys.yml b/changelogs/unreleased/sh-handle-orphaned-deploy-keys.yml new file mode 100644 index 00000000000..7d3b622534e --- /dev/null +++ b/changelogs/unreleased/sh-handle-orphaned-deploy-keys.yml @@ -0,0 +1,5 @@ +--- +title: Gracefully handle orphaned write deploy keys in /internal/post_receive +merge_request: +author: +type: fixed diff --git a/lib/api/internal.rb b/lib/api/internal.rb index ccaaeca10d4..79b302aae70 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -190,9 +190,12 @@ module API project = Gitlab::GlRepository.parse(params[:gl_repository]).first user = identify(params[:identifier]) - redirect_message = Gitlab::Checks::ProjectMoved.fetch_redirect_message(user.id, project.id) - if redirect_message - output[:redirected_message] = redirect_message + + # A user is not guaranteed to be returned; an orphaned write deploy + # key could be used + if user + redirect_message = Gitlab::Checks::ProjectMoved.fetch_redirect_message(user.id, project.id) + output[:redirected_message] = redirect_message if redirect_message end output diff --git a/spec/requests/api/internal_spec.rb b/spec/requests/api/internal_spec.rb index bbcd1194ae5..7b25047ea8f 100644 --- a/spec/requests/api/internal_spec.rb +++ b/spec/requests/api/internal_spec.rb @@ -784,6 +784,16 @@ describe API::Internal do expect(json_response["redirected_message"]).to eq(project_moved.redirect_message) end end + + context 'with an orphaned write deploy key' do + it 'does not try to notify that project moved' do + allow_any_instance_of(Gitlab::Identifier).to receive(:identify).and_return(nil) + + post api("/internal/post_receive"), valid_params + + expect(response).to have_gitlab_http_status(200) + end + end end describe 'POST /internal/pre_receive' do