Skip to content

Commit 2f92f12

Browse files
committed
pass gl_username through to hooks
1 parent bee2bcc commit 2f92f12

File tree

5 files changed

+77
-19
lines changed

5 files changed

+77
-19
lines changed

lib/gitlab_access_status.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
require 'json'
22

33
class GitAccessStatus
4-
attr_reader :message, :gl_repository, :repository_path, :gitaly, :geo_node
4+
attr_reader :message, :gl_repository, :gl_username, :repository_path, :gitaly, :geo_node
55

6-
def initialize(status, message, gl_repository, repository_path, gitaly, geo_node = false)
6+
def initialize(status, message, gl_repository:, gl_username:, repository_path:, gitaly:, geo_node:)
77
@status = status
88
@message = message
99
@gl_repository = gl_repository
10+
@gl_username = gl_username
1011
@repository_path = repository_path
1112
@gitaly = gitaly
1213
@geo_node = geo_node
@@ -16,10 +17,11 @@ def self.create_from_json(json)
1617
values = JSON.parse(json)
1718
self.new(values["status"],
1819
values["message"],
19-
values["gl_repository"],
20-
values["repository_path"],
21-
values["gitaly"],
22-
values["geo_node"])
20+
gl_repository: values["gl_repository"],
21+
gl_username: values["gl_username"],
22+
repository_path: values["repository_path"],
23+
gitaly: values["gitaly"],
24+
geo_node: values["geo_node"])
2325
end
2426

2527
def allowed?

lib/gitlab_net.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ def check_access(cmd, gl_repository, repo, actor, changes, protocol, env: {})
4040
if resp.code == '200'
4141
GitAccessStatus.create_from_json(resp.body)
4242
else
43-
GitAccessStatus.new(false, 'API is not accessible', nil, nil, nil)
43+
GitAccessStatus.new(false,
44+
'API is not accessible',
45+
gl_repository: nil,
46+
gl_username: nil,
47+
repository_path: nil,
48+
gitaly: nil,
49+
geo_node: false)
4450
end
4551
end
4652

lib/gitlab_shell.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class InvalidRepositoryPathError < StandardError; end
2020
# to undo an already set parameter: https://www.spinics.net/lists/git/msg256772.html
2121
GIT_CONFIG_SHOW_ALL_REFS = "transfer.hideRefs=!refs".freeze
2222

23-
attr_accessor :key_id, :gl_repository, :repo_name, :command, :git_access, :show_all_refs
23+
attr_accessor :key_id, :gl_repository, :repo_name, :command, :git_access, :show_all_refs, :username
2424
attr_reader :repo_path
2525

2626
def initialize(key_id)
@@ -113,6 +113,7 @@ def verify_access
113113
@gl_repository = status.gl_repository
114114
@gitaly = status.gitaly
115115
@show_all_refs = status.geo_node
116+
@username = status.gl_username
116117
end
117118

118119
def process_cmd(args)
@@ -139,7 +140,8 @@ def process_cmd(args)
139140
gitaly_request = {
140141
'repository' => @gitaly['repository'],
141142
'gl_repository' => @gl_repository,
142-
'gl_id' => @key_id
143+
'gl_id' => @key_id,
144+
'gl_username' => @username
143145
}
144146

145147
gitaly_request['git_config_options'] = [GIT_CONFIG_SHOW_ALL_REFS] if @show_all_refs
@@ -168,7 +170,8 @@ def exec_cmd(*args)
168170
'LANG' => ENV['LANG'],
169171
'GL_ID' => @key_id,
170172
'GL_PROTOCOL' => GL_PROTOCOL,
171-
'GL_REPOSITORY' => @gl_repository
173+
'GL_REPOSITORY' => @gl_repository,
174+
'GL_USERNAME' => @username
172175
}
173176
if @gitaly && @gitaly.include?('token')
174177
env['GITALY_TOKEN'] = @gitaly['token']

spec/gitlab_access_spec.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@
77
let(:repo_path) { File.join(repository_path, repo_name) + ".git" }
88
let(:api) do
99
double(GitlabNet).tap do |api|
10-
api.stub(check_access: GitAccessStatus.new(true, 'ok', 'project-1', '/home/git/repositories', nil))
10+
api.stub(check_access: GitAccessStatus.new(true,
11+
'ok',
12+
gl_repository: 'project-1',
13+
gl_username: 'testuser',
14+
repository_path: '/home/git/repositories',
15+
gitaly: nil,
16+
geo_node: nil))
1117
end
1218
end
1319
subject do
@@ -38,7 +44,15 @@
3844
context "access is denied" do
3945

4046
before do
41-
api.stub(check_access: GitAccessStatus.new(false, 'denied', nil, nil, nil))
47+
api.stub(check_access: GitAccessStatus.new(
48+
false,
49+
'denied',
50+
gl_repository: nil,
51+
gl_username: nil,
52+
repository_path: nil,
53+
gitaly: nil,
54+
geo_node: nil
55+
))
4256
end
4357

4458
it "returns false" do

spec/gitlab_shell_spec.rb

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,28 @@
1919
end
2020
end
2121

22-
let(:gitaly_check_access) { GitAccessStatus.new(true, 'ok', gl_repository, repo_path, { 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default'} , 'address' => 'unix:gitaly.socket' }) }
22+
let(:gitaly_check_access) { GitAccessStatus.new(
23+
true,
24+
'ok',
25+
gl_repository: gl_repository,
26+
gl_username: gl_username,
27+
repository_path: repo_path,
28+
gitaly: { 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default'} , 'address' => 'unix:gitaly.socket' },
29+
geo_node: false
30+
)
31+
}
2332

2433
let(:api) do
2534
double(GitlabNet).tap do |api|
2635
api.stub(discover: { 'name' => 'John Doe' })
27-
api.stub(check_access: GitAccessStatus.new(true, 'ok', gl_repository, repo_path, nil))
36+
api.stub(check_access: GitAccessStatus.new(
37+
true,
38+
'ok',
39+
gl_repository: gl_repository,
40+
gl_username: gl_username,
41+
repository_path: repo_path,
42+
gitaly: nil,
43+
geo_node: nil))
2844
api.stub(two_factor_recovery_codes: {
2945
'success' => true,
3046
'recovery_codes' => ['f67c514de60c4953', '41278385fc00c1e0']
@@ -39,6 +55,7 @@
3955
let(:repo_name) { 'gitlab-ci.git' }
4056
let(:repo_path) { File.join(tmp_repos_path, repo_name) }
4157
let(:gl_repository) { 'project-1' }
58+
let(:gl_username) { 'testuser' }
4259

4360
before do
4461
GitlabConfig.any_instance.stub(audit_usernames: false)
@@ -130,7 +147,7 @@
130147
end
131148

132149
describe :exec do
133-
let(:gitaly_message) { JSON.dump({ 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default' }, 'gl_repository' => gl_repository , 'gl_id' => key_id}) }
150+
let(:gitaly_message) { JSON.dump({ 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default' }, 'gl_repository' => gl_repository, 'gl_id' => key_id, 'gl_username' => gl_username}) }
134151

135152
shared_examples_for 'upload-pack' do |command|
136153
let(:ssh_cmd) { "#{command} gitlab-ci.git" }
@@ -167,8 +184,15 @@
167184

168185
context 'gitaly-upload-pack with GeoNode' do
169186
let(:ssh_cmd) { "git-upload-pack gitlab-ci.git" }
170-
let(:gitaly_check_access_with_geo) { GitAccessStatus.new(true, 'ok', gl_repository, repo_path, { 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default'} , 'address' => 'unix:gitaly.socket' }, true) }
171-
let(:gitaly_message_with_all_refs) { JSON.dump({ 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default' }, 'gl_repository' => gl_repository , 'gl_id' => key_id, 'git_config_options' => [GitlabShell::GIT_CONFIG_SHOW_ALL_REFS]}) }
187+
let(:gitaly_check_access_with_geo) { GitAccessStatus.new(
188+
true,
189+
'ok',
190+
gl_repository: gl_repository,
191+
gl_username: gl_username,
192+
repository_path: repo_path,
193+
gitaly: { 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default'} , 'address' => 'unix:gitaly.socket' },
194+
geo_node: true) }
195+
let(:gitaly_message_with_all_refs) { JSON.dump({ 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default' }, 'gl_repository' => gl_repository , 'gl_id' => key_id, 'gl_username' => gl_username, 'git_config_options' => [GitlabShell::GIT_CONFIG_SHOW_ALL_REFS]}) }
172196
before { api.stub(check_access: gitaly_check_access_with_geo) }
173197
after { subject.exec(ssh_cmd) }
174198

@@ -346,7 +370,14 @@
346370
end
347371

348372
it "should disallow access and log the attempt if check_access returns false status" do
349-
api.stub(check_access: GitAccessStatus.new(false, 'denied', nil, nil, nil))
373+
api.stub(check_access: GitAccessStatus.new(
374+
false,
375+
'denied',
376+
gl_repository: nil,
377+
gl_username: nil,
378+
repository_path: nil,
379+
gitaly: nil,
380+
geo_node: nil))
350381
message = "gitlab-shell: Access denied for git command <git-upload-pack gitlab-ci.git> "
351382
message << "by user with key #{key_id}."
352383
$logger.should_receive(:warn).with(message)
@@ -383,13 +414,15 @@
383414
'LANG' => ENV['LANG'],
384415
'GL_ID' => key_id,
385416
'GL_PROTOCOL' => 'ssh',
386-
'GL_REPOSITORY' => gl_repository
417+
'GL_REPOSITORY' => gl_repository,
418+
'GL_USERNAME' => 'testuser'
387419
}
388420
end
389421
let(:exec_options) { { unsetenv_others: true, chdir: ROOT_PATH } }
390422
before do
391423
Kernel.stub(:exec)
392424
shell.gl_repository = gl_repository
425+
shell.username = gl_username
393426
end
394427

395428
it "uses Kernel::exec method" do

0 commit comments

Comments
 (0)