From ebedab837012f844a47645f86412d3ec69aa2b4b Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sat, 16 Feb 2019 07:04:15 -0800 Subject: Add support for using gl_project_path This will enable us to track the human-readable project path with SSH access in gitlab-shell. Currently the Gitaly logs will only show the hashed storage and gl_repository path (e.g. project-1234). --- lib/gitlab_access_status.rb | 7 +++++-- lib/gitlab_shell.rb | 4 +++- spec/gitlab_access_spec.rb | 2 ++ spec/gitlab_net_spec.rb | 4 ++++ spec/gitlab_shell_spec.rb | 5 +++++ spec/vcr_cassettes/allowed-pull.yml | 2 +- spec/vcr_cassettes/allowed-push.yml | 2 +- 7 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/gitlab_access_status.rb b/lib/gitlab_access_status.rb index dd6562e..dc577f4 100644 --- a/lib/gitlab_access_status.rb +++ b/lib/gitlab_access_status.rb @@ -3,15 +3,17 @@ require 'json' class GitAccessStatus HTTP_MULTIPLE_CHOICES = '300'.freeze - attr_reader :message, :gl_repository, :gl_id, :gl_username, :gitaly, :git_protocol, :git_config_options, :payload + attr_reader :message, :gl_repository, :gl_project_path, :gl_id, :gl_username, :gitaly, :git_protocol, :git_config_options, :payload - def initialize(status, status_code, message, gl_repository: nil, gl_id: nil, + def initialize(status, status_code, message, gl_repository: nil, + gl_project_path: nil, gl_id: nil, gl_username: nil, gitaly: nil, git_protocol: nil, git_config_options: nil, payload: nil) @status = status @status_code = status_code @message = message @gl_repository = gl_repository + @gl_project_path = gl_project_path @gl_id = gl_id @gl_username = gl_username @git_config_options = git_config_options @@ -26,6 +28,7 @@ class GitAccessStatus status_code, values["message"], gl_repository: values["gl_repository"], + gl_project_path: values["gl_project_path"], gl_id: values["gl_id"], gl_username: values["gl_username"], git_config_options: values["git_config_options"], diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb index c1bb976..2cb76a8 100644 --- a/lib/gitlab_shell.rb +++ b/lib/gitlab_shell.rb @@ -27,7 +27,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength TWO_FACTOR_RECOVERY_COMMAND = '2fa_recovery_codes' GL_PROTOCOL = 'ssh' - attr_accessor :gl_id, :gl_repository, :repo_name, :command, :git_access, :git_protocol + attr_accessor :gl_id, :gl_repository, :gl_project_path, :repo_name, :command, :git_access, :git_protocol def initialize(who) who_sym, = GitlabNet.parse_who(who) @@ -58,6 +58,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength @gl_repository = access_status.gl_repository @git_protocol = ENV['GIT_PROTOCOL'] + @gl_project_path = access_status.gl_project_path @gitaly = access_status.gitaly @username = access_status.gl_username @git_config_options = access_status.git_config_options @@ -167,6 +168,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength args = JSON.dump( 'repository' => @gitaly['repository'], 'gl_repository' => @gl_repository, + 'gl_project_path' => @gl_project_path, 'gl_id' => @gl_id, 'gl_username' => @username, 'git_config_options' => @git_config_options, diff --git a/spec/gitlab_access_spec.rb b/spec/gitlab_access_spec.rb index 92268e2..6c1d348 100644 --- a/spec/gitlab_access_spec.rb +++ b/spec/gitlab_access_spec.rb @@ -11,6 +11,7 @@ describe GitlabAccess do '200', 'ok', gl_repository: 'project-1', + gl_project_path: 'group/subgroup/project', gl_id: 'user-123', gl_username: 'testuser', git_config_options: ['receive.MaxInputSize=10000'], @@ -49,6 +50,7 @@ describe GitlabAccess do '401', 'denied', gl_repository: nil, + gl_project_path: nil, gl_id: nil, gl_username: nil, git_config_options: nil, diff --git a/spec/gitlab_net_spec.rb b/spec/gitlab_net_spec.rb index c6245d4..d7c46dc 100644 --- a/spec/gitlab_net_spec.rb +++ b/spec/gitlab_net_spec.rb @@ -287,7 +287,9 @@ describe GitlabNet, vcr: true do it 'should allow push access for host' do VCR.use_cassette("allowed-push") do access = gitlab_net.check_access('git-receive-pack', nil, project, key, changes, 'ssh') + expect(access.allowed?).to be_truthy + expect(access.gl_project_path).to eq('gitlab-org/gitlab.test') end end @@ -322,7 +324,9 @@ describe GitlabNet, vcr: true do it 'should allow pull access for host' do VCR.use_cassette("allowed-pull") do access = gitlab_net.check_access('git-upload-pack', nil, project, key, changes, 'ssh') + expect(access.allowed?).to be_truthy + expect(access.gl_project_path).to eq('gitlab-org/gitlab.test') end end end diff --git a/spec/gitlab_shell_spec.rb b/spec/gitlab_shell_spec.rb index eef572e..c261e6f 100644 --- a/spec/gitlab_shell_spec.rb +++ b/spec/gitlab_shell_spec.rb @@ -28,6 +28,7 @@ describe GitlabShell do '200', 'ok', gl_repository: gl_repository, + gl_project_path: gl_project_path, gl_id: gl_id, gl_username: gl_username, git_config_options: git_config_options, @@ -44,6 +45,7 @@ describe GitlabShell do '200', 'ok', gl_repository: gl_repository, + gl_project_path: gl_project_path, gl_id: gl_id, gl_username: gl_username, git_config_options: nil, @@ -62,6 +64,7 @@ describe GitlabShell do let(:repo_name) { 'gitlab-ci.git' } let(:gl_repository) { 'project-1' } + let(:gl_project_path) { 'group/subgroup/gitlab-ci' } let(:gl_id) { 'user-1' } let(:gl_username) { 'testuser' } let(:git_config_options) { ['receive.MaxInputSize=10000'] } @@ -169,6 +172,7 @@ describe GitlabShell do JSON.dump( 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default' }, 'gl_repository' => gl_repository, + 'gl_project_path' => gl_project_path, 'gl_id' => gl_id, 'gl_username' => gl_username, 'git_config_options' => git_config_options, @@ -446,6 +450,7 @@ describe GitlabShell do false, 'denied', gl_repository: nil, + gl_project_path: nil, gl_id: nil, gl_username: nil, git_config_options: nil, diff --git a/spec/vcr_cassettes/allowed-pull.yml b/spec/vcr_cassettes/allowed-pull.yml index b073476..d324316 100644 --- a/spec/vcr_cassettes/allowed-pull.yml +++ b/spec/vcr_cassettes/allowed-pull.yml @@ -40,7 +40,7 @@ http_interactions: - '0.230871' body: encoding: UTF-8 - string: '{"status":true,"gl_repository":"project-3","repository_path":"/Users/dzaporozhets/Projects/gitlab-development-kit/repositories/gitlab-org/gitlab-test.git"}' + string: '{"status":true,"gl_repository":"project-3","gl_project_path":"gitlab-org/gitlab.test","repository_path":"/Users/dzaporozhets/Projects/gitlab-development-kit/repositories/gitlab-org/gitlab-test.git"}' http_version: recorded_at: Wed, 21 Jun 2017 10:44:52 GMT recorded_with: VCR 2.4.0 diff --git a/spec/vcr_cassettes/allowed-push.yml b/spec/vcr_cassettes/allowed-push.yml index 1636fd5..025614a 100644 --- a/spec/vcr_cassettes/allowed-push.yml +++ b/spec/vcr_cassettes/allowed-push.yml @@ -40,7 +40,7 @@ http_interactions: - '0.289759' body: encoding: UTF-8 - string: '{"status":true,"gl_repository":"project-3","repository_path":"/Users/dzaporozhets/Projects/gitlab-development-kit/repositories/gitlab-org/gitlab-test.git"}' + string: '{"status":true,"gl_repository":"project-3","gl_project_path":"gitlab-org/gitlab.test","repository_path":"/Users/dzaporozhets/Projects/gitlab-development-kit/repositories/gitlab-org/gitlab-test.git"}' http_version: recorded_at: Wed, 21 Jun 2017 10:44:52 GMT recorded_with: VCR 2.4.0 -- cgit v1.2.1