Web3Signer Flag support List in YML config (#11041)

* initial commit

* fixing linting

* fixing linting

* fixing deepsource

* changing visibility of validator flags

* updating package information

* fixing typo

* fixing another typo

* testing bazel config

* fixing linting

* fixing build

* switching flag to stringslice and adding unit tests

* fixing bazel

* rolling back bazel

* migrating unit test into validator flags

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
james-prysm 2022-07-14 12:34:59 -04:00 committed by GitHub
parent 2162ffb05f
commit 96aba590b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 138 additions and 49 deletions

View File

@ -1,4 +1,4 @@
load("@prysm//tools/go:def.bzl", "go_library")
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
@ -18,3 +18,14 @@ go_library(
"@com_github_urfave_cli_v2//:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["flags_test.go"],
embed = [":go_default_library"],
deps = [
"//cmd:go_default_library",
"//testing/require:go_default_library",
"@com_github_urfave_cli_v2//:go_default_library",
],
)

View File

@ -281,10 +281,9 @@ var (
// example with external url: --validators-external-signer-public-keys= https://web3signer.com/api/v1/eth2/publicKeys
// example with public key: --validators-external-signer-public-keys=0xa99a...e44c,0xb89b...4a0b
// web3signer documentation can be found in Consensys' web3signer project docs```
Web3SignerPublicValidatorKeysFlag = &cli.StringFlag{
Web3SignerPublicValidatorKeysFlag = &cli.StringSliceFlag{
Name: "validators-external-signer-public-keys",
Usage: "comma separated list of public keys OR an external url endpoint for the validator to retrieve public keys from for usage with web3signer",
Value: "",
}
// KeymanagerKindFlag defines the kind of keymanager desired by a user during wallet creation.

View File

@ -0,0 +1,49 @@
package flags
import (
"flag"
"fmt"
"os"
"strings"
"testing"
"github.com/prysmaticlabs/prysm/cmd"
"github.com/prysmaticlabs/prysm/testing/require"
"github.com/urfave/cli/v2"
)
func TestLoadFlagsFromConfig_PreProcessing_Web3signer(t *testing.T) {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
context := cli.NewContext(&app, set, nil)
pubkey1 := "0xbd36226746676565cd40141a7f0fe1445b9a3fbeb222288b226392c4b230ed0b"
pubkey2 := "0xbd36226746676565cd40141a7f0fe1445b9a3fbeb222288b226392c4b230ed0a"
require.NoError(t, os.WriteFile("flags_test.yaml", []byte(fmt.Sprintf("%s:\n - %s\n - %s\n", Web3SignerPublicValidatorKeysFlag.Name,
pubkey1,
pubkey2)), 0666))
require.NoError(t, set.Parse([]string{"test-command", "--" + cmd.ConfigFileFlag.Name, "flags_test.yaml"}))
command := &cli.Command{
Name: "test-command",
Flags: cmd.WrapFlags([]cli.Flag{
&cli.StringFlag{
Name: cmd.ConfigFileFlag.Name,
},
&cli.StringSliceFlag{
Name: Web3SignerPublicValidatorKeysFlag.Name,
},
}),
Before: func(cliCtx *cli.Context) error {
return cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags)
},
Action: func(cliCtx *cli.Context) error {
require.Equal(t, strings.Join([]string{pubkey1, pubkey2}, ","),
strings.Join(cliCtx.StringSlice(Web3SignerPublicValidatorKeysFlag.Name), ","))
return nil
},
}
require.NoError(t, command.Run(context))
require.NoError(t, os.Remove("flags_test.yaml"))
}

View File

@ -457,13 +457,21 @@ func web3SignerConfig(cliCtx *cli.Context) (*remoteweb3signer.SetupConfig, error
log.Warnf("%s was provided while using web3signer and will be ignored", flags.WalletPasswordFileFlag.Name)
}
if cliCtx.IsSet(flags.Web3SignerPublicValidatorKeysFlag.Name) {
publicKeysStr := cliCtx.String(flags.Web3SignerPublicValidatorKeysFlag.Name)
pURL, err := url.ParseRequestURI(publicKeysStr)
if err == nil && pURL.Scheme != "" && pURL.Host != "" {
web3signerConfig.PublicKeysURL = publicKeysStr
} else {
publicKeysSlice := cliCtx.StringSlice(flags.Web3SignerPublicValidatorKeysFlag.Name)
pks := make([]string, 0)
if len(publicKeysSlice) == 1 {
pURL, err := url.ParseRequestURI(publicKeysSlice[0])
if err == nil && pURL.Scheme != "" && pURL.Host != "" {
web3signerConfig.PublicKeysURL = publicKeysSlice[0]
} else {
pks = strings.Split(publicKeysSlice[0], ",")
}
} else if len(publicKeysSlice) > 1 {
pks = publicKeysSlice
}
if len(pks) > 0 {
var validatorKeys [][48]byte
for _, key := range strings.Split(publicKeysStr, ",") {
for _, key := range pks {
decodedKey, decodeErr := hexutil.Decode(key)
if decodeErr != nil {
return nil, errors.Wrapf(decodeErr, "could not decode public key for web3signer: %s", key)

View File

@ -10,12 +10,11 @@ import (
"path/filepath"
"testing"
"github.com/prysmaticlabs/prysm/config/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
"github.com/prysmaticlabs/prysm/config/params"
validatorserviceconfig "github.com/prysmaticlabs/prysm/config/validator/service"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/testing/assert"
@ -83,21 +82,21 @@ func TestWeb3SignerConfig(t *testing.T) {
bytepubkey2 := bytesutil.ToBytes48(pubkey2decoded)
type args struct {
baseURL string
publicKeysOrURL string
baseURL string
publicKeysOrURLs []string
}
tests := []struct {
name string
args args
args *args
want *remoteweb3signer.SetupConfig
wantErrMsg string
}{
{
name: "happy path with public keys",
args: args{
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURL: "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b",
publicKeysOrURLs: []string{"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b"},
},
want: &remoteweb3signer.SetupConfig{
BaseEndpoint: "http://localhost:8545",
@ -111,9 +110,9 @@ func TestWeb3SignerConfig(t *testing.T) {
},
{
name: "happy path with external url",
args: args{
baseURL: "http://localhost:8545",
publicKeysOrURL: "http://localhost:8545/api/v1/eth2/publicKeys",
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURLs: []string{"http://localhost:8545/api/v1/eth2/publicKeys"},
},
want: &remoteweb3signer.SetupConfig{
BaseEndpoint: "http://localhost:8545",
@ -124,74 +123,97 @@ func TestWeb3SignerConfig(t *testing.T) {
},
{
name: "Bad base URL",
args: args{
args: &args{
baseURL: "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88,",
publicKeysOrURL: "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b",
publicKeysOrURLs: []string{"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b"},
},
want: nil,
wantErrMsg: "web3signer url 0xa99a76ed7796f7be22d5b7e85deeb7c5677e88, is invalid: parse \"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88,\": invalid URI for request",
},
{
name: "Bad publicKeys",
args: args{
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURL: "0xa99a76ed7796f7be22c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b",
publicKeysOrURLs: []string{"0xa99a76ed7796f7be22c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b"},
},
want: nil,
wantErrMsg: "could not decode public key for web3signer: 0xa99a76ed7796f7be22c: hex string of odd length",
},
{
name: "Bad publicKeysURL",
args: args{
baseURL: "http://localhost:8545",
publicKeysOrURL: "localhost",
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURLs: []string{"localhost"},
},
want: nil,
wantErrMsg: "could not decode public key for web3signer: localhost: hex string without 0x prefix",
},
{
name: "Base URL missing scheme or host",
args: args{
baseURL: "localhost:8545",
publicKeysOrURL: "localhost",
args: &args{
baseURL: "localhost:8545",
publicKeysOrURLs: []string{"localhost"},
},
want: nil,
wantErrMsg: "web3signer url must be in the format of http(s)://host:port url used: localhost:8545",
},
{
name: "Public Keys URL missing scheme or host",
args: args{
baseURL: "http://localhost:8545",
publicKeysOrURL: "localhost:8545",
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURLs: []string{"localhost:8545"},
},
want: nil,
wantErrMsg: "could not decode public key for web3signer: localhost:8545: hex string without 0x prefix",
},
{
name: "incorrect amount of flag calls used",
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURLs: []string{"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", "0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c"},
},
want: nil,
wantErrMsg: "could not decode public key for web3signer",
},
{
name: "incorrect amount of flag calls used with url",
args: &args{
baseURL: "http://localhost:8545",
publicKeysOrURLs: []string{"0xa99a76ed7796f7be22d5b7e85deeb7c5677e88e511e0b337618f8c4eb61349b4bf2d153f649f7b53359fe8b94a38e44c," +
"0xb89bebc699769726a318c8e9971bd3171297c61aea4a6578a7a4f94b547dcba5bac16a89108b6b6a1fe3695d1a874a0b", "http://localhost:8545/api/v1/eth2/publicKeys"},
},
want: nil,
wantErrMsg: "could not decode public key for web3signer",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := web3SignerConfig(newWeb3SignerCli(t, tt.args.baseURL, tt.args.publicKeysOrURL))
if (tt.wantErrMsg != "") && (tt.wantErrMsg != fmt.Sprintf("%v", err)) {
t.Errorf("web3SignerConfig error = %v, wantErrMsg = %v", err, tt.wantErrMsg)
app := cli.App{}
set := flag.NewFlagSet(tt.name, 0)
set.String("validators-external-signer-url", tt.args.baseURL, "baseUrl")
c := &cli.StringSliceFlag{
Name: "validators-external-signer-public-keys",
}
err := c.Apply(set)
require.NoError(t, err)
require.NoError(t, set.Set(flags.Web3SignerURLFlag.Name, tt.args.baseURL))
for _, key := range tt.args.publicKeysOrURLs {
require.NoError(t, set.Set(flags.Web3SignerPublicValidatorKeysFlag.Name, key))
}
cliCtx := cli.NewContext(&app, set, nil)
got, err := web3SignerConfig(cliCtx)
if tt.wantErrMsg != "" {
require.ErrorContains(t, tt.wantErrMsg, err)
return
}
require.DeepEqual(t, got, tt.want)
require.DeepEqual(t, tt.want, got)
})
}
}
func newWeb3SignerCli(t *testing.T, baseUrl string, publicKeysOrURL string) *cli.Context {
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("validators-external-signer-url", baseUrl, "baseUrl")
set.String("validators-external-signer-public-keys", publicKeysOrURL, "publicKeys or URL")
require.NoError(t, set.Set(flags.Web3SignerURLFlag.Name, baseUrl))
require.NoError(t, set.Set(flags.Web3SignerPublicValidatorKeysFlag.Name, publicKeysOrURL))
return cli.NewContext(&app, set, nil)
}
func TestProposerSettings(t *testing.T) {
hook := logtest.NewGlobal()