clean up code around setting geth client headers (#11748)

This commit is contained in:
Roberto Bayardo 2023-09-26 08:15:28 -07:00 committed by GitHub
parent 0919b2245f
commit a46370f5bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 17 deletions

View File

@ -3,6 +3,7 @@ package execution
import (
"context"
"fmt"
"net/http"
"strings"
"time"
@ -106,28 +107,26 @@ func (s *Service) retryExecutionClientConnection(ctx context.Context, err error)
// Initializes an RPC connection with authentication headers.
func (s *Service) newRPCClientWithAuth(ctx context.Context, endpoint network.Endpoint) (*gethRPC.Client, error) {
client, err := network.NewExecutionRPCClient(ctx, endpoint)
if err != nil {
return nil, err
}
headers := http.Header{}
if endpoint.Auth.Method != authorization.None {
header, err := endpoint.Auth.ToHeaderValue()
if err != nil {
return nil, err
}
client.SetHeader("Authorization", header)
headers.Set("Authorization", header)
}
for _, h := range s.cfg.headers {
if h != "" {
keyValue := strings.Split(h, "=")
if len(keyValue) < 2 {
log.Warnf("Incorrect HTTP header flag format. Skipping %v", keyValue[0])
continue
}
client.SetHeader(keyValue[0], strings.Join(keyValue[1:], "="))
if h == "" {
continue
}
keyValue := strings.Split(h, "=")
if len(keyValue) < 2 {
log.Warnf("Incorrect HTTP header flag format. Skipping %v", keyValue[0])
continue
}
headers.Set(keyValue[0], strings.Join(keyValue[1:], "="))
}
return client, nil
return network.NewExecutionRPCClient(ctx, endpoint, headers)
}
// Checks the chain ID of the execution client to ensure

View File

@ -41,7 +41,7 @@ func (e Endpoint) HttpClient() *http.Client {
}
// Equals compares two authorization data objects for equality.
func (d AuthorizationData) Equals(other AuthorizationData) bool {
func (d *AuthorizationData) Equals(other AuthorizationData) bool {
return d.Method == other.Method && d.Value == other.Value
}
@ -123,7 +123,7 @@ func NewHttpClientWithSecret(secret string) *http.Client {
}
}
func NewExecutionRPCClient(ctx context.Context, endpoint Endpoint) (*gethRPC.Client, error) {
func NewExecutionRPCClient(ctx context.Context, endpoint Endpoint, headers http.Header) (*gethRPC.Client, error) {
// Need to handle ipc and http
var client *gethRPC.Client
u, err := url.Parse(endpoint.Url)
@ -132,7 +132,7 @@ func NewExecutionRPCClient(ctx context.Context, endpoint Endpoint) (*gethRPC.Cli
}
switch u.Scheme {
case "http", "https":
client, err = gethRPC.DialOptions(ctx, endpoint.Url, gethRPC.WithHTTPClient(endpoint.HttpClient()))
client, err = gethRPC.DialOptions(ctx, endpoint.Url, gethRPC.WithHTTPClient(endpoint.HttpClient()), gethRPC.WithHeaders(headers))
if err != nil {
return nil, err
}

View File

@ -124,7 +124,7 @@ func New(opts ...Option) (*Builder, error) {
endpoint := network.HttpEndpoint(p.cfg.destinationUrl.String())
endpoint.Auth.Method = authorization.Bearer
endpoint.Auth.Value = p.cfg.secret
execClient, err := network.NewExecutionRPCClient(context.Background(), endpoint)
execClient, err := network.NewExecutionRPCClient(context.Background(), endpoint, nil)
if err != nil {
return nil, err
}