Skip to content

Commit 1344e30

Browse files
committed
cleanup and linter
1 parent 8e5dcf8 commit 1344e30

File tree

7 files changed

+60
-40
lines changed

7 files changed

+60
-40
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ jobs:
6161
run: go install honnef.co/go/tools/cmd/[email protected]
6262

6363
- name: Install golangci-lint
64-
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.64.8
64+
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@v2.1.2
6565

6666
- name: Install NilAway
6767
run: go install go.uber.org/nilaway/cmd/[email protected]

.golangci.yaml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
version: "2"
12
run:
23
tests: false
34
linters:
@@ -6,7 +7,6 @@ linters:
67
- cyclop
78
- forbidigo
89
- funlen
9-
- gci
1010
- gochecknoglobals
1111
- gochecknoinits
1212
- gocritic
@@ -43,7 +43,6 @@ linters:
4343
#
4444
# Disabled because deprecated:
4545
#
46-
- tenv
4746

4847
linters-settings:
4948
#
@@ -91,3 +90,18 @@ linters-settings:
9190
- 'MockBeaconClient'
9291
- 'RelayAPI'
9392
- 'Webserver'
93+
formatters:
94+
enable:
95+
- gci
96+
- gofmt
97+
- gofumpt
98+
- goimports
99+
settings:
100+
gofumpt:
101+
extra-rules: true
102+
exclusions:
103+
generated: lax
104+
paths:
105+
- third_party$
106+
- builtin$
107+
- examples$

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
VERSION := $(shell git describe --tags --always --dirty="-dev")
66

7+
# Admin API auth for curl examples (set ADMIN_AUTH="admin:secret" or similar. can be empty when server is run with --disable-admin-auth)
8+
ADMIN_AUTH ?=
9+
CURL ?= curl -v
10+
CURL_AUTH := $(if $(ADMIN_AUTH),-u $(ADMIN_AUTH),)
11+
712
# A few colors
813
RED:=\033[0;31m
914
BLUE:=\033[0;34m
@@ -92,16 +97,16 @@ db-dump: ## Dump the database contents to file 'database.dump'
9297
.PHONY: dev-db-setup
9398
dev-db-setup: ## Create the basic database entries for testing and development
9499
@printf "$(BLUE)Create the allow-all measurements $(NC)\n"
95-
curl -v --request POST --url http://localhost:8081/api/admin/v1/measurements --data '{"measurement_id": "test1","attestation_type": "test","measurements": {}}'
100+
$(CURL) $(CURL_AUTH) --request POST --url http://localhost:8081/api/admin/v1/measurements --data '{"measurement_id": "test1","attestation_type": "test","measurements": {}}'
96101

97102
@printf "$(BLUE)Enable the measurements $(NC)\n"
98-
curl -v --request POST --url http://localhost:8081/api/admin/v1/measurements/activation/test1 --data '{"enabled": true}'
103+
$(CURL) $(CURL_AUTH) --request POST --url http://localhost:8081/api/admin/v1/measurements/activation/test1 --data '{"enabled": true}'
99104

100105
@printf "$(BLUE)Create the builder $(NC)\n"
101-
curl -v --request POST --url http://localhost:8081/api/admin/v1/builders --data '{"name": "test_builder","ip_address": "1.2.3.4", "network": "production"}'
106+
$(CURL) $(CURL_AUTH) --request POST --url http://localhost:8081/api/admin/v1/builders --data '{"name": "test_builder","ip_address": "1.2.3.4", "network": "production"}'
102107

103108
@printf "$(BLUE)Create the builder configuration $(NC)\n"
104-
curl -v --request POST --url http://localhost:8081/api/admin/v1/builders/configuration/test_builder --data '{"dns_name": "foobar-v1.a.b.c","rbuilder": {"extra_data": "FooBar"}}'
109+
$(CURL) $(CURL_AUTH) --request POST --url http://localhost:8081/api/admin/v1/builders/configuration/test_builder --data '{"dns_name": "foobar-v1.a.b.c","rbuilder": {"extra_data": "FooBar"}}'
105110

106111
@printf "$(BLUE)Enable the builder $(NC)\n"
107-
curl -v --request POST --url http://localhost:8081/api/admin/v1/builders/activation/test_builder --data '{"enabled": true}'
112+
$(CURL) $(CURL_AUTH) --request POST --url http://localhost:8081/api/admin/v1/builders/activation/test_builder --data '{"enabled": true}'

adapters/database/service.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func NewDatabaseService(dsn string) (*Service, error) {
2525
return nil, err
2626
}
2727

28-
db.DB.SetMaxOpenConns(50)
29-
db.DB.SetMaxIdleConns(10)
30-
db.DB.SetConnMaxIdleTime(0)
28+
db.SetMaxOpenConns(50)
29+
db.SetMaxIdleConns(10)
30+
db.SetConnMaxIdleTime(0)
3131

3232
dbService := &Service{DB: db} //nolint:exhaustruct
3333
return dbService, err
@@ -117,9 +117,9 @@ func (s *Service) RegisterCredentialsForBuilder(ctx context.Context, builderName
117117
}
118118

119119
_, err = tx.Exec(`
120-
INSERT INTO service_credential_registrations
120+
INSERT INTO service_credential_registrations
121121
(builder_name, service, tls_cert, ecdsa_pubkey, is_active, measurement_id)
122-
VALUES ($1, $2, $3, $4, true,
122+
VALUES ($1, $2, $3, $4, true,
123123
(SELECT id FROM measurements_whitelist WHERE name = $5 AND attestation_type = $6)
124124
)
125125
`, builderName, service, nullableTLSCert, ecdsaPubKey, measurementName, attestationType)
@@ -150,26 +150,26 @@ func (s *Service) GetActiveConfigForBuilder(ctx context.Context, builderName str
150150

151151
func (s *Service) GetActiveBuildersWithServiceCredentials(ctx context.Context, network string) ([]domain.BuilderWithServices, error) {
152152
rows, err := s.DB.QueryContext(ctx, `
153-
SELECT
153+
SELECT
154154
b.name,
155155
b.ip_address,
156156
b.dns_name,
157157
scr.service,
158158
scr.tls_cert,
159159
scr.ecdsa_pubkey
160-
FROM
160+
FROM
161161
builders b
162-
LEFT JOIN
162+
LEFT JOIN
163163
service_credential_registrations scr ON b.name = scr.builder_name
164-
WHERE
164+
WHERE
165165
b.is_active = true AND (scr.is_active = true OR scr.is_active IS NULL) AND b.network = $1
166-
ORDER BY
166+
ORDER BY
167167
b.name, scr.service
168168
`, network)
169169
if err != nil {
170170
return nil, err
171171
}
172-
defer rows.Close()
172+
defer rows.Close() //nolint:errcheck
173173

174174
buildersMap := make(map[string]*BuilderWithCredentials)
175175

@@ -227,9 +227,9 @@ func (s *Service) GetActiveBuildersWithServiceCredentials(ctx context.Context, n
227227
func (s *Service) LogEvent(ctx context.Context, eventName, builderName, name string) error {
228228
// Insert new event log entry with a subquery to fetch the measurement_id
229229
_, err := s.DB.ExecContext(ctx, `
230-
INSERT INTO event_log
230+
INSERT INTO event_log
231231
(event_name, builder_name, measurement_id)
232-
VALUES ($1, $2,
232+
VALUES ($1, $2,
233233
(SELECT id FROM measurements_whitelist WHERE name = $3)
234234
)
235235
`, eventName, builderName, name)

cmd/httpserver/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func runCli(cCtx *cli.Context) error {
171171
log.Error("failed to create database", "err", err)
172172
return err
173173
}
174-
defer db.Close()
174+
defer db.Close() //nolint:errcheck
175175

176176
var sm ports.AdminSecretService
177177

docker/docker-compose.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ services:
4141
ADMIN_ADDR: "0.0.0.0:8081"
4242
INTERNAL_ADDR: "0.0.0.0:8082"
4343
METRICS_ADDR: "0.0.0.0:8090"
44+
DISABLE_ADMIN_AUTH: "1" # local dev only; do not use in production
4445

4546
proxy:
4647
image: flashbots/builder-hub-mock-proxy

httpserver/auth_middleware_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ func protectedHandler(t *testing.T, user, bcryptHash string) (http.Handler, *Ser
2828
}
2929

3030
func protectedHandlerDisabled(t *testing.T, disabled bool) http.Handler {
31-
t.Helper()
32-
srv := &Server{cfg: &HTTPServerConfig{AdminAuthDisabled: disabled}, log: testLogger()}
33-
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34-
w.WriteHeader(http.StatusOK)
35-
_, _ = fmt.Fprint(w, "ok")
36-
})
37-
return srv.basicAuthMiddleware()(next)
31+
t.Helper()
32+
srv := &Server{cfg: &HTTPServerConfig{AdminAuthDisabled: disabled}, log: testLogger()}
33+
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34+
w.WriteHeader(http.StatusOK)
35+
_, _ = fmt.Fprint(w, "ok")
36+
})
37+
return srv.basicAuthMiddleware()(next)
3838
}
3939

4040
func Test_AdminAuth_DeniesWithoutHash(t *testing.T) {
@@ -78,17 +78,17 @@ func Test_AdminAuth_AllowsCorrectCreds(t *testing.T) {
7878
}
7979

8080
func Test_AdminAuth_Disabled_AllowsWithoutCreds(t *testing.T) {
81-
h := protectedHandlerDisabled(t, true)
82-
req := httptest.NewRequest(http.MethodGet, "/", nil)
83-
rr := httptest.NewRecorder()
84-
h.ServeHTTP(rr, req)
85-
require.Equal(t, http.StatusOK, rr.Code)
86-
87-
// When not disabled, should be unauthorized without hash/creds
88-
h2 := protectedHandlerDisabled(t, false)
89-
rr2 := httptest.NewRecorder()
90-
h2.ServeHTTP(rr2, req)
91-
require.Equal(t, http.StatusUnauthorized, rr2.Code)
81+
h := protectedHandlerDisabled(t, true)
82+
req := httptest.NewRequest(http.MethodGet, "/", nil)
83+
rr := httptest.NewRecorder()
84+
h.ServeHTTP(rr, req)
85+
require.Equal(t, http.StatusOK, rr.Code)
86+
87+
// When not disabled, should be unauthorized without hash/creds
88+
h2 := protectedHandlerDisabled(t, false)
89+
rr2 := httptest.NewRecorder()
90+
h2.ServeHTTP(rr2, req)
91+
require.Equal(t, http.StatusUnauthorized, rr2.Code)
9292
}
9393

9494
func Test_PasswordHash(t *testing.T) {

0 commit comments

Comments
 (0)