2020-01-24 13:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Repositories
|
|
|
|
class GitHttpController < Repositories::GitHttpClientController
|
|
|
|
include WorkhorseRequest
|
|
|
|
|
|
|
|
before_action :access_check
|
|
|
|
prepend_before_action :deny_head_requests, only: [:info_refs]
|
|
|
|
|
2020-02-26 13:09:24 -05:00
|
|
|
rescue_from Gitlab::GitAccess::ForbiddenError, with: :render_403_with_exception
|
2020-01-24 13:09:00 -05:00
|
|
|
rescue_from Gitlab::GitAccess::NotFoundError, with: :render_404_with_exception
|
2020-06-09 17:08:21 -04:00
|
|
|
rescue_from Gitlab::GitAccessProject::CreationError, with: :render_422_with_exception
|
2020-01-24 13:09:00 -05:00
|
|
|
rescue_from Gitlab::GitAccess::TimeoutError, with: :render_503_with_exception
|
2021-10-13 08:12:20 -04:00
|
|
|
rescue_from GRPC::Unavailable do |e|
|
|
|
|
render_503_with_exception(e, message: 'The git server, Gitaly, is not available at this time. Please contact your administrator.')
|
|
|
|
end
|
2020-01-24 13:09:00 -05:00
|
|
|
|
|
|
|
# GET /foo/bar.git/info/refs?service=git-upload-pack (git pull)
|
|
|
|
# GET /foo/bar.git/info/refs?service=git-receive-pack (git push)
|
|
|
|
def info_refs
|
|
|
|
log_user_activity if upload_pack?
|
|
|
|
|
|
|
|
render_ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /foo/bar.git/git-upload-pack (git pull)
|
|
|
|
def git_upload_pack
|
2020-04-21 11:21:10 -04:00
|
|
|
update_fetch_statistics
|
2020-01-24 13:09:00 -05:00
|
|
|
|
|
|
|
render_ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# POST /foo/bar.git/git-receive-pack" (git push)
|
|
|
|
def git_receive_pack
|
|
|
|
render_ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def deny_head_requests
|
|
|
|
head :forbidden if request.head?
|
|
|
|
end
|
|
|
|
|
|
|
|
def download_request?
|
|
|
|
upload_pack?
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_pack?
|
|
|
|
git_command == 'git-upload-pack'
|
|
|
|
end
|
|
|
|
|
|
|
|
def git_command
|
|
|
|
if action_name == 'info_refs'
|
|
|
|
params[:service]
|
|
|
|
else
|
|
|
|
action_name.dasherize
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_ok
|
|
|
|
set_workhorse_internal_api_content_type
|
|
|
|
render json: Gitlab::Workhorse.git_http_ok(repository, repo_type, user, action_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_403_with_exception(exception)
|
|
|
|
render plain: exception.message, status: :forbidden
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_404_with_exception(exception)
|
|
|
|
render plain: exception.message, status: :not_found
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_422_with_exception(exception)
|
|
|
|
render plain: exception.message, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2021-10-13 08:12:20 -04:00
|
|
|
def render_503_with_exception(exception, message: nil)
|
|
|
|
render plain: message || exception.message, status: :service_unavailable
|
2020-01-24 13:09:00 -05:00
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
def update_fetch_statistics
|
|
|
|
return unless project
|
2021-08-02 05:10:09 -04:00
|
|
|
return if Gitlab::Database.read_only?
|
2020-01-24 13:09:00 -05:00
|
|
|
return unless repo_type.project?
|
|
|
|
|
2021-02-23 10:10:47 -05:00
|
|
|
OnboardingProgressService.async(project.namespace_id).execute(action: :git_pull)
|
|
|
|
|
|
|
|
return if Feature.enabled?(:disable_git_http_fetch_writes)
|
2020-12-03 13:10:10 -05:00
|
|
|
|
2021-07-06 05:07:05 -04:00
|
|
|
Projects::FetchStatisticsIncrementService.new(project).execute
|
2020-01-24 13:09:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def access
|
2020-03-04 19:07:49 -05:00
|
|
|
@access ||= access_klass.new(access_actor, container, 'http',
|
2020-01-24 13:09:00 -05:00
|
|
|
authentication_abilities: authentication_abilities,
|
2020-03-04 19:07:49 -05:00
|
|
|
repository_path: repository_path,
|
2020-01-24 13:09:00 -05:00
|
|
|
redirected_path: redirected_path,
|
|
|
|
auth_result_type: auth_result_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_actor
|
|
|
|
return user if user
|
|
|
|
return :ci if ci?
|
|
|
|
end
|
|
|
|
|
|
|
|
def access_check
|
|
|
|
access.check(git_command, Gitlab::GitAccess::ANY)
|
2020-03-04 19:07:49 -05:00
|
|
|
|
|
|
|
if repo_type.project? && !container
|
2020-07-21 14:09:45 -04:00
|
|
|
@project = @container = access.container
|
2020-03-04 19:07:49 -05:00
|
|
|
end
|
2020-01-24 13:09:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def access_klass
|
|
|
|
@access_klass ||= repo_type.access_checker_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_user_activity
|
|
|
|
Users::ActivityService.new(user).execute
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Repositories::GitHttpController.prepend_mod_with('Repositories::GitHttpController')
|