As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Unverified Commit a703b51e authored by Archish Thakkar's avatar Archish Thakkar Committed by GitLab
Browse files

Lint fixes for commandargs packages

parent 55cd93ff
No related branches found
No related tags found
No related merge requests found
Showing
with 105 additions and 81 deletions
Loading
@@ -58,7 +58,7 @@ func TestParseSuccess(t *testing.T) {
Loading
@@ -58,7 +58,7 @@ func TestParseSuccess(t *testing.T) {
desc: "It parses authorized-principals command", desc: "It parses authorized-principals command",
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck}, executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
arguments: []string{"key", "principal-1", "principal-2"}, arguments: []string{"key", "principal-1", "principal-2"},
expectedArgs: &commandargs.AuthorizedPrincipals{Arguments: []string{"key", "principal-1", "principal-2"}, KeyId: "key", Principals: []string{"principal-1", "principal-2"}}, expectedArgs: &commandargs.AuthorizedPrincipals{Arguments: []string{"key", "principal-1", "principal-2"}, KeyID: "key", Principals: []string{"principal-1", "principal-2"}},
}, },
} }
Loading
Loading
Loading
@@ -41,7 +41,7 @@ func NewWithKey(gitlabKeyID string, env sshenv.Env, config *config.Config, readW
Loading
@@ -41,7 +41,7 @@ func NewWithKey(gitlabKeyID string, env sshenv.Env, config *config.Config, readW
return nil, err return nil, err
} }
args.GitlabKeyId = gitlabKeyID args.GitlabKeyID = gitlabKeyID
if cmd := Build(args, config, readWriter); cmd != nil { if cmd := Build(args, config, readWriter); cmd != nil {
return cmd, nil return cmd, nil
} }
Loading
Loading
Loading
@@ -256,112 +256,112 @@ func TestParseSuccess(t *testing.T) {
Loading
@@ -256,112 +256,112 @@ func TestParseSuccess(t *testing.T) {
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{}, CommandType: commandargs.Discover, Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{}, CommandType: commandargs.Discover, Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
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}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{"hello", "key-123"}, arguments: []string{"hello", "key-123"},
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "key-123"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabKeyId: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "key-123"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabKeyID: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
desc: "It finds the key id only if the argument is of <key-id> format", desc: "It finds the key id only if the argument is of <key-id> format",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{"hello", "username-key-123"}, arguments: []string{"hello", "username-key-123"},
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-key-123"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-key-123"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
desc: "It finds the key id if the key is listed as the last argument", desc: "It finds the key id if the key is listed as the last argument",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{"hello", "gitlab-shell -c key-123"}, arguments: []string{"hello", "gitlab-shell -c key-123"},
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "gitlab-shell -c key-123"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabKeyId: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "gitlab-shell -c key-123"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabKeyID: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
desc: "It finds the username if the username is listed as the last argument", desc: "It finds the username if the username is listed as the last argument",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{"hello", "gitlab-shell -c username-jane-doe"}, arguments: []string{"hello", "gitlab-shell -c username-jane-doe"},
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "gitlab-shell -c username-jane-doe"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "jane-doe", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "gitlab-shell -c username-jane-doe"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "jane-doe", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
desc: "It finds the key id only if the last argument is of <key-id> format", desc: "It finds the key id only if the last argument is of <key-id> format",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{"hello", "gitlab-shell -c username-key-123"}, arguments: []string{"hello", "gitlab-shell -c username-key-123"},
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "gitlab-shell -c username-key-123"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "gitlab-shell -c username-key-123"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
desc: "It finds the username in any passed arguments", desc: "It finds the username in any passed arguments",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}, env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
arguments: []string{"hello", "username-jane-doe"}, arguments: []string{"hello", "username-jane-doe"},
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-jane-doe"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "jane-doe", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}}, expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-jane-doe"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "jane-doe", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
}, },
{ {
desc: "It parses 2fa_recovery_codes command", desc: "It parses 2fa_recovery_codes command",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: commandargs.TwoFactorRecover, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"2fa_recovery_codes"}, CommandType: commandargs.TwoFactorRecover, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"}},
}, },
{ {
desc: "It parses git-receive-pack command", desc: "It parses git-receive-pack command",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack group/repo"}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack group/repo"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack group/repo"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack group/repo"}},
}, },
{ {
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}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack 'group/repo'"}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack 'group/repo'"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack 'group/repo'"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack 'group/repo'"}},
}, },
{ {
desc: `It parses "git receive-pack" command`, desc: `It parses "git receive-pack" command`,
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack "group/repo"`}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack "group/repo"`},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack "group/repo"`}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack "group/repo"`}},
}, },
{ {
desc: `It parses a command followed by control characters`, desc: `It parses a command followed by control characters`,
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack group/repo; any command`}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack group/repo; any command`},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack group/repo; any command`}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-receive-pack", "group/repo"}, CommandType: commandargs.ReceivePack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack group/repo; any command`}},
}, },
{ {
desc: "It parses git-upload-pack command", desc: "It parses git-upload-pack command",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git upload-pack "group/repo"`}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git upload-pack "group/repo"`},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-upload-pack", "group/repo"}, CommandType: commandargs.UploadPack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git upload-pack "group/repo"`}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-upload-pack", "group/repo"}, CommandType: commandargs.UploadPack, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git upload-pack "group/repo"`}},
}, },
{ {
desc: "It parses git-upload-archive command", desc: "It parses git-upload-archive command",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-upload-archive 'group/repo'"}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-upload-archive 'group/repo'"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-upload-archive", "group/repo"}, CommandType: commandargs.UploadArchive, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-upload-archive 'group/repo'"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-upload-archive", "group/repo"}, CommandType: commandargs.UploadArchive, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-upload-archive 'group/repo'"}},
}, },
{ {
desc: "It parses git-lfs-authenticate command", desc: "It parses git-lfs-authenticate command",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-authenticate 'group/repo' download"}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-authenticate 'group/repo' download"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: commandargs.LfsAuthenticate, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-authenticate 'group/repo' download"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-lfs-authenticate", "group/repo", "download"}, CommandType: commandargs.LfsAuthenticate, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-authenticate 'group/repo' download"}},
}, },
{ {
desc: "It parses git-lfs-transfer command", desc: "It parses git-lfs-transfer command",
executable: &executable.Executable{Name: executable.GitlabShell}, executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-transfer 'group/repo' download"}, env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-transfer 'group/repo' download"},
arguments: []string{}, arguments: []string{},
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"git-lfs-transfer", "group/repo", "download"}, CommandType: commandargs.LfsTransfer, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-transfer 'group/repo' download"}}, expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"git-lfs-transfer", "group/repo", "download"}, CommandType: commandargs.LfsTransfer, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-transfer 'group/repo' download"}},
}, },
} }
Loading
@@ -427,7 +427,7 @@ func TestNewWithUsername(t *testing.T) {
Loading
@@ -427,7 +427,7 @@ func TestNewWithUsername(t *testing.T) {
Args: &commandargs.Shell{ Args: &commandargs.Shell{
CommandType: commandargs.ReceivePack, CommandType: commandargs.ReceivePack,
GitlabUsername: "username", GitlabUsername: "username",
SshArgs: []string{"git-receive-pack", "group/repo"}, SSHArgs: []string{"git-receive-pack", "group/repo"},
Env: sshenv.Env{ Env: sshenv.Env{
IsSSHConnection: true, IsSSHConnection: true,
OriginalCommand: "git-receive-pack 'group/repo'", OriginalCommand: "git-receive-pack 'group/repo'",
Loading
@@ -442,7 +442,7 @@ func TestNewWithUsername(t *testing.T) {
Loading
@@ -442,7 +442,7 @@ func TestNewWithUsername(t *testing.T) {
Args: &commandargs.Shell{ Args: &commandargs.Shell{
CommandType: commandargs.TwoFactorRecover, CommandType: commandargs.TwoFactorRecover,
GitlabUsername: "username", GitlabUsername: "username",
SshArgs: []string{"2fa_recovery_codes"}, SSHArgs: []string{"2fa_recovery_codes"},
Env: sshenv.Env{ Env: sshenv.Env{
IsSSHConnection: true, IsSSHConnection: true,
OriginalCommand: "2fa_recovery_codes", OriginalCommand: "2fa_recovery_codes",
Loading
@@ -463,7 +463,7 @@ func TestNewWithUsername(t *testing.T) {
Loading
@@ -463,7 +463,7 @@ func TestNewWithUsername(t *testing.T) {
Args: &commandargs.Shell{ Args: &commandargs.Shell{
CommandType: commandargs.ReceivePack, CommandType: commandargs.ReceivePack,
GitlabUsername: "username", GitlabUsername: "username",
SshArgs: []string{"git-receive-pack", "group/repo"}, SSHArgs: []string{"git-receive-pack", "group/repo"},
Env: sshenv.Env{ Env: sshenv.Env{
IsSSHConnection: true, IsSSHConnection: true,
OriginalCommand: "git-receive-pack 'group/repo'", OriginalCommand: "git-receive-pack 'group/repo'",
Loading
Loading
Loading
@@ -40,7 +40,7 @@ func (c *Command) printPrincipalLines() error {
Loading
@@ -40,7 +40,7 @@ func (c *Command) printPrincipalLines() error {
} }
func (c *Command) printPrincipalLine(principal string) error { func (c *Command) printPrincipalLine(principal string) error {
principalKeyLine, err := keyline.NewPrincipalKeyLine(c.Args.KeyId, principal, c.Config) principalKeyLine, err := keyline.NewPrincipalKeyLine(c.Args.KeyID, principal, c.Config)
if err != nil { if err != nil {
return err return err
} }
Loading
Loading
Loading
@@ -22,12 +22,12 @@ func TestExecute(t *testing.T) {
Loading
@@ -22,12 +22,12 @@ func TestExecute(t *testing.T) {
}{ }{
{ {
desc: "With single principal", desc: "With single principal",
arguments: &commandargs.AuthorizedPrincipals{KeyId: "key", Principals: []string{"principal"}}, arguments: &commandargs.AuthorizedPrincipals{KeyID: "key", Principals: []string{"principal"}},
expectedOutput: "command=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal\n", expectedOutput: "command=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal\n",
}, },
{ {
desc: "With multiple principals", desc: "With multiple principals",
arguments: &commandargs.AuthorizedPrincipals{KeyId: "key", Principals: []string{"principal-1", "principal-2"}}, arguments: &commandargs.AuthorizedPrincipals{KeyID: "key", Principals: []string{"principal-1", "principal-2"}},
expectedOutput: "command=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal-1\ncommand=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal-2\n", expectedOutput: "command=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal-1\ncommand=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal-2\n",
}, },
} }
Loading
Loading
// Package commandargs defines structures and methods for handling command-line arguments
// related to authorized key checks in the GitLab shell.
package commandargs package commandargs
import ( import (
Loading
@@ -5,6 +7,7 @@ import (
Loading
@@ -5,6 +7,7 @@ import (
"fmt" "fmt"
) )
// AuthorizedKeys holds the arguments and user information for key authorization checks.
type AuthorizedKeys struct { type AuthorizedKeys struct {
Arguments []string Arguments []string
ExpectedUser string ExpectedUser string
Loading
@@ -12,6 +15,7 @@ type AuthorizedKeys struct {
Loading
@@ -12,6 +15,7 @@ type AuthorizedKeys struct {
Key string Key string
} }
// Parse parses and validates the arguments, setting ExpectedUser, ActualUser, and Key.
func (ak *AuthorizedKeys) Parse() error { func (ak *AuthorizedKeys) Parse() error {
if err := ak.validate(); err != nil { if err := ak.validate(); err != nil {
return err return err
Loading
@@ -24,6 +28,7 @@ func (ak *AuthorizedKeys) Parse() error {
Loading
@@ -24,6 +28,7 @@ func (ak *AuthorizedKeys) Parse() error {
return nil return nil
} }
// GetArguments returns the list of command-line arguments.
func (ak *AuthorizedKeys) GetArguments() []string { func (ak *AuthorizedKeys) GetArguments() []string {
return ak.Arguments return ak.Arguments
} }
Loading
@@ -32,7 +37,7 @@ func (ak *AuthorizedKeys) validate() error {
Loading
@@ -32,7 +37,7 @@ func (ak *AuthorizedKeys) validate() error {
argsSize := len(ak.Arguments) argsSize := len(ak.Arguments)
if argsSize != 3 { if argsSize != 3 {
return errors.New(fmt.Sprintf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-keys-check <expected-username> <actual-username> <key>", argsSize)) return fmt.Errorf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-keys-check <expected-username> <actual-username> <key>", argsSize)
} }
expectedUsername := ak.Arguments[0] expectedUsername := ak.Arguments[0]
Loading
Loading
// Package commandargs provides functionality for handling and parsing command-line arguments
// related to authorized principals for GitLab shell commands.
package commandargs package commandargs
import ( import (
Loading
@@ -5,23 +7,27 @@ import (
Loading
@@ -5,23 +7,27 @@ import (
"fmt" "fmt"
) )
// AuthorizedPrincipals holds the arguments for checking authorized principals and the key ID.
type AuthorizedPrincipals struct { type AuthorizedPrincipals struct {
Arguments []string Arguments []string
KeyId string KeyID string
Principals []string Principals []string
} }
// Parse validates and extracts the key ID and principals from the Arguments slice.
// Returns an error if validation fails.
func (ap *AuthorizedPrincipals) Parse() error { func (ap *AuthorizedPrincipals) Parse() error {
if err := ap.validate(); err != nil { if err := ap.validate(); err != nil {
return err return err
} }
ap.KeyId = ap.Arguments[0] ap.KeyID = ap.Arguments[0]
ap.Principals = ap.Arguments[1:] ap.Principals = ap.Arguments[1:]
return nil return nil
} }
// GetArguments returns the list of command-line arguments provided.
func (ap *AuthorizedPrincipals) GetArguments() []string { func (ap *AuthorizedPrincipals) GetArguments() []string {
return ap.Arguments return ap.Arguments
} }
Loading
@@ -30,13 +36,13 @@ func (ap *AuthorizedPrincipals) validate() error {
Loading
@@ -30,13 +36,13 @@ func (ap *AuthorizedPrincipals) validate() error {
argsSize := len(ap.Arguments) argsSize := len(ap.Arguments)
if argsSize < 2 { if argsSize < 2 {
return errors.New(fmt.Sprintf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-principals-check <key-id> <principal1> [<principal2>...]", argsSize)) return fmt.Errorf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-principals-check <key-id> <principal1> [<principal2>...]", argsSize)
} }
keyId := ap.Arguments[0] keyID := ap.Arguments[0]
principals := ap.Arguments[1:] principals := ap.Arguments[1:]
if keyId == "" { if keyID == "" {
return errors.New("# No key_id provided") return errors.New("# No key_id provided")
} }
Loading
Loading
// Package commandargs defines types and interfaces for handling command-line arguments
// in GitLab shell commands.
package commandargs package commandargs
// CommandType represents a type of command identified by a string.
type CommandType string type CommandType string
// CommandArgs is an interface for parsing and accessing command-line arguments.
type CommandArgs interface { type CommandArgs interface {
Parse() error Parse() error
GetArguments() []string GetArguments() []string
Loading
Loading
// Package commandargs provides functionality to handle and parse command-line arguments
// for various GitLab shell commands, including SSH arguments and command types.
package commandargs package commandargs
import ( import (
Loading
@@ -9,6 +11,7 @@ import (
Loading
@@ -9,6 +11,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/sshenv" "gitlab.com/gitlab-org/gitlab-shell/v14/internal/sshenv"
) )
// Define supported command types
const ( const (
Discover CommandType = "discover" Discover CommandType = "discover"
TwoFactorRecover CommandType = "2fa_recovery_codes" TwoFactorRecover CommandType = "2fa_recovery_codes"
Loading
@@ -21,23 +24,27 @@ const (
Loading
@@ -21,23 +24,27 @@ const (
PersonalAccessToken CommandType = "personal_access_token" PersonalAccessToken CommandType = "personal_access_token"
) )
// Regular expressions for parsing key IDs and usernames from arguments
var ( var (
whoKeyRegex = regexp.MustCompile(`\Akey-(?P<keyid>\d+)\z`) whoKeyRegex = regexp.MustCompile(`\Akey-(?P<keyid>\d+)\z`)
whoUsernameRegex = regexp.MustCompile(`\Ausername-(?P<username>\S+)\z`) whoUsernameRegex = regexp.MustCompile(`\Ausername-(?P<username>\S+)\z`)
// List of Git commands that are handled in a special way
GitCommands = []CommandType{LfsAuthenticate, UploadPack, ReceivePack, UploadArchive} GitCommands = []CommandType{LfsAuthenticate, UploadPack, ReceivePack, UploadArchive}
) )
// Shell represents a parsed shell command with its arguments and related information.
type Shell struct { type Shell struct {
Arguments []string Arguments []string
GitlabUsername string GitlabUsername string
GitlabKeyId string GitlabKeyID string
GitlabKrb5Principal string GitlabKrb5Principal string
SshArgs []string SSHArgs []string
CommandType CommandType CommandType CommandType
Env sshenv.Env Env sshenv.Env
} }
// Parse validates and parses the command-line arguments and SSH environment.
func (s *Shell) Parse() error { func (s *Shell) Parse() error {
if err := s.validate(); err != nil { if err := s.validate(); err != nil {
return err return err
Loading
@@ -48,17 +55,18 @@ func (s *Shell) Parse() error {
Loading
@@ -48,17 +55,18 @@ func (s *Shell) Parse() error {
return nil return nil
} }
// GetArguments returns the list of command-line arguments.
func (s *Shell) GetArguments() []string { func (s *Shell) GetArguments() []string {
return s.Arguments return s.Arguments
} }
func (s *Shell) validate() error { func (s *Shell) validate() error {
if !s.Env.IsSSHConnection { if !s.Env.IsSSHConnection {
return fmt.Errorf("Only SSH allowed") return fmt.Errorf("Only SSH allowed") //nolint:stylecheck //message is customer facing
} }
if err := s.ParseCommand(s.Env.OriginalCommand); err != nil { if err := s.ParseCommand(s.Env.OriginalCommand); err != nil {
return fmt.Errorf("Invalid SSH command: %w", err) return fmt.Errorf("Invalid SSH command: %w", err) //nolint:stylecheck //message is customer facing
} }
return nil return nil
Loading
@@ -66,8 +74,8 @@ func (s *Shell) validate() error {
Loading
@@ -66,8 +74,8 @@ func (s *Shell) validate() error {
func (s *Shell) parseWho() { func (s *Shell) parseWho() {
for _, argument := range s.Arguments { for _, argument := range s.Arguments {
if keyId := tryParseKeyId(argument); keyId != "" { if keyID := tryParseKeyID(argument); keyID != "" {
s.GitlabKeyId = keyId s.GitlabKeyID = keyID
break break
} }
Loading
@@ -95,7 +103,7 @@ func tryParse(r *regexp.Regexp, argument string) string {
Loading
@@ -95,7 +103,7 @@ func tryParse(r *regexp.Regexp, argument string) string {
return "" return ""
} }
func tryParseKeyId(argument string) string { func tryParseKeyID(argument string) string {
return tryParse(whoKeyRegex, argument) return tryParse(whoKeyRegex, argument)
} }
Loading
@@ -103,6 +111,7 @@ func tryParseUsername(argument string) string {
Loading
@@ -103,6 +111,7 @@ func tryParseUsername(argument string) string {
return tryParse(whoUsernameRegex, argument) return tryParse(whoUsernameRegex, argument)
} }
// ParseCommand parses the command string into a slice of arguments.
func (s *Shell) ParseCommand(commandString string) error { func (s *Shell) ParseCommand(commandString string) error {
args, err := shellwords.Parse(commandString) args, err := shellwords.Parse(commandString)
if err != nil { if err != nil {
Loading
@@ -117,7 +126,7 @@ func (s *Shell) ParseCommand(commandString string) error {
Loading
@@ -117,7 +126,7 @@ func (s *Shell) ParseCommand(commandString string) error {
args = append([]string{command}, commandArgs...) args = append([]string{command}, commandArgs...)
} }
s.SshArgs = args s.SSHArgs = args
s.defineCommandType() s.defineCommandType()
Loading
@@ -125,9 +134,9 @@ func (s *Shell) ParseCommand(commandString string) error {
Loading
@@ -125,9 +134,9 @@ func (s *Shell) ParseCommand(commandString string) error {
} }
func (s *Shell) defineCommandType() { func (s *Shell) defineCommandType() {
if len(s.SshArgs) == 0 { if len(s.SSHArgs) == 0 {
s.CommandType = Discover s.CommandType = Discover
} else { } else {
s.CommandType = CommandType(s.SshArgs[0]) s.CommandType = CommandType(s.SSHArgs[0])
} }
} }
Loading
@@ -59,12 +59,12 @@ func TestExecute(t *testing.T) {
Loading
@@ -59,12 +59,12 @@ func TestExecute(t *testing.T) {
}, },
{ {
desc: "With a known key id", desc: "With a known key id",
arguments: &commandargs.Shell{GitlabKeyId: "1"}, arguments: &commandargs.Shell{GitlabKeyID: "1"},
expectedUsername: "@alex-doe", expectedUsername: "@alex-doe",
}, },
{ {
desc: "With an unknown key", desc: "With an unknown key",
arguments: &commandargs.Shell{GitlabKeyId: "-1"}, arguments: &commandargs.Shell{GitlabKeyID: "-1"},
expectedUsername: "Anonymous", expectedUsername: "Anonymous",
}, },
{ {
Loading
Loading
Loading
@@ -46,7 +46,7 @@ type logInfo struct{}
Loading
@@ -46,7 +46,7 @@ type logInfo struct{}
// Execute executes the LFS authentication command // Execute executes the LFS authentication command
func (c *Command) Execute(ctx context.Context) (context.Context, error) { func (c *Command) Execute(ctx context.Context) (context.Context, error) {
args := c.Args.SshArgs args := c.Args.SSHArgs
if len(args) < 3 { if len(args) < 3 {
return ctx, disallowedcommand.Error return ctx, disallowedcommand.Error
} }
Loading
Loading
Loading
@@ -37,12 +37,12 @@ func TestFailedRequests(t *testing.T) {
Loading
@@ -37,12 +37,12 @@ func TestFailedRequests(t *testing.T) {
}, },
{ {
desc: "With disallowed command", desc: "With disallowed command",
arguments: &commandargs.Shell{GitlabKeyId: "1", SshArgs: []string{"git-lfs-authenticate", "group/repo", "unknown"}}, arguments: &commandargs.Shell{GitlabKeyID: "1", SSHArgs: []string{"git-lfs-authenticate", "group/repo", "unknown"}},
expectedOutput: "Disallowed command", expectedOutput: "Disallowed command",
}, },
{ {
desc: "With disallowed user", desc: "With disallowed user",
arguments: &commandargs.Shell{GitlabKeyId: "disallowed", SshArgs: []string{"git-lfs-authenticate", "group/repo", "download"}}, arguments: &commandargs.Shell{GitlabKeyID: "disallowed", SSHArgs: []string{"git-lfs-authenticate", "group/repo", "download"}},
expectedOutput: "Disallowed by API call", expectedOutput: "Disallowed by API call",
}, },
} }
Loading
@@ -149,7 +149,7 @@ func TestLfsAuthenticateRequests(t *testing.T) {
Loading
@@ -149,7 +149,7 @@ func TestLfsAuthenticateRequests(t *testing.T) {
output := &bytes.Buffer{} output := &bytes.Buffer{}
cmd := &Command{ cmd := &Command{
Config: &config.Config{GitlabUrl: url}, Config: &config.Config{GitlabUrl: url},
Args: &commandargs.Shell{GitlabUsername: tc.username, SshArgs: []string{"git-lfs-authenticate", "group/repo", operation}}, Args: &commandargs.Shell{GitlabUsername: tc.username, SSHArgs: []string{"git-lfs-authenticate", "group/repo", operation}},
ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output}, ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output},
} }
Loading
Loading
Loading
@@ -30,7 +30,7 @@ type Command struct {
Loading
@@ -30,7 +30,7 @@ type Command struct {
} }
func (c *Command) Execute(ctx context.Context) (context.Context, error) { func (c *Command) Execute(ctx context.Context) (context.Context, error) {
args := c.Args.SshArgs args := c.Args.SSHArgs
if len(args) != 3 { if len(args) != 3 {
return ctx, disallowedcommand.Error return ctx, disallowedcommand.Error
} }
Loading
Loading
Loading
@@ -1474,7 +1474,7 @@ func setup(t *testing.T, keyID string, repo string, op string) (string, *Command
Loading
@@ -1474,7 +1474,7 @@ func setup(t *testing.T, keyID string, repo string, op string) (string, *Command
cmd := &Command{ cmd := &Command{
Config: &config.Config{GitlabUrl: url, Secret: "very secret"}, Config: &config.Config{GitlabUrl: url, Secret: "very secret"},
Args: &commandargs.Shell{GitlabKeyId: keyID, SshArgs: []string{"git-lfs-transfer", repo, op}}, Args: &commandargs.Shell{GitlabKeyID: keyID, SSHArgs: []string{"git-lfs-transfer", repo, op}},
ReadWriter: &readwriter.ReadWriter{ErrOut: errorSink, Out: outputSink, In: inputSource}, ReadWriter: &readwriter.ReadWriter{ErrOut: errorSink, Out: outputSink, In: inputSource},
} }
pl := pktline.NewPktline(outputSource, inputSink) pl := pktline.NewPktline(outputSource, inputSink)
Loading
Loading
Loading
@@ -62,12 +62,12 @@ func (c *Command) Execute(ctx context.Context) (context.Context, error) {
Loading
@@ -62,12 +62,12 @@ func (c *Command) Execute(ctx context.Context) (context.Context, error) {
} }
func (c *Command) parseTokenArgs() error { func (c *Command) parseTokenArgs() error {
if len(c.Args.SshArgs) < 3 || len(c.Args.SshArgs) > 4 { if len(c.Args.SSHArgs) < 3 || len(c.Args.SSHArgs) > 4 {
return errors.New(usageText) // nolint:stylecheck // usageText is customer facing return errors.New(usageText) // nolint:stylecheck // usageText is customer facing
} }
var rectfiedScopes []string var rectfiedScopes []string
requestedScopes := strings.Split(c.Args.SshArgs[2], ",") requestedScopes := strings.Split(c.Args.SSHArgs[2], ",")
if len(c.Config.PATConfig.AllowedScopes) > 0 { if len(c.Config.PATConfig.AllowedScopes) > 0 {
for _, requestedScope := range requestedScopes { for _, requestedScope := range requestedScopes {
if slices.Contains(c.Config.PATConfig.AllowedScopes, requestedScope) { if slices.Contains(c.Config.PATConfig.AllowedScopes, requestedScope) {
Loading
@@ -78,15 +78,15 @@ func (c *Command) parseTokenArgs() error {
Loading
@@ -78,15 +78,15 @@ func (c *Command) parseTokenArgs() error {
rectfiedScopes = requestedScopes rectfiedScopes = requestedScopes
} }
c.TokenArgs = &tokenArgs{ c.TokenArgs = &tokenArgs{
Name: c.Args.SshArgs[1], Name: c.Args.SSHArgs[1],
Scopes: rectfiedScopes, Scopes: rectfiedScopes,
} }
if len(c.Args.SshArgs) < 4 { if len(c.Args.SSHArgs) < 4 {
c.TokenArgs.ExpiresDate = time.Now().AddDate(0, 0, 30).Format(expiresDateFormat) c.TokenArgs.ExpiresDate = time.Now().AddDate(0, 0, 30).Format(expiresDateFormat)
return nil return nil
} }
rawTTL := c.Args.SshArgs[3] rawTTL := c.Args.SSHArgs[3]
TTL, err := strconv.Atoi(rawTTL) TTL, err := strconv.Atoi(rawTTL)
if err != nil || TTL < 0 { if err != nil || TTL < 0 {
Loading
Loading
Loading
@@ -96,29 +96,29 @@ func TestExecute(t *testing.T) {
Loading
@@ -96,29 +96,29 @@ func TestExecute(t *testing.T) {
{ {
desc: "With too few arguments", desc: "With too few arguments",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken"}, SSHArgs: []string{cmdname, "newtoken"},
}, },
expectedError: usageText, expectedError: usageText,
}, },
{ {
desc: "With too many arguments", desc: "With too many arguments",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "api", "bad_ttl", "toomany"}, SSHArgs: []string{cmdname, "newtoken", "api", "bad_ttl", "toomany"},
}, },
expectedError: usageText, expectedError: usageText,
}, },
{ {
desc: "With a bad ttl_days argument", desc: "With a bad ttl_days argument",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "api", "bad_ttl"}, SSHArgs: []string{cmdname, "newtoken", "api", "bad_ttl"},
}, },
expectedError: "Invalid value for days_ttl: 'bad_ttl'", expectedError: "Invalid value for days_ttl: 'bad_ttl'",
}, },
{ {
desc: "Without a ttl argument", desc: "Without a ttl argument",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "default", GitlabKeyID: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" + expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api,read_repository\n" + "Scopes: read_api,read_repository\n" +
Loading
@@ -127,8 +127,8 @@ func TestExecute(t *testing.T) {
Loading
@@ -127,8 +127,8 @@ func TestExecute(t *testing.T) {
{ {
desc: "With a ttl argument", desc: "With a ttl argument",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "default", GitlabKeyID: "default",
SshArgs: []string{cmdname, "newtoken", "api", "30"}, SSHArgs: []string{cmdname, "newtoken", "api", "30"},
}, },
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" + expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: api\n" + "Scopes: api\n" +
Loading
@@ -137,31 +137,31 @@ func TestExecute(t *testing.T) {
Loading
@@ -137,31 +137,31 @@ func TestExecute(t *testing.T) {
{ {
desc: "With bad response", desc: "With bad response",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "badresponse", GitlabKeyID: "badresponse",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedError: "parsing failed", expectedError: "parsing failed",
}, },
{ {
desc: "when API returns an error", desc: "when API returns an error",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "forbidden", GitlabKeyID: "forbidden",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedError: "Forbidden!", expectedError: "Forbidden!",
}, },
{ {
desc: "When API fails", desc: "When API fails",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "broken", GitlabKeyID: "broken",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedError: "Internal API unreachable", expectedError: "Internal API unreachable",
}, },
{ {
desc: "Without KeyID or User", desc: "Without KeyID or User",
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedError: "who='' is invalid", expectedError: "who='' is invalid",
}, },
Loading
@@ -169,8 +169,8 @@ func TestExecute(t *testing.T) {
Loading
@@ -169,8 +169,8 @@ func TestExecute(t *testing.T) {
desc: "With restricted scopes", desc: "With restricted scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api"}}, PATConfig: config.PATConfig{AllowedScopes: []string{"read_api"}},
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "default", GitlabKeyID: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" + expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api\n" + "Scopes: read_api\n" +
Loading
@@ -180,8 +180,8 @@ func TestExecute(t *testing.T) {
Loading
@@ -180,8 +180,8 @@ func TestExecute(t *testing.T) {
desc: "With unknown configured scopes", desc: "With unknown configured scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}}, //nolint:misspell //testing purpose PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}}, //nolint:misspell //testing purpose
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "default", GitlabKeyID: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"}, SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
}, },
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" + expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: \n" + "Scopes: \n" +
Loading
@@ -191,8 +191,8 @@ func TestExecute(t *testing.T) {
Loading
@@ -191,8 +191,8 @@ func TestExecute(t *testing.T) {
desc: "With unknown requested scopes", desc: "With unknown requested scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_repository"}}, PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_repository"}},
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "default", GitlabKeyID: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"}, //nolint:misspell //testing purpose SSHArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"}, //nolint:misspell //testing purpose
}, },
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" + expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api\n" + "Scopes: read_api\n" +
Loading
@@ -202,8 +202,8 @@ func TestExecute(t *testing.T) {
Loading
@@ -202,8 +202,8 @@ func TestExecute(t *testing.T) {
desc: "With matching unknown requested scopes", desc: "With matching unknown requested scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}}, //nolint:misspell //testing purpose PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}}, //nolint:misspell //testing purpose
arguments: &commandargs.Shell{ arguments: &commandargs.Shell{
GitlabKeyId: "invalidscope", GitlabKeyID: "invalidscope",
SshArgs: []string{cmdname, "newtoken", "read_reposotory"}, //nolint:misspell //testing purpose SSHArgs: []string{cmdname, "newtoken", "read_reposotory"}, //nolint:misspell //testing purpose
}, },
expectedError: "Invalid scope: 'read_reposotory'. Valid scopes are: [\"api\", \"create_runner\", \"k8s_proxy\", \"read_api\", \"read_registry\", \"read_repository\", \"read_user\", \"write_registry\", \"write_repository\"]", expectedError: "Invalid scope: 'read_reposotory'. Valid scopes are: [\"api\", \"create_runner\", \"k8s_proxy\", \"read_api\", \"read_registry\", \"read_repository\", \"read_user\", \"write_registry\", \"write_repository\"]",
}, },
Loading
Loading
Loading
@@ -51,14 +51,14 @@ func TestReceivePack(t *testing.T) {
Loading
@@ -51,14 +51,14 @@ func TestReceivePack(t *testing.T) {
args := &commandargs.Shell{ args := &commandargs.Shell{
CommandType: commandargs.ReceivePack, CommandType: commandargs.ReceivePack,
SshArgs: []string{"git-receive-pack", repo}, SSHArgs: []string{"git-receive-pack", repo},
Env: env, Env: env,
} }
if tc.username != "" { if tc.username != "" {
args.GitlabUsername = tc.username args.GitlabUsername = tc.username
} else { } else {
args.GitlabKeyId = tc.keyId args.GitlabKeyID = tc.keyId
} }
cfg := &config.Config{GitlabUrl: url} cfg := &config.Config{GitlabUrl: url}
Loading
Loading
Loading
@@ -26,7 +26,7 @@ type logData struct{}
Loading
@@ -26,7 +26,7 @@ type logData struct{}
// Execute executes the receive-pack command // Execute executes the receive-pack command
func (c *Command) Execute(ctx context.Context) (context.Context, error) { func (c *Command) Execute(ctx context.Context) (context.Context, error) {
args := c.Args.SshArgs args := c.Args.SSHArgs
if len(args) != 2 { if len(args) != 2 {
return ctx, disallowedcommand.Error return ctx, disallowedcommand.Error
} }
Loading
Loading
Loading
@@ -54,7 +54,7 @@ func setup(t *testing.T, keyID string, requests []testserver.TestRequestHandler)
Loading
@@ -54,7 +54,7 @@ func setup(t *testing.T, keyID string, requests []testserver.TestRequestHandler)
cmd := &Command{ cmd := &Command{
Config: &config.Config{GitlabUrl: url}, Config: &config.Config{GitlabUrl: url},
Args: &commandargs.Shell{GitlabKeyId: keyID, SshArgs: []string{"git-receive-pack", "group/repo"}}, Args: &commandargs.Shell{GitlabKeyID: keyID, SSHArgs: []string{"git-receive-pack", "group/repo"}},
ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output, In: input}, ReadWriter: &readwriter.ReadWriter{ErrOut: output, Out: output, In: input},
} }
Loading
Loading
Loading
@@ -65,7 +65,7 @@ func setup(t *testing.T) (*Command, *bytes.Buffer, *bytes.Buffer) {
Loading
@@ -65,7 +65,7 @@ func setup(t *testing.T) (*Command, *bytes.Buffer, *bytes.Buffer) {
func TestMissingUser(t *testing.T) { func TestMissingUser(t *testing.T) {
cmd, _, _ := setup(t) cmd, _, _ := setup(t)
cmd.Args = &commandargs.Shell{GitlabKeyId: "2"} cmd.Args = &commandargs.Shell{GitlabKeyID: "2"}
_, err := cmd.Verify(context.Background(), action, repo) _, err := cmd.Verify(context.Background(), action, repo)
require.Equal(t, "missing user", err.Error()) require.Equal(t, "missing user", err.Error())
Loading
@@ -74,7 +74,7 @@ func TestMissingUser(t *testing.T) {
Loading
@@ -74,7 +74,7 @@ func TestMissingUser(t *testing.T) {
func TestConsoleMessages(t *testing.T) { func TestConsoleMessages(t *testing.T) {
cmd, errBuf, outBuf := setup(t) cmd, errBuf, outBuf := setup(t)
cmd.Args = &commandargs.Shell{GitlabKeyId: "1"} cmd.Args = &commandargs.Shell{GitlabKeyID: "1"}
cmd.Verify(context.Background(), action, repo) cmd.Verify(context.Background(), action, repo)
require.Equal(t, "remote: \nremote: console\nremote: message\nremote: \n", errBuf.String()) require.Equal(t, "remote: \nremote: console\nremote: message\nremote: \n", errBuf.String())
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment