Commit d762f4ec authored by Bob Van Landuyt's avatar Bob Van Landuyt

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.
parent 7215126b
...@@ -41,17 +41,16 @@ func main() { ...@@ -41,17 +41,16 @@ func main() {
cmd, err := command.New(os.Args, config) cmd, err := command.New(os.Args, config)
if err != nil { if err != nil {
// Failed to build the command, fall back to ruby.
// For now this could happen if `SSH_CONNECTION` is not set on // For now this could happen if `SSH_CONNECTION` is not set on
// the environment // the environment
fmt.Fprintf(os.Stderr, "Failed to build command: %v\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
execRuby() os.Exit(1)
} }
// The command will write to STDOUT on execution or replace the current // The command will write to STDOUT on execution or replace the current
// process in case of the `fallback.Command` // process in case of the `fallback.Command`
if err = cmd.Execute(); err != nil { if err = cmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err) fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1) os.Exit(1)
} }
} }
...@@ -37,15 +37,15 @@ func Parse(arguments []string) (*CommandArgs, error) { ...@@ -37,15 +37,15 @@ func Parse(arguments []string) (*CommandArgs, error) {
return info, nil return info, nil
} }
func (info *CommandArgs) parseWho(arguments []string) { func (c *CommandArgs) parseWho(arguments []string) {
for _, argument := range arguments { for _, argument := range arguments {
if keyId := tryParseKeyId(argument); keyId != "" { if keyId := tryParseKeyId(argument); keyId != "" {
info.GitlabKeyId = keyId c.GitlabKeyId = keyId
break break
} }
if username := tryParseUsername(argument); username != "" { if username := tryParseUsername(argument); username != "" {
info.GitlabUsername = username c.GitlabUsername = username
break break
} }
} }
......
require_relative 'spec_helper' require_relative 'spec_helper'
require 'open3'
describe 'bin/gitlab-shell' do describe 'bin/gitlab-shell' do
def original_root_path def original_root_path
ROOT_PATH ROOT_PATH
...@@ -60,14 +62,14 @@ describe 'bin/gitlab-shell' do ...@@ -60,14 +62,14 @@ describe 'bin/gitlab-shell' do
shared_examples 'results with keys' do shared_examples 'results with keys' do
# Basic valid input # Basic valid input
it 'succeeds and prints username when a valid known key id is given' do 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(output).to eq("Welcome to GitLab, @someuser!\n")
expect(status).to be_success expect(status).to be_success
end end
it 'succeeds and prints username when a valid known username is given' do 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(output).to eq("Welcome to GitLab, @someuser!\n")
expect(status).to be_success expect(status).to be_success
...@@ -75,52 +77,51 @@ describe 'bin/gitlab-shell' do ...@@ -75,52 +77,51 @@ describe 'bin/gitlab-shell' do
# Valid but unknown input # Valid but unknown input
it 'succeeds and prints Anonymous when a valid unknown key id is given' do 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(output).to eq("Welcome to GitLab, Anonymous!\n")
expect(status).to be_success expect(status).to be_success
end end
it 'succeeds and prints Anonymous when a valid unknown username is given' do 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(output).to eq("Welcome to GitLab, Anonymous!\n")
expect(status).to be_success expect(status).to be_success
end end
# Invalid input. TODO: capture stderr & compare
it 'gets an ArgumentError on invalid input (empty)' do 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 expect(status).not_to be_success
end end
it 'gets an ArgumentError on invalid input (unknown)' do 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 expect(status).not_to be_success
end end
it 'gets an ArgumentError on invalid input (multiple unknown)' do 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 expect(status).not_to be_success
end end
# Not so basic valid input # Not so basic valid input
# (https://gitlab.com/gitlab-org/gitlab-shell/issues/145) # (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 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(output).to eq("Welcome to GitLab, @someuser!\n")
expect(status).to be_success expect(status).to be_success
end end
it 'succeeds and prints username when a valid known username is given in the middle of other input' do 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(output).to eq("Welcome to GitLab, @someuser!\n")
expect(status).to be_success expect(status).to be_success
...@@ -144,24 +145,27 @@ describe 'bin/gitlab-shell' do ...@@ -144,24 +145,27 @@ describe 'bin/gitlab-shell' do
) )
end end
it_behaves_like 'results with keys' do it_behaves_like 'results with keys' do
before do before do
pending pending
end end
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 end
def run!(args) def run!(args, env: {'SSH_CONNECTION' => 'fake'})
cmd = [ cmd = [
gitlab_shell_path, gitlab_shell_path,
args args
].flatten.compact ].flatten.compact.join(' ')
output = IO.popen({'SSH_CONNECTION' => 'fake'}, cmd, &:read)
[output, $?] Open3.capture3(env, cmd)
end end
def write_config(config) def write_config(config)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment