Skip to content

Commit 933b40a

Browse files
committed
Introduce gocritic linter and address warnings
1 parent 9cb1f21 commit 933b40a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+699
-742
lines changed

.golangci.yml

+14-6
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,30 @@ linters-settings:
3636
- performance
3737
- style
3838
- experimental
39+
- diagnostic
3940
disabled-checks:
40-
- wrapperFunc
41-
- dupImport # https://github.com/go-critic/go-critic/issues/845
41+
- commentFormatting
42+
- commentedOutCode
43+
- evalOrder
44+
- hugeParam
45+
- octalLiteral
46+
- rangeValCopy
47+
- tooManyResultsChecker
48+
- unnamedResult
4249

4350
linters:
4451
disable-all: true
4552
enable:
53+
- deadcode
54+
- gocritic
4655
- gofmt
47-
- revive
56+
- gosimple
4857
- govet
49-
- misspell
5058
- ineffassign
51-
- deadcode
59+
- misspell
60+
- revive
5261
- staticcheck
5362
- unused
54-
- gosimple
5563

5664
run:
5765
skip-dirs:

acme/api/account.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type NewAccountRequest struct {
1919

2020
func validateContacts(cs []string) error {
2121
for _, c := range cs {
22-
if len(c) == 0 {
22+
if c == "" {
2323
return acme.NewError(acme.ErrorMalformedType, "contact cannot be empty string")
2424
}
2525
}

acme/api/account_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func TestHandler_GetOrdersByAccountID(t *testing.T) {
178178
provName := url.PathEscape(prov.GetName())
179179
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
180180

181-
url := fmt.Sprintf("http://ca.smallstep.com/acme/%s/account/%s/orders", provName, accID)
181+
u := fmt.Sprintf("http://ca.smallstep.com/acme/%s/account/%s/orders", provName, accID)
182182

183183
oids := []string{"foo", "bar"}
184184
oidURLs := []string{
@@ -255,7 +255,7 @@ func TestHandler_GetOrdersByAccountID(t *testing.T) {
255255
tc := run(t)
256256
t.Run(name, func(t *testing.T) {
257257
h := &Handler{db: tc.db, linker: NewLinker("dns", "acme")}
258-
req := httptest.NewRequest("GET", url, nil)
258+
req := httptest.NewRequest("GET", u, nil)
259259
req = req.WithContext(tc.ctx)
260260
w := httptest.NewRecorder()
261261
h.GetOrdersByAccountID(w, req)

acme/api/handler_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestHandler_GetAuthorization(t *testing.T) {
148148
// Request with chi context
149149
chiCtx := chi.NewRouteContext()
150150
chiCtx.URLParams.Add("authzID", az.ID)
151-
url := fmt.Sprintf("%s/acme/%s/authz/%s",
151+
u := fmt.Sprintf("%s/acme/%s/authz/%s",
152152
baseURL.String(), provName, az.ID)
153153

154154
type test struct {
@@ -280,7 +280,7 @@ func TestHandler_GetAuthorization(t *testing.T) {
280280
expB, err := json.Marshal(az)
281281
assert.FatalError(t, err)
282282
assert.Equals(t, bytes.TrimSpace(body), expB)
283-
assert.Equals(t, res.Header["Location"], []string{url})
283+
assert.Equals(t, res.Header["Location"], []string{u})
284284
assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
285285
}
286286
})
@@ -314,7 +314,7 @@ func TestHandler_GetCertificate(t *testing.T) {
314314
// Request with chi context
315315
chiCtx := chi.NewRouteContext()
316316
chiCtx.URLParams.Add("certID", certID)
317-
url := fmt.Sprintf("%s/acme/%s/certificate/%s",
317+
u := fmt.Sprintf("%s/acme/%s/certificate/%s",
318318
baseURL.String(), provName, certID)
319319

320320
type test struct {
@@ -396,7 +396,7 @@ func TestHandler_GetCertificate(t *testing.T) {
396396
tc := run(t)
397397
t.Run(name, func(t *testing.T) {
398398
h := &Handler{db: tc.db}
399-
req := httptest.NewRequest("GET", url, nil)
399+
req := httptest.NewRequest("GET", u, nil)
400400
req = req.WithContext(tc.ctx)
401401
w := httptest.NewRecorder()
402402
h.GetCertificate(w, req)
@@ -434,7 +434,7 @@ func TestHandler_GetChallenge(t *testing.T) {
434434

435435
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
436436

437-
url := fmt.Sprintf("%s/acme/%s/challenge/%s/%s",
437+
u := fmt.Sprintf("%s/acme/%s/challenge/%s/%s",
438438
baseURL.String(), provName, "authzID", "chID")
439439

440440
type test struct {
@@ -635,7 +635,7 @@ func TestHandler_GetChallenge(t *testing.T) {
635635
AuthorizationID: "authzID",
636636
Type: acme.HTTP01,
637637
AccountID: "accID",
638-
URL: url,
638+
URL: u,
639639
Error: acme.NewError(acme.ErrorConnectionType, "force"),
640640
},
641641
vco: &acme.ValidateChallengeOptions{
@@ -652,7 +652,7 @@ func TestHandler_GetChallenge(t *testing.T) {
652652
tc := run(t)
653653
t.Run(name, func(t *testing.T) {
654654
h := &Handler{db: tc.db, linker: NewLinker("dns", "acme"), validateChallengeOptions: tc.vco}
655-
req := httptest.NewRequest("GET", url, nil)
655+
req := httptest.NewRequest("GET", u, nil)
656656
req = req.WithContext(tc.ctx)
657657
w := httptest.NewRecorder()
658658
h.GetChallenge(w, req)
@@ -678,7 +678,7 @@ func TestHandler_GetChallenge(t *testing.T) {
678678
assert.FatalError(t, err)
679679
assert.Equals(t, bytes.TrimSpace(body), expB)
680680
assert.Equals(t, res.Header["Link"], []string{fmt.Sprintf("<%s/acme/%s/authz/%s>;rel=\"up\"", baseURL, provName, "authzID")})
681-
assert.Equals(t, res.Header["Location"], []string{url})
681+
assert.Equals(t, res.Header["Location"], []string{u})
682682
assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
683683
}
684684
})

acme/api/middleware.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (h *Handler) validateJWS(next nextHTTP) nextHTTP {
223223
api.WriteError(w, acme.NewError(acme.ErrorMalformedType, "jwk and kid are mutually exclusive"))
224224
return
225225
}
226-
if hdr.JSONWebKey == nil && len(hdr.KeyID) == 0 {
226+
if hdr.JSONWebKey == nil && hdr.KeyID == "" {
227227
api.WriteError(w, acme.NewError(acme.ErrorMalformedType, "either jwk or kid must be defined in jws protected header"))
228228
return
229229
}
@@ -367,7 +367,7 @@ func (h *Handler) verifyAndExtractJWSPayload(next nextHTTP) nextHTTP {
367367
api.WriteError(w, err)
368368
return
369369
}
370-
if len(jwk.Algorithm) != 0 && jwk.Algorithm != jws.Signatures[0].Protected.Algorithm {
370+
if jwk.Algorithm != "" && jwk.Algorithm != jws.Signatures[0].Protected.Algorithm {
371371
api.WriteError(w, acme.NewError(acme.ErrorMalformedType, "verifier and signature algorithm do not match"))
372372
return
373373
}

acme/api/middleware_test.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestHandler_baseURLFromRequest(t *testing.T) {
108108
}
109109

110110
func TestHandler_addNonce(t *testing.T) {
111-
url := "https://ca.smallstep.com/acme/new-nonce"
111+
u := "https://ca.smallstep.com/acme/new-nonce"
112112
type test struct {
113113
db acme.DB
114114
err *acme.Error
@@ -141,7 +141,7 @@ func TestHandler_addNonce(t *testing.T) {
141141
tc := run(t)
142142
t.Run(name, func(t *testing.T) {
143143
h := &Handler{db: tc.db}
144-
req := httptest.NewRequest("GET", url, nil)
144+
req := httptest.NewRequest("GET", u, nil)
145145
w := httptest.NewRecorder()
146146
h.addNonce(testNext)(w, req)
147147
res := w.Result()
@@ -230,7 +230,7 @@ func TestHandler_verifyContentType(t *testing.T) {
230230
prov := newProv()
231231
escProvName := url.PathEscape(prov.GetName())
232232
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
233-
url := fmt.Sprintf("%s/acme/%s/certificate/abc123", baseURL.String(), escProvName)
233+
u := fmt.Sprintf("%s/acme/%s/certificate/abc123", baseURL.String(), escProvName)
234234
type test struct {
235235
h Handler
236236
ctx context.Context
@@ -245,7 +245,7 @@ func TestHandler_verifyContentType(t *testing.T) {
245245
h: Handler{
246246
linker: NewLinker("dns", "acme"),
247247
},
248-
url: url,
248+
url: u,
249249
ctx: context.Background(),
250250
contentType: "foo",
251251
statusCode: 500,
@@ -257,7 +257,7 @@ func TestHandler_verifyContentType(t *testing.T) {
257257
h: Handler{
258258
linker: NewLinker("dns", "acme"),
259259
},
260-
url: url,
260+
url: u,
261261
ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
262262
contentType: "foo",
263263
statusCode: 400,
@@ -319,11 +319,11 @@ func TestHandler_verifyContentType(t *testing.T) {
319319
for name, run := range tests {
320320
tc := run(t)
321321
t.Run(name, func(t *testing.T) {
322-
_url := url
322+
_u := u
323323
if tc.url != "" {
324-
_url = tc.url
324+
_u = tc.url
325325
}
326-
req := httptest.NewRequest("GET", _url, nil)
326+
req := httptest.NewRequest("GET", _u, nil)
327327
req = req.WithContext(tc.ctx)
328328
req.Header.Add("Content-Type", tc.contentType)
329329
w := httptest.NewRecorder()
@@ -353,7 +353,7 @@ func TestHandler_verifyContentType(t *testing.T) {
353353
}
354354

355355
func TestHandler_isPostAsGet(t *testing.T) {
356-
url := "https://ca.smallstep.com/acme/new-account"
356+
u := "https://ca.smallstep.com/acme/new-account"
357357
type test struct {
358358
ctx context.Context
359359
err *acme.Error
@@ -392,7 +392,7 @@ func TestHandler_isPostAsGet(t *testing.T) {
392392
tc := run(t)
393393
t.Run(name, func(t *testing.T) {
394394
h := &Handler{}
395-
req := httptest.NewRequest("GET", url, nil)
395+
req := httptest.NewRequest("GET", u, nil)
396396
req = req.WithContext(tc.ctx)
397397
w := httptest.NewRecorder()
398398
h.isPostAsGet(testNext)(w, req)
@@ -430,7 +430,7 @@ func (errReader) Close() error {
430430
}
431431

432432
func TestHandler_parseJWS(t *testing.T) {
433-
url := "https://ca.smallstep.com/acme/new-account"
433+
u := "https://ca.smallstep.com/acme/new-account"
434434
type test struct {
435435
next nextHTTP
436436
body io.Reader
@@ -483,7 +483,7 @@ func TestHandler_parseJWS(t *testing.T) {
483483
tc := run(t)
484484
t.Run(name, func(t *testing.T) {
485485
h := &Handler{}
486-
req := httptest.NewRequest("GET", url, tc.body)
486+
req := httptest.NewRequest("GET", u, tc.body)
487487
w := httptest.NewRecorder()
488488
h.parseJWS(tc.next)(w, req)
489489
res := w.Result()
@@ -528,7 +528,7 @@ func TestHandler_verifyAndExtractJWSPayload(t *testing.T) {
528528
assert.FatalError(t, err)
529529
parsedJWS, err := jose.ParseJWS(raw)
530530
assert.FatalError(t, err)
531-
url := "https://ca.smallstep.com/acme/account/1234"
531+
u := "https://ca.smallstep.com/acme/account/1234"
532532
type test struct {
533533
ctx context.Context
534534
next func(http.ResponseWriter, *http.Request)
@@ -681,7 +681,7 @@ func TestHandler_verifyAndExtractJWSPayload(t *testing.T) {
681681
tc := run(t)
682682
t.Run(name, func(t *testing.T) {
683683
h := &Handler{}
684-
req := httptest.NewRequest("GET", url, nil)
684+
req := httptest.NewRequest("GET", u, nil)
685685
req = req.WithContext(tc.ctx)
686686
w := httptest.NewRecorder()
687687
h.verifyAndExtractJWSPayload(tc.next)(w, req)
@@ -713,7 +713,7 @@ func TestHandler_lookupJWK(t *testing.T) {
713713
prov := newProv()
714714
provName := url.PathEscape(prov.GetName())
715715
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
716-
url := fmt.Sprintf("%s/acme/%s/account/1234",
716+
u := fmt.Sprintf("%s/acme/%s/account/1234",
717717
baseURL, provName)
718718
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
719719
assert.FatalError(t, err)
@@ -883,7 +883,7 @@ func TestHandler_lookupJWK(t *testing.T) {
883883
tc := run(t)
884884
t.Run(name, func(t *testing.T) {
885885
h := &Handler{db: tc.db, linker: tc.linker}
886-
req := httptest.NewRequest("GET", url, nil)
886+
req := httptest.NewRequest("GET", u, nil)
887887
req = req.WithContext(tc.ctx)
888888
w := httptest.NewRecorder()
889889
h.lookupJWK(tc.next)(w, req)
@@ -934,7 +934,7 @@ func TestHandler_extractJWK(t *testing.T) {
934934
assert.FatalError(t, err)
935935
parsedJWS, err := jose.ParseJWS(raw)
936936
assert.FatalError(t, err)
937-
url := fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/1234",
937+
u := fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/1234",
938938
provName)
939939
type test struct {
940940
db acme.DB
@@ -1079,7 +1079,7 @@ func TestHandler_extractJWK(t *testing.T) {
10791079
tc := run(t)
10801080
t.Run(name, func(t *testing.T) {
10811081
h := &Handler{db: tc.db}
1082-
req := httptest.NewRequest("GET", url, nil)
1082+
req := httptest.NewRequest("GET", u, nil)
10831083
req = req.WithContext(tc.ctx)
10841084
w := httptest.NewRecorder()
10851085
h.extractJWK(tc.next)(w, req)
@@ -1108,7 +1108,7 @@ func TestHandler_extractJWK(t *testing.T) {
11081108
}
11091109

11101110
func TestHandler_validateJWS(t *testing.T) {
1111-
url := "https://ca.smallstep.com/acme/account/1234"
1111+
u := "https://ca.smallstep.com/acme/account/1234"
11121112
type test struct {
11131113
db acme.DB
11141114
ctx context.Context
@@ -1198,7 +1198,7 @@ func TestHandler_validateJWS(t *testing.T) {
11981198
Algorithm: jose.RS256,
11991199
JSONWebKey: &pub,
12001200
ExtraHeaders: map[jose.HeaderKey]interface{}{
1201-
"url": url,
1201+
"url": u,
12021202
},
12031203
},
12041204
},
@@ -1226,7 +1226,7 @@ func TestHandler_validateJWS(t *testing.T) {
12261226
Algorithm: jose.RS256,
12271227
JSONWebKey: &pub,
12281228
ExtraHeaders: map[jose.HeaderKey]interface{}{
1229-
"url": url,
1229+
"url": u,
12301230
},
12311231
},
12321232
},
@@ -1298,7 +1298,7 @@ func TestHandler_validateJWS(t *testing.T) {
12981298
},
12991299
ctx: context.WithValue(context.Background(), jwsContextKey, jws),
13001300
statusCode: 400,
1301-
err: acme.NewError(acme.ErrorMalformedType, "url header in JWS (foo) does not match request url (%s)", url),
1301+
err: acme.NewError(acme.ErrorMalformedType, "url header in JWS (foo) does not match request url (%s)", u),
13021302
}
13031303
},
13041304
"fail/both-jwk-kid": func(t *testing.T) test {
@@ -1313,7 +1313,7 @@ func TestHandler_validateJWS(t *testing.T) {
13131313
KeyID: "bar",
13141314
JSONWebKey: &pub,
13151315
ExtraHeaders: map[jose.HeaderKey]interface{}{
1316-
"url": url,
1316+
"url": u,
13171317
},
13181318
},
13191319
},
@@ -1337,7 +1337,7 @@ func TestHandler_validateJWS(t *testing.T) {
13371337
Protected: jose.Header{
13381338
Algorithm: jose.ES256,
13391339
ExtraHeaders: map[jose.HeaderKey]interface{}{
1340-
"url": url,
1340+
"url": u,
13411341
},
13421342
},
13431343
},
@@ -1362,7 +1362,7 @@ func TestHandler_validateJWS(t *testing.T) {
13621362
Algorithm: jose.ES256,
13631363
KeyID: "bar",
13641364
ExtraHeaders: map[jose.HeaderKey]interface{}{
1365-
"url": url,
1365+
"url": u,
13661366
},
13671367
},
13681368
},
@@ -1392,7 +1392,7 @@ func TestHandler_validateJWS(t *testing.T) {
13921392
Algorithm: jose.ES256,
13931393
JSONWebKey: &pub,
13941394
ExtraHeaders: map[jose.HeaderKey]interface{}{
1395-
"url": url,
1395+
"url": u,
13961396
},
13971397
},
13981398
},
@@ -1422,7 +1422,7 @@ func TestHandler_validateJWS(t *testing.T) {
14221422
Algorithm: jose.RS256,
14231423
JSONWebKey: &pub,
14241424
ExtraHeaders: map[jose.HeaderKey]interface{}{
1425-
"url": url,
1425+
"url": u,
14261426
},
14271427
},
14281428
},
@@ -1446,7 +1446,7 @@ func TestHandler_validateJWS(t *testing.T) {
14461446
tc := run(t)
14471447
t.Run(name, func(t *testing.T) {
14481448
h := &Handler{db: tc.db}
1449-
req := httptest.NewRequest("GET", url, nil)
1449+
req := httptest.NewRequest("GET", u, nil)
14501450
req = req.WithContext(tc.ctx)
14511451
w := httptest.NewRecorder()
14521452
h.validateJWS(tc.next)(w, req)

0 commit comments

Comments
 (0)