summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorIgor Drozdov <idrozdov@gitlab.com>2021-09-20 08:19:42 +0000
committerIgor Drozdov <idrozdov@gitlab.com>2021-09-20 08:19:42 +0000
commit52e676c87f931a6d847866ef3018e262459bf11d (patch)
treed0d579de49e3ba34934e24cc3dac4b985bf5f5b1 /internal
parent1c17b914b9a2c6a01c3cd458a26e791fd0f8a52d (diff)
parent65dadb7e51e206b6411a4518f8a26471d586bc6f (diff)
downloadgitlab-shell-52e676c87f931a6d847866ef3018e262459bf11d.tar.gz
Merge branch 'refactor/unify-shell' into 'main'
refactor: unify instantiation of command.Shell Closes #517 See merge request gitlab-org/gitlab-shell!520
Diffstat (limited to 'internal')
-rw-r--r--internal/sshd/session.go31
-rw-r--r--internal/sshd/session_test.go2
2 files changed, 15 insertions, 18 deletions
diff --git a/internal/sshd/session.go b/internal/sshd/session.go
index b58598e..d5a0174 100644
--- a/internal/sshd/session.go
+++ b/internal/sshd/session.go
@@ -2,13 +2,14 @@ package sshd
import (
"context"
+ "errors"
"fmt"
"golang.org/x/crypto/ssh"
shellCmd "gitlab.com/gitlab-org/gitlab-shell/cmd/gitlab-shell/command"
- "gitlab.com/gitlab-org/gitlab-shell/internal/command/commandargs"
"gitlab.com/gitlab-org/gitlab-shell/internal/command/readwriter"
+ "gitlab.com/gitlab-org/gitlab-shell/internal/command/shared/disallowedcommand"
"gitlab.com/gitlab-org/gitlab-shell/internal/config"
"gitlab.com/gitlab-org/gitlab-shell/internal/sshenv"
)
@@ -104,19 +105,11 @@ func (s *session) handleShell(ctx context.Context, req *ssh.Request) uint32 {
req.Reply(true, []byte{})
}
- args := &commandargs.Shell{
- GitlabKeyId: s.gitlabKeyId,
- Env: sshenv.Env{
- IsSSHConnection: true,
- OriginalCommand: s.execCmd,
- GitProtocolVersion: s.gitProtocolVersion,
- RemoteAddr: s.remoteAddr,
- },
- }
-
- if err := args.ParseCommand(s.execCmd); err != nil {
- s.toStderr("Failed to parse command: %v\n", err.Error())
- return 128
+ env := sshenv.Env{
+ IsSSHConnection: true,
+ OriginalCommand: s.execCmd,
+ GitProtocolVersion: s.gitProtocolVersion,
+ RemoteAddr: s.remoteAddr,
}
rw := &readwriter.ReadWriter{
@@ -125,9 +118,13 @@ func (s *session) handleShell(ctx context.Context, req *ssh.Request) uint32 {
ErrOut: s.channel.Stderr(),
}
- cmd := shellCmd.Build(args, s.cfg, rw)
- if cmd == nil {
- s.toStderr("Unknown command: %v\n", args.CommandType)
+ cmd, err := shellCmd.NewWithKey(s.gitlabKeyId, env, s.cfg, rw)
+
+ if err != nil {
+ if !errors.Is(err, disallowedcommand.Error) {
+ s.toStderr("Failed to parse command: %v\n", err.Error())
+ }
+ s.toStderr("Unknown command: %v\n", s.execCmd)
return 128
}
diff --git a/internal/sshd/session_test.go b/internal/sshd/session_test.go
index e4e312c..b014c0c 100644
--- a/internal/sshd/session_test.go
+++ b/internal/sshd/session_test.go
@@ -145,7 +145,7 @@ func TestHandleShell(t *testing.T) {
{
desc: "fails to parse command",
cmd: `\`,
- errMsg: "Failed to parse command: invalid command line string\n",
+ errMsg: "Failed to parse command: Invalid SSH command\nUnknown command: \\\n",
gitlabKeyId: "root",
expectedExitCode: 128,
}, {