Skip to content

Commit 206491f

Browse files
committed
Lint
1 parent d4ab6fd commit 206491f

7 files changed

+614
-8
lines changed

go.mod

+38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
11
module github.com/Rocket-Rescue-Node/guarded-beacon-proxy
22

33
go 1.19
4+
5+
require (
6+
github.com/ethereum/go-ethereum v1.12.0
7+
github.com/gorilla/mux v1.8.0
8+
github.com/mwitkow/grpc-proxy v0.0.0-20230212185441-f345521cb9c9
9+
github.com/prysmaticlabs/prysm/v3 v3.2.2
10+
google.golang.org/grpc v1.55.0
11+
google.golang.org/protobuf v1.30.0
12+
)
13+
14+
require (
15+
github.com/beorn7/perks v1.0.1 // indirect
16+
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
17+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
18+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
19+
github.com/golang/protobuf v1.5.3 // indirect
20+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.0.1 // indirect
21+
github.com/holiman/uint256 v1.2.2-0.20230321075855-87b91420868c // indirect
22+
github.com/klauspost/cpuid/v2 v2.2.1 // indirect
23+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
24+
github.com/minio/sha256-simd v1.0.0 // indirect
25+
github.com/mitchellh/mapstructure v1.4.1 // indirect
26+
github.com/pkg/errors v0.9.1 // indirect
27+
github.com/prometheus/client_golang v1.14.0 // indirect
28+
github.com/prometheus/client_model v0.3.0 // indirect
29+
github.com/prometheus/common v0.39.0 // indirect
30+
github.com/prometheus/procfs v0.9.0 // indirect
31+
github.com/prysmaticlabs/fastssz v0.0.0-20220628121656-93dfe28febab // indirect
32+
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect
33+
github.com/prysmaticlabs/gohashtree v0.0.2-alpha // indirect
34+
github.com/thomaso-mirodin/intmath v0.0.0-20160323211736-5dc6d854e46e // indirect
35+
golang.org/x/crypto v0.5.0 // indirect
36+
golang.org/x/net v0.8.0 // indirect
37+
golang.org/x/sys v0.7.0 // indirect
38+
golang.org/x/text v0.8.0 // indirect
39+
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
40+
gopkg.in/yaml.v2 v2.4.0 // indirect
41+
)

go.sum

+502
Large diffs are not rendered by default.

grpc.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package guarded_beacon_proxy
1+
package guardedbeaconproxy
22

33
import (
44
"context"
@@ -18,6 +18,18 @@ import (
1818
"google.golang.org/protobuf/proto"
1919
)
2020

21+
// GRPCAuthenticator is a function type that authenticates gRPC traffic.
22+
// The authentication method must be based on gRPC Metadata, as gRPC does not
23+
// support BasicAuth out of box.
24+
//
25+
// Returning an AuthenticationStatus other than Allowed will prevent the request
26+
// from being proxied. You may optionally return a Context, which will be passed
27+
// to the PrepareBeaconProposerGuard/RegisterValidatorGuard functions provided.
28+
// In particular, conext.WithValue allows the authentication method to share state
29+
// with the guard methods.
30+
//
31+
// Any error returned will be sent back to the client, so do not encode sensitive
32+
// information.
2133
type GRPCAuthenticator func(metadata.MD) (AuthenticationStatus, context.Context, error)
2234

2335
type prepareBeaconProposerStreamGuard struct {

guarded-beacon-proxy.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package guarded_beacon_proxy
1+
package guardedbeaconproxy
22

33
import (
44
"context"
@@ -14,9 +14,26 @@ import (
1414
"google.golang.org/grpc"
1515
)
1616

17+
// PrepareBeaconProposerGuard is a function that validates whether or not a PrepareBeaconProposer call
18+
// should be proxied. The provided Context is whatever was returned by the authenticator.
1719
type PrepareBeaconProposerGuard func(PrepareBeaconProposerRequest, context.Context) (AuthenticationStatus, error)
20+
21+
// RegisterValidatorGuard is a function that validates whether or not a RegisterValidator call
22+
// should be proxied. The provided Context is whatever was returned by the authenticator.
1823
type RegisterValidatorGuard func(RegisterValidatorRequest, context.Context) (AuthenticationStatus, error)
1924

25+
// GuardedBeaconProxy is a reverse proxy for guarding beacon nodes with custom logic.
26+
//
27+
// The main goal is to provide easy hooks for custom request authentication and fee recipient
28+
// validation, which is achieved through the Authenticator and Guard callbacks.
29+
//
30+
// Since Prysm uses gRPC, GuardedBeaconProxy can optionally run a gRPC reverse
31+
// proxy in addition to an HTTP reverse proxy.
32+
//
33+
// If GRPCBeaconURL is set, all GRPC fields are required except the TLS block.
34+
// TLS is currently only supported for gRPC.
35+
//
36+
// Fields in GuardedBeaconProxy should be set prior to calling ListenAndServe.
2037
type GuardedBeaconProxy struct {
2138
// URL of the upstream beacon node
2239
BeaconURL *url.URL
@@ -60,15 +77,30 @@ type GuardedBeaconProxy struct {
6077
upstream *grpc.ClientConn
6178
}
6279

80+
// Stop attempts to gracefully shut down the GuardedBeaconProxy.
81+
//
82+
// After gracePeriod has elapsed, the GuardedBeaconProxy will be
83+
// immediately stopped instead.
6384
func (gbp *GuardedBeaconProxy) Stop(gracePeriod time.Duration) {
6485
go func() {
6586
time.Sleep(gracePeriod)
6687
gbp.server.Close()
88+
if gbp.gRPCProxy != nil {
89+
gbp.gRPCProxy.Stop()
90+
}
6791
}()
6892

69-
gbp.server.Shutdown(context.Background())
93+
if gbp.gRPCProxy != nil {
94+
go gbp.gRPCProxy.GracefulStop()
95+
}
96+
go gbp.server.Shutdown(context.Background())
7097
}
7198

99+
// ListenAndServe binds the GuardedBeaconProxy to its HTTP port, and
100+
// optionally its gRPC port, and prepares to receive and proxy
101+
// traffic from validators.
102+
//
103+
// ListenAndServe blocks until Stop is called or an error is encountered.
72104
func (gbp *GuardedBeaconProxy) ListenAndServe() error {
73105

74106
gbp.server.Addr = gbp.Addr
@@ -133,6 +165,7 @@ func (gbp *GuardedBeaconProxy) ListenAndServe() error {
133165
grpcErrChan <- gbp.gRPCProxy.Serve(listener)
134166
close(grpcErrChan)
135167
}()
168+
defer gbp.gRPCProxy.Stop()
136169

137170
select {
138171
case httpErr := <-httpErrChan:
@@ -141,5 +174,4 @@ func (gbp *GuardedBeaconProxy) ListenAndServe() error {
141174
return grpcErr
142175
}
143176

144-
return nil
145177
}

http.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package guarded_beacon_proxy
1+
package guardedbeaconproxy
22

33
import (
44
"bytes"
@@ -9,6 +9,17 @@ import (
99
"net/http"
1010
)
1111

12+
// HTTPAuthenticator is a function type which can authenticate HTTP requests.
13+
// For example, by checking the contents of the BasicAuth header.
14+
//
15+
// Returning an AuthenticationStatus other than Allowed will prevent the request
16+
// from being proxied. You may optionally return a Context, which will be passed
17+
// to the PrepareBeaconProposerGuard/RegisterValidatorGuard functions provided.
18+
// In particular, conext.WithValue allows the authentication method to share state
19+
// with the guard methods.
20+
//
21+
// Any error returned will be sent back to the client, so do not encode sensitive
22+
// information.
1223
type HTTPAuthenticator func(*http.Request) (AuthenticationStatus, context.Context, error)
1324

1425
func (gbp *GuardedBeaconProxy) authenticationMiddleware(next http.Handler) http.Handler {
@@ -25,7 +36,6 @@ func (gbp *GuardedBeaconProxy) authenticationMiddleware(next http.Handler) http.
2536
}
2637

2738
gbp.httpError(w, status.httpStatus(), err)
28-
return
2939
})
3040
}
3141

status.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
package guarded_beacon_proxy
1+
package guardedbeaconproxy
22

33
import (
44
"net/http"
55

66
"google.golang.org/grpc/codes"
77
)
88

9+
// AuthenticationStatus is a generic status response representing auth or guard
10+
// results.
11+
//
12+
// It is returned by the custom authentication or guard functions on the GuardedBeaconProxy,
13+
// and mapped to an appropriate HTTP or gRPC error as needed.
914
type AuthenticationStatus uint32
1015

16+
// These constants are the only allowable AuthenticationStatus values
1117
const (
1218
Allowed AuthenticationStatus = iota
1319
BadRequest

types.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
package guarded_beacon_proxy
1+
package guardedbeaconproxy
22

3+
// PrepareBeaconProposerRequest is the in-memory representation of a
4+
// prepare_beacon_proposer API call, be it gRPC or HTTP.
35
type PrepareBeaconProposerRequest []struct {
46
ValidatorIndex string `json:"validator_index"`
57
FeeRecipient string `json:"fee_recipient"`
68
}
79

10+
// RegisterValidatorMessage is the in-memory representation of a
11+
// register_validator API call entry, be it gRPC or HTTP.
812
type RegisterValidatorMessage struct {
913
FeeRecipient string `json:"fee_recipient"`
1014
GasLimit string `json:"gas_limit"`
1115
Timestamp string `json:"timestamp"`
1216
Pubkey string `json:"pubkey"`
1317
}
1418

19+
// RegisterValidatorRequest is the in-memory representation of a
20+
// register_validator API call, be it gRPC or HTTP.
1521
type RegisterValidatorRequest []struct {
1622
Message RegisterValidatorMessage `json:"message"`
1723
Signature string `json:"signature"`

0 commit comments

Comments
 (0)