From 7215126b6674abd4b5ff6b97d30bab6c544bf8df Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Fri, 21 Dec 2018 18:05:18 +0100 Subject: Allow enabling gitlab-shell "discover"-feature This adds the possibility to enable features for GitLab shell. The first feature being recognized is "Discover": It's the command that is executed when running `ssh git@gitlab.example.com` and is called without a command. The gitlab key id or username is already parsed from the command line arguments. Currently we only support communicating with GitLab-rails using unix sockets. So features will not be enabled if the GitLab-url is using a different protocol. The url for this read from the config yaml. Pending ruby-specs have been added for the gitlab-shell command. Refactor to have separate command packages --- spec/gitlab_shell_gitlab_shell_spec.rb | 134 ++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 52 deletions(-) (limited to 'spec') diff --git a/spec/gitlab_shell_gitlab_shell_spec.rb b/spec/gitlab_shell_gitlab_shell_spec.rb index 9afeac8..271ac9d 100644 --- a/spec/gitlab_shell_gitlab_shell_spec.rb +++ b/spec/gitlab_shell_gitlab_shell_spec.rb @@ -43,11 +43,7 @@ describe 'bin/gitlab-shell' do sleep(0.1) while @webrick_thread.alive? && @server.status != :Running raise "Couldn't start stub GitlabNet server" unless @server.status == :Running - - File.open(config_path, 'w') do |f| - f.write("---\ngitlab_url: http+unix://#{CGI.escape(tmp_socket_path)}\n") - end - + system(original_root_path, 'bin/compile') copy_dirs = ['bin', 'lib'] FileUtils.rm_rf(copy_dirs.map { |d| File.join(tmp_root_path, d) }) FileUtils.cp_r(copy_dirs, tmp_root_path) @@ -61,72 +57,100 @@ describe 'bin/gitlab-shell' do let(:gitlab_shell_path) { File.join(tmp_root_path, 'bin', 'gitlab-shell') } - # Basic valid input - it 'succeeds and prints username when a valid known key id is given' do - output, status = run!(["key-100"]) + shared_examples 'results with keys' do + # Basic valid input + it 'succeeds and prints username when a valid known key id is given' do + output, status = run!(["key-100"]) - expect(output).to eq("Welcome to GitLab, @someuser!\n") - expect(status).to be_success - end + expect(output).to eq("Welcome to GitLab, @someuser!\n") + expect(status).to be_success + end - it 'succeeds and prints username when a valid known username is given' do - output, status = run!(["username-someuser"]) + it 'succeeds and prints username when a valid known username is given' do + output, status = run!(["username-someuser"]) - expect(output).to eq("Welcome to GitLab, @someuser!\n") - expect(status).to be_success - end + expect(output).to eq("Welcome to GitLab, @someuser!\n") + expect(status).to be_success + end - # Valid but unknown input - it 'succeeds and prints Anonymous when a valid unknown key id is given' do - output, status = run!(["key-12345"]) + # Valid but unknown input + it 'succeeds and prints Anonymous when a valid unknown key id is given' do + output, status = run!(["key-12345"]) - expect(output).to eq("Welcome to GitLab, Anonymous!\n") - expect(status).to be_success - end + expect(output).to eq("Welcome to GitLab, Anonymous!\n") + expect(status).to be_success + end - it 'succeeds and prints Anonymous when a valid unknown username is given' do - output, status = run!(["username-unknown"]) + it 'succeeds and prints Anonymous when a valid unknown username is given' do + output, status = run!(["username-unknown"]) - expect(output).to eq("Welcome to GitLab, Anonymous!\n") - expect(status).to be_success - end + expect(output).to eq("Welcome to GitLab, Anonymous!\n") + expect(status).to be_success + end - # Invalid input. TODO: capture stderr & compare - it 'gets an ArgumentError on invalid input (empty)' do - output, status = run!([]) + # Invalid input. TODO: capture stderr & compare + it 'gets an ArgumentError on invalid input (empty)' do + output, status = run!([]) - expect(output).to eq("") - expect(status).not_to be_success - end + expect(output).to eq("") + expect(status).not_to be_success + end - it 'gets an ArgumentError on invalid input (unknown)' do - output, status = run!(["whatever"]) + it 'gets an ArgumentError on invalid input (unknown)' do + output, status = run!(["whatever"]) - expect(output).to eq("") - expect(status).not_to be_success - end + expect(output).to eq("") + expect(status).not_to be_success + end - it 'gets an ArgumentError on invalid input (multiple unknown)' do - output, status = run!(["this", "is", "all", "invalid"]) + it 'gets an ArgumentError on invalid input (multiple unknown)' do + output, status = run!(["this", "is", "all", "invalid"]) - expect(output).to eq("") - expect(status).not_to be_success + expect(output).to eq("") + expect(status).not_to be_success + end + + # Not so basic valid input + # (https://gitlab.com/gitlab-org/gitlab-shell/issues/145) + it 'succeeds and prints username when a valid known key id is given in the middle of other input' do + output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "key-100", "2foo"]) + + expect(output).to eq("Welcome to GitLab, @someuser!\n") + expect(status).to be_success + end + + it 'succeeds and prints username when a valid known username is given in the middle of other input' do + output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser" ,"foo"]) + + expect(output).to eq("Welcome to GitLab, @someuser!\n") + expect(status).to be_success + end end - # Not so basic valid input - # (https://gitlab.com/gitlab-org/gitlab-shell/issues/145) - it 'succeeds and prints username when a valid known key id is given in the middle of other input' do - output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "key-100", "2foo"]) + describe 'without go features' do + before(:context) do + write_config("gitlab_url" => "http+unix://#{CGI.escape(tmp_socket_path)}") + end - expect(output).to eq("Welcome to GitLab, @someuser!\n") - expect(status).to be_success + it_behaves_like 'results with keys' end - it 'succeeds and prints username when a valid known username is given in the middle of other input' do - output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser" ,"foo"]) + describe 'with the go discover feature', :go do + before(:context) do + write_config( + "gitlab_url" => "http+unix://#{CGI.escape(tmp_socket_path)}", + "migration" => { "enabled" => true, + "features" => ["discover"] } + ) + end + + - expect(output).to eq("Welcome to GitLab, @someuser!\n") - expect(status).to be_success + it_behaves_like 'results with keys' do + before do + pending + end + end end def run!(args) @@ -139,4 +163,10 @@ describe 'bin/gitlab-shell' do [output, $?] end + + def write_config(config) + File.open(config_path, 'w') do |f| + f.write(config.to_yaml) + end + end end -- cgit v1.2.1 From d762f4ec9ea35cb00309b41ad60055cd3c5709ba Mon Sep 17 00:00:00 2001 From: Bob Van Landuyt Date: Tue, 15 Jan 2019 22:34:38 +0100 Subject: Don't fall back to ruby for non SSH connections When SSH_CONNECTION is not set, we don't fall back to ruby, but instead fail directly in go writing the error to stderr. --- spec/gitlab_shell_gitlab_shell_spec.rb | 44 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'spec') diff --git a/spec/gitlab_shell_gitlab_shell_spec.rb b/spec/gitlab_shell_gitlab_shell_spec.rb index 271ac9d..11692d3 100644 --- a/spec/gitlab_shell_gitlab_shell_spec.rb +++ b/spec/gitlab_shell_gitlab_shell_spec.rb @@ -1,5 +1,7 @@ require_relative 'spec_helper' +require 'open3' + describe 'bin/gitlab-shell' do def original_root_path ROOT_PATH @@ -60,14 +62,14 @@ describe 'bin/gitlab-shell' do shared_examples 'results with keys' do # Basic valid input it 'succeeds and prints username when a valid known key id is given' do - output, status = run!(["key-100"]) + output, _, status = run!(["key-100"]) expect(output).to eq("Welcome to GitLab, @someuser!\n") expect(status).to be_success end it 'succeeds and prints username when a valid known username is given' do - output, status = run!(["username-someuser"]) + output, _, status = run!(["username-someuser"]) expect(output).to eq("Welcome to GitLab, @someuser!\n") expect(status).to be_success @@ -75,52 +77,51 @@ describe 'bin/gitlab-shell' do # Valid but unknown input it 'succeeds and prints Anonymous when a valid unknown key id is given' do - output, status = run!(["key-12345"]) + output, _, status = run!(["key-12345"]) expect(output).to eq("Welcome to GitLab, Anonymous!\n") expect(status).to be_success end it 'succeeds and prints Anonymous when a valid unknown username is given' do - output, status = run!(["username-unknown"]) + output, _, status = run!(["username-unknown"]) expect(output).to eq("Welcome to GitLab, Anonymous!\n") expect(status).to be_success end - # Invalid input. TODO: capture stderr & compare it 'gets an ArgumentError on invalid input (empty)' do - output, status = run!([]) + _, stderr, status = run!([]) - expect(output).to eq("") + expect(stderr).to match(/who='' is invalid/) expect(status).not_to be_success end it 'gets an ArgumentError on invalid input (unknown)' do - output, status = run!(["whatever"]) + _, stderr, status = run!(["whatever"]) - expect(output).to eq("") + expect(stderr).to match(/who='' is invalid/) expect(status).not_to be_success end it 'gets an ArgumentError on invalid input (multiple unknown)' do - output, status = run!(["this", "is", "all", "invalid"]) + _, stderr, status = run!(["this", "is", "all", "invalid"]) - expect(output).to eq("") + expect(stderr).to match(/who='' is invalid/) expect(status).not_to be_success end # Not so basic valid input # (https://gitlab.com/gitlab-org/gitlab-shell/issues/145) it 'succeeds and prints username when a valid known key id is given in the middle of other input' do - output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "key-100", "2foo"]) + output, _, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "key-100", "2foo"]) expect(output).to eq("Welcome to GitLab, @someuser!\n") expect(status).to be_success end it 'succeeds and prints username when a valid known username is given in the middle of other input' do - output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser" ,"foo"]) + output, _, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser" ,"foo"]) expect(output).to eq("Welcome to GitLab, @someuser!\n") expect(status).to be_success @@ -144,24 +145,27 @@ describe 'bin/gitlab-shell' do ) end - - it_behaves_like 'results with keys' do before do pending end end + + it 'outputs "Only ssh allowed"' do + _, stderr, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser"], env: {}) + + expect(stderr).to eq("Only ssh allowed\n") + expect(status).not_to be_success + end end - def run!(args) + def run!(args, env: {'SSH_CONNECTION' => 'fake'}) cmd = [ gitlab_shell_path, args - ].flatten.compact - - output = IO.popen({'SSH_CONNECTION' => 'fake'}, cmd, &:read) + ].flatten.compact.join(' ') - [output, $?] + Open3.capture3(env, cmd) end def write_config(config) -- cgit v1.2.1