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
Loading
@@ -58,7 +58,7 @@ func TestParseSuccess(t *testing.T) {
desc: "It parses authorized-principals command",
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
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
Loading
@@ -41,7 +41,7 @@ func NewWithKey(gitlabKeyID string, env sshenv.Env, config *config.Config, readW
return nil, err
}
args.GitlabKeyId = gitlabKeyID
args.GitlabKeyID = gitlabKeyID
if cmd := Build(args, config, readWriter); cmd != nil {
return cmd, nil
}
Loading
Loading
Loading
Loading
@@ -256,112 +256,112 @@ func TestParseSuccess(t *testing.T) {
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack group/repo"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack 'group/repo'"},
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`,
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack "group/repo"`},
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`,
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack group/repo; any command`},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git upload-pack "group/repo"`},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-upload-archive 'group/repo'"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-authenticate 'group/repo' download"},
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",
executable: &executable.Executable{Name: executable.GitlabShell},
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-transfer 'group/repo' download"},
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
Loading
@@ -427,7 +427,7 @@ func TestNewWithUsername(t *testing.T) {
Args: &commandargs.Shell{
CommandType: commandargs.ReceivePack,
GitlabUsername: "username",
SshArgs: []string{"git-receive-pack", "group/repo"},
SSHArgs: []string{"git-receive-pack", "group/repo"},
Env: sshenv.Env{
IsSSHConnection: true,
OriginalCommand: "git-receive-pack 'group/repo'",
Loading
Loading
@@ -442,7 +442,7 @@ func TestNewWithUsername(t *testing.T) {
Args: &commandargs.Shell{
CommandType: commandargs.TwoFactorRecover,
GitlabUsername: "username",
SshArgs: []string{"2fa_recovery_codes"},
SSHArgs: []string{"2fa_recovery_codes"},
Env: sshenv.Env{
IsSSHConnection: true,
OriginalCommand: "2fa_recovery_codes",
Loading
Loading
@@ -463,7 +463,7 @@ func TestNewWithUsername(t *testing.T) {
Args: &commandargs.Shell{
CommandType: commandargs.ReceivePack,
GitlabUsername: "username",
SshArgs: []string{"git-receive-pack", "group/repo"},
SSHArgs: []string{"git-receive-pack", "group/repo"},
Env: sshenv.Env{
IsSSHConnection: true,
OriginalCommand: "git-receive-pack 'group/repo'",
Loading
Loading
Loading
Loading
@@ -40,7 +40,7 @@ func (c *Command) printPrincipalLines() 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 {
return err
}
Loading
Loading
Loading
Loading
@@ -22,12 +22,12 @@ func TestExecute(t *testing.T) {
}{
{
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",
},
{
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",
},
}
Loading
Loading
// Package commandargs defines structures and methods for handling command-line arguments
// related to authorized key checks in the GitLab shell.
package commandargs
import (
Loading
Loading
@@ -5,6 +7,7 @@ import (
"fmt"
)
// AuthorizedKeys holds the arguments and user information for key authorization checks.
type AuthorizedKeys struct {
Arguments []string
ExpectedUser string
Loading
Loading
@@ -12,6 +15,7 @@ type AuthorizedKeys struct {
Key string
}
// Parse parses and validates the arguments, setting ExpectedUser, ActualUser, and Key.
func (ak *AuthorizedKeys) Parse() error {
if err := ak.validate(); err != nil {
return err
Loading
Loading
@@ -24,6 +28,7 @@ func (ak *AuthorizedKeys) Parse() error {
return nil
}
// GetArguments returns the list of command-line arguments.
func (ak *AuthorizedKeys) GetArguments() []string {
return ak.Arguments
}
Loading
Loading
@@ -32,7 +37,7 @@ func (ak *AuthorizedKeys) validate() error {
argsSize := len(ak.Arguments)
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]
Loading
Loading
// Package commandargs provides functionality for handling and parsing command-line arguments
// related to authorized principals for GitLab shell commands.
package commandargs
import (
Loading
Loading
@@ -5,23 +7,27 @@ import (
"fmt"
)
// AuthorizedPrincipals holds the arguments for checking authorized principals and the key ID.
type AuthorizedPrincipals struct {
Arguments []string
KeyId string
KeyID 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 {
if err := ap.validate(); err != nil {
return err
}
ap.KeyId = ap.Arguments[0]
ap.KeyID = ap.Arguments[0]
ap.Principals = ap.Arguments[1:]
return nil
}
// GetArguments returns the list of command-line arguments provided.
func (ap *AuthorizedPrincipals) GetArguments() []string {
return ap.Arguments
}
Loading
Loading
@@ -30,13 +36,13 @@ func (ap *AuthorizedPrincipals) validate() error {
argsSize := len(ap.Arguments)
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:]
if keyId == "" {
if keyID == "" {
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
// CommandType represents a type of command identified by a string.
type CommandType string
// CommandArgs is an interface for parsing and accessing command-line arguments.
type CommandArgs interface {
Parse() error
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
import (
Loading
Loading
@@ -9,6 +11,7 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/v14/internal/sshenv"
)
// Define supported command types
const (
Discover CommandType = "discover"
TwoFactorRecover CommandType = "2fa_recovery_codes"
Loading
Loading
@@ -21,23 +24,27 @@ const (
PersonalAccessToken CommandType = "personal_access_token"
)
// Regular expressions for parsing key IDs and usernames from arguments
var (
whoKeyRegex = regexp.MustCompile(`\Akey-(?P<keyid>\d+)\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}
)
// Shell represents a parsed shell command with its arguments and related information.
type Shell struct {
Arguments []string
GitlabUsername string
GitlabKeyId string
GitlabKeyID string
GitlabKrb5Principal string
SshArgs []string
SSHArgs []string
CommandType CommandType
Env sshenv.Env
}
// Parse validates and parses the command-line arguments and SSH environment.
func (s *Shell) Parse() error {
if err := s.validate(); err != nil {
return err
Loading
Loading
@@ -48,17 +55,18 @@ func (s *Shell) Parse() error {
return nil
}
// GetArguments returns the list of command-line arguments.
func (s *Shell) GetArguments() []string {
return s.Arguments
}
func (s *Shell) validate() error {
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 {
return fmt.Errorf("Invalid SSH command: %w", err)
return fmt.Errorf("Invalid SSH command: %w", err) //nolint:stylecheck //message is customer facing
}
return nil
Loading
Loading
@@ -66,8 +74,8 @@ func (s *Shell) validate() error {
func (s *Shell) parseWho() {
for _, argument := range s.Arguments {
if keyId := tryParseKeyId(argument); keyId != "" {
s.GitlabKeyId = keyId
if keyID := tryParseKeyID(argument); keyID != "" {
s.GitlabKeyID = keyID
break
}
Loading
Loading
@@ -95,7 +103,7 @@ func tryParse(r *regexp.Regexp, argument string) string {
return ""
}
func tryParseKeyId(argument string) string {
func tryParseKeyID(argument string) string {
return tryParse(whoKeyRegex, argument)
}
Loading
Loading
@@ -103,6 +111,7 @@ func tryParseUsername(argument string) string {
return tryParse(whoUsernameRegex, argument)
}
// ParseCommand parses the command string into a slice of arguments.
func (s *Shell) ParseCommand(commandString string) error {
args, err := shellwords.Parse(commandString)
if err != nil {
Loading
Loading
@@ -117,7 +126,7 @@ func (s *Shell) ParseCommand(commandString string) error {
args = append([]string{command}, commandArgs...)
}
s.SshArgs = args
s.SSHArgs = args
s.defineCommandType()
Loading
Loading
@@ -125,9 +134,9 @@ func (s *Shell) ParseCommand(commandString string) error {
}
func (s *Shell) defineCommandType() {
if len(s.SshArgs) == 0 {
if len(s.SSHArgs) == 0 {
s.CommandType = Discover
} else {
s.CommandType = CommandType(s.SshArgs[0])
s.CommandType = CommandType(s.SSHArgs[0])
}
}
Loading
Loading
@@ -59,12 +59,12 @@ func TestExecute(t *testing.T) {
},
{
desc: "With a known key id",
arguments: &commandargs.Shell{GitlabKeyId: "1"},
arguments: &commandargs.Shell{GitlabKeyID: "1"},
expectedUsername: "@alex-doe",
},
{
desc: "With an unknown key",
arguments: &commandargs.Shell{GitlabKeyId: "-1"},
arguments: &commandargs.Shell{GitlabKeyID: "-1"},
expectedUsername: "Anonymous",
},
{
Loading
Loading
Loading
Loading
@@ -46,7 +46,7 @@ type logInfo struct{}
// Execute executes the LFS authentication command
func (c *Command) Execute(ctx context.Context) (context.Context, error) {
args := c.Args.SshArgs
args := c.Args.SSHArgs
if len(args) < 3 {
return ctx, disallowedcommand.Error
}
Loading
Loading
Loading
Loading
@@ -37,12 +37,12 @@ func TestFailedRequests(t *testing.T) {
},
{
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",
},
{
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",
},
}
Loading
Loading
@@ -149,7 +149,7 @@ func TestLfsAuthenticateRequests(t *testing.T) {
output := &bytes.Buffer{}
cmd := &Command{
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},
}
Loading
Loading
Loading
Loading
@@ -30,7 +30,7 @@ type Command struct {
}
func (c *Command) Execute(ctx context.Context) (context.Context, error) {
args := c.Args.SshArgs
args := c.Args.SSHArgs
if len(args) != 3 {
return ctx, disallowedcommand.Error
}
Loading
Loading
Loading
Loading
@@ -1474,7 +1474,7 @@ func setup(t *testing.T, keyID string, repo string, op string) (string, *Command
cmd := &Command{
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},
}
pl := pktline.NewPktline(outputSource, inputSink)
Loading
Loading
Loading
Loading
@@ -62,12 +62,12 @@ func (c *Command) Execute(ctx context.Context) (context.Context, 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
}
var rectfiedScopes []string
requestedScopes := strings.Split(c.Args.SshArgs[2], ",")
requestedScopes := strings.Split(c.Args.SSHArgs[2], ",")
if len(c.Config.PATConfig.AllowedScopes) > 0 {
for _, requestedScope := range requestedScopes {
if slices.Contains(c.Config.PATConfig.AllowedScopes, requestedScope) {
Loading
Loading
@@ -78,15 +78,15 @@ func (c *Command) parseTokenArgs() error {
rectfiedScopes = requestedScopes
}
c.TokenArgs = &tokenArgs{
Name: c.Args.SshArgs[1],
Name: c.Args.SSHArgs[1],
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)
return nil
}
rawTTL := c.Args.SshArgs[3]
rawTTL := c.Args.SSHArgs[3]
TTL, err := strconv.Atoi(rawTTL)
if err != nil || TTL < 0 {
Loading
Loading
Loading
Loading
@@ -96,29 +96,29 @@ func TestExecute(t *testing.T) {
{
desc: "With too few arguments",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken"},
SSHArgs: []string{cmdname, "newtoken"},
},
expectedError: usageText,
},
{
desc: "With too many arguments",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "api", "bad_ttl", "toomany"},
SSHArgs: []string{cmdname, "newtoken", "api", "bad_ttl", "toomany"},
},
expectedError: usageText,
},
{
desc: "With a bad ttl_days argument",
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'",
},
{
desc: "Without a ttl argument",
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
GitlabKeyID: "default",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api,read_repository\n" +
Loading
Loading
@@ -127,8 +127,8 @@ func TestExecute(t *testing.T) {
{
desc: "With a ttl argument",
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "api", "30"},
GitlabKeyID: "default",
SSHArgs: []string{cmdname, "newtoken", "api", "30"},
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: api\n" +
Loading
Loading
@@ -137,31 +137,31 @@ func TestExecute(t *testing.T) {
{
desc: "With bad response",
arguments: &commandargs.Shell{
GitlabKeyId: "badresponse",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
GitlabKeyID: "badresponse",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "parsing failed",
},
{
desc: "when API returns an error",
arguments: &commandargs.Shell{
GitlabKeyId: "forbidden",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
GitlabKeyID: "forbidden",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "Forbidden!",
},
{
desc: "When API fails",
arguments: &commandargs.Shell{
GitlabKeyId: "broken",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
GitlabKeyID: "broken",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "Internal API unreachable",
},
{
desc: "Without KeyID or User",
arguments: &commandargs.Shell{
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedError: "who='' is invalid",
},
Loading
Loading
@@ -169,8 +169,8 @@ func TestExecute(t *testing.T) {
desc: "With restricted scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api"}},
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
GitlabKeyID: "default",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api\n" +
Loading
Loading
@@ -180,8 +180,8 @@ func TestExecute(t *testing.T) {
desc: "With unknown configured scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}}, //nolint:misspell //testing purpose
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
GitlabKeyID: "default",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: \n" +
Loading
Loading
@@ -191,8 +191,8 @@ func TestExecute(t *testing.T) {
desc: "With unknown requested scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_repository"}},
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"}, //nolint:misspell //testing purpose
GitlabKeyID: "default",
SSHArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"}, //nolint:misspell //testing purpose
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api\n" +
Loading
Loading
@@ -202,8 +202,8 @@ func TestExecute(t *testing.T) {
desc: "With matching unknown requested scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}}, //nolint:misspell //testing purpose
arguments: &commandargs.Shell{
GitlabKeyId: "invalidscope",
SshArgs: []string{cmdname, "newtoken", "read_reposotory"}, //nolint:misspell //testing purpose
GitlabKeyID: "invalidscope",
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\"]",
},
Loading
Loading
Loading
Loading
@@ -51,14 +51,14 @@ func TestReceivePack(t *testing.T) {
args := &commandargs.Shell{
CommandType: commandargs.ReceivePack,
SshArgs: []string{"git-receive-pack", repo},
SSHArgs: []string{"git-receive-pack", repo},
Env: env,
}
if tc.username != "" {
args.GitlabUsername = tc.username
} else {
args.GitlabKeyId = tc.keyId
args.GitlabKeyID = tc.keyId
}
cfg := &config.Config{GitlabUrl: url}
Loading
Loading
Loading
Loading
@@ -26,7 +26,7 @@ type logData struct{}
// Execute executes the receive-pack command
func (c *Command) Execute(ctx context.Context) (context.Context, error) {
args := c.Args.SshArgs
args := c.Args.SSHArgs
if len(args) != 2 {
return ctx, disallowedcommand.Error
}
Loading
Loading
Loading
Loading
@@ -54,7 +54,7 @@ func setup(t *testing.T, keyID string, requests []testserver.TestRequestHandler)
cmd := &Command{
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},
}
Loading
Loading
Loading
Loading
@@ -65,7 +65,7 @@ func setup(t *testing.T) (*Command, *bytes.Buffer, *bytes.Buffer) {
func TestMissingUser(t *testing.T) {
cmd, _, _ := setup(t)
cmd.Args = &commandargs.Shell{GitlabKeyId: "2"}
cmd.Args = &commandargs.Shell{GitlabKeyID: "2"}
_, err := cmd.Verify(context.Background(), action, repo)
require.Equal(t, "missing user", err.Error())
Loading
Loading
@@ -74,7 +74,7 @@ func TestMissingUser(t *testing.T) {
func TestConsoleMessages(t *testing.T) {
cmd, errBuf, outBuf := setup(t)
cmd.Args = &commandargs.Shell{GitlabKeyId: "1"}
cmd.Args = &commandargs.Shell{GitlabKeyID: "1"}
cmd.Verify(context.Background(), action, repo)
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