Integrate Engine Proxy into E2E (#10808)

* add it in

* support jwt secret

* fix it

* fix

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:
Nishant Das
2022-06-07 07:35:54 +08:00
committed by GitHub
parent 5b12f5a27d
commit 6c39301f33
13 changed files with 302 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ go_library(
importpath = "github.com/prysmaticlabs/prysm/testing/middleware/engine-api-proxy",
visibility = ["//visibility:public"],
deps = [
"//network:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],

View File

@@ -2,6 +2,7 @@ package proxy
import (
"net/url"
"os"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -12,6 +13,7 @@ type config struct {
proxyHost string
destinationUrl *url.URL
logger *logrus.Logger
secret string
}
type Option func(p *Proxy) error
@@ -54,3 +56,24 @@ func WithLogger(l *logrus.Logger) Option {
return nil
}
}
// WithLogFile specifies a log file to write
// the proxies output to.
func WithLogFile(f *os.File) Option {
return func(p *Proxy) error {
if p.cfg.logger == nil {
return errors.New("nil logger provided")
}
p.cfg.logger.SetOutput(f)
return nil
}
}
// WithJwtSecret adds in support for jwt authenticated
// connections for our proxy.
func WithJwtSecret(secret string) Option {
return func(p *Proxy) error {
p.cfg.secret = secret
return nil
}
}

View File

@@ -16,6 +16,7 @@ import (
"sync"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/network"
"github.com/sirupsen/logrus"
)
@@ -128,6 +129,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (p *Proxy) AddRequestInterceptor(rpcMethodName string, response interface{}, trigger func() bool) {
p.lock.Lock()
defer p.lock.Unlock()
p.cfg.logger.Infof("Adding in interceptor for method %s", rpcMethodName)
p.interceptors[rpcMethodName] = &interceptorConfig{
response,
trigger,
@@ -168,6 +170,13 @@ func (p *Proxy) interceptIfNeeded(requestBytes []byte, w http.ResponseWriter) (h
// Create a new proxy request to the execution client.
func (p *Proxy) proxyRequest(requestBytes []byte, w http.ResponseWriter, r *http.Request) {
jreq, err := unmarshalRPCObject(requestBytes)
if err != nil {
p.cfg.logger.WithError(err).Error("Could not unmarshal request")
// Continue and mark it as unknown.
jreq = &jsonRPCObject{Method: "unknown"}
}
p.cfg.logger.Infof("Forwarding %s request for method %s to %s", r.Method, jreq.Method, p.cfg.destinationUrl.String())
proxyReq, err := http.NewRequest(r.Method, p.cfg.destinationUrl.String(), r.Body)
if err != nil {
p.cfg.logger.WithError(err).Error("Could create new request")
@@ -183,11 +192,16 @@ func (p *Proxy) proxyRequest(requestBytes []byte, w http.ResponseWriter, r *http
proxyReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
if p.cfg.secret != "" {
client = network.NewHttpClientWithSecret(p.cfg.secret)
}
proxyRes, err := client.Do(proxyReq)
if err != nil {
p.cfg.logger.WithError(err).Error("Could not forward request to destination server")
return
}
p.cfg.logger.Infof("Received response for %s request with method %s from %s", r.Method, jreq.Method, p.cfg.destinationUrl.String())
defer func() {
if err = proxyRes.Body.Close(); err != nil {
p.cfg.logger.WithError(err).Error("Could not do close proxy response body")