API Middleware for Keymanager Standard API Endpoints (#9936)

* begin the middleware approach

* attempt middleware

* middleware works in tandem with web ui

* handle delete as well

* delete request

* DELETE working

* tool to perform imports

* functioning

* commentary

* build

* gaz

* smol test

* enable keymanager api use protonames

* edit

* one rule

* rem gw

* Fix custom compiler

(cherry picked from commit 3b1f65919e04ddf7e07c8f60cba1be883a736476)

* gen proto

* imports

* Update validator/node/node.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* remaining comments

* update item

* rpc

* add

* run gateway

* simplify

* rem flag

* deep source

Co-authored-by: prestonvanloon <preston@prysmaticlabs.com>
Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Raul Jordan
2021-12-07 15:26:21 -05:00
committed by GitHub
parent cee3b626f3
commit 424c8f6b46
16 changed files with 424 additions and 251 deletions

View File

@@ -0,0 +1,15 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"endpoint_factory.go",
"structs.go",
],
importpath = "github.com/prysmaticlabs/prysm/validator/rpc/apimiddleware",
visibility = ["//visibility:public"],
deps = [
"//api/gateway/apimiddleware:go_default_library",
"@com_github_pkg_errors//:go_default_library",
],
)

View File

@@ -0,0 +1,38 @@
package apimiddleware
import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/api/gateway/apimiddleware"
)
// ValidatorEndpointFactory creates endpoints used for running validator API calls through the API Middleware.
type ValidatorEndpointFactory struct {
}
func (f *ValidatorEndpointFactory) IsNil() bool {
return f == nil
}
// Paths is a collection of all valid validator API paths.
func (*ValidatorEndpointFactory) Paths() []string {
return []string{
"/eth/v1/keystores",
}
}
// Create returns a new endpoint for the provided API path.
func (*ValidatorEndpointFactory) Create(path string) (*apimiddleware.Endpoint, error) {
endpoint := apimiddleware.DefaultEndpoint()
switch path {
case "/eth/v1/keystores":
endpoint.GetResponse = &listKeystoresResponseJson{}
endpoint.PostRequest = &importKeystoresRequestJson{}
endpoint.PostResponse = &importKeystoresResponseJson{}
endpoint.DeleteRequest = &deleteKeystoresRequestJson{}
endpoint.DeleteResponse = &deleteKeystoresResponseJson{}
default:
return nil, errors.New("invalid path")
}
endpoint.Path = path
return &endpoint, nil
}

View File

@@ -0,0 +1,34 @@
package apimiddleware
type listKeystoresResponseJson struct {
Keystores []*keystoreJson `json:"keystores"`
}
type keystoreJson struct {
ValidatingPubkey string `json:"validating_pubkey" hex:"true"`
DerivationPath string `json:"derivation_path"`
}
type importKeystoresRequestJson struct {
Keystores []string `json:"keystores"`
Passwords []string `json:"passwords"`
SlashingProtection string `json:"slashing_protection"`
}
type importKeystoresResponseJson struct {
Statuses []*statusJson `json:"statuses"`
}
type deleteKeystoresRequestJson struct {
PublicKeys []string `json:"public_keys" hex:"true"`
}
type statusJson struct {
Status string `json:"status"`
Message string `json:"message"`
}
type deleteKeystoresResponseJson struct {
Statuses []*statusJson `json:"statuses"`
SlashingProtection string `json:"slashing_protection"`
}

View File

@@ -97,6 +97,9 @@ func (s *Server) DeleteKeystores(
if !ok {
return nil, status.Error(codes.Internal, "Keymanager kind cannot delete keys")
}
if len(req.PublicKeys) == 0 {
return &ethpbservice.DeleteKeystoresResponse{Statuses: make([]*ethpbservice.DeletedKeystoreStatus, 0)}, nil
}
statuses, err := deleter.DeleteKeystores(ctx, req.PublicKeys)
if err != nil {
return nil, status.Errorf(codes.Internal, "Could not delete keys: %v", err)

View File

@@ -203,7 +203,6 @@ func TestServer_ImportKeystores(t *testing.T) {
}
})
}
func TestServer_DeleteKeystores(t *testing.T) {
ctx := context.Background()
srv := setupServerWithWallet(t)
@@ -247,6 +246,14 @@ func TestServer_DeleteKeystores(t *testing.T) {
})
require.NoError(t, err)
t.Run("no slashing protection response if no keys in request even if we have a history in DB", func(t *testing.T) {
resp, err := srv.DeleteKeystores(context.Background(), &ethpbservice.DeleteKeystoresRequest{
PublicKeys: nil,
})
require.NoError(t, err)
require.Equal(t, "", resp.SlashingProtection)
})
// For ease of test setup, we'll give each public key a string identifier.
publicKeysWithId := map[string][48]byte{
"a": publicKeys[0],