Skip to content

Commit 475b7eb

Browse files
committed
Fix new golangci-lint issues
1 parent cee7040 commit 475b7eb

File tree

16 files changed

+385
-33
lines changed

16 files changed

+385
-33
lines changed

command/ca/acme/eab/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/smallstep/cli-utils/errs"
1414

1515
"github.com/smallstep/cli/flags"
16+
"github.com/smallstep/cli/internal/cast"
1617
"github.com/smallstep/cli/utils/cautils"
1718
)
1819

@@ -123,7 +124,7 @@ func listAction(ctx *cli.Context) (err error) {
123124
startedPager := false
124125

125126
for {
126-
options := []ca.AdminOption{ca.WithAdminCursor(cursor), ca.WithAdminLimit(int(limit))}
127+
options := []ca.AdminOption{ca.WithAdminCursor(cursor), ca.WithAdminLimit(cast.Int(limit))}
127128
eaksResponse, err := client.GetExternalAccountKeysPaginate(provisioner, reference, options...)
128129
if err != nil {
129130
return errors.Wrap(notImplemented(err), "error retrieving ACME EAB keys")

command/ca/provisioner/add.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"go.step.sm/crypto/pemutil"
2323

2424
"github.com/smallstep/cli/flags"
25+
"github.com/smallstep/cli/internal/cast"
2526
"github.com/smallstep/cli/internal/sliceutil"
2627
"github.com/smallstep/cli/utils"
2728
)
@@ -846,10 +847,10 @@ func createSCEPDetails(ctx *cli.Context) (*linkedca.ProvisionerDetails, error) {
846847
ForceCn: ctx.Bool("force-cn"),
847848
Challenge: challenge,
848849
Capabilities: ctx.StringSlice("capabilities"),
849-
MinimumPublicKeyLength: int32(ctx.Int("min-public-key-length")),
850+
MinimumPublicKeyLength: cast.Int32(ctx.Int("min-public-key-length")),
850851
IncludeRoot: ctx.Bool("include-root"),
851852
ExcludeIntermediate: ctx.Bool("exclude-intermediate"),
852-
EncryptionAlgorithmIdentifier: int32(ctx.Int("encryption-algorithm-identifier")),
853+
EncryptionAlgorithmIdentifier: cast.Int32(ctx.Int("encryption-algorithm-identifier")),
853854
}
854855
decrypter := &linkedca.SCEPDecrypter{}
855856
if decrypterCertificateFile := ctx.String("scep-decrypter-certificate-file"); decrypterCertificateFile != "" {

command/ca/provisioner/update.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"go.step.sm/crypto/pemutil"
2323

2424
"github.com/smallstep/cli/flags"
25+
"github.com/smallstep/cli/internal/cast"
2526
"github.com/smallstep/cli/internal/sliceutil"
2627
"github.com/smallstep/cli/utils"
2728
)
@@ -972,7 +973,7 @@ func updateSCEPDetails(ctx *cli.Context, p *linkedca.Provisioner) error {
972973
details.Capabilities = ctx.StringSlice("capabilities")
973974
}
974975
if ctx.IsSet("min-public-key-length") {
975-
details.MinimumPublicKeyLength = int32(ctx.Int("min-public-key-length"))
976+
details.MinimumPublicKeyLength = cast.Int32(ctx.Int("min-public-key-length"))
976977
}
977978
if ctx.IsSet("include-root") {
978979
details.IncludeRoot = ctx.Bool("include-root")
@@ -981,7 +982,7 @@ func updateSCEPDetails(ctx *cli.Context, p *linkedca.Provisioner) error {
981982
details.ExcludeIntermediate = ctx.Bool("exclude-intermediate")
982983
}
983984
if ctx.IsSet("encryption-algorithm-identifier") {
984-
details.EncryptionAlgorithmIdentifier = int32(ctx.Int("encryption-algorithm-identifier"))
985+
details.EncryptionAlgorithmIdentifier = cast.Int32(ctx.Int("encryption-algorithm-identifier"))
985986
}
986987

987988
decrypter := details.GetDecrypter()

command/crypto/otp/generate.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/smallstep/cli-utils/errs"
1515

1616
"github.com/smallstep/cli/flags"
17+
"github.com/smallstep/cli/internal/cast"
1718
"github.com/smallstep/cli/utils"
1819
)
1920

@@ -130,8 +131,8 @@ func generate(ctx *cli.Context) (*otp.Key, error) {
130131
return totp.Generate(totp.GenerateOpts{
131132
Issuer: ctx.String("issuer"),
132133
AccountName: ctx.String("account"),
133-
Period: uint(ctx.Int("period")),
134-
SecretSize: uint(ctx.Int("secret-size")),
134+
Period: cast.Uint(ctx.Int("period")),
135+
SecretSize: cast.Uint(ctx.Int("secret-size")),
135136
Digits: otp.Digits(ctx.Int("length")),
136137
Algorithm: alg,
137138
})

command/crypto/winpe/winpe_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// This test will write the chrome.exe installer into a temporary file
1414
// Then it will just run the extractPE function.
1515
func TestExtract(t *testing.T) {
16-
tmpfile, err := os.CreateTemp("", "step-crypto-winpe-extract-chrome.*.exe")
16+
tmpfile, err := os.CreateTemp(t.TempDir(), "step-crypto-winpe-extract-chrome.*.exe")
1717
assert.NoError(t, err)
1818
defer os.Remove(tmpfile.Name())
1919
defer tmpfile.Close()

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.7
55
require (
66
github.com/Microsoft/go-winio v0.6.2
77
github.com/ThomasRooney/gexpect v0.0.0-20161231170123-5482f0350944
8+
github.com/ccoveille/go-safecast v1.5.0
89
github.com/fxamacker/cbor/v2 v2.7.0
910
github.com/go-jose/go-jose/v3 v3.0.3
1011
github.com/google/go-cmp v0.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
8686
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
8787
github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyXcs=
8888
github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
89+
github.com/ccoveille/go-safecast v1.5.0 h1:cT/3uVQ/i5PTiJvhvkSU81HeKNurtyQtBndXEH3hDg4=
90+
github.com/ccoveille/go-safecast v1.5.0/go.mod h1:QqwNjxQ7DAqY0C721OIO9InMk9zCwcsO7tnRuHytad8=
8991
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
9092
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
9193
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=

internal/cast/cast.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package cast
2+
3+
import (
4+
"github.com/ccoveille/go-safecast"
5+
)
6+
7+
type signed interface {
8+
~int | ~int8 | ~int16 | ~int32 | ~int64
9+
}
10+
11+
type unsigned interface {
12+
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
13+
}
14+
15+
type number interface {
16+
signed | unsigned
17+
}
18+
19+
func SafeUint(x int) (uint, error) {
20+
return safecast.ToUint(x)
21+
}
22+
23+
func Uint(x int) uint {
24+
u, err := SafeUint(x)
25+
if err != nil {
26+
panic(err)
27+
}
28+
29+
return u
30+
}
31+
32+
func SafeInt(x uint) (int, error) {
33+
return safecast.ToInt(x)
34+
}
35+
36+
func Int(x uint) int {
37+
i, err := SafeInt(x)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
return i
43+
}
44+
45+
func SafeInt64[T number](x T) (int64, error) {
46+
return safecast.ToInt64(x)
47+
}
48+
49+
func Int64[T number](x T) int64 {
50+
i64, err := SafeInt64(x)
51+
if err != nil {
52+
panic(err)
53+
}
54+
55+
return i64
56+
}
57+
58+
func SafeUint64[T signed](x T) (uint64, error) {
59+
return safecast.ToUint64(x)
60+
}
61+
62+
func Uint64[T signed](x T) uint64 {
63+
u64, err := SafeUint64(x)
64+
if err != nil {
65+
panic(err)
66+
}
67+
68+
return u64
69+
}
70+
71+
func SafeInt32[T signed](x T) (int32, error) {
72+
return safecast.ToInt32(x)
73+
}
74+
75+
func Int32[T signed](x T) int32 {
76+
i32, err := SafeInt32(x)
77+
if err != nil {
78+
panic(err)
79+
}
80+
81+
return i32
82+
}
83+
84+
func SafeUint32[T number](x T) (uint32, error) {
85+
return safecast.ToUint32(x)
86+
}
87+
88+
func Uint32[T number](x T) uint32 {
89+
u32, err := SafeUint32(x)
90+
if err != nil {
91+
panic(err)
92+
}
93+
94+
return u32
95+
}
96+
97+
func SafeUint8(x int) (uint8, error) {
98+
return safecast.ToUint8(x)
99+
}
100+
101+
func Uint8(x int) uint8 {
102+
u8, err := SafeUint8(x)
103+
if err != nil {
104+
panic(err)
105+
}
106+
107+
return u8
108+
}

internal/cast/cast_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package cast_test
2+
3+
import (
4+
"math"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/smallstep/cli/internal/cast"
10+
)
11+
12+
func TestUintConvertsValues(t *testing.T) {
13+
require.Equal(t, uint(0), cast.Uint(0))
14+
require.Equal(t, uint(math.MaxInt), cast.Uint(math.MaxInt))
15+
require.Equal(t, uint(42), cast.Uint(42))
16+
}
17+
18+
func TestUintPanicsOnNegativeValue(t *testing.T) {
19+
require.Panics(t, func() { cast.Uint(-1) })
20+
}
21+
22+
func TestIntConvertsValues(t *testing.T) {
23+
require.Equal(t, int(0), cast.Int(0))
24+
require.Equal(t, int(math.MaxInt), cast.Int(math.MaxInt))
25+
require.Equal(t, int(42), cast.Int(42))
26+
}
27+
28+
func TestIntPanicsOnLargeValue(t *testing.T) {
29+
require.Panics(t, func() { cast.Int(uint(math.MaxInt + 1)) })
30+
}
31+
32+
func TestInt64ConvertsValues(t *testing.T) {
33+
require.Equal(t, int64(0), cast.Int64(0))
34+
require.Equal(t, int64(math.MaxInt), cast.Int64(math.MaxInt))
35+
require.Equal(t, int64(42), cast.Int64(42))
36+
}
37+
38+
func TestInt64PanicsOnLargeValue(t *testing.T) {
39+
require.Panics(t, func() { cast.Int64(uint64(math.MaxInt + 1)) })
40+
}
41+
42+
func TestUint64ConvertsValues(t *testing.T) {
43+
require.Equal(t, uint64(0), cast.Uint64(0))
44+
require.Equal(t, uint64(math.MaxInt), cast.Uint64((math.MaxInt)))
45+
require.Equal(t, uint64(42), cast.Uint64(42))
46+
}
47+
48+
func TestUint64PanicsOnNegativeValue(t *testing.T) {
49+
require.Panics(t, func() { cast.Uint64(-1) })
50+
}
51+
52+
func TestInt32ConvertsValues(t *testing.T) {
53+
require.Equal(t, int32(0), cast.Int32(0))
54+
require.Equal(t, int32(math.MaxInt32), cast.Int32(math.MaxInt32))
55+
require.Equal(t, int32(42), cast.Int32(42))
56+
}
57+
58+
func TestInt32PanicsOnTooSmallValue(t *testing.T) {
59+
require.Panics(t, func() { cast.Int32(math.MinInt32 - 1) })
60+
}
61+
62+
func TestInt32PanicsOnLargeValue(t *testing.T) {
63+
require.Panics(t, func() { cast.Int32(math.MaxInt32 + 1) })
64+
}
65+
66+
func TestUint32ConvertsValues(t *testing.T) {
67+
require.Equal(t, uint32(0), cast.Uint32(0))
68+
require.Equal(t, uint32(math.MaxUint32), cast.Uint32(math.MaxUint32))
69+
require.Equal(t, uint32(42), cast.Uint32(42))
70+
}
71+
72+
func TestUint32PanicsOnNegativeValue(t *testing.T) {
73+
require.Panics(t, func() { cast.Uint32(-1) })
74+
}
75+
76+
func TestUint32PanicsOnLargeValue(t *testing.T) {
77+
require.Panics(t, func() { cast.Uint32(math.MaxUint32 + 1) })
78+
}
79+
func TestUint8ConvertsValues(t *testing.T) {
80+
require.Equal(t, uint8(0), cast.Uint8(0))
81+
require.Equal(t, uint8(math.MaxUint8), cast.Uint8(math.MaxUint8))
82+
require.Equal(t, uint8(42), cast.Uint8(42))
83+
}
84+
85+
func TestUint8PanicsOnNegativeValue(t *testing.T) {
86+
require.Panics(t, func() { cast.Uint8(-1) })
87+
}
88+
89+
func TestUint8PanicsOnLargeValue(t *testing.T) {
90+
require.Panics(t, func() { cast.Uint8(math.MaxUint32 + 1) })
91+
}

internal/kdf/argon2.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/pkg/errors"
7+
"github.com/smallstep/cli/internal/cast"
78
)
89

910
const (
@@ -57,8 +58,8 @@ func newArgon2Params(s string) (*argon2Param, error) {
5758
}
5859

5960
return &argon2Param{
60-
t: uint32(t),
61-
m: uint32(m),
62-
p: uint8(p),
61+
t: cast.Uint32(t),
62+
m: cast.Uint32(m),
63+
p: cast.Uint8(p),
6364
}, nil
6465
}

internal/kdf/kdf.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"strconv"
66

77
"github.com/pkg/errors"
8-
"go.step.sm/crypto/randutil"
98
"golang.org/x/crypto/argon2"
109
"golang.org/x/crypto/bcrypt"
1110
"golang.org/x/crypto/scrypt"
11+
12+
"go.step.sm/crypto/randutil"
13+
14+
"github.com/smallstep/cli/internal/cast"
1215
)
1316

1417
// KDF is the type that all the key derivation functions implements. The
@@ -109,7 +112,7 @@ func Compare(password, phc []byte) (bool, error) {
109112
if version != 0 && version != argon2.Version {
110113
return false, errors.Errorf("unsupported argon2 version '%d'", version)
111114
}
112-
hashedPass = argon2.Key(password, salt, p.t, p.m, p.p, uint32(len(hash)))
115+
hashedPass = argon2.Key(password, salt, p.t, p.m, p.p, cast.Uint32(len(hash)))
113116
case argon2idHash:
114117
p, err := newArgon2Params(params)
115118
if err != nil {
@@ -118,7 +121,7 @@ func Compare(password, phc []byte) (bool, error) {
118121
if version != 0 && version != argon2.Version {
119122
return false, errors.Errorf("unsupported argon2 version '%d'", version)
120123
}
121-
hashedPass = argon2.IDKey(password, salt, p.t, p.m, p.p, uint32(len(hash)))
124+
hashedPass = argon2.IDKey(password, salt, p.t, p.m, p.p, cast.Uint32(len(hash)))
122125
default:
123126
return false, errors.Errorf("invalid or unsupported hash method with id '%s'", id)
124127
}

internal/sshutil/agent.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/pkg/errors"
10+
"github.com/smallstep/cli/internal/cast"
1011
"golang.org/x/crypto/ssh"
1112
"golang.org/x/crypto/ssh/agent"
1213
)
@@ -66,7 +67,7 @@ func WithRemoveExpiredCerts(t time.Time) AgentOption {
6667
return func(o *options) {
6768
o.removeExpiredKey = func(a *Agent, k *agent.Key) bool {
6869
if cert, err := ParseCertificate(k.Marshal()); err == nil {
69-
if before := int64(cert.ValidBefore); cert.ValidBefore != uint64(ssh.CertTimeInfinity) && (unixNow >= before || before < 0) {
70+
if before := cast.Int64(cert.ValidBefore); cert.ValidBefore != uint64(ssh.CertTimeInfinity) && (unixNow >= before || before < 0) {
7071
if err := a.Remove(k); err == nil {
7172
return true
7273
}
@@ -244,7 +245,7 @@ func (a *Agent) RemoveAllKeys(opts ...AgentOption) (bool, error) {
244245
func (a *Agent) AddCertificate(subject string, cert *ssh.Certificate, priv interface{}) error {
245246
var (
246247
lifetime uint64
247-
now = uint64(time.Now().Unix())
248+
now = cast.Uint64(time.Now().Unix())
248249
)
249250
switch {
250251
case cert.ValidBefore == ssh.CertTimeInfinity:
@@ -265,6 +266,6 @@ func (a *Agent) AddCertificate(subject string, cert *ssh.Certificate, priv inter
265266
PrivateKey: priv,
266267
Certificate: cert,
267268
Comment: subject,
268-
LifetimeSecs: uint32(lifetime),
269+
LifetimeSecs: cast.Uint32(lifetime),
269270
}), "error adding key to agent")
270271
}

0 commit comments

Comments
 (0)