summaryrefslogtreecommitdiff
path: root/go/internal/command/commandargs
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-01-15 23:36:21 +0000
committerNick Thomas <nick@gitlab.com>2019-01-15 23:36:21 +0000
commit1fcb56f42cdb2b6f562d8875abc4e33f7ef3e258 (patch)
treec246e5b43a1e7b5744e10e75d6ed125629f45936 /go/internal/command/commandargs
parent6c5b195353a632095d7f672d28b9985fd879b077 (diff)
parentd762f4ec9ea35cb00309b41ad60055cd3c5709ba (diff)
downloadgitlab-shell-1fcb56f42cdb2b6f562d8875abc4e33f7ef3e258.tar.gz
Merge branch 'bvl-feature-flag-commands' into 'master'
Parse commands to enable feature flags See merge request gitlab-org/gitlab-shell!270
Diffstat (limited to 'go/internal/command/commandargs')
-rw-r--r--go/internal/command/commandargs/command_args.go82
-rw-r--r--go/internal/command/commandargs/command_args_test.go73
2 files changed, 155 insertions, 0 deletions
diff --git a/go/internal/command/commandargs/command_args.go b/go/internal/command/commandargs/command_args.go
new file mode 100644
index 0000000..9e679d3
--- /dev/null
+++ b/go/internal/command/commandargs/command_args.go
@@ -0,0 +1,82 @@
+package commandargs
+
+import (
+ "errors"
+ "os"
+ "regexp"
+)
+
+type CommandType string
+
+const (
+ Discover CommandType = "discover"
+)
+
+var (
+ whoKeyRegex = regexp.MustCompile(`\bkey-(?P<keyid>\d+)\b`)
+ whoUsernameRegex = regexp.MustCompile(`\busername-(?P<username>\S+)\b`)
+)
+
+type CommandArgs struct {
+ GitlabUsername string
+ GitlabKeyId string
+ SshCommand string
+ CommandType CommandType
+}
+
+func Parse(arguments []string) (*CommandArgs, error) {
+ if sshConnection := os.Getenv("SSH_CONNECTION"); sshConnection == "" {
+ return nil, errors.New("Only ssh allowed")
+ }
+
+ info := &CommandArgs{}
+
+ info.parseWho(arguments)
+ info.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND"))
+
+ return info, nil
+}
+
+func (c *CommandArgs) parseWho(arguments []string) {
+ for _, argument := range arguments {
+ if keyId := tryParseKeyId(argument); keyId != "" {
+ c.GitlabKeyId = keyId
+ break
+ }
+
+ if username := tryParseUsername(argument); username != "" {
+ c.GitlabUsername = username
+ break
+ }
+ }
+}
+
+func tryParseKeyId(argument string) string {
+ matchInfo := whoKeyRegex.FindStringSubmatch(argument)
+ if len(matchInfo) == 2 {
+ // The first element is the full matched string
+ // The second element is the named `keyid`
+ return matchInfo[1]
+ }
+
+ return ""
+}
+
+func tryParseUsername(argument string) string {
+ matchInfo := whoUsernameRegex.FindStringSubmatch(argument)
+ if len(matchInfo) == 2 {
+ // The first element is the full matched string
+ // The second element is the named `username`
+ return matchInfo[1]
+ }
+
+ return ""
+}
+
+func (c *CommandArgs) parseCommand(commandString string) {
+ c.SshCommand = commandString
+
+ if commandString == "" {
+ c.CommandType = Discover
+ }
+}
diff --git a/go/internal/command/commandargs/command_args_test.go b/go/internal/command/commandargs/command_args_test.go
new file mode 100644
index 0000000..10c46fe
--- /dev/null
+++ b/go/internal/command/commandargs/command_args_test.go
@@ -0,0 +1,73 @@
+package commandargs
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper"
+)
+
+func TestParseSuccess(t *testing.T) {
+ testCases := []struct {
+ desc string
+ arguments []string
+ environment map[string]string
+ expectedArgs *CommandArgs
+ }{
+ // Setting the used env variables for every case to ensure we're
+ // not using anything set in the original env.
+ {
+ desc: "It sets discover as the command when the command string was empty",
+ environment: map[string]string{
+ "SSH_CONNECTION": "1",
+ "SSH_ORIGINAL_COMMAND": "",
+ },
+ expectedArgs: &CommandArgs{CommandType: Discover},
+ },
+ {
+ desc: "It passes on the original ssh command from the environment",
+ environment: map[string]string{
+ "SSH_CONNECTION": "1",
+ "SSH_ORIGINAL_COMMAND": "hello world",
+ },
+ expectedArgs: &CommandArgs{SshCommand: "hello world"},
+ }, {
+ desc: "It finds the key id in any passed arguments",
+ environment: map[string]string{
+ "SSH_CONNECTION": "1",
+ "SSH_ORIGINAL_COMMAND": "",
+ },
+ arguments: []string{"hello", "key-123"},
+ expectedArgs: &CommandArgs{CommandType: Discover, GitlabKeyId: "123"},
+ }, {
+ desc: "It finds the username in any passed arguments",
+ environment: map[string]string{
+ "SSH_CONNECTION": "1",
+ "SSH_ORIGINAL_COMMAND": "",
+ },
+ arguments: []string{"hello", "username-jane-doe"},
+ expectedArgs: &CommandArgs{CommandType: Discover, GitlabUsername: "jane-doe"},
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.desc, func(t *testing.T) {
+ restoreEnv := testhelper.TempEnv(tc.environment)
+ defer restoreEnv()
+
+ result, err := Parse(tc.arguments)
+
+ assert.NoError(t, err)
+ assert.Equal(t, tc.expectedArgs, result)
+ })
+ }
+}
+
+func TestParseFailure(t *testing.T) {
+ t.Run("It fails if SSH connection is not set", func(t *testing.T) {
+ _, err := Parse([]string{})
+
+ assert.Error(t, err, "Only ssh allowed")
+ })
+
+}