summaryrefslogtreecommitdiff
path: root/go/internal/command/commandargs/command_args.go
diff options
context:
space:
mode:
authorPatrick Bajao <ebajao@gitlab.com>2019-07-29 14:33:01 +0800
committerPatrick Bajao <ebajao@gitlab.com>2019-07-29 14:58:32 +0800
commitaab85f3600caf04b491d6ca4fc3f0f004d9e3fc0 (patch)
treeda3f6ab04de4e0c1ba5b79a281c6ca91852e0aa1 /go/internal/command/commandargs/command_args.go
parented0460374a5ca13d9ea17c6a9c21151319b7fd53 (diff)
downloadgitlab-shell-aab85f3600caf04b491d6ca4fc3f0f004d9e3fc0.tar.gz
Support falling back to ruby version of checkers
Rename the ruby scripts to have `-ruby` suffix and add a symlink for both to `./gitlab-shell`. The executable name will be used to determine how args will be parsed. For now, we only parse the arguments for gitlab-shell commands. If the executable is `gitlab-shell-authorized-keys-check` or `gitlab-shell-authorized-principals-check`, it'll always fallback to the ruby version. Ruby specs test the ruby script, the fallback from go to ruby and go implementation of both (still pending).
Diffstat (limited to 'go/internal/command/commandargs/command_args.go')
-rw-r--r--go/internal/command/commandargs/command_args.go39
1 files changed, 28 insertions, 11 deletions
diff --git a/go/internal/command/commandargs/command_args.go b/go/internal/command/commandargs/command_args.go
index d8fe32d..d68f5d2 100644
--- a/go/internal/command/commandargs/command_args.go
+++ b/go/internal/command/commandargs/command_args.go
@@ -3,12 +3,14 @@ package commandargs
import (
"errors"
"os"
+ "path/filepath"
"regexp"
"github.com/mattn/go-shellwords"
)
type CommandType string
+type Executable string
const (
Discover CommandType = "discover"
@@ -17,6 +19,7 @@ const (
ReceivePack CommandType = "git-receive-pack"
UploadPack CommandType = "git-upload-pack"
UploadArchive CommandType = "git-upload-archive"
+ GitlabShell Executable = "gitlab-shell"
)
var (
@@ -25,6 +28,7 @@ var (
)
type CommandArgs struct {
+ arguments []string
GitlabUsername string
GitlabKeyId string
SshArgs []string
@@ -32,23 +36,36 @@ type CommandArgs struct {
}
func Parse(arguments []string) (*CommandArgs, error) {
- if sshConnection := os.Getenv("SSH_CONNECTION"); sshConnection == "" {
- return nil, errors.New("Only ssh allowed")
- }
+ args := &CommandArgs{arguments: arguments}
+
+ if args.Executable() == GitlabShell {
+ if sshConnection := os.Getenv("SSH_CONNECTION"); sshConnection == "" {
+ return nil, errors.New("Only ssh allowed")
+ }
- args := &CommandArgs{}
- args.parseWho(arguments)
+ args.parseWho()
+
+ if err := args.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND")); err != nil {
+ return nil, errors.New("Invalid ssh command")
+ }
+ args.defineCommandType()
- if err := args.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND")); err != nil {
- return nil, errors.New("Invalid ssh command")
+ return args, nil
+ } else {
+ return args, nil
}
- args.defineCommandType()
+}
+
+func (c *CommandArgs) Executable() Executable {
+ return Executable(filepath.Base(c.arguments[0]))
+}
- return args, nil
+func (c *CommandArgs) Arguments() []string {
+ return c.arguments[1:]
}
-func (c *CommandArgs) parseWho(arguments []string) {
- for _, argument := range arguments {
+func (c *CommandArgs) parseWho() {
+ for _, argument := range c.arguments {
if keyId := tryParseKeyId(argument); keyId != "" {
c.GitlabKeyId = keyId
break