Skip to content

Commit 2654a93

Browse files
authored
chore: Fix golangci-lint configuration and patch errors (#34)
* chore: Fix golangci-lint configuration and patch errors Due to misconfiguration of a linting rules directory, our linter has not been working properly. This change fixes the configuration issue, and all remaining linting errors. * Fix race in peer logging * Fix race and return * Lock on bufferred amount low * Fix mutex lock
1 parent 6a919ae commit 2654a93

38 files changed

+283
-255
lines changed

.golangci.yml

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ linters-settings:
100100
# - whyNoLint
101101
# - wrapperFunc
102102
# - yodaStyleExpr
103-
settings:
104-
ruleguard:
105-
failOn: all
106-
rules: "${configDir}/lib/go/lintrules/*.go"
107103

108104
goimports:
109105
local-prefixes: coder.com,cdr.dev,go.coder.com,github.com/cdr,github.com/coder
@@ -113,24 +109,6 @@ linters-settings:
113109

114110
importas:
115111
no-unaliased: true
116-
alias:
117-
- pkg: k8s.io/api/(\w+)/(v[\w\d]+)
118-
alias: ${1}${2}
119-
120-
- pkg: k8s.io/apimachinery/pkg/apis/meta/(v[\w\d]+)
121-
alias: meta${1}
122-
123-
- pkg: k8s.io/client-go/kubernetes/typed/(\w+)/(v[\w\d]+)
124-
alias: ${1}${2}client
125-
126-
- pkg: k8s.io/metrics/pkg/apis/metrics/(v[\w\d]+)
127-
alias: metrics${1}
128-
129-
- pkg: github.com/docker/docker/api/types
130-
alias: dockertypes
131-
132-
- pkg: github.com/docker/docker/client
133-
alias: dockerclient
134112

135113
misspell:
136114
locale: US
@@ -195,6 +173,20 @@ linters-settings:
195173
- name: var-declaration
196174
- name: var-naming
197175
- name: waitgroup-by-value
176+
varnamelen:
177+
ignore-names:
178+
- err
179+
- rw
180+
- r
181+
- i
182+
- db
183+
# Optional list of variable declarations that should be ignored completely. (defaults to empty list)
184+
# Entries must be in the form of "<variable name> <type>" or "<variable name> *<type>" for
185+
# variables, or "const <name>" for constants.
186+
ignore-decls:
187+
- rw http.ResponseWriter
188+
- r *http.Request
189+
- t testing.T
198190

199191
issues:
200192
# Rules listed here: https://github.com/securego/gosec#available-rules
@@ -222,7 +214,6 @@ linters:
222214
- asciicheck
223215
- bidichk
224216
- bodyclose
225-
- contextcheck
226217
- deadcode
227218
- dogsled
228219
- errcheck
@@ -239,7 +230,6 @@ linters:
239230
- govet
240231
- importas
241232
- ineffassign
242-
# - ireturn
243233
- makezero
244234
- misspell
245235
- nilnil

cmd/coder/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package main
33
import "fmt"
44

55
func main() {
6-
fmt.Println("Hello World!")
6+
_, _ = fmt.Println("Hello World!")
77
}

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func New(options *Options) http.Handler {
3939
httpmw.ExtractAPIKey(options.Database, nil),
4040
httpmw.ExtractUser(options.Database),
4141
)
42-
r.Get("/user", users.getAuthenticatedUser)
42+
r.Get("/user", users.authenticatedUser)
4343
})
4444
})
4545
r.NotFound(site.Handler().ServeHTTP)

coderd/coderdtest/coderdtest.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ func New(t *testing.T) Server {
3232
Database: db,
3333
})
3434
srv := httptest.NewServer(handler)
35-
u, err := url.Parse(srv.URL)
35+
serverURL, err := url.Parse(srv.URL)
3636
require.NoError(t, err)
3737
t.Cleanup(srv.Close)
3838

39-
client := codersdk.New(u)
39+
client := codersdk.New(serverURL)
4040
_, err = client.CreateInitialUser(context.Background(), coderd.CreateUserRequest{
4141
Email: "testuser@coder.com",
4242
Username: "testuser",
@@ -54,6 +54,6 @@ func New(t *testing.T) Server {
5454

5555
return Server{
5656
Client: client,
57-
URL: u,
57+
URL: serverURL,
5858
}
5959
}

coderd/userpassword/userpassword.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ func Compare(hashed string, password string) (bool, error) {
3535
if len(parts[0]) != 0 {
3636
return false, xerrors.Errorf("hash prefix is invalid")
3737
}
38-
if string(parts[1]) != hashScheme {
38+
if parts[1] != hashScheme {
3939
return false, xerrors.Errorf("hash isn't %q scheme: %q", hashScheme, parts[1])
4040
}
41-
iter, err := strconv.Atoi(string(parts[2]))
41+
iter, err := strconv.Atoi(parts[2])
4242
if err != nil {
4343
return false, xerrors.Errorf("parse iter from hash: %w", err)
4444
}
45-
salt, err := base64.RawStdEncoding.DecodeString(string(parts[3]))
45+
salt, err := base64.RawStdEncoding.DecodeString(parts[3])
4646
if err != nil {
4747
return false, xerrors.Errorf("decode salt: %w", err)
4848
}

coderd/users.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
7070
})
7171
return
7272
}
73-
user, err := users.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
73+
_, err = users.Database.GetUserByEmailOrUsername(r.Context(), database.GetUserByEmailOrUsernameParams{
7474
Email: createUser.Email,
7575
Username: createUser.Username,
7676
})
@@ -91,7 +91,7 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
9191
return
9292
}
9393

94-
user, err = users.Database.InsertUser(context.Background(), database.InsertUserParams{
94+
user, err := users.Database.InsertUser(context.Background(), database.InsertUserParams{
9595
ID: uuid.NewString(),
9696
Email: createUser.Email,
9797
HashedPassword: []byte(hashedPassword),
@@ -111,7 +111,7 @@ func (users *users) createInitialUser(rw http.ResponseWriter, r *http.Request) {
111111
}
112112

113113
// Returns the currently authenticated user.
114-
func (users *users) getAuthenticatedUser(rw http.ResponseWriter, r *http.Request) {
114+
func (*users) authenticatedUser(rw http.ResponseWriter, r *http.Request) {
115115
user := httpmw.User(r)
116116

117117
render.JSON(rw, r, User{
@@ -158,11 +158,17 @@ func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
158158
return
159159
}
160160

161-
id, secret, err := generateAPIKeyIDSecret()
162-
hashed := sha256.Sum256([]byte(secret))
161+
keyID, keySecret, err := generateAPIKeyIDSecret()
162+
if err != nil {
163+
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
164+
Message: fmt.Sprintf("generate api key parts: %s", err.Error()),
165+
})
166+
return
167+
}
168+
hashed := sha256.Sum256([]byte(keySecret))
163169

164170
_, err = users.Database.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{
165-
ID: id,
171+
ID: keyID,
166172
UserID: user.ID,
167173
ExpiresAt: database.Now().Add(24 * time.Hour),
168174
CreatedAt: database.Now(),
@@ -178,7 +184,7 @@ func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
178184
}
179185

180186
// This format is consumed by the APIKey middleware.
181-
sessionToken := fmt.Sprintf("%s-%s", id, secret)
187+
sessionToken := fmt.Sprintf("%s-%s", keyID, keySecret)
182188
http.SetCookie(rw, &http.Cookie{
183189
Name: httpmw.AuthCookie,
184190
Value: sessionToken,
@@ -194,14 +200,14 @@ func (users *users) loginWithPassword(rw http.ResponseWriter, r *http.Request) {
194200
}
195201

196202
// Generates a new ID and secret for an API key.
197-
func generateAPIKeyIDSecret() (string, string, error) {
203+
func generateAPIKeyIDSecret() (id string, secret string, err error) {
198204
// Length of an API Key ID.
199-
id, err := cryptorand.String(10)
205+
id, err = cryptorand.String(10)
200206
if err != nil {
201207
return "", "", err
202208
}
203209
// Length of an API Key secret.
204-
secret, err := cryptorand.String(22)
210+
secret, err = cryptorand.String(22)
205211
if err != nil {
206212
return "", "", err
207213
}

coderd/users_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55
"testing"
66

7+
"github.com/stretchr/testify/require"
8+
79
"github.com/coder/coder/coderd"
810
"github.com/coder/coder/coderd/coderdtest"
9-
"github.com/stretchr/testify/require"
1011
)
1112

1213
func TestUsers(t *testing.T) {

codersdk/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
)
1919

2020
// New creates a Coder client for the provided URL.
21-
func New(url *url.URL) *Client {
21+
func New(serverURL *url.URL) *Client {
2222
return &Client{
23-
url: url,
23+
url: serverURL,
2424
httpClient: &http.Client{},
2525
}
2626
}
@@ -50,7 +50,7 @@ func (c *Client) SetSessionToken(token string) error {
5050
// request performs an HTTP request with the body provided.
5151
// The caller is responsible for closing the response body.
5252
func (c *Client) request(ctx context.Context, method, path string, body interface{}) (*http.Response, error) {
53-
url, err := c.url.Parse(path)
53+
serverURL, err := c.url.Parse(path)
5454
if err != nil {
5555
return nil, xerrors.Errorf("parse url: %w", err)
5656
}
@@ -65,7 +65,7 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
6565
}
6666
}
6767

68-
req, err := http.NewRequestWithContext(ctx, method, url.String(), &buf)
68+
req, err := http.NewRequestWithContext(ctx, method, serverURL.String(), &buf)
6969
if err != nil {
7070
return nil, xerrors.Errorf("create request: %w", err)
7171
}
@@ -81,7 +81,7 @@ func (c *Client) request(ctx context.Context, method, path string, body interfac
8181
}
8282

8383
// readBodyAsError reads the response as an httpapi.Message, and
84-
// wraps it in a codersdk.Error type for easy marshalling.
84+
// wraps it in a codersdk.Error type for easy marshaling.
8585
func readBodyAsError(res *http.Response) error {
8686
var m httpapi.Response
8787
err := json.NewDecoder(res.Body).Decode(&m)

codersdk/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *Client) CreateInitialUser(ctx context.Context, req coderd.CreateUserReq
2626

2727
// User returns a user for the ID provided.
2828
// If the ID string is empty, the current user will be returned.
29-
func (c *Client) User(ctx context.Context, id string) (coderd.User, error) {
29+
func (c *Client) User(ctx context.Context, _ string) (coderd.User, error) {
3030
res, err := c.request(ctx, http.MethodGet, "/api/v2/user", nil)
3131
if err != nil {
3232
return coderd.User{}, err

codersdk/users_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
"net/http"
66
"testing"
77

8+
"github.com/stretchr/testify/require"
9+
810
"github.com/coder/coder/coderd"
911
"github.com/coder/coder/coderd/coderdtest"
1012
"github.com/coder/coder/codersdk"
11-
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestUsers(t *testing.T) {

cryptorand/numbers.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,42 +73,43 @@ func Int() (int, error) {
7373
return int(i), nil
7474
}
7575

76-
// Int63n returns a non-negative random integer in [0,n) as a int64.
77-
func Int63n(n int64) (int64, error) {
78-
if n <= 0 {
76+
// Int63n returns a non-negative random integer in [0,max) as a int64.
77+
func Int63n(max int64) (int64, error) {
78+
if max <= 0 {
7979
panic("invalid argument to Int63n")
8080
}
8181

82-
max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
82+
trueMax := int64((1 << 63) - 1 - (1<<63)%uint64(max))
8383
i, err := Int63()
8484
if err != nil {
8585
return 0, err
8686
}
8787

88-
for i > max {
88+
for i > trueMax {
8989
i, err = Int63()
9090
if err != nil {
9191
return 0, err
9292
}
9393
}
9494

95-
return i % n, nil
95+
return i % max, nil
9696
}
9797

98-
// Int31n returns a non-negative integer in [0,n) as a int32.
99-
func Int31n(n int32) (int32, error) {
98+
// Int31n returns a non-negative integer in [0,max) as a int32.
99+
func Int31n(max int32) (int32, error) {
100100
i, err := Uint32()
101101
if err != nil {
102102
return 0, err
103103
}
104104

105-
return UnbiasedModulo32(i, n)
105+
return UnbiasedModulo32(i, max)
106106
}
107107

108108
// UnbiasedModulo32 uniformly modulos v by n over a sufficiently large data
109109
// set, regenerating v if necessary. n must be > 0. All input bits in v must be
110110
// fully random, you cannot cast a random uint8/uint16 for input into this
111111
// function.
112+
//nolint:varnamelen
112113
func UnbiasedModulo32(v uint32, n int32) (int32, error) {
113114
prod := uint64(v) * uint64(n)
114115
low := uint32(prod)
@@ -127,22 +128,22 @@ func UnbiasedModulo32(v uint32, n int32) (int32, error) {
127128
return int32(prod >> 32), nil
128129
}
129130

130-
// Intn returns a non-negative integer in [0,n) as a int.
131-
func Intn(n int) (int, error) {
132-
if n <= 0 {
131+
// Intn returns a non-negative integer in [0,max) as a int.
132+
func Intn(max int) (int, error) {
133+
if max <= 0 {
133134
panic("n must be a positive nonzero number")
134135
}
135136

136-
if n <= 1<<31-1 {
137-
i, err := Int31n(int32(n))
137+
if max <= 1<<31-1 {
138+
i, err := Int31n(int32(max))
138139
if err != nil {
139140
return 0, err
140141
}
141142

142143
return int(i), nil
143144
}
144145

145-
i, err := Int63n(int64(n))
146+
i, err := Int63n(int64(max))
146147
if err != nil {
147148
return 0, err
148149
}

cryptorand/numbers_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"encoding/binary"
66
"testing"
77

8-
"github.com/coder/coder/cryptorand"
98
"github.com/stretchr/testify/require"
9+
10+
"github.com/coder/coder/cryptorand"
1011
)
1112

1213
func TestInt63(t *testing.T) {
@@ -144,7 +145,7 @@ func TestBool(t *testing.T) {
144145
const iterations = 10000
145146
trueCount := 0
146147

147-
for i := 0; i < iterations; i += 1 {
148+
for i := 0; i < iterations; i++ {
148149
v, err := cryptorand.Bool()
149150
require.NoError(t, err, "unexpected error from Bool")
150151
if v {

cryptorand/strings.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ func StringCharset(charSetStr string, size int) (string, error) {
5353
buf.Grow(size)
5454

5555
for i := 0; i < size; i++ {
56-
c, err := UnbiasedModulo32(
56+
count, err := UnbiasedModulo32(
5757
binary.BigEndian.Uint32(ibuf[i*4:(i+1)*4]),
5858
int32(len(charSet)),
5959
)
6060
if err != nil {
6161
return "", err
6262
}
6363

64-
_, _ = buf.WriteRune(charSet[c])
64+
_, _ = buf.WriteRune(charSet[count])
6565
}
6666

6767
return buf.String(), nil

0 commit comments

Comments
 (0)