From aab85f3600caf04b491d6ca4fc3f0f004d9e3fc0 Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Mon, 29 Jul 2019 14:33:01 +0800 Subject: 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). --- .../command/commandargs/command_args_test.go | 45 +++++++++++++--------- 1 file changed, 27 insertions(+), 18 deletions(-) (limited to 'go/internal/command/commandargs/command_args_test.go') diff --git a/go/internal/command/commandargs/command_args_test.go b/go/internal/command/commandargs/command_args_test.go index e60bb92..21a676c 100644 --- a/go/internal/command/commandargs/command_args_test.go +++ b/go/internal/command/commandargs/command_args_test.go @@ -3,16 +3,16 @@ package commandargs import ( "testing" - "github.com/stretchr/testify/require" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper" + + "github.com/stretchr/testify/require" ) func TestParseSuccess(t *testing.T) { testCases := []struct { desc string - arguments []string environment map[string]string + arguments []string expectedArgs *CommandArgs }{ // Setting the used env variables for every case to ensure we're @@ -23,7 +23,8 @@ func TestParseSuccess(t *testing.T) { "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "", }, - expectedArgs: &CommandArgs{SshArgs: []string{}, CommandType: Discover}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{}, CommandType: Discover}, }, { desc: "It finds the key id in any passed arguments", @@ -31,72 +32,80 @@ func TestParseSuccess(t *testing.T) { "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "", }, - arguments: []string{"hello", "key-123"}, - expectedArgs: &CommandArgs{SshArgs: []string{}, CommandType: Discover, GitlabKeyId: "123"}, + arguments: []string{string(GitlabShell), "hello", "key-123"}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell), "hello", "key-123"}, SshArgs: []string{}, 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{SshArgs: []string{}, CommandType: Discover, GitlabUsername: "jane-doe"}, + arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}, SshArgs: []string{}, CommandType: Discover, GitlabUsername: "jane-doe"}, }, { desc: "It parses 2fa_recovery_codes command", environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "2fa_recovery_codes", }, - expectedArgs: &CommandArgs{SshArgs: []string{"2fa_recovery_codes"}, CommandType: TwoFactorRecover}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: TwoFactorRecover}, }, { desc: "It parses git-receive-pack command", environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git-receive-pack group/repo", }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: "It parses git-receive-pack command and a project with single quotes", environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git receive-pack 'group/repo'", }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: `It parses "git receive-pack" command`, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git receive-pack "group/repo"`, }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: `It parses a command followed by control characters`, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git-receive-pack group/repo; any command`, }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: "It parses git-upload-pack command", environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git upload-pack "group/repo"`, }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: UploadPack}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: UploadPack}, }, { desc: "It parses git-upload-archive command", environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git-upload-archive 'group/repo'", }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: UploadArchive}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: UploadArchive}, }, { desc: "It parses git-lfs-authenticate command", environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git-lfs-authenticate 'group/repo' download", }, - expectedArgs: &CommandArgs{SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: LfsAuthenticate}, + arguments: []string{string(GitlabShell)}, + expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: LfsAuthenticate}, }, } @@ -115,7 +124,7 @@ func TestParseSuccess(t *testing.T) { func TestParseFailure(t *testing.T) { t.Run("It fails if SSH connection is not set", func(t *testing.T) { - _, err := Parse([]string{}) + _, err := Parse([]string{string(GitlabShell)}) require.Error(t, err, "Only ssh allowed") }) @@ -128,7 +137,7 @@ func TestParseFailure(t *testing.T) { restoreEnv := testhelper.TempEnv(environment) defer restoreEnv() - _, err := Parse([]string{}) + _, err := Parse([]string{string(GitlabShell)}) require.Error(t, err, "Invalid ssh command") }) -- cgit v1.2.1 From 3b0176df497263323da2fae793a79b568502e6db Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Mon, 29 Jul 2019 15:56:00 +0800 Subject: Support different CommandArgs type `CommandArgs` has been renamed to `Shell`. An interface has been added that includes `Executable()` and `Arguments()` method. The `BaseArgs` implement this methods and should be embeeded in each type. --- .../command/commandargs/command_args_test.go | 77 ++++++++++++++-------- 1 file changed, 50 insertions(+), 27 deletions(-) (limited to 'go/internal/command/commandargs/command_args_test.go') diff --git a/go/internal/command/commandargs/command_args_test.go b/go/internal/command/commandargs/command_args_test.go index 21a676c..1fe3ba7 100644 --- a/go/internal/command/commandargs/command_args_test.go +++ b/go/internal/command/commandargs/command_args_test.go @@ -13,7 +13,7 @@ func TestParseSuccess(t *testing.T) { desc string environment map[string]string arguments []string - expectedArgs *CommandArgs + expectedArgs CommandArgs }{ // Setting the used env variables for every case to ensure we're // not using anything set in the original env. @@ -24,7 +24,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "", }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{}, CommandType: Discover}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{}, CommandType: Discover}, }, { desc: "It finds the key id in any passed arguments", @@ -33,7 +33,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "", }, arguments: []string{string(GitlabShell), "hello", "key-123"}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell), "hello", "key-123"}, SshArgs: []string{}, CommandType: Discover, GitlabKeyId: "123"}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell), "hello", "key-123"}}, SshArgs: []string{}, CommandType: Discover, GitlabKeyId: "123"}, }, { desc: "It finds the username in any passed arguments", environment: map[string]string{ @@ -41,7 +41,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "", }, arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}, SshArgs: []string{}, CommandType: Discover, GitlabUsername: "jane-doe"}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}}, SshArgs: []string{}, CommandType: Discover, GitlabUsername: "jane-doe"}, }, { desc: "It parses 2fa_recovery_codes command", environment: map[string]string{ @@ -49,7 +49,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "2fa_recovery_codes", }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: TwoFactorRecover}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: TwoFactorRecover}, }, { desc: "It parses git-receive-pack command", environment: map[string]string{ @@ -57,7 +57,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "git-receive-pack group/repo", }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: "It parses git-receive-pack command and a project with single quotes", environment: map[string]string{ @@ -65,7 +65,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "git receive-pack 'group/repo'", }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: `It parses "git receive-pack" command`, environment: map[string]string{ @@ -73,7 +73,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": `git receive-pack "group/repo"`, }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: `It parses a command followed by control characters`, environment: map[string]string{ @@ -81,7 +81,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": `git-receive-pack group/repo; any command`, }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { desc: "It parses git-upload-pack command", environment: map[string]string{ @@ -89,7 +89,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": `git upload-pack "group/repo"`, }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: UploadPack}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: UploadPack}, }, { desc: "It parses git-upload-archive command", environment: map[string]string{ @@ -97,7 +97,7 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "git-upload-archive 'group/repo'", }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: UploadArchive}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: UploadArchive}, }, { desc: "It parses git-lfs-authenticate command", environment: map[string]string{ @@ -105,7 +105,11 @@ func TestParseSuccess(t *testing.T) { "SSH_ORIGINAL_COMMAND": "git-lfs-authenticate 'group/repo' download", }, arguments: []string{string(GitlabShell)}, - expectedArgs: &CommandArgs{arguments: []string{string(GitlabShell)}, SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: LfsAuthenticate}, + expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: LfsAuthenticate}, + }, { + desc: "Unknown executable", + arguments: []string{"unknown"}, + expectedArgs: &BaseArgs{arguments: []string{"unknown"}}, }, } @@ -123,22 +127,41 @@ func TestParseSuccess(t *testing.T) { } func TestParseFailure(t *testing.T) { - t.Run("It fails if SSH connection is not set", func(t *testing.T) { - _, err := Parse([]string{string(GitlabShell)}) - - require.Error(t, err, "Only ssh allowed") - }) + testCases := []struct { + desc string + environment map[string]string + arguments []string + expectedError string + }{ + { + desc: "It fails if SSH connection is not set", + arguments: []string{string(GitlabShell)}, + expectedError: "Only ssh allowed", + }, + { + desc: "It fails if SSH command is invalid", + environment: map[string]string{ + "SSH_CONNECTION": "1", + "SSH_ORIGINAL_COMMAND": `git receive-pack "`, + }, + arguments: []string{string(GitlabShell)}, + expectedError: "Only ssh allowed", + }, + { + desc: "It fails if arguments is empty", + arguments: []string{}, + expectedError: "arguments should include the executable", + }, + } - t.Run("It fails if SSH command is invalid", func(t *testing.T) { - environment := map[string]string{ - "SSH_CONNECTION": "1", - "SSH_ORIGINAL_COMMAND": `git receive-pack "`, - } - restoreEnv := testhelper.TempEnv(environment) - defer restoreEnv() + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + restoreEnv := testhelper.TempEnv(tc.environment) + defer restoreEnv() - _, err := Parse([]string{string(GitlabShell)}) + _, err := Parse(tc.arguments) - require.Error(t, err, "Invalid ssh command") - }) + require.Error(t, err, tc.expectedError) + }) + } } -- cgit v1.2.1 From 3b6f9f7583755e041e76142d7caf7716937907fa Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Fri, 2 Aug 2019 16:10:17 +0800 Subject: Add Executable struct This struct is responsible for determining the name and root dir of the executable. The `RootDir` property will be used to find the config. The `Name` property will be used to determine what `Command` and `CommandArgs` to be built. --- .../command/commandargs/command_args_test.go | 104 ++++++++++++--------- 1 file changed, 58 insertions(+), 46 deletions(-) (limited to 'go/internal/command/commandargs/command_args_test.go') diff --git a/go/internal/command/commandargs/command_args_test.go b/go/internal/command/commandargs/command_args_test.go index 1fe3ba7..148c987 100644 --- a/go/internal/command/commandargs/command_args_test.go +++ b/go/internal/command/commandargs/command_args_test.go @@ -3,6 +3,7 @@ package commandargs import ( "testing" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/executable" "gitlab.com/gitlab-org/gitlab-shell/go/internal/testhelper" "github.com/stretchr/testify/require" @@ -11,6 +12,7 @@ import ( func TestParseSuccess(t *testing.T) { testCases := []struct { desc string + executable *executable.Executable environment map[string]string arguments []string expectedArgs CommandArgs @@ -18,98 +20,110 @@ func TestParseSuccess(t *testing.T) { // 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", + desc: "It sets discover as the command when the command string was empty", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "", }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{}, CommandType: Discover}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{}, CommandType: Discover}, }, { - desc: "It finds the key id in any passed arguments", + desc: "It finds the key id in any passed arguments", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "", }, - arguments: []string{string(GitlabShell), "hello", "key-123"}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell), "hello", "key-123"}}, SshArgs: []string{}, CommandType: Discover, GitlabKeyId: "123"}, + arguments: []string{"hello", "key-123"}, + expectedArgs: &Shell{Arguments: []string{"hello", "key-123"}, SshArgs: []string{}, CommandType: Discover, GitlabKeyId: "123"}, }, { - desc: "It finds the username in any passed arguments", + desc: "It finds the username in any passed arguments", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "", }, - arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell), "hello", "username-jane-doe"}}, SshArgs: []string{}, CommandType: Discover, GitlabUsername: "jane-doe"}, + arguments: []string{"hello", "username-jane-doe"}, + expectedArgs: &Shell{Arguments: []string{"hello", "username-jane-doe"}, SshArgs: []string{}, CommandType: Discover, GitlabUsername: "jane-doe"}, }, { - desc: "It parses 2fa_recovery_codes command", + desc: "It parses 2fa_recovery_codes command", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "2fa_recovery_codes", }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: TwoFactorRecover}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: TwoFactorRecover}, }, { - desc: "It parses git-receive-pack command", + desc: "It parses git-receive-pack command", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git-receive-pack group/repo", }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { - desc: "It parses git-receive-pack command and a project with single quotes", + desc: "It parses git-receive-pack command and a project with single quotes", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git receive-pack 'group/repo'", }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { - desc: `It parses "git receive-pack" command`, + desc: `It parses "git receive-pack" command`, + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git receive-pack "group/repo"`, }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { - desc: `It parses a command followed by control characters`, + desc: `It parses a command followed by control characters`, + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git-receive-pack group/repo; any command`, }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: ReceivePack}, }, { - desc: "It parses git-upload-pack command", + desc: "It parses git-upload-pack command", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git upload-pack "group/repo"`, }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: UploadPack}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: UploadPack}, }, { - desc: "It parses git-upload-archive command", + desc: "It parses git-upload-archive command", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git-upload-archive 'group/repo'", }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: UploadArchive}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: UploadArchive}, }, { - desc: "It parses git-lfs-authenticate command", + desc: "It parses git-lfs-authenticate command", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": "git-lfs-authenticate 'group/repo' download", }, - arguments: []string{string(GitlabShell)}, - expectedArgs: &Shell{BaseArgs: &BaseArgs{arguments: []string{string(GitlabShell)}}, SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: LfsAuthenticate}, + arguments: []string{}, + expectedArgs: &Shell{Arguments: []string{}, SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: LfsAuthenticate}, }, { desc: "Unknown executable", - arguments: []string{"unknown"}, - expectedArgs: &BaseArgs{arguments: []string{"unknown"}}, + executable: &executable.Executable{Name: "unknown"}, + arguments: []string{}, + expectedArgs: &GenericArgs{Arguments: []string{}}, }, } @@ -118,7 +132,7 @@ func TestParseSuccess(t *testing.T) { restoreEnv := testhelper.TempEnv(tc.environment) defer restoreEnv() - result, err := Parse(tc.arguments) + result, err := Parse(tc.executable, tc.arguments) require.NoError(t, err) require.Equal(t, tc.expectedArgs, result) @@ -129,28 +143,26 @@ func TestParseSuccess(t *testing.T) { func TestParseFailure(t *testing.T) { testCases := []struct { desc string + executable *executable.Executable environment map[string]string arguments []string expectedError string }{ { desc: "It fails if SSH connection is not set", - arguments: []string{string(GitlabShell)}, - expectedError: "Only ssh allowed", + executable: &executable.Executable{Name: executable.GitlabShell}, + arguments: []string{}, + expectedError: "Only SSH allowed", }, { - desc: "It fails if SSH command is invalid", + desc: "It fails if SSH command is invalid", + executable: &executable.Executable{Name: executable.GitlabShell}, environment: map[string]string{ "SSH_CONNECTION": "1", "SSH_ORIGINAL_COMMAND": `git receive-pack "`, }, - arguments: []string{string(GitlabShell)}, - expectedError: "Only ssh allowed", - }, - { - desc: "It fails if arguments is empty", arguments: []string{}, - expectedError: "arguments should include the executable", + expectedError: "Invalid SSH allowed", }, } @@ -159,7 +171,7 @@ func TestParseFailure(t *testing.T) { restoreEnv := testhelper.TempEnv(tc.environment) defer restoreEnv() - _, err := Parse(tc.arguments) + _, err := Parse(tc.executable, tc.arguments) require.Error(t, err, tc.expectedError) }) -- cgit v1.2.1