X Tutup
Skip to content

Commit ab79f96

Browse files
authored
Fixup staticcheck and stylecheck, and violations thereof (letsencrypt#5897)
Add `stylecheck` to our list of lints, since it got separated out from `staticcheck`. Fix the way we configure both to be clearer and not rely on regexes. Additionally fix a number of easy-to-change `staticcheck` and `stylecheck` violations, allowing us to reduce our number of ignored checks. Part of letsencrypt#5681
1 parent 757495f commit ab79f96

38 files changed

+183
-203
lines changed

.golangci.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,27 @@ linters:
44
- errcheck
55
- gofmt
66
- gosec
7+
- gosimple
78
- govet
89
- ineffassign
910
- misspell
1011
- staticcheck
12+
- stylecheck
13+
- unused
1114
linters-settings:
1215
errcheck:
1316
ignore: fmt:[FS]?[Pp]rint*,io:Write,os:Remove,net/http:Write,github.com/miekg/dns:WriteMsg,net:Write,encoding/binary:Write
17+
gosimple:
18+
# S1029: Range over the string directly
19+
checks: ["all", "-S1029"]
20+
staticcheck:
21+
# SA1019: Using a deprecated function, variable, constant or field
22+
# SA6003: Converting a string to a slice of runes before ranging over it
23+
checks: ["all", "-SA1019", "-SA6003"]
24+
stylecheck:
25+
# ST1003: Poorly chosen identifier
26+
# ST1005: Incorrectly formatted error string
27+
checks: ["all", "-ST1003", "-ST1005"]
1428
issues:
1529
exclude-rules:
1630
- linters:
@@ -29,6 +43,3 @@ issues:
2943
# G501: Blacklisted import `crypto/md5`: weak cryptographic primitive
3044
# G505: Blacklisted import `crypto/sha1`: weak cryptographic primitive
3145
text: "G(101|102|107|201|202|306|401|402|403|404|501|505)"
32-
- linters:
33-
- staticcheck
34-
text: "(SA1019|ST1005|ST1013|SA6003|SA5011|S1029|SA2002):"

akamai/cache-client.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ func NewCachePurgeClient(
9696
}, []string{"type"})
9797
stats.MustRegister(purges)
9898

99-
if strings.HasSuffix(endpoint, "/") {
100-
endpoint = endpoint[:len(endpoint)-1]
101-
}
99+
endpoint = strings.TrimSuffix(endpoint, "/")
102100
apiURL, err := url.Parse(endpoint)
103101
if err != nil {
104102
return nil, err

ca/ocsp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func newOCSPLogQueue(
206206
wg: sync.WaitGroup{},
207207
depth: depth,
208208
logger: logger,
209-
clk: clock.Default(),
209+
clk: clock.New(),
210210
}
211211
olq.wg.Add(1)
212212
return &olq

canceled/canceled.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package canceled
33
import (
44
"context"
55

6-
"google.golang.org/grpc"
76
"google.golang.org/grpc/codes"
7+
"google.golang.org/grpc/status"
88
)
99

1010
// Is returns true if err is non-nil and is either context.Canceled, or has a
1111
// grpc code of Canceled. This is useful because cancellations propagate through
1212
// gRPC boundaries, and if we choose to treat in-process cancellations a certain
1313
// way, we usually want to treat cross-process cancellations the same way.
1414
func Is(err error) bool {
15-
return err == context.Canceled || grpc.Code(err) == codes.Canceled
15+
return err == context.Canceled || status.Code(err) == codes.Canceled
1616
}

canceled/canceled_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import (
55
"errors"
66
"testing"
77

8-
"google.golang.org/grpc"
98
"google.golang.org/grpc/codes"
9+
"google.golang.org/grpc/status"
1010
)
1111

1212
func TestCanceled(t *testing.T) {
1313
if !Is(context.Canceled) {
1414
t.Errorf("Expected context.Canceled to be canceled, but wasn't.")
1515
}
16-
if !Is(grpc.Errorf(codes.Canceled, "hi")) {
16+
if !Is(status.Errorf(codes.Canceled, "hi")) {
1717
t.Errorf("Expected gRPC cancellation to be cancelled, but wasn't.")
1818
}
1919
if Is(errors.New("hi")) {

cmd/boulder-wfe2/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/letsencrypt/boulder/cmd"
1919
"github.com/letsencrypt/boulder/features"
2020
"github.com/letsencrypt/boulder/goodkey"
21-
"github.com/letsencrypt/boulder/grpc"
2221
bgrpc "github.com/letsencrypt/boulder/grpc"
2322
"github.com/letsencrypt/boulder/issuance"
2423
blog "github.com/letsencrypt/boulder/log"
@@ -286,23 +285,23 @@ func setupWFE(c Config, logger blog.Logger, stats prometheus.Registerer, clk clo
286285
tlsConfig, err := c.WFE.TLS.Load()
287286
cmd.FailOnError(err, "TLS config")
288287
clientMetrics := bgrpc.NewClientMetrics(stats)
289-
raConn, err := bgrpc.ClientSetup(c.WFE.RAService, tlsConfig, clientMetrics, clk, grpc.CancelTo408Interceptor)
288+
raConn, err := bgrpc.ClientSetup(c.WFE.RAService, tlsConfig, clientMetrics, clk, bgrpc.CancelTo408Interceptor)
290289
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to RA")
291290
rac := rapb.NewRegistrationAuthorityClient(raConn)
292291

293-
saConn, err := bgrpc.ClientSetup(c.WFE.SAService, tlsConfig, clientMetrics, clk, grpc.CancelTo408Interceptor)
292+
saConn, err := bgrpc.ClientSetup(c.WFE.SAService, tlsConfig, clientMetrics, clk, bgrpc.CancelTo408Interceptor)
294293
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to SA")
295294
sac := sapb.NewStorageAuthorityClient(saConn)
296295

297296
var rns noncepb.NonceServiceClient
298297
npm := map[string]noncepb.NonceServiceClient{}
299298
if c.WFE.GetNonceService != nil {
300-
rnsConn, err := bgrpc.ClientSetup(c.WFE.GetNonceService, tlsConfig, clientMetrics, clk, grpc.CancelTo408Interceptor)
299+
rnsConn, err := bgrpc.ClientSetup(c.WFE.GetNonceService, tlsConfig, clientMetrics, clk, bgrpc.CancelTo408Interceptor)
301300
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to get nonce service")
302301
rns = noncepb.NewNonceServiceClient(rnsConn)
303302
for prefix, serviceConfig := range c.WFE.RedeemNonceServices {
304303
serviceConfig := serviceConfig
305-
conn, err := bgrpc.ClientSetup(&serviceConfig, tlsConfig, clientMetrics, clk, grpc.CancelTo408Interceptor)
304+
conn, err := bgrpc.ClientSetup(&serviceConfig, tlsConfig, clientMetrics, clk, bgrpc.CancelTo408Interceptor)
306305
cmd.FailOnError(err, "Failed to load credentials and create gRPC connection to redeem nonce service")
307306
npm[prefix] = noncepb.NewNonceServiceClient(conn)
308307
}

cmd/contact-auditor/main_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ type testCtx struct {
130130
cleanUp func()
131131
}
132132

133-
func (c testCtx) addRegistrations(t *testing.T) {
133+
func (tc testCtx) addRegistrations(t *testing.T) {
134134
emailA := "mailto:" + emailARaw
135135
emailB := "mailto:" + emailBRaw
136136
emailC := "mailto:" + emailCRaw
@@ -189,17 +189,17 @@ func (c testCtx) addRegistrations(t *testing.T) {
189189

190190
// Add the four test registrations
191191
ctx := context.Background()
192-
regA, err = c.ssa.NewRegistration(ctx, regA)
192+
regA, err = tc.ssa.NewRegistration(ctx, regA)
193193
test.AssertNotError(t, err, "Couldn't store regA")
194-
regB, err = c.ssa.NewRegistration(ctx, regB)
194+
regB, err = tc.ssa.NewRegistration(ctx, regB)
195195
test.AssertNotError(t, err, "Couldn't store regB")
196-
regC, err = c.ssa.NewRegistration(ctx, regC)
196+
regC, err = tc.ssa.NewRegistration(ctx, regC)
197197
test.AssertNotError(t, err, "Couldn't store regC")
198-
regD, err = c.ssa.NewRegistration(ctx, regD)
198+
regD, err = tc.ssa.NewRegistration(ctx, regD)
199199
test.AssertNotError(t, err, "Couldn't store regD")
200200
}
201201

202-
func (ctx testCtx) addCertificates(t *testing.T) {
202+
func (tc testCtx) addCertificates(t *testing.T) {
203203
serial1 := big.NewInt(1336)
204204
serial1String := core.SerialToString(serial1)
205205
serial2 := big.NewInt(1337)
@@ -238,9 +238,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
238238
Expires: rawCertA.NotAfter,
239239
DER: certDerA,
240240
}
241-
err := ctx.dbMap.Insert(certA)
241+
err := tc.dbMap.Insert(certA)
242242
test.AssertNotError(t, err, "Couldn't add certA")
243-
_, err = ctx.dbMap.Exec(
243+
_, err = tc.dbMap.Exec(
244244
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
245245
"com.example-a",
246246
serial1String,
@@ -263,9 +263,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
263263
Expires: rawCertB.NotAfter,
264264
DER: certDerB,
265265
}
266-
err = ctx.dbMap.Insert(certB)
266+
err = tc.dbMap.Insert(certB)
267267
test.AssertNotError(t, err, "Couldn't add certB")
268-
_, err = ctx.dbMap.Exec(
268+
_, err = tc.dbMap.Exec(
269269
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
270270
"com.example-b",
271271
serial2String,
@@ -288,9 +288,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
288288
Expires: rawCertC.NotAfter,
289289
DER: certDerC,
290290
}
291-
err = ctx.dbMap.Insert(certC)
291+
err = tc.dbMap.Insert(certC)
292292
test.AssertNotError(t, err, "Couldn't add certC")
293-
_, err = ctx.dbMap.Exec(
293+
_, err = tc.dbMap.Exec(
294294
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
295295
"com.example-c",
296296
serial3String,
@@ -313,9 +313,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
313313
Expires: rawCertD.NotAfter,
314314
DER: certDerD,
315315
}
316-
err = ctx.dbMap.Insert(certD)
316+
err = tc.dbMap.Insert(certD)
317317
test.AssertNotError(t, err, "Couldn't add certD")
318-
_, err = ctx.dbMap.Exec(
318+
_, err = tc.dbMap.Exec(
319319
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
320320
"com.example-d",
321321
serial4String,

cmd/id-exporter/main_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ type testCtx struct {
244244
cleanUp func()
245245
}
246246

247-
func (c testCtx) addRegistrations(t *testing.T) {
247+
func (tc testCtx) addRegistrations(t *testing.T) {
248248
emailA := "mailto:" + emailARaw
249249
emailB := "mailto:" + emailBRaw
250250
emailC := "mailto:" + emailCRaw
@@ -304,17 +304,17 @@ func (c testCtx) addRegistrations(t *testing.T) {
304304

305305
// Add the four test registrations
306306
ctx := context.Background()
307-
regA, err = c.ssa.NewRegistration(ctx, regA)
307+
regA, err = tc.ssa.NewRegistration(ctx, regA)
308308
test.AssertNotError(t, err, "Couldn't store regA")
309-
regB, err = c.ssa.NewRegistration(ctx, regB)
309+
regB, err = tc.ssa.NewRegistration(ctx, regB)
310310
test.AssertNotError(t, err, "Couldn't store regB")
311-
regC, err = c.ssa.NewRegistration(ctx, regC)
311+
regC, err = tc.ssa.NewRegistration(ctx, regC)
312312
test.AssertNotError(t, err, "Couldn't store regC")
313-
regD, err = c.ssa.NewRegistration(ctx, regD)
313+
regD, err = tc.ssa.NewRegistration(ctx, regD)
314314
test.AssertNotError(t, err, "Couldn't store regD")
315315
}
316316

317-
func (ctx testCtx) addCertificates(t *testing.T) {
317+
func (tc testCtx) addCertificates(t *testing.T) {
318318
serial1 := big.NewInt(1336)
319319
serial1String := core.SerialToString(serial1)
320320
serial2 := big.NewInt(1337)
@@ -353,9 +353,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
353353
Expires: rawCertA.NotAfter,
354354
DER: certDerA,
355355
}
356-
err := ctx.c.dbMap.Insert(certA)
356+
err := tc.c.dbMap.Insert(certA)
357357
test.AssertNotError(t, err, "Couldn't add certA")
358-
_, err = ctx.c.dbMap.Exec(
358+
_, err = tc.c.dbMap.Exec(
359359
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
360360
"com.example-a",
361361
serial1String,
@@ -378,9 +378,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
378378
Expires: rawCertB.NotAfter,
379379
DER: certDerB,
380380
}
381-
err = ctx.c.dbMap.Insert(certB)
381+
err = tc.c.dbMap.Insert(certB)
382382
test.AssertNotError(t, err, "Couldn't add certB")
383-
_, err = ctx.c.dbMap.Exec(
383+
_, err = tc.c.dbMap.Exec(
384384
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
385385
"com.example-b",
386386
serial2String,
@@ -403,9 +403,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
403403
Expires: rawCertC.NotAfter,
404404
DER: certDerC,
405405
}
406-
err = ctx.c.dbMap.Insert(certC)
406+
err = tc.c.dbMap.Insert(certC)
407407
test.AssertNotError(t, err, "Couldn't add certC")
408-
_, err = ctx.c.dbMap.Exec(
408+
_, err = tc.c.dbMap.Exec(
409409
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
410410
"com.example-c",
411411
serial3String,
@@ -428,9 +428,9 @@ func (ctx testCtx) addCertificates(t *testing.T) {
428428
Expires: rawCertD.NotAfter,
429429
DER: certDerD,
430430
}
431-
err = ctx.c.dbMap.Insert(certD)
431+
err = tc.c.dbMap.Insert(certD)
432432
test.AssertNotError(t, err, "Couldn't add certD")
433-
_, err = ctx.c.dbMap.Exec(
433+
_, err = tc.c.dbMap.Exec(
434434
"INSERT INTO issuedNames (reversedName, serial, notBefore) VALUES (?,?,0)",
435435
"com.example-d",
436436
serial4String,

cmd/log-validator/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
blog "github.com/letsencrypt/boulder/log"
2020
)
2121

22-
var invalidChecksumErr = errors.New("invalid checksum length")
22+
var errInvalidChecksum = errors.New("invalid checksum length")
2323

2424
func lineValid(text string) error {
2525
// Line format should match the following rsyslog omfile template:
@@ -53,7 +53,7 @@ func lineValid(text string) error {
5353
"%s expected a 7 character base64 raw URL decodable string, got %q: %w",
5454
errorPrefix,
5555
checksum,
56-
invalidChecksumErr,
56+
errInvalidChecksum,
5757
)
5858
}
5959

@@ -186,7 +186,7 @@ func main() {
186186
continue
187187
}
188188
if err := lineValid(line.Text); err != nil {
189-
if errors.Is(err, invalidChecksumErr) {
189+
if errors.Is(err, errInvalidChecksum) {
190190
lineCounter.WithLabelValues(t.Filename, "invalid checksum length").Inc()
191191
} else {
192192
lineCounter.WithLabelValues(t.Filename, "bad").Inc()

cmd/log-validator/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestLineValidRejects(t *testing.T) {
1919
func TestLineValidRejectsNotAChecksum(t *testing.T) {
2020
err := lineValid("2020-07-06T18:07:43.109389+00:00 70877f679c72 datacenter 6 boulder-wfe[1595]: xxxx Caught SIGTERM")
2121
test.AssertError(t, err, "didn't error on invalid checksum")
22-
test.AssertErrorIs(t, err, invalidChecksumErr)
22+
test.AssertErrorIs(t, err, errInvalidChecksum)
2323
}
2424

2525
func TestLineValidNonOurobouros(t *testing.T) {

0 commit comments

Comments
 (0)
X Tutup