Skip to content

Commit a703b51

Browse files
Archish27GitLab
authored and
GitLab
committed
Lint fixes for commandargs packages
1 parent 55cd93f commit a703b51

40 files changed

+157
-161
lines changed

cmd/gitlab-shell-authorized-principals-check/command/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestParseSuccess(t *testing.T) {
5858
desc: "It parses authorized-principals command",
5959
executable: &executable.Executable{Name: executable.AuthorizedPrincipalsCheck},
6060
arguments: []string{"key", "principal-1", "principal-2"},
61-
expectedArgs: &commandargs.AuthorizedPrincipals{Arguments: []string{"key", "principal-1", "principal-2"}, KeyId: "key", Principals: []string{"principal-1", "principal-2"}},
61+
expectedArgs: &commandargs.AuthorizedPrincipals{Arguments: []string{"key", "principal-1", "principal-2"}, KeyID: "key", Principals: []string{"principal-1", "principal-2"}},
6262
},
6363
}
6464

cmd/gitlab-shell/command/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewWithKey(gitlabKeyID string, env sshenv.Env, config *config.Config, readW
4141
return nil, err
4242
}
4343

44-
args.GitlabKeyId = gitlabKeyID
44+
args.GitlabKeyID = gitlabKeyID
4545
if cmd := Build(args, config, readWriter); cmd != nil {
4646
return cmd, nil
4747
}

cmd/gitlab-shell/command/command_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,112 +256,112 @@ func TestParseSuccess(t *testing.T) {
256256
executable: &executable.Executable{Name: executable.GitlabShell},
257257
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
258258
arguments: []string{},
259-
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{}, CommandType: commandargs.Discover, Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
259+
expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{}, CommandType: commandargs.Discover, Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
260260
},
261261
{
262262
desc: "It finds the key id in any passed arguments",
263263
executable: &executable.Executable{Name: executable.GitlabShell},
264264
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
265265
arguments: []string{"hello", "key-123"},
266-
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "key-123"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabKeyId: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
266+
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "key-123"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabKeyID: "123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
267267
},
268268
{
269269
desc: "It finds the key id only if the argument is of <key-id> format",
270270
executable: &executable.Executable{Name: executable.GitlabShell},
271271
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
272272
arguments: []string{"hello", "username-key-123"},
273-
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-key-123"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
273+
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-key-123"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "key-123", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
274274
},
275275
{
276276
desc: "It finds the key id if the key is listed as the last argument",
277277
executable: &executable.Executable{Name: executable.GitlabShell},
278278
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
279279
arguments: []string{"hello", "gitlab-shell -c key-123"},
280-
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"}},
280+
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"}},
281281
},
282282
{
283283
desc: "It finds the username if the username is listed as the last argument",
284284
executable: &executable.Executable{Name: executable.GitlabShell},
285285
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
286286
arguments: []string{"hello", "gitlab-shell -c username-jane-doe"},
287-
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"}},
287+
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"}},
288288
},
289289
{
290290
desc: "It finds the key id only if the last argument is of <key-id> format",
291291
executable: &executable.Executable{Name: executable.GitlabShell},
292292
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
293293
arguments: []string{"hello", "gitlab-shell -c username-key-123"},
294-
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"}},
294+
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"}},
295295
},
296296
{
297297
desc: "It finds the username in any passed arguments",
298298
executable: &executable.Executable{Name: executable.GitlabShell},
299299
env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"},
300300
arguments: []string{"hello", "username-jane-doe"},
301-
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-jane-doe"}, SshArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "jane-doe", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
301+
expectedArgs: &commandargs.Shell{Arguments: []string{"hello", "username-jane-doe"}, SSHArgs: []string{}, CommandType: commandargs.Discover, GitlabUsername: "jane-doe", Env: sshenv.Env{IsSSHConnection: true, RemoteAddr: "1"}},
302302
},
303303
{
304304
desc: "It parses 2fa_recovery_codes command",
305305
executable: &executable.Executable{Name: executable.GitlabShell},
306306
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"},
307307
arguments: []string{},
308-
expectedArgs: &commandargs.Shell{Arguments: []string{}, SshArgs: []string{"2fa_recovery_codes"}, CommandType: commandargs.TwoFactorRecover, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"}},
308+
expectedArgs: &commandargs.Shell{Arguments: []string{}, SSHArgs: []string{"2fa_recovery_codes"}, CommandType: commandargs.TwoFactorRecover, Env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "2fa_recovery_codes"}},
309309
},
310310
{
311311
desc: "It parses git-receive-pack command",
312312
executable: &executable.Executable{Name: executable.GitlabShell},
313313
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack group/repo"},
314314
arguments: []string{},
315-
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"}},
315+
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"}},
316316
},
317317
{
318318
desc: "It parses git-receive-pack command and a project with single quotes",
319319
executable: &executable.Executable{Name: executable.GitlabShell},
320320
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-receive-pack 'group/repo'"},
321321
arguments: []string{},
322-
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'"}},
322+
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'"}},
323323
},
324324
{
325325
desc: `It parses "git receive-pack" command`,
326326
executable: &executable.Executable{Name: executable.GitlabShell},
327327
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack "group/repo"`},
328328
arguments: []string{},
329-
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"`}},
329+
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"`}},
330330
},
331331
{
332332
desc: `It parses a command followed by control characters`,
333333
executable: &executable.Executable{Name: executable.GitlabShell},
334334
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git-receive-pack group/repo; any command`},
335335
arguments: []string{},
336-
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`}},
336+
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`}},
337337
},
338338
{
339339
desc: "It parses git-upload-pack command",
340340
executable: &executable.Executable{Name: executable.GitlabShell},
341341
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: `git upload-pack "group/repo"`},
342342
arguments: []string{},
343-
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"`}},
343+
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"`}},
344344
},
345345
{
346346
desc: "It parses git-upload-archive command",
347347
executable: &executable.Executable{Name: executable.GitlabShell},
348348
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-upload-archive 'group/repo'"},
349349
arguments: []string{},
350-
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'"}},
350+
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'"}},
351351
},
352352
{
353353
desc: "It parses git-lfs-authenticate command",
354354
executable: &executable.Executable{Name: executable.GitlabShell},
355355
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-authenticate 'group/repo' download"},
356356
arguments: []string{},
357-
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"}},
357+
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"}},
358358
},
359359
{
360360
desc: "It parses git-lfs-transfer command",
361361
executable: &executable.Executable{Name: executable.GitlabShell},
362362
env: sshenv.Env{IsSSHConnection: true, OriginalCommand: "git-lfs-transfer 'group/repo' download"},
363363
arguments: []string{},
364-
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"}},
364+
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"}},
365365
},
366366
}
367367

@@ -427,7 +427,7 @@ func TestNewWithUsername(t *testing.T) {
427427
Args: &commandargs.Shell{
428428
CommandType: commandargs.ReceivePack,
429429
GitlabUsername: "username",
430-
SshArgs: []string{"git-receive-pack", "group/repo"},
430+
SSHArgs: []string{"git-receive-pack", "group/repo"},
431431
Env: sshenv.Env{
432432
IsSSHConnection: true,
433433
OriginalCommand: "git-receive-pack 'group/repo'",
@@ -442,7 +442,7 @@ func TestNewWithUsername(t *testing.T) {
442442
Args: &commandargs.Shell{
443443
CommandType: commandargs.TwoFactorRecover,
444444
GitlabUsername: "username",
445-
SshArgs: []string{"2fa_recovery_codes"},
445+
SSHArgs: []string{"2fa_recovery_codes"},
446446
Env: sshenv.Env{
447447
IsSSHConnection: true,
448448
OriginalCommand: "2fa_recovery_codes",
@@ -463,7 +463,7 @@ func TestNewWithUsername(t *testing.T) {
463463
Args: &commandargs.Shell{
464464
CommandType: commandargs.ReceivePack,
465465
GitlabUsername: "username",
466-
SshArgs: []string{"git-receive-pack", "group/repo"},
466+
SSHArgs: []string{"git-receive-pack", "group/repo"},
467467
Env: sshenv.Env{
468468
IsSSHConnection: true,
469469
OriginalCommand: "git-receive-pack 'group/repo'",

internal/command/authorizedprincipals/authorized_principals.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *Command) printPrincipalLines() error {
4040
}
4141

4242
func (c *Command) printPrincipalLine(principal string) error {
43-
principalKeyLine, err := keyline.NewPrincipalKeyLine(c.Args.KeyId, principal, c.Config)
43+
principalKeyLine, err := keyline.NewPrincipalKeyLine(c.Args.KeyID, principal, c.Config)
4444
if err != nil {
4545
return err
4646
}

internal/command/authorizedprincipals/authorized_principals_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ func TestExecute(t *testing.T) {
2222
}{
2323
{
2424
desc: "With single principal",
25-
arguments: &commandargs.AuthorizedPrincipals{KeyId: "key", Principals: []string{"principal"}},
25+
arguments: &commandargs.AuthorizedPrincipals{KeyID: "key", Principals: []string{"principal"}},
2626
expectedOutput: "command=\"/tmp/bin/gitlab-shell username-key\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty principal\n",
2727
},
2828
{
2929
desc: "With multiple principals",
30-
arguments: &commandargs.AuthorizedPrincipals{KeyId: "key", Principals: []string{"principal-1", "principal-2"}},
30+
arguments: &commandargs.AuthorizedPrincipals{KeyID: "key", Principals: []string{"principal-1", "principal-2"}},
3131
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",
3232
},
3333
}

internal/command/commandargs/authorized_keys.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
// Package commandargs defines structures and methods for handling command-line arguments
2+
// related to authorized key checks in the GitLab shell.
13
package commandargs
24

35
import (
46
"errors"
57
"fmt"
68
)
79

10+
// AuthorizedKeys holds the arguments and user information for key authorization checks.
811
type AuthorizedKeys struct {
912
Arguments []string
1013
ExpectedUser string
1114
ActualUser string
1215
Key string
1316
}
1417

18+
// Parse parses and validates the arguments, setting ExpectedUser, ActualUser, and Key.
1519
func (ak *AuthorizedKeys) Parse() error {
1620
if err := ak.validate(); err != nil {
1721
return err
@@ -24,6 +28,7 @@ func (ak *AuthorizedKeys) Parse() error {
2428
return nil
2529
}
2630

31+
// GetArguments returns the list of command-line arguments.
2732
func (ak *AuthorizedKeys) GetArguments() []string {
2833
return ak.Arguments
2934
}
@@ -32,7 +37,7 @@ func (ak *AuthorizedKeys) validate() error {
3237
argsSize := len(ak.Arguments)
3338

3439
if argsSize != 3 {
35-
return errors.New(fmt.Sprintf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-keys-check <expected-username> <actual-username> <key>", argsSize))
40+
return fmt.Errorf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-keys-check <expected-username> <actual-username> <key>", argsSize)
3641
}
3742

3843
expectedUsername := ak.Arguments[0]

internal/command/commandargs/authorized_principals.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1+
// Package commandargs provides functionality for handling and parsing command-line arguments
2+
// related to authorized principals for GitLab shell commands.
13
package commandargs
24

35
import (
46
"errors"
57
"fmt"
68
)
79

10+
// AuthorizedPrincipals holds the arguments for checking authorized principals and the key ID.
811
type AuthorizedPrincipals struct {
912
Arguments []string
10-
KeyId string
13+
KeyID string
1114
Principals []string
1215
}
1316

17+
// Parse validates and extracts the key ID and principals from the Arguments slice.
18+
// Returns an error if validation fails.
1419
func (ap *AuthorizedPrincipals) Parse() error {
1520
if err := ap.validate(); err != nil {
1621
return err
1722
}
1823

19-
ap.KeyId = ap.Arguments[0]
24+
ap.KeyID = ap.Arguments[0]
2025
ap.Principals = ap.Arguments[1:]
2126

2227
return nil
2328
}
2429

30+
// GetArguments returns the list of command-line arguments provided.
2531
func (ap *AuthorizedPrincipals) GetArguments() []string {
2632
return ap.Arguments
2733
}
@@ -30,13 +36,13 @@ func (ap *AuthorizedPrincipals) validate() error {
3036
argsSize := len(ap.Arguments)
3137

3238
if argsSize < 2 {
33-
return errors.New(fmt.Sprintf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-principals-check <key-id> <principal1> [<principal2>...]", argsSize))
39+
return fmt.Errorf("# Insufficient arguments. %d. Usage\n#\tgitlab-shell-authorized-principals-check <key-id> <principal1> [<principal2>...]", argsSize)
3440
}
3541

36-
keyId := ap.Arguments[0]
42+
keyID := ap.Arguments[0]
3743
principals := ap.Arguments[1:]
3844

39-
if keyId == "" {
45+
if keyID == "" {
4046
return errors.New("# No key_id provided")
4147
}
4248

internal/command/commandargs/command_args.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
// Package commandargs defines types and interfaces for handling command-line arguments
2+
// in GitLab shell commands.
13
package commandargs
24

5+
// CommandType represents a type of command identified by a string.
36
type CommandType string
47

8+
// CommandArgs is an interface for parsing and accessing command-line arguments.
59
type CommandArgs interface {
610
Parse() error
711
GetArguments() []string

0 commit comments

Comments
 (0)