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 78aa6a5f authored by Ash McKenzie's avatar Ash McKenzie Committed by GitLab
Browse files

Merge branch '789-personalaccesstoken-lint' into 'main'

Lint fixes for personalaccesstoken package

Closes #789

See merge request https://gitlab.com/gitlab-org/gitlab-shell/-/merge_requests/1146



Merged-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Approved-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Reviewed-by: default avatarAsh McKenzie <amckenzie@gitlab.com>
Co-authored-by: default avatarArchish <archishthakkar@gmail.com>
parents c5394a4b 8de7929a
No related branches found
No related tags found
No related merge requests found
// Package personalaccesstoken handles operations related to personal access tokens,
// including parsing arguments, requesting tokens, and formatting responses.
package personalaccesstoken
import (
Loading
Loading
@@ -22,6 +24,7 @@ const (
expiresDateFormat = "2006-01-02"
)
// Command represents a command to manage personal access tokens.
type Command struct {
Config *config.Config
Args *commandargs.Shell
Loading
Loading
@@ -35,6 +38,7 @@ type tokenArgs struct {
ExpiresDate string // Calculated, a TTL is passed from command-line.
}
// Execute processes the command, requests a personal access token, and prints the result.
func (c *Command) Execute(ctx context.Context) (context.Context, error) {
err := c.parseTokenArgs()
if err != nil {
Loading
Loading
@@ -50,16 +54,16 @@ func (c *Command) Execute(ctx context.Context) (context.Context, error) {
return ctx, err
}
fmt.Fprint(c.ReadWriter.Out, "Token: "+response.Token+"\n")
fmt.Fprint(c.ReadWriter.Out, "Scopes: "+strings.Join(response.Scopes, ",")+"\n")
fmt.Fprint(c.ReadWriter.Out, "Expires: "+response.ExpiresAt+"\n")
_, _ = fmt.Fprint(c.ReadWriter.Out, "Token: "+response.Token+"\n")
_, _ = fmt.Fprint(c.ReadWriter.Out, "Scopes: "+strings.Join(response.Scopes, ",")+"\n")
_, _ = fmt.Fprint(c.ReadWriter.Out, "Expires: "+response.ExpiresAt+"\n")
return ctx, nil
}
func (c *Command) parseTokenArgs() error {
if len(c.Args.SshArgs) < 3 || len(c.Args.SshArgs) > 4 {
return errors.New(usageText)
return errors.New(usageText) // nolint:stylecheck // usageText is customer facing
}
var rectfiedScopes []string
Loading
Loading
@@ -86,7 +90,7 @@ func (c *Command) parseTokenArgs() error {
TTL, err := strconv.Atoi(rawTTL)
if err != nil || TTL < 0 {
return fmt.Errorf("Invalid value for days_ttl: '%s'", rawTTL)
return fmt.Errorf("Invalid value for days_ttl: '%s'", rawTTL) //nolint:stylecheck //message is customer facing
}
c.TokenArgs.ExpiresDate = time.Now().AddDate(0, 0, TTL+1).Format(expiresDateFormat)
Loading
Loading
Loading
Loading
@@ -9,6 +9,7 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
Loading
Loading
@@ -28,7 +29,7 @@ func setup(t *testing.T) {
b, err := io.ReadAll(r.Body)
defer r.Body.Close()
require.NoError(t, err)
assert.NoError(t, err)
var requestBody *personalaccesstoken.RequestBody
json.Unmarshal(b, &requestBody)
Loading
Loading
@@ -177,7 +178,7 @@ func TestExecute(t *testing.T) {
},
{
desc: "With unknown configured scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}},
PATConfig: config.PATConfig{AllowedScopes: []string{"read_reposotory"}}, //nolint:misspell //testing purpose
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_repository"},
Loading
Loading
@@ -191,7 +192,7 @@ func TestExecute(t *testing.T) {
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_repository"}},
arguments: &commandargs.Shell{
GitlabKeyId: "default",
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"},
SshArgs: []string{cmdname, "newtoken", "read_api,read_reposotory"}, //nolint:misspell //testing purpose
},
expectedOutput: "Token: YXuxvUgCEmeePY3G1YAa\n" +
"Scopes: read_api\n" +
Loading
Loading
@@ -199,10 +200,10 @@ func TestExecute(t *testing.T) {
},
{
desc: "With matching unknown requested scopes",
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}},
PATConfig: config.PATConfig{AllowedScopes: []string{"read_api", "read_reposotory"}}, //nolint:misspell //testing purpose
arguments: &commandargs.Shell{
GitlabKeyId: "invalidscope",
SshArgs: []string{cmdname, "newtoken", "read_reposotory"},
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
@@ -7,6 +7,7 @@ import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitlab-shell/v14/client"
"gitlab.com/gitlab-org/gitlab-shell/v14/client/testserver"
Loading
Loading
@@ -27,7 +28,7 @@ func initialize(t *testing.T) {
b, err := io.ReadAll(r.Body)
defer r.Body.Close()
require.NoError(t, err)
assert.NoError(t, err)
var requestBody *RequestBody
json.Unmarshal(b, &requestBody)
Loading
Loading
Loading
Loading
@@ -155,19 +155,6 @@ internal/command/lfstransfer/lfstransfer_test.go:1436:5: go-require: do not use
internal/command/lfstransfer/lfstransfer_test.go:1451:5: go-require: do not use require in http handlers (testifylint)
internal/command/lfstransfer/lfstransfer_test.go:1453:5: go-require: do not use require in http handlers (testifylint)
internal/command/lfstransfer/lfstransfer_test.go:1454:5: go-require: do not use require in http handlers (testifylint)
internal/command/personalaccesstoken/personalaccesstoken.go:1:1: package-comments: should have a package comment (revive)
internal/command/personalaccesstoken/personalaccesstoken.go:25:6: exported: exported type Command should have comment or be unexported (revive)
internal/command/personalaccesstoken/personalaccesstoken.go:38:1: exported: exported method Command.Execute should have comment or be unexported (revive)
internal/command/personalaccesstoken/personalaccesstoken.go:53:12: Error return value of `fmt.Fprint` is not checked (errcheck)
internal/command/personalaccesstoken/personalaccesstoken.go:54:12: Error return value of `fmt.Fprint` is not checked (errcheck)
internal/command/personalaccesstoken/personalaccesstoken.go:55:12: Error return value of `fmt.Fprint` is not checked (errcheck)
internal/command/personalaccesstoken/personalaccesstoken.go:62:10: ST1005: error strings should not be capitalized (stylecheck)
internal/command/personalaccesstoken/personalaccesstoken.go:89:10: ST1005: error strings should not be capitalized (stylecheck)
internal/command/personalaccesstoken/personalaccesstoken_test.go:31:5: go-require: do not use require in http handlers (testifylint)
internal/command/personalaccesstoken/personalaccesstoken_test.go:180:62: `reposotory` is a misspelling of `repository` (misspell)
internal/command/personalaccesstoken/personalaccesstoken_test.go:194:63: `reposotory` is a misspelling of `repository` (misspell)
internal/command/personalaccesstoken/personalaccesstoken_test.go:202:74: `reposotory` is a misspelling of `repository` (misspell)
internal/command/personalaccesstoken/personalaccesstoken_test.go:205:54: `reposotory` is a misspelling of `repository` (misspell)
internal/command/readwriter/readwriter.go:1:1: package-comments: should have a package comment (revive)
internal/command/readwriter/readwriter.go:7:6: exported: exported type ReadWriter should have comment or be unexported (revive)
internal/command/receivepack/gitalycall_test.go:24:4: S1038: should use t.Logf(...) instead of t.Log(fmt.Sprintf(...)) (gosimple)
Loading
Loading
@@ -243,7 +230,6 @@ internal/gitlabnet/client.go:27:1: exported: exported function ParseJSON should
internal/gitlabnet/client.go:35:1: exported: exported function ParseIP should have comment or be unexported (revive)
internal/gitlabnet/healthcheck/client_test.go:19:41: unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
internal/gitlabnet/lfstransfer/client.go:137: internal/gitlabnet/lfstransfer/client.go:137: Line contains TODO/BUG/FIXME/NOTE/OPTIMIZE/HACK: "FIXME: This causes tests to fail" (godox)
internal/gitlabnet/personalaccesstoken/client_test.go:30:5: go-require: do not use require in http handlers (testifylint)
internal/sshd/server_config_test.go:5:2: SA1019: "crypto/dsa" has been deprecated since Go 1.16 because it shouldn't be used: DSA is a legacy algorithm, and modern alternatives such as Ed25519 (implemented by package crypto/ed25519) should be used instead. Keys with 1024-bit moduli (L1024N160 parameters) are cryptographically weak, while bigger keys are not widely supported. Note that FIPS 186-5 no longer approves DSA for signature generation. (staticcheck)
internal/sshd/sshd.go:268:6: func `extractDataFromContext` is unused (unused)
internal/testhelper/requesthandlers/requesthandlers.go:25:5: go-require: do not use require in http handlers (testifylint)
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