summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSean McGivern <sean@mcgivern.me.uk>2018-09-06 11:27:27 +0000
committerSean McGivern <sean@mcgivern.me.uk>2018-09-06 11:27:27 +0000
commit228d819b5761de1e2362952a9d0f08828c88424d (patch)
tree9abf4a26597c63cc2cadc1bf67aa308c35b68f76 /lib
parent98ae35a8b08c86f7a380aecfb967f092cc1e0ef0 (diff)
parent21cccabe24c518db502b019bac5039383db21067 (diff)
downloadgitlab-ce-228d819b5761de1e2362952a9d0f08828c88424d.tar.gz
Merge branch 'ash.mckenzie/geo-git-push-ssh-proxy' into 'master'
Custom Action support See merge request gitlab-org/gitlab-ce!21034
Diffstat (limited to 'lib')
-rw-r--r--lib/api/internal.rb59
-rw-r--r--lib/gitlab/git_access.rb10
-rw-r--r--lib/gitlab/git_access_result/custom_action.rb25
-rw-r--r--lib/gitlab/git_access_result/success.rb8
4 files changed, 80 insertions, 22 deletions
diff --git a/lib/api/internal.rb b/lib/api/internal.rb
index 516f25db15b..0990e2a1fba 100644
--- a/lib/api/internal.rb
+++ b/lib/api/internal.rb
@@ -6,8 +6,17 @@ module API
helpers ::API::Helpers::InternalHelpers
helpers ::Gitlab::Identifier
+ UNKNOWN_CHECK_RESULT_ERROR = 'Unknown check result'.freeze
+
+ helpers do
+ def response_with_status(code: 200, success: true, message: nil, **extra_options)
+ status code
+ { status: success, message: message }.merge(extra_options).compact
+ end
+ end
+
namespace 'internal' do
- # Check if git command is allowed to project
+ # Check if git command is allowed for project
#
# Params:
# key_id - ssh key id for Git over SSH
@@ -18,8 +27,6 @@ module API
# action - git action (git-upload-pack or git-receive-pack)
# changes - changes as "oldrev newrev ref", see Gitlab::ChangesList
post "/allowed" do
- status 200
-
# Stores some Git-specific env thread-safely
env = parse_env
Gitlab::Git::HookEnv.set(gl_repository, env) if project
@@ -49,27 +56,37 @@ module API
namespace_path: namespace_path, project_path: project_path,
redirected_path: redirected_path)
- begin
- access_checker.check(params[:action], params[:changes])
- @project ||= access_checker.project
- rescue Gitlab::GitAccess::UnauthorizedError, Gitlab::GitAccess::NotFoundError => e
- break { status: false, message: e.message }
- end
+ check_result = begin
+ result = access_checker.check(params[:action], params[:changes])
+ @project ||= access_checker.project
+ result
+ rescue Gitlab::GitAccess::UnauthorizedError => e
+ break response_with_status(code: 401, success: false, message: e.message)
+ rescue Gitlab::GitAccess::NotFoundError => e
+ break response_with_status(code: 404, success: false, message: e.message)
+ end
log_user_activity(actor)
- {
- status: true,
- gl_repository: gl_repository,
- gl_id: Gitlab::GlId.gl_id(user),
- gl_username: user&.username,
-
- # This repository_path is a bogus value but gitlab-shell still requires
- # its presence. https://gitlab.com/gitlab-org/gitlab-shell/issues/135
- repository_path: '/',
-
- gitaly: gitaly_payload(params[:action])
- }
+ case check_result
+ when ::Gitlab::GitAccessResult::Success
+ payload = {
+ gl_repository: gl_repository,
+ gl_id: Gitlab::GlId.gl_id(user),
+ gl_username: user&.username,
+
+ # This repository_path is a bogus value but gitlab-shell still requires
+ # its presence. https://gitlab.com/gitlab-org/gitlab-shell/issues/135
+ repository_path: '/',
+
+ gitaly: gitaly_payload(params[:action])
+ }
+ response_with_status(**payload)
+ when ::Gitlab::GitAccessResult::CustomAction
+ response_with_status(code: 300, message: check_result.message, payload: check_result.payload)
+ else
+ response_with_status(code: 500, success: false, message: UNKNOWN_CHECK_RESULT_ERROR)
+ end
end
post "/lfs_authenticate" do
diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb
index 258e19a340b..93720500711 100644
--- a/lib/gitlab/git_access.rb
+++ b/lib/gitlab/git_access.rb
@@ -50,6 +50,10 @@ module Gitlab
check_authentication_abilities!(cmd)
check_command_disabled!(cmd)
check_command_existence!(cmd)
+
+ custom_action = check_custom_action(cmd)
+ return custom_action if custom_action
+
check_db_accessibility!(cmd)
ensure_project_on_push!(cmd, changes)
@@ -65,7 +69,7 @@ module Gitlab
check_push_access!
end
- true
+ ::Gitlab::GitAccessResult::Success.new
end
def guest_can_download_code?
@@ -92,6 +96,10 @@ module Gitlab
private
+ def check_custom_action(cmd)
+ nil
+ end
+
def check_valid_actor!
return unless actor.is_a?(Key)
diff --git a/lib/gitlab/git_access_result/custom_action.rb b/lib/gitlab/git_access_result/custom_action.rb
new file mode 100644
index 00000000000..a05a4baed82
--- /dev/null
+++ b/lib/gitlab/git_access_result/custom_action.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module GitAccessResult
+ class CustomAction
+ attr_reader :payload, :message
+
+ # Example of payload:
+ #
+ # {
+ # 'action' => 'geo_proxy_to_primary',
+ # 'data' => {
+ # 'api_endpoints' => %w{geo/proxy_git_push_ssh/info_refs geo/proxy_git_push_ssh/push},
+ # 'gl_username' => user.username,
+ # 'primary_repo' => geo_primary_http_url_to_repo(project_or_wiki)
+ # }
+ # }
+ #
+ def initialize(payload, message)
+ @payload = payload
+ @message = message
+ end
+ end
+ end
+end
diff --git a/lib/gitlab/git_access_result/success.rb b/lib/gitlab/git_access_result/success.rb
new file mode 100644
index 00000000000..7bb9f24cb0e
--- /dev/null
+++ b/lib/gitlab/git_access_result/success.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module GitAccessResult
+ class Success
+ end
+ end
+end