Skip to content

Commit d762f4e

Browse files
committed
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.
1 parent 7215126 commit d762f4e

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

go/cmd/gitlab-shell/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,16 @@ func main() {
4141

4242
cmd, err := command.New(os.Args, config)
4343
if err != nil {
44-
// Failed to build the command, fall back to ruby.
4544
// For now this could happen if `SSH_CONNECTION` is not set on
4645
// the environment
47-
fmt.Fprintf(os.Stderr, "Failed to build command: %v\n", err)
48-
execRuby()
46+
fmt.Fprintf(os.Stderr, "%v\n", err)
47+
os.Exit(1)
4948
}
5049

5150
// The command will write to STDOUT on execution or replace the current
5251
// process in case of the `fallback.Command`
5352
if err = cmd.Execute(); err != nil {
54-
fmt.Fprintf(os.Stderr, "%s\n", err)
53+
fmt.Fprintf(os.Stderr, "%v\n", err)
5554
os.Exit(1)
5655
}
5756
}

go/internal/command/commandargs/command_args.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ func Parse(arguments []string) (*CommandArgs, error) {
3737
return info, nil
3838
}
3939

40-
func (info *CommandArgs) parseWho(arguments []string) {
40+
func (c *CommandArgs) parseWho(arguments []string) {
4141
for _, argument := range arguments {
4242
if keyId := tryParseKeyId(argument); keyId != "" {
43-
info.GitlabKeyId = keyId
43+
c.GitlabKeyId = keyId
4444
break
4545
}
4646

4747
if username := tryParseUsername(argument); username != "" {
48-
info.GitlabUsername = username
48+
c.GitlabUsername = username
4949
break
5050
}
5151
}

spec/gitlab_shell_gitlab_shell_spec.rb

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require_relative 'spec_helper'
22

3+
require 'open3'
4+
35
describe 'bin/gitlab-shell' do
46
def original_root_path
57
ROOT_PATH
@@ -60,67 +62,66 @@ def tmp_socket_path
6062
shared_examples 'results with keys' do
6163
# Basic valid input
6264
it 'succeeds and prints username when a valid known key id is given' do
63-
output, status = run!(["key-100"])
65+
output, _, status = run!(["key-100"])
6466

6567
expect(output).to eq("Welcome to GitLab, @someuser!\n")
6668
expect(status).to be_success
6769
end
6870

6971
it 'succeeds and prints username when a valid known username is given' do
70-
output, status = run!(["username-someuser"])
72+
output, _, status = run!(["username-someuser"])
7173

7274
expect(output).to eq("Welcome to GitLab, @someuser!\n")
7375
expect(status).to be_success
7476
end
7577

7678
# Valid but unknown input
7779
it 'succeeds and prints Anonymous when a valid unknown key id is given' do
78-
output, status = run!(["key-12345"])
80+
output, _, status = run!(["key-12345"])
7981

8082
expect(output).to eq("Welcome to GitLab, Anonymous!\n")
8183
expect(status).to be_success
8284
end
8385

8486
it 'succeeds and prints Anonymous when a valid unknown username is given' do
85-
output, status = run!(["username-unknown"])
87+
output, _, status = run!(["username-unknown"])
8688

8789
expect(output).to eq("Welcome to GitLab, Anonymous!\n")
8890
expect(status).to be_success
8991
end
9092

91-
# Invalid input. TODO: capture stderr & compare
9293
it 'gets an ArgumentError on invalid input (empty)' do
93-
output, status = run!([])
94+
_, stderr, status = run!([])
9495

95-
expect(output).to eq("")
96+
expect(stderr).to match(/who='' is invalid/)
9697
expect(status).not_to be_success
9798
end
9899

99100
it 'gets an ArgumentError on invalid input (unknown)' do
100-
output, status = run!(["whatever"])
101+
_, stderr, status = run!(["whatever"])
101102

102-
expect(output).to eq("")
103+
expect(stderr).to match(/who='' is invalid/)
103104
expect(status).not_to be_success
104105
end
105106

106107
it 'gets an ArgumentError on invalid input (multiple unknown)' do
107-
output, status = run!(["this", "is", "all", "invalid"])
108+
_, stderr, status = run!(["this", "is", "all", "invalid"])
108109

109-
expect(output).to eq("")
110+
expect(stderr).to match(/who='' is invalid/)
110111
expect(status).not_to be_success
111112
end
112113

113114
# Not so basic valid input
114115
# (https://gitlab.com/gitlab-org/gitlab-shell/issues/145)
115116
it 'succeeds and prints username when a valid known key id is given in the middle of other input' do
116-
output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "key-100", "2foo"])
117+
output, _, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "key-100", "2foo"])
117118

118119
expect(output).to eq("Welcome to GitLab, @someuser!\n")
119120
expect(status).to be_success
120121
end
121122

122123
it 'succeeds and prints username when a valid known username is given in the middle of other input' do
123-
output, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser" ,"foo"])
124+
output, _, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser" ,"foo"])
124125

125126
expect(output).to eq("Welcome to GitLab, @someuser!\n")
126127
expect(status).to be_success
@@ -144,24 +145,27 @@ def tmp_socket_path
144145
)
145146
end
146147

147-
148-
149148
it_behaves_like 'results with keys' do
150149
before do
151150
pending
152151
end
153152
end
153+
154+
it 'outputs "Only ssh allowed"' do
155+
_, stderr, status = run!(["-c/usr/share/webapps/gitlab-shell/bin/gitlab-shell", "username-someuser"], env: {})
156+
157+
expect(stderr).to eq("Only ssh allowed\n")
158+
expect(status).not_to be_success
159+
end
154160
end
155161

156-
def run!(args)
162+
def run!(args, env: {'SSH_CONNECTION' => 'fake'})
157163
cmd = [
158164
gitlab_shell_path,
159165
args
160-
].flatten.compact
161-
162-
output = IO.popen({'SSH_CONNECTION' => 'fake'}, cmd, &:read)
166+
].flatten.compact.join(' ')
163167

164-
[output, $?]
168+
Open3.capture3(env, cmd)
165169
end
166170

167171
def write_config(config)

0 commit comments

Comments
 (0)