summaryrefslogtreecommitdiff
path: root/spec/gitlab_shell_spec.rb
blob: eef0cafcfa92ae90f9533581f83107a18c986650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
require_relative 'spec_helper'
require_relative '../lib/gitlab_shell'
require_relative '../lib/gitlab_access_status'

describe GitlabShell do
  before do
    FileUtils.mkdir_p(tmp_repos_path)
  end

  after do
    FileUtils.rm_rf(tmp_repos_path)
  end

  subject do
    ARGV[0] = key_id
    GitlabShell.new(key_id).tap do |shell|
      shell.stub(exec_cmd: :exec_called)
      shell.stub(api: api)
    end
  end

  let(:gitaly_check_access) { GitAccessStatus.new(
    true,
    'ok',
    gl_repository: gl_repository,
    gl_username: gl_username,
    repository_path: repo_path,
    gitaly: { 'repository' => { 'relative_path' => repo_name, 'storage_name' => 'default'} , 'address' => 'unix:gitaly.socket' }
  )
  }

  let(:api) do
    double(GitlabNet).tap do |api|
      api.stub(discover: { 'name' => 'John Doe', 'username' => 'testuser' })
      api.stub(check_access: GitAccessStatus.new(
                true,
                'ok',
                gl_repository: gl_repository,
                gl_username: gl_username,
                repository_path: repo_path,
                gitaly: nil))
      api.stub(two_factor_recovery_codes: {
        'success' => true,
        'recovery_codes' => ['f67c514de60c4953', '41278385fc00c1e0']
      })
    end
  end

  let(:key_id) { "key-#{rand(100) + 100}" }
  let(:ssh_cmd) { nil }
  let(:tmp_repos_path) { File.join(ROOT_PATH, 'tmp', 'repositories') }

  let(:repo_name) { 'gitlab-ci.git' }
  let(:repo_path) { File.join(tmp_repos_path, repo_name) }
  let(:gl_repository) { 'project-1' }
  let(:gl_username) { 'testuser' }

  before do
    GitlabConfig.any_instance.stub(audit_usernames: false)
  end

  describe :initialize do
    let(:ssh_cmd) { 'git-receive-pack' }

    its(:key_id) { should == key_id }
  end

  describe :parse_cmd do
    describe 'git' do
      context 'w/o namespace' do
        let(:ssh_args) { %W(git-upload-pack gitlab-ci.git) }

        before do
          subject.send :parse_cmd, ssh_args
        end

        its(:repo_name) { should == 'gitlab-ci.git' }
        its(:command) { should == 'git-upload-pack' }
      end

      context 'namespace' do
        let(:repo_name) { 'dmitriy.zaporozhets/gitlab-ci.git' }
        let(:ssh_args) { %W(git-upload-pack dmitriy.zaporozhets/gitlab-ci.git) }

        before do
          subject.send :parse_cmd, ssh_args
        end

        its(:repo_name) { should == 'dmitriy.zaporozhets/gitlab-ci.git' }
        its(:command) { should == 'git-upload-pack' }
      end

      context 'with an invalid number of arguments' do
        let(:ssh_args) { %W(foobar) }

        it "should raise an DisallowedCommandError" do
          expect { subject.send :parse_cmd, ssh_args }.to raise_error(GitlabShell::DisallowedCommandError)
        end
      end

      context 'with an API command' do
        before do
          subject.send :parse_cmd, ssh_args
        end

        context 'when generating recovery codes' do
          let(:ssh_args) { %w(2fa_recovery_codes) }

          it 'sets the correct command' do
            expect(subject.command).to eq('2fa_recovery_codes')
          end

          it 'does not set repo name' do
            expect(subject.repo_name).to be_nil
          end
        end
      end
    end

    describe 'git-lfs' do
      let(:repo_name) { 'dzaporozhets/gitlab.git' }
      let(:ssh_args) { %W(git-lfs-authenticate dzaporozhets/gitlab.git download) }

      before do
        subject.send :parse_cmd, ssh_args
      end

      its(:repo_name) { should == 'dzaporozhets/gitlab.git' }
      its(:command) { should == 'git-lfs-authenticate' }
      its(:git_access) { should == 'git-upload-pack' }
    end

    describe 'git-lfs old clients' do
      let(:repo_name) { 'dzaporozhets/gitlab.git' }
      let(:ssh_args) { %W(git-lfs-authenticate dzaporozhets/gitlab.git download long_oid) }

      before do
        subject.send :parse_cmd, ssh_args
      end

      its(:repo_name) { should == 'dzaporozhets/gitlab.git' }
      its(:command) { should == 'git-lfs-authenticate' }
      its(:git_access) { should == 'git-upload-pack' }
    end
  end

  describe :exec do
    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}) }

    shared_examples_for 'upload-pack' do |command|
      let(:ssh_cmd) { "#{command} gitlab-ci.git" }
      after { subject.exec(ssh_cmd) }

      it "should process the command" do
        subject.should_receive(:process_cmd).with(%W(git-upload-pack gitlab-ci.git))
      end

      it "should execute the command" do
        subject.should_receive(:exec_cmd).with('git-upload-pack', repo_path)
      end

      it "should log the command execution" do
        message = "executing git command"
        user_string = "user with key #{key_id}"
        $logger.should_receive(:info).with(message, command: "git-upload-pack #{repo_path}", user: user_string)
      end

      it "should use usernames if configured to do so" do
        GitlabConfig.any_instance.stub(audit_usernames: true)
        $logger.should_receive(:info).with("executing git command", hash_including(user: 'testuser'))
      end
    end

    context 'git-upload-pack' do
      it_behaves_like 'upload-pack', 'git-upload-pack'
    end

    context 'git upload-pack' do
      it_behaves_like 'upload-pack', 'git upload-pack'
    end

    context 'gitaly-upload-pack' do
      let(:ssh_cmd) { "git-upload-pack gitlab-ci.git" }
      before {
        api.stub(check_access: gitaly_check_access)
      }
      after { subject.exec(ssh_cmd) }

      it "should process the command" do
        subject.should_receive(:process_cmd).with(%W(git-upload-pack gitlab-ci.git))
      end

      it "should execute the command" do
        subject.should_receive(:exec_cmd).with(File.join(ROOT_PATH, "bin/gitaly-upload-pack"), 'unix:gitaly.socket', gitaly_message)
      end

      it "should log the command execution" do
        message = "executing git command"
        user_string = "user with key #{key_id}"
        $logger.should_receive(:info).with(message, command: "gitaly-upload-pack unix:gitaly.socket #{gitaly_message}", user: user_string)
      end

      it "should use usernames if configured to do so" do
        GitlabConfig.any_instance.stub(audit_usernames: true)
        $logger.should_receive(:info).with("executing git command", hash_including(user: 'testuser'))
      end
    end

    context 'git-receive-pack' do
      let(:ssh_cmd) { "git-receive-pack gitlab-ci.git" }
      after { subject.exec(ssh_cmd) }

      it "should process the command" do
        subject.should_receive(:process_cmd).with(%W(git-receive-pack gitlab-ci.git))
      end

      it "should execute the command" do
        subject.should_receive(:exec_cmd).with('git-receive-pack', repo_path)
      end

      it "should log the command execution" do
        message = "executing git command"
        user_string = "user with key #{key_id}"
        $logger.should_receive(:info).with(message, command: "git-receive-pack #{repo_path}", user: user_string)
      end
    end

    context 'gitaly-receive-pack' do
      let(:ssh_cmd) { "git-receive-pack gitlab-ci.git" }
      before {
        api.stub(check_access: gitaly_check_access)
      }
      after { subject.exec(ssh_cmd) }

      it "should process the command" do
        subject.should_receive(:process_cmd).with(%W(git-receive-pack gitlab-ci.git))
      end

      it "should execute the command" do
        subject.should_receive(:exec_cmd).with(File.join(ROOT_PATH, "bin/gitaly-receive-pack"), 'unix:gitaly.socket', gitaly_message)
      end

      it "should log the command execution" do
        message = "executing git command"
        user_string = "user with key #{key_id}"
        $logger.should_receive(:info).with(message, command: "gitaly-receive-pack unix:gitaly.socket #{gitaly_message}", user: user_string)
      end

      it "should use usernames if configured to do so" do
        GitlabConfig.any_instance.stub(audit_usernames: true)
        $logger.should_receive(:info).with("executing git command", hash_including(user: 'testuser'))
      end
    end

    shared_examples_for 'upload-archive' do |command|
      let(:ssh_cmd) { "#{command} gitlab-ci.git" }
      let(:exec_cmd_params) { ['git-upload-archive', repo_path] }
      let(:exec_cmd_log_params) { exec_cmd_params }

      after { subject.exec(ssh_cmd) }

      it "should process the command" do
        subject.should_receive(:process_cmd).with(%W(git-upload-archive gitlab-ci.git))
      end

      it "should execute the command" do
        subject.should_receive(:exec_cmd).with(*exec_cmd_params)
      end

      it "should log the command execution" do
        message = "executing git command"
        user_string = "user with key #{key_id}"
        $logger.should_receive(:info).with(message, command: exec_cmd_log_params.join(' '), user: user_string)
      end

      it "should use usernames if configured to do so" do
        GitlabConfig.any_instance.stub(audit_usernames: true)
        $logger.should_receive(:info).with("executing git command", hash_including(user: 'testuser'))
      end
    end

    context 'git-upload-archive' do
      it_behaves_like 'upload-archive', 'git-upload-archive'
    end

    context 'git upload-archive' do
      it_behaves_like 'upload-archive', 'git upload-archive'
    end

    context 'gitaly-upload-archive' do
      before do
        api.stub(check_access: gitaly_check_access)
      end

      it_behaves_like 'upload-archive', 'git-upload-archive' do
        let(:gitaly_executable) { "gitaly-upload-archive" }
        let(:exec_cmd_params) do
          [
            File.join(ROOT_PATH, "bin", gitaly_executable),
            'unix:gitaly.socket',
            gitaly_message
          ]
        end
        let(:exec_cmd_log_params) do
          [gitaly_executable, 'unix:gitaly.socket', gitaly_message]
        end
      end
    end

    context 'arbitrary command' do
      let(:ssh_cmd) { 'arbitrary command' }
      after { subject.exec(ssh_cmd) }

      it "should not process the command" do
        subject.should_not_receive(:process_cmd)
      end

      it "should not execute the command" do
        subject.should_not_receive(:exec_cmd)
      end

      it "should log the attempt" do
        message = 'Denied disallowed command'
        user_string = "user with key #{key_id}"
        $logger.should_receive(:warn).with(message, command: 'arbitrary command', user: user_string)
      end
    end

    context 'no command' do
      after { subject.exec(nil) }

      it "should call api.discover" do
        api.should_receive(:discover).with(key_id)
      end
    end

    context "failed connection" do
      let(:ssh_cmd) { 'git-upload-pack gitlab-ci.git' }

      before {
        api.stub(:check_access).and_raise(GitlabNet::ApiUnreachableError)
      }
      after { subject.exec(ssh_cmd) }

      it "should not process the command" do
        subject.should_not_receive(:process_cmd)
      end

      it "should not execute the command" do
        subject.should_not_receive(:exec_cmd)
      end
    end

    context 'with an API command' do
      before do
        allow(subject).to receive(:continue?).and_return(true)
      end

      context 'when generating recovery codes' do
        let(:ssh_cmd) { '2fa_recovery_codes' }
        after do
          subject.exec(ssh_cmd)
        end

        it 'does not call verify_access' do
          expect(subject).not_to receive(:verify_access)
        end

        it 'calls the corresponding method' do
          expect(subject).to receive(:api_2fa_recovery_codes)
        end

        it 'outputs recovery codes' do
          expect($stdout).to receive(:puts)
            .with(/f67c514de60c4953\n41278385fc00c1e0/)
        end

        context 'when the process is unsuccessful' do
          it 'displays the error to the user' do
            api.stub(two_factor_recovery_codes: {
              'success' => false,
              'message' => 'Could not find the given key'
            })

            expect($stdout).to receive(:puts)
              .with(/Could not find the given key/)
          end
        end
      end
    end
  end

  describe :validate_access do
    let(:ssh_cmd) { "git-upload-pack gitlab-ci.git" }

    describe 'check access with api' do
      after { subject.exec(ssh_cmd) }

      it "should call api.check_access" do
        api.should_receive(:check_access).with('git-upload-pack', nil, 'gitlab-ci.git', key_id, '_any', 'ssh')
      end

      it "should disallow access and log the attempt if check_access returns false status" do
        api.stub(check_access: GitAccessStatus.new(
                  false,
                  'denied',
                  gl_repository: nil,
                  gl_username: nil,
                  repository_path: nil,
                  gitaly: nil))
        message = 'Access denied'
        user_string = "user with key #{key_id}"
        $logger.should_receive(:warn).with(message, command: 'git-upload-pack gitlab-ci.git', user: user_string)
      end
    end

    describe 'set the repository path' do
      context 'with a correct path' do
        before { subject.exec(ssh_cmd) }

        its(:repo_path) { should == repo_path }
      end

      context "with a path that doesn't match an absolute path" do
        before do
          File.stub(:absolute_path) { 'y/gitlab-ci.git' }
        end

        it "refuses to assign the path" do
          $stderr.should_receive(:puts).with("GitLab: Invalid repository path")
          expect(subject.exec(ssh_cmd)).to be_false
        end
      end
    end
  end

  describe :exec_cmd do
    let(:shell) { GitlabShell.new(key_id) }
    let(:env) do
      {
        'HOME' => ENV['HOME'],
        'PATH' => ENV['PATH'],
        'LD_LIBRARY_PATH' => ENV['LD_LIBRARY_PATH'],
        'LANG' => ENV['LANG'],
        'GL_ID' => key_id,
        'GL_PROTOCOL' => 'ssh',
        'GL_REPOSITORY' => gl_repository,
        'GL_USERNAME' => 'testuser'
      }
    end
    let(:exec_options) { { unsetenv_others: true, chdir: ROOT_PATH } }
    before do
      Kernel.stub(:exec)
      shell.gl_repository = gl_repository
      shell.instance_variable_set(:@username, gl_username)
    end

    it "uses Kernel::exec method" do
      Kernel.should_receive(:exec).with(env, 1, 2, exec_options).once
      shell.send :exec_cmd, 1, 2
    end

    it "refuses to execute a lone non-array argument" do
      expect { shell.send :exec_cmd, 1 }.to raise_error(GitlabShell::DisallowedCommandError)
    end

    it "allows one argument if it is an array" do
      Kernel.should_receive(:exec).with(env, [1, 2], exec_options).once
      shell.send :exec_cmd, [1, 2]
    end

    context "when specifying a git_tracing log file" do
      let(:git_trace_log_file) { '/tmp/git_trace_performance.log' }

      before do
        GitlabConfig.any_instance.stub(git_trace_log_file: git_trace_log_file)
        shell
      end

      it "uses GIT_TRACE_PERFORMANCE" do
        expected_hash = hash_including(
          'GIT_TRACE' => git_trace_log_file,
          'GIT_TRACE_PACKET' => git_trace_log_file,
          'GIT_TRACE_PERFORMANCE' => git_trace_log_file
        )
        Kernel.should_receive(:exec).with(expected_hash, [1, 2], exec_options).once

        shell.send :exec_cmd, [1, 2]
      end

      context "when provides a relative path" do
        let(:git_trace_log_file) { 'git_trace_performance.log' }

        it "does not uses GIT_TRACE*" do
          # If we try to use it we'll show a warning to the users
          expected_hash = hash_excluding(
            'GIT_TRACE', 'GIT_TRACE_PACKET', 'GIT_TRACE_PERFORMANCE'
          )
          Kernel.should_receive(:exec).with(expected_hash, [1, 2], exec_options).once

          shell.send :exec_cmd, [1, 2]
        end

        it "writes an entry on the log" do
          message = 'git trace log path must be absolute, ignoring'

          expect($logger).to receive(:warn).
            with(message, git_trace_log_file: git_trace_log_file)

          Kernel.should_receive(:exec).with(env, [1, 2], exec_options).once
          shell.send :exec_cmd, [1, 2]
        end
      end

      context "when provides a file not writable" do
        before do
          expect(File).to receive(:open).with(git_trace_log_file, 'a').and_raise(Errno::EACCES)
        end

        it "does not uses GIT_TRACE*" do
          # If we try to use it we'll show a warning to the users
          expected_hash = hash_excluding(
            'GIT_TRACE', 'GIT_TRACE_PACKET', 'GIT_TRACE_PERFORMANCE'
          )
          Kernel.should_receive(:exec).with(expected_hash, [1, 2], exec_options).once

          shell.send :exec_cmd, [1, 2]
        end

        it "writes an entry on the log" do
          message = 'Failed to open git trace log file'
          error = 'Permission denied'

          expect($logger).to receive(:warn).
            with(message, git_trace_log_file: git_trace_log_file, error: error)

          Kernel.should_receive(:exec).with(env, [1, 2], exec_options).once
          shell.send :exec_cmd, [1, 2]
        end
      end
    end
  end

  describe :api do
    let(:shell) { GitlabShell.new(key_id) }
    subject { shell.send :api }

    it { should be_a(GitlabNet) }
  end
end