|
| 1 | +require_relative 'spec_helper' |
| 2 | + |
| 3 | +describe 'bin/gitlab-shell-authorized-keys-check' do |
| 4 | + def config_path |
| 5 | + File.join(ROOT_PATH, 'config.yml') |
| 6 | + end |
| 7 | + |
| 8 | + def tmp_config_path |
| 9 | + config_path + ".#{$$}" |
| 10 | + end |
| 11 | + |
| 12 | + def tmp_socket_path |
| 13 | + File.join(ROOT_PATH, 'tmp', 'gitlab-shell-authorized-keys-check-socket') |
| 14 | + end |
| 15 | + |
| 16 | + before(:all) do |
| 17 | + FileUtils.mkdir_p(File.dirname(tmp_socket_path)) |
| 18 | + FileUtils.touch(File.join(ROOT_PATH, '.gitlab_shell_secret')) |
| 19 | + |
| 20 | + @server = HTTPUNIXServer.new(BindAddress: tmp_socket_path) |
| 21 | + @server.mount_proc('/api/v4/internal/authorized_keys') do |req, res| |
| 22 | + if req.query['key'] == 'known-rsa-key' |
| 23 | + res.status = 200 |
| 24 | + res.content_type = 'application/json' |
| 25 | + res.body = '{"key":"known-rsa-key", "id": 1}' |
| 26 | + else |
| 27 | + res.status = 404 |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + @webrick_thread = Thread.new { @server.start } |
| 32 | + |
| 33 | + sleep(0.1) while @webrick_thread.alive? && @server.status != :Running |
| 34 | + raise "Couldn't start stub GitlabNet server" unless @server.status == :Running |
| 35 | + |
| 36 | + FileUtils.mv(config_path, tmp_config_path) if File.exist?(config_path) |
| 37 | + File.open(config_path, 'w') do |f| |
| 38 | + f.write("---\ngitlab_url: http+unix://#{CGI.escape(tmp_socket_path)}\n") |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + after(:all) do |
| 43 | + @server.shutdown if @server |
| 44 | + @webrick_thread.join if @webrick_thread |
| 45 | + FileUtils.rm_f(config_path) |
| 46 | + FileUtils.mv(tmp_config_path, config_path) if File.exist?(tmp_config_path) |
| 47 | + end |
| 48 | + |
| 49 | + let(:gitlab_shell_path) { File.join(ROOT_PATH, 'bin', 'gitlab-shell') } |
| 50 | + let(:authorized_keys_check_path) { File.join(ROOT_PATH, 'bin', 'gitlab-shell-authorized-keys-check') } |
| 51 | + |
| 52 | + it 'succeeds when a valid key is given' do |
| 53 | + output, status = run! |
| 54 | + |
| 55 | + expect(output).to eq("command=\"#{gitlab_shell_path} key-1\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty known-rsa-key\n") |
| 56 | + expect(status).to be_success |
| 57 | + end |
| 58 | + |
| 59 | + it 'returns nothing when an unknown key is given' do |
| 60 | + output, status = run!(key: 'unknown-key') |
| 61 | + |
| 62 | + expect(output).to eq("# No key was found for unknown-key\n") |
| 63 | + expect(status).to be_success |
| 64 | + end |
| 65 | + |
| 66 | + it' fails when not enough arguments are given' do |
| 67 | + output, status = run!(key: nil) |
| 68 | + |
| 69 | + expect(output).to eq('') |
| 70 | + expect(status).not_to be_success |
| 71 | + end |
| 72 | + |
| 73 | + it' fails when too many arguments are given' do |
| 74 | + output, status = run!(key: ['a', 'b']) |
| 75 | + |
| 76 | + expect(output).to eq('') |
| 77 | + expect(status).not_to be_success |
| 78 | + end |
| 79 | + |
| 80 | + it 'skips when run as the wrong user' do |
| 81 | + output, status = run!(expected_user: 'unknown-user') |
| 82 | + |
| 83 | + expect(output).to eq('') |
| 84 | + expect(status).to be_success |
| 85 | + end |
| 86 | + |
| 87 | + it 'skips when the wrong users connects' do |
| 88 | + output, status = run!(actual_user: 'unknown-user') |
| 89 | + |
| 90 | + expect(output).to eq('') |
| 91 | + expect(status).to be_success |
| 92 | + end |
| 93 | + |
| 94 | + def run!(expected_user: 'git', actual_user: 'git', key: 'known-rsa-key') |
| 95 | + cmd = [ |
| 96 | + authorized_keys_check_path, |
| 97 | + expected_user, |
| 98 | + actual_user, |
| 99 | + key |
| 100 | + ].flatten.compact |
| 101 | + |
| 102 | + output = IO.popen(cmd, &:read) |
| 103 | + |
| 104 | + [output, $?] |
| 105 | + end |
| 106 | +end |
0 commit comments