-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consistent auth token for validator apis #13747
Changes from all commits
b5dfd5e
d575b2f
ad72aa4
cb156d7
3629ebe
82c0b33
7435093
352812c
76920bd
4f7ad89
377f53b
722c096
4a0fc69
6d20c9b
a2c9e52
2b9eda6
1173ad8
415f6ad
00a2ca6
6a01eb4
9b2c1e3
ad42e4b
25741af
e96ca2c
2af2f05
2aea1c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
load("@prysm//tools/go:def.bzl", "go_library") | ||
load("@prysm//tools/go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = [ | ||
"constants.go", | ||
"headers.go", | ||
"jwt.go", | ||
], | ||
importpath = "github.com/prysmaticlabs/prysm/v5/api", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//crypto/rand:go_default_library", | ||
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library", | ||
"@com_github_pkg_errors//:go_default_library", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["jwt_test.go"], | ||
embed = [":go_default_library"], | ||
deps = ["//testing/require:go_default_library"], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/pkg/errors" | ||
"github.com/prysmaticlabs/prysm/v5/crypto/rand" | ||
) | ||
|
||
// GenerateRandomHexString generates a random hex string that follows the standards for jwt token | ||
// used for beacon node -> execution client | ||
// used for web client -> validator client | ||
func GenerateRandomHexString() (string, error) { | ||
secret := make([]byte, 32) | ||
randGen := rand.NewGenerator() | ||
n, err := randGen.Read(secret) | ||
if err != nil { | ||
return "", err | ||
} else if n != 32 { | ||
return "", errors.New("rand: unexpected length") | ||
} | ||
return hexutil.Encode(secret), nil | ||
} | ||
|
||
// ValidateAuthToken validating auth token for web | ||
func ValidateAuthToken(token string) error { | ||
b, err := hexutil.Decode(token) | ||
// token should be hex-encoded and at least 256 bits | ||
if err != nil || len(b) < 32 { | ||
return errors.New("invalid auth token: token should be hex-encoded and at least 256 bits") | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package api | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
) | ||
|
||
func TestGenerateRandomHexString(t *testing.T) { | ||
token, err := GenerateRandomHexString() | ||
require.NoError(t, err) | ||
require.NoError(t, ValidateAuthToken(token)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ package web | |
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/prysmaticlabs/prysm/v5/api" | ||
"github.com/prysmaticlabs/prysm/v5/cmd" | ||
"github.com/prysmaticlabs/prysm/v5/cmd/validator/flags" | ||
"github.com/prysmaticlabs/prysm/v5/config/features" | ||
|
@@ -24,6 +26,7 @@ var Commands = &cli.Command{ | |
flags.WalletDirFlag, | ||
flags.GRPCGatewayHost, | ||
flags.GRPCGatewayPort, | ||
flags.AuthTokenPathFlag, | ||
cmd.AcceptTosFlag, | ||
}), | ||
Before: func(cliCtx *cli.Context) error { | ||
|
@@ -43,7 +46,12 @@ var Commands = &cli.Command{ | |
gatewayHost := cliCtx.String(flags.GRPCGatewayHost.Name) | ||
gatewayPort := cliCtx.Int(flags.GRPCGatewayPort.Name) | ||
validatorWebAddr := fmt.Sprintf("%s:%d", gatewayHost, gatewayPort) | ||
if err := rpc.CreateAuthToken(walletDirPath, validatorWebAddr); err != nil { | ||
authTokenPath := filepath.Join(walletDirPath, api.AuthTokenFileName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
tempAuthTokenPath := cliCtx.String(flags.AuthTokenPathFlag.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure what this means, i think accepttosflag is at the edge where the command is launched |
||
if tempAuthTokenPath != "" { | ||
authTokenPath = tempAuthTokenPath | ||
} | ||
if err := rpc.CreateAuthToken(authTokenPath, validatorWebAddr); err != nil { | ||
log.WithError(err).Fatal("Could not create web auth token") | ||
} | ||
return nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -639,6 +639,17 @@ func (c *ValidatorClient) registerRPCService(router *mux.Router) error { | |
walletDir := c.cliCtx.String(flags.WalletDirFlag.Name) | ||
grpcHeaders := c.cliCtx.String(flags.GrpcHeadersFlag.Name) | ||
clientCert := c.cliCtx.String(flags.CertFlag.Name) | ||
|
||
authTokenPath := c.cliCtx.String(flags.AuthTokenPathFlag.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it would be worth to add some comments here to explain how |
||
// if no auth token path flag was passed try to set a default value | ||
if authTokenPath == "" { | ||
authTokenPath = flags.AuthTokenPathFlag.Value | ||
// if a wallet dir is passed without an auth token then override the default with the wallet dir | ||
if walletDir != "" { | ||
authTokenPath = filepath.Join(walletDir, api.AuthTokenFileName) | ||
} | ||
} | ||
|
||
server := rpc.NewServer(c.cliCtx.Context, &rpc.Config{ | ||
ValDB: c.db, | ||
Host: rpcHost, | ||
|
@@ -648,6 +659,7 @@ func (c *ValidatorClient) registerRPCService(router *mux.Router) error { | |
SyncChecker: vs, | ||
GenesisFetcher: vs, | ||
NodeGatewayEndpoint: nodeGatewayEndpoint, | ||
AuthTokenPath: authTokenPath, | ||
WalletDir: walletDir, | ||
Wallet: c.wallet, | ||
ValidatorGatewayHost: validatorGatewayHost, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably include the term
jwt
here, for when people grep through help textThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm...seems like the old thing is called jwt