Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

golangci-lint: update to latest version & related tweaks #5737

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ issues:
- testdata$
- test/mock
exclude-files:
- ".*\\.pb\\.go"
- ".*\\.pb\\.go$"
exclude-rules:
- linters: [ staticcheck ]
path: ".*_test\\.go$"
text: "Subjects has been deprecated since Go 1\\.18.*Subjects will not include the system roots"
- linters: [ gosec ]
path: "(.*_test\\.go$)|(^test/.*)"
text: "integer overflow conversion"

linters:
enable:
Expand Down
2 changes: 1 addition & 1 deletion .spire-tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
golangci_lint v1.60.1
golangci_lint v1.63.4
markdown_lint v0.37.0
protoc 24.4
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ endif
lint: lint-code lint-md

lint-code: $(golangci_lint_bin)
$(E)PATH="$(go_bin_dir):$(PATH)" GOLANGCI_LINT_CACHE="$(golangci_lint_cache)" $(golangci_lint_bin) run ./...
$(E)PATH="$(go_bin_dir):$(PATH)" GOLANGCI_LINT_CACHE="$(golangci_lint_cache)" $(golangci_lint_bin) run --max-issues-per-linter=0 --max-same-issues=0 ./...

lint-md:
$(E)docker run --rm -v "$(DIR):/workdir" $(markdown_lint_image) "**/*.md"
Expand Down
26 changes: 19 additions & 7 deletions cmd/spire-server/cli/entry/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"errors"
"flag"
"fmt"

"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/idutil"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -21,7 +23,7 @@ func NewCreateCommand() cli.Command {
}

func newCreateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &createCommand{env: env})
return serverutil.AdaptCommand(env, &createCommand{env: env})
}

type createCommand struct {
Expand Down Expand Up @@ -104,7 +106,7 @@ func (c *createCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintCreate)
}

func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -175,6 +177,16 @@ func (c *createCommand) parseConfig() ([]*types.Entry, error) {
return nil, err
}

x509SvidTTL, err := util.CheckedCast[int32](c.x509SVIDTTL)
if err != nil {
return nil, fmt.Errorf("X509 SVID TTL: %w", err)
}

jwtSvidTTL, err := util.CheckedCast[int32](c.jwtSVIDTTL)
if err != nil {
return nil, fmt.Errorf("JWT SVID TTL: %w", err)
}

e := &types.Entry{
Id: c.entryID,
ParentId: parentID,
Expand All @@ -183,14 +195,14 @@ func (c *createCommand) parseConfig() ([]*types.Entry, error) {
ExpiresAt: c.entryExpiry,
DnsNames: c.dnsNames,
StoreSvid: c.storeSVID,
X509SvidTtl: int32(c.x509SVIDTTL),
JwtSvidTtl: int32(c.jwtSVIDTTL),
X509SvidTtl: x509SvidTTL,
JwtSvidTtl: jwtSvidTTL,
Hint: c.hint,
}

selectors := []*types.Selector{}
for _, s := range c.selectors {
cs, err := util.ParseSelector(s)
cs, err := serverutil.ParseSelector(s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -254,7 +266,7 @@ func prettyPrintCreate(env *commoncli.Env, results ...any) error {

for _, r := range failed {
env.ErrPrintf("Failed to create the following entry (code: %s, msg: %q):\n",
codes.Code(r.Status.Code),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printEntry(r.Entry, env.ErrPrintf)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/spire-server/cli/entry/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (

"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -22,7 +23,7 @@ func NewDeleteCommand() cli.Command {
}

func newDeleteCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &deleteCommand{env: env})
return serverutil.AdaptCommand(env, &deleteCommand{env: env})
}

type deleteCommand struct {
Expand Down Expand Up @@ -70,7 +71,7 @@ func parseEntryDeleteJSON(path string) ([]string, error) {
return batchDeleteEntryRequest.Ids, nil
}

func (c *deleteCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *deleteCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -135,7 +136,7 @@ func (c *deleteCommand) prettyPrintDelete(env *commoncli.Env, results ...any) er
for _, result := range failed {
env.ErrPrintf("Failed to delete entry with ID %s (code: %s, msg: %q)\n",
result.Id,
codes.Code(result.Status.Code),
util.MustCast[codes.Code](result.Status.Code),
result.Status.Message)
}

Expand Down
26 changes: 19 additions & 7 deletions cmd/spire-server/cli/entry/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"context"
"errors"
"flag"
"fmt"

"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -20,7 +22,7 @@ func NewUpdateCommand() cli.Command {
}

func newUpdateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &updateCommand{env: env})
return serverutil.AdaptCommand(env, &updateCommand{env: env})
}

type updateCommand struct {
Expand Down Expand Up @@ -99,7 +101,7 @@ func (c *updateCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintUpdate)
}

func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
Expand Down Expand Up @@ -169,21 +171,31 @@ func (c *updateCommand) parseConfig() ([]*types.Entry, error) {
return nil, err
}

x509SvidTTL, err := util.CheckedCast[int32](c.x509SvidTTL)
if err != nil {
return nil, fmt.Errorf("X509 SVID TTL: %w", err)
}

jwtSvidTTL, err := util.CheckedCast[int32](c.jwtSvidTTL)
if err != nil {
return nil, fmt.Errorf("JWT SVID TTL: %w", err)
}

e := &types.Entry{
Id: c.entryID,
ParentId: parentID,
SpiffeId: spiffeID,
Downstream: c.downstream,
ExpiresAt: c.entryExpiry,
DnsNames: c.dnsNames,
X509SvidTtl: int32(c.x509SvidTTL),
JwtSvidTtl: int32(c.jwtSvidTTL),
X509SvidTtl: x509SvidTTL,
JwtSvidTtl: jwtSvidTTL,
Hint: c.hint,
}

selectors := []*types.Selector{}
for _, s := range c.selectors {
cs, err := util.ParseSelector(s)
cs, err := serverutil.ParseSelector(s)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -240,7 +252,7 @@ func prettyPrintUpdate(env *commoncli.Env, results ...any) error {
// Print entries that failed to be updated
for _, r := range failed {
env.ErrPrintf("Failed to update the following entry (code: %s, msg: %q):\n",
codes.Code(r.Status.Code),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printEntry(r.Entry, env.ErrPrintf)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/spire-server/cli/federation/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/mitchellh/cli"
trustdomainv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -26,7 +27,7 @@ func NewCreateCommand() cli.Command {
}

func newCreateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &createCommand{env: env})
return serverutil.AdaptCommand(env, &createCommand{env: env})
}

type createCommand struct {
Expand All @@ -52,7 +53,7 @@ func (c *createCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, c.prettyPrintCreate)
}

func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *createCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
federationRelationships, err := getRelationships(c.config, c.path)
if err != nil {
return err
Expand Down Expand Up @@ -101,7 +102,7 @@ func (c *createCommand) prettyPrintCreate(env *commoncli.Env, results ...any) er
for _, r := range failed {
env.Println()
env.ErrPrintf("Failed to create the following federation relationship (code: %s, msg: %q):\n",
codes.Code(r.Status.Code),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printFederationRelationship(r.FederationRelationship, env.ErrPrintf)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/spire-server/cli/federation/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/mitchellh/cli"
trustdomainv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/trustdomain/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/util"
"google.golang.org/grpc/codes"
)

Expand All @@ -21,7 +22,7 @@ func NewUpdateCommand() cli.Command {
}

func newUpdateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &updateCommand{env: env})
return serverutil.AdaptCommand(env, &updateCommand{env: env})
}

type updateCommand struct {
Expand All @@ -47,7 +48,7 @@ func (c *updateCommand) AppendFlags(f *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, c.prettyPrintUpdate)
}

func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient serverutil.ServerClient) error {
federationRelationships, err := getRelationships(c.config, c.path)
if err != nil {
return err
Expand Down Expand Up @@ -97,7 +98,7 @@ func (c *updateCommand) prettyPrintUpdate(env *commoncli.Env, results ...any) er
for _, r := range failed {
env.Println()
env.ErrPrintf("Failed to update the following federation relationship (code: %s, msg: %q):\n",
codes.Code(r.Status.Code),
util.MustCast[codes.Code](r.Status.Code),
r.Status.Message)
printFederationRelationship(r.FederationRelationship, env.ErrPrintf)
}
Expand Down
17 changes: 11 additions & 6 deletions cmd/spire-server/cli/jwt/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ import (
"github.com/spiffe/go-spiffe/v2/spiffeid"
svidv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/svid/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
serverutil "github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"github.com/spiffe/spire/pkg/common/diskutil"
"github.com/spiffe/spire/pkg/common/jwtsvid"
"github.com/spiffe/spire/pkg/common/util"
)

func NewMintCommand() cli.Command {
return newMintCommand(commoncli.DefaultEnv)
}

func newMintCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &mintCommand{env: env})
return serverutil.AdaptCommand(env, &mintCommand{env: env})
}

type mintCommand struct {
Expand Down Expand Up @@ -52,7 +53,7 @@ func (c *mintCommand) AppendFlags(fs *flag.FlagSet) {
cliprinter.AppendFlagWithCustomPretty(&c.printer, fs, c.env, prettyPrintMint)
}

func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient util.ServerClient) error {
func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient serverutil.ServerClient) error {
if c.spiffeID == "" {
return errors.New("spiffeID must be specified")
}
Expand All @@ -63,14 +64,18 @@ func (c *mintCommand) Run(ctx context.Context, env *commoncli.Env, serverClient
if err != nil {
return err
}
ttl, err := ttlToSeconds(c.ttl)
if err != nil {
return fmt.Errorf("TTL: %w", err)
}

client := serverClient.NewSVIDClient()
resp, err := client.MintJWTSVID(ctx, &svidv1.MintJWTSVIDRequest{
Id: &types.SPIFFEID{
TrustDomain: spiffeID.TrustDomain().Name(),
Path: spiffeID.Path(),
},
Ttl: ttlToSeconds(c.ttl),
Ttl: ttl,
Audience: c.audience,
})
if err != nil {
Expand Down Expand Up @@ -132,8 +137,8 @@ func getJWTSVIDEndOfLife(token string) (time.Time, error) {

// ttlToSeconds returns the number of seconds in a duration, rounded up to
// the nearest second
func ttlToSeconds(ttl time.Duration) int32 {
return int32((ttl + time.Second - 1) / time.Second)
func ttlToSeconds(ttl time.Duration) (int32, error) {
return util.CheckedCast[int32]((ttl + time.Second - 1) / time.Second)
}

func prettyPrintMint(env *commoncli.Env, results ...any) error {
Expand Down
Loading
Loading