Commit 6305cb21 authored by Ash McKenzie's avatar Ash McKenzie

Improve readability

* Rename ALLOWED_CONTROLLER_AND_ACTIONS to CONTROLLER_AND_ACTIONS_TO_REDIRECT
* Rename git_receive_pack? to git_receive_pack_request?
* Added comments for greater context
parent a979aeac
......@@ -3,6 +3,29 @@ module EE
module GitHttpClientController
extend ActiveSupport::Concern
# This module is responsible for determining if an incoming secondary bound
# HTTP request should be redirected to the primary.
#
# Why? A secondary is not allowed to perform any write actions, so any
# request of this type need to be sent through to the primary. By
# redirecting within code, we allow clients to git pull/push using their
# secondary git remote without needing an additional primary remote.
#
# Current secondary HTTP requests to redirect: -
#
# * git push
# * GET /repo.git/info/refs?service=git-receive-pack
# * POST /repo.git/git-receive-pack
#
# * git lfs push (usually happens automatically as part of a `git push`)
# * POST /repo.git/info/lfs/objects/batch (and we examine
# params[:operation] to ensure we're dealing with an upload request)
#
# For more detail, see the following links:
#
# git: https://git-scm.com/book/en/v2/Git-Internals-Transfer-Protocols
# git-lfs: https://github.com/git-lfs/git-lfs/blob/master/docs/api
#
prepended do
before_action do
redirect_to(primary_full_url) if redirect?
......@@ -14,7 +37,7 @@ module EE
class RouteHelper
attr_reader :controller_name, :action_name
ALLOWED_CONTROLLER_AND_ACTIONS = {
CONTROLLER_AND_ACTIONS_TO_REDIRECT = {
'git_http' => %w{git_receive_pack},
'lfs_locks_api' => %w{create unlock verify}
}.freeze
......@@ -30,23 +53,40 @@ module EE
end
def redirect?
git_receive_pack? || controller_and_action_match?
!!CONTROLLER_AND_ACTIONS_TO_REDIRECT[controller_name]&.include?(action_name) ||
git_receive_pack_request?
end
private
attr_reader :service
def git_receive_pack?
service_or_action_name == 'git-receive-pack'
# Examples:
#
# /repo.git/info/refs?service=git-receive-pack returns 'git-receive-pack'
# /repo.git/info/refs?service=git-upload-pack returns 'git-upload-pack'
# /repo.git/git-receive-pack returns 'git-receive-pack'
# /repo.git/git-upload-pack returns 'git-upload-pack'
#
def service_or_action_name
info_refs_request? ? service : action_name.dasherize
end
def controller_and_action_match?
!!ALLOWED_CONTROLLER_AND_ACTIONS[controller_name]&.include?(action_name)
# Matches:
#
# GET /repo.git/info/refs?service=git-receive-pack
# POST /repo.git/git-receive-pack
#
def git_receive_pack_request?
service_or_action_name == 'git-receive-pack'
end
def service_or_action_name
action_name == 'info_refs' ? service : action_name.dasherize
# Matches:
#
# GET /repo.git/info/refs
#
def info_refs_request?
action_name == 'info_refs'
end
end
......@@ -117,19 +157,27 @@ module EE
end
def redirect?
# Don't redirect if we're not a secondary with a primary
return false unless ::Gitlab::Geo.secondary_with_primary?
# Redirect as the request matches RouteHelper::CONTROLLER_AND_ACTIONS_TO_REDIRECT
return true if route_helper.redirect?
# Look to redirect, as we're an LFS batch upload request
if git_lfs_helper.redirect?
# Redirect as git-lfs version is at least 2.4.2
return true if git_lfs_helper.version_ok?
# git-lfs 2.4.2 is really only required for requests that involve
# redirection, so we only render if it's an LFS upload operation
#
render(git_lfs_helper.incorrect_version_response)
# Don't redirect
return false
end
# Don't redirect
false
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment