Skip to content

Commit 0dfed50

Browse files
authored
chore: make linters happy (#1052)
1 parent f8ebd49 commit 0dfed50

40 files changed

+430
-173
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ linters:
1414
- lll
1515
- mnd
1616
- musttag
17+
- noinlineerr
1718
- nolintlint
1819
- nonamedreturns
1920
- paralleltest

authorization.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212

1313
// claims contains Mercure's JWT claims.
1414
type claims struct {
15+
jwt.RegisteredClaims
16+
1517
Mercure mercureClaim `json:"mercure"`
1618
// Optional fallback
1719
MercureNamespaced *mercureClaim `json:"https://mercure.rocks/"`
18-
jwt.RegisteredClaims
1920
}
2021

2122
type mercureClaim struct {
@@ -27,9 +28,10 @@ type mercureClaim struct {
2728
type role int
2829

2930
const (
30-
defaultCookieName = "mercureAuthorization"
31-
bearerPrefix = "Bearer "
32-
roleSubscriber role = iota
31+
defaultCookieName = "mercureAuthorization"
32+
bearerPrefix = "Bearer "
33+
34+
roleSubscriber role = iota
3335
rolePublisher
3436
)
3537

@@ -44,8 +46,6 @@ var (
4446
ErrOriginNotAllowed = errors.New("origin not allowed to post updates")
4547
// ErrInvalidJWT is returned when the JWT is invalid.
4648
ErrInvalidJWT = errors.New("invalid JWT")
47-
// ErrPublicKey is returned when there is an error with the public key.
48-
ErrPublicKey = errors.New("public key error")
4949
)
5050

5151
// Authorize validates the JWT that may be provided through an "Authorization" HTTP header or an authorization cookie.
@@ -137,6 +137,7 @@ func canReceive(s *TopicSelectorStore, topics, topicSelectors []string) bool {
137137
func canDispatch(s *TopicSelectorStore, topics, topicSelectors []string) bool {
138138
for _, topic := range topics {
139139
var matched bool
140+
140141
for _, topicSelector := range topicSelectors {
141142
if topicSelector == "*" {
142143
return true
@@ -159,6 +160,7 @@ func canDispatch(s *TopicSelectorStore, topics, topicSelectors []string) bool {
159160

160161
func (h *Hub) httpAuthorizationError(w http.ResponseWriter, r *http.Request, err error) {
161162
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
163+
162164
if c := h.logger.Check(zap.DebugLevel, "Topic selectors not matched, not provided or authorization error"); c != nil {
163165
c.Write(zap.String("remote_addr", r.RemoteAddr), zap.Error(err))
164166
}

authorization_test.go

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//go:debug rsa1024min=0
21
package mercure
32

43
import (
@@ -262,7 +261,7 @@ func TestAuthorizeAuthorizationHeaderInvalidSignature(t *testing.T) {
262261
}
263262

264263
func TestAuthorizeAuthorizationHeaderNoContent(t *testing.T) {
265-
t.Parallel()
264+
t.Setenv("GODEBUG", "rsa1024min=0")
266265

267266
for _, testdata := range AuthTestData {
268267
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -278,7 +277,7 @@ func TestAuthorizeAuthorizationHeaderNoContent(t *testing.T) {
278277
}
279278

280279
func TestAuthorizeAuthorizationHeader(t *testing.T) {
281-
t.Parallel()
280+
t.Setenv("GODEBUG", "rsa1024min=0")
282281

283282
for _, testdata := range AuthTestData {
284283
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -294,7 +293,7 @@ func TestAuthorizeAuthorizationHeader(t *testing.T) {
294293
}
295294

296295
func TestAuthorizeAuthorizationHeaderWithCert(t *testing.T) {
297-
t.Parallel()
296+
t.Setenv("GODEBUG", "rsa1024min=0")
298297

299298
for _, testdata := range AuthTestData {
300299
if testdata.validForCert != "" {
@@ -312,7 +311,7 @@ func TestAuthorizeAuthorizationHeaderWithCert(t *testing.T) {
312311
}
313312

314313
func TestAuthorizeAuthorizationHeaderNamespaced(t *testing.T) {
315-
t.Parallel()
314+
t.Setenv("GODEBUG", "rsa1024min=0")
316315

317316
for _, testdata := range AuthTestData {
318317
if testdata.validNamespaced != "" {
@@ -418,7 +417,7 @@ func TestAuthorizeAuthorizationQueryInvalidSignature(t *testing.T) {
418417
}
419418

420419
func TestAuthorizeAuthorizationQueryNoContent(t *testing.T) {
421-
t.Parallel()
420+
t.Setenv("GODEBUG", "rsa1024min=0")
422421

423422
for _, testdata := range AuthTestData {
424423
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -436,7 +435,7 @@ func TestAuthorizeAuthorizationQueryNoContent(t *testing.T) {
436435
}
437436

438437
func TestAuthorizeAuthorizationQuery(t *testing.T) {
439-
t.Parallel()
438+
t.Setenv("GODEBUG", "rsa1024min=0")
440439

441440
for _, testdata := range AuthTestData {
442441
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -454,7 +453,7 @@ func TestAuthorizeAuthorizationQuery(t *testing.T) {
454453
}
455454

456455
func TestAuthorizeAuthorizationQueryNamespaced(t *testing.T) {
457-
t.Parallel()
456+
t.Setenv("GODEBUG", "rsa1024min=0")
458457

459458
for _, testdata := range AuthTestData {
460459
if testdata.validNamespaced != "" {
@@ -474,7 +473,7 @@ func TestAuthorizeAuthorizationQueryNamespaced(t *testing.T) {
474473
}
475474

476475
func TestAuthorizeAuthorizationQueryRsaWithCert(t *testing.T) {
477-
t.Parallel()
476+
t.Setenv("GODEBUG", "rsa1024min=0")
478477

479478
for _, testdata := range AuthTestData {
480479
if testdata.validForCert != "" {
@@ -494,7 +493,7 @@ func TestAuthorizeAuthorizationQueryRsaWithCert(t *testing.T) {
494493
}
495494

496495
func TestAuthorizeAuthorizationQueryWrongAlgorithm(t *testing.T) {
497-
t.Parallel()
496+
t.Setenv("GODEBUG", "rsa1024min=0")
498497

499498
for idx, testdata := range AuthTestData {
500499
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -513,7 +512,7 @@ func TestAuthorizeAuthorizationQueryWrongAlgorithm(t *testing.T) {
513512
}
514513

515514
func TestAuthorizeCookieInvalidAlg(t *testing.T) {
516-
t.Parallel()
515+
t.Setenv("GODEBUG", "rsa1024min=0")
517516

518517
for _, testdata := range AuthTestData {
519518
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -528,7 +527,7 @@ func TestAuthorizeCookieInvalidAlg(t *testing.T) {
528527
}
529528

530529
func TestAuthorizeCookieInvalidKey(t *testing.T) {
531-
t.Parallel()
530+
t.Setenv("GODEBUG", "rsa1024min=0")
532531

533532
for _, testdata := range AuthTestData {
534533
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -544,7 +543,7 @@ func TestAuthorizeCookieInvalidKey(t *testing.T) {
544543
}
545544

546545
func TestAuthorizeCookieInvalidSignature(t *testing.T) {
547-
t.Parallel()
546+
t.Setenv("GODEBUG", "rsa1024min=0")
548547

549548
for _, testdata := range AuthTestData {
550549
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -560,7 +559,7 @@ func TestAuthorizeCookieInvalidSignature(t *testing.T) {
560559
}
561560

562561
func TestAuthorizeCookieNoContent(t *testing.T) {
563-
t.Parallel()
562+
t.Setenv("GODEBUG", "rsa1024min=0")
564563

565564
for _, testdata := range AuthTestData {
566565
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -576,7 +575,7 @@ func TestAuthorizeCookieNoContent(t *testing.T) {
576575
}
577576

578577
func TestAuthorizeCookie(t *testing.T) {
579-
t.Parallel()
578+
t.Setenv("GODEBUG", "rsa1024min=0")
580579

581580
for _, testdata := range AuthTestData {
582581
r, _ := http.NewRequest(http.MethodGet, defaultHubURL, nil)
@@ -592,7 +591,7 @@ func TestAuthorizeCookie(t *testing.T) {
592591
}
593592

594593
func TestAuthorizeCookieNoOriginNoReferer(t *testing.T) {
595-
t.Parallel()
594+
t.Setenv("GODEBUG", "rsa1024min=0")
596595

597596
for _, testdata := range AuthTestData {
598597
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
@@ -655,7 +654,7 @@ func TestAuthorizeCookieInvalidReferer(t *testing.T) {
655654
}
656655

657656
func TestAuthorizeCookieOriginHasPriority(t *testing.T) {
658-
t.Parallel()
657+
t.Setenv("GODEBUG", "rsa1024min=0")
659658

660659
for _, testdata := range AuthTestData {
661660
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
@@ -673,7 +672,7 @@ func TestAuthorizeCookieOriginHasPriority(t *testing.T) {
673672
}
674673

675674
func TestAuthorizeAllOriginsAllowed(t *testing.T) {
676-
t.Parallel()
675+
t.Setenv("GODEBUG", "rsa1024min=0")
677676

678677
for _, testdata := range AuthTestData {
679678
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)
@@ -688,7 +687,7 @@ func TestAuthorizeAllOriginsAllowed(t *testing.T) {
688687
}
689688

690689
func TestAuthorizeCustomCookieName(t *testing.T) {
691-
t.Parallel()
690+
t.Setenv("GODEBUG", "rsa1024min=0")
692691

693692
for _, testdata := range AuthTestData {
694693
r, _ := http.NewRequest(http.MethodPost, defaultHubURL, nil)

bolt.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const defaultBoltBucketName = "updates"
2626
// BoltTransport implements the TransportInterface using the Bolt database.
2727
type BoltTransport struct {
2828
sync.RWMutex
29+
2930
subscribers *SubscriberList
3031
logger Logger
3132
db *bolt.DB
@@ -43,8 +44,10 @@ type BoltTransport struct {
4344
// Deprecated: use NewBoltTransport() instead.
4445
func DeprecatedNewBoltTransport(u *url.URL, l Logger) (Transport, error) { //nolint:ireturn
4546
var err error
47+
4648
q := u.Query()
4749
bucketName := defaultBoltBucketName
50+
4851
if q.Get("bucket_name") != "" {
4952
bucketName = q.Get("bucket_name")
5053
}
@@ -59,6 +62,7 @@ func DeprecatedNewBoltTransport(u *url.URL, l Logger) (Transport, error) { //nol
5962

6063
cleanupFrequency := BoltDefaultCleanupFrequency
6164
cleanupFrequencyParameter := q.Get("cleanup_frequency")
65+
6266
if cleanupFrequencyParameter != "" {
6367
cleanupFrequency, err = strconv.ParseFloat(cleanupFrequencyParameter, 64)
6468
if err != nil {
@@ -67,9 +71,11 @@ func DeprecatedNewBoltTransport(u *url.URL, l Logger) (Transport, error) { //nol
6771
}
6872

6973
path := u.Path // absolute path (bolt:///path.db)
74+
7075
if path == "" {
7176
path = u.Host // relative path (bolt://path.db)
7277
}
78+
7379
if path == "" {
7480
return nil, &TransportError{u.Redacted(), "missing path", err}
7581
}
@@ -118,6 +124,7 @@ func NewBoltTransport(
118124

119125
func getDBLastEventID(db *bolt.DB, bucketName string) (string, error) {
120126
lastEventID := EarliestLastEventID
127+
121128
err := db.View(func(tx *bolt.Tx) error {
122129
b := tx.Bucket([]byte(bucketName))
123130
if b == nil {
@@ -146,6 +153,7 @@ func (t *BoltTransport) Dispatch(update *Update) error {
146153
}
147154

148155
AssignUUID(update)
156+
149157
updateJSON, err := json.Marshal(*update)
150158
if err != nil {
151159
return fmt.Errorf("error when marshaling update: %w", err)
@@ -200,6 +208,7 @@ func (t *BoltTransport) RemoveSubscriber(s *LocalSubscriber) error {
200208

201209
t.Lock()
202210
defer t.Unlock()
211+
203212
t.subscribers.Remove(s)
204213

205214
return nil
@@ -248,6 +257,7 @@ func (t *BoltTransport) dispatchHistory(s *LocalSubscriber, toSeq uint64) error
248257

249258
c := b.Cursor()
250259
responseLastEventID := EarliestLastEventID
260+
251261
afterFromID := s.RequestLastEventID == EarliestLastEventID
252262
for k, v := c.First(); k != nil; k, v = c.Next() {
253263
if !afterFromID {
@@ -262,6 +272,7 @@ func (t *BoltTransport) dispatchHistory(s *LocalSubscriber, toSeq uint64) error
262272
var update *Update
263273
if err := json.Unmarshal(v, &update); err != nil {
264274
s.HistoryDispatched(responseLastEventID)
275+
265276
if c := t.logger.Check(zap.ErrorLevel, "Unable to unmarshal update coming from the Bolt DB"); c != nil {
266277
c.Write(zap.Error(err))
267278
}
@@ -275,7 +286,9 @@ func (t *BoltTransport) dispatchHistory(s *LocalSubscriber, toSeq uint64) error
275286
return nil
276287
}
277288
}
289+
278290
s.HistoryDispatched(responseLastEventID)
291+
279292
if !afterFromID {
280293
if c := t.logger.Check(zap.InfoLevel, "Can't find requested LastEventID"); c != nil {
281294
c.Write(zap.String("LastEventID", s.RequestLastEventID))
@@ -336,6 +349,7 @@ func (t *BoltTransport) cleanup(bucket *bolt.Bucket, lastID uint64) error {
336349
}
337350

338351
removeUntil := lastID - t.size
352+
339353
c := bucket.Cursor()
340354
for k, _ := c.First(); k != nil; k, _ = c.Next() {
341355
if binary.BigEndian.Uint64(k[:8]) > removeUntil {

0 commit comments

Comments
 (0)