Skip to content
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

Add flag for specifying an extra http header in RPC requests #48

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions client/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ const (
)

type rpcClient struct {
client *retryablehttp.Client
cfg Config
log *slog.Logger
bufPool *sync.Pool
client *retryablehttp.Client
cfg Config
log *slog.Logger
bufPool *sync.Pool
httpHeaders map[string]string
}

func NewClient(log *slog.Logger, cfg Config) (*rpcClient, error) { // revive:disable-line:unexported-return
Expand Down Expand Up @@ -61,6 +62,7 @@ func NewClient(log *slog.Logger, cfg Config) (*rpcClient, error) { // revive:dis
return new(bytes.Buffer)
},
},
httpHeaders: cfg.HTTPHeaders,
}
// lets validate RPC node is up & reachable
_, err := rpc.LatestBlockNumber()
Expand Down Expand Up @@ -112,6 +114,11 @@ func (c *rpcClient) getResponseBody(
if err != nil {
return err
}
if c.httpHeaders != nil {
for k, v := range c.httpHeaders {
req.Header.Set(k, v)
}
}
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request for method %s: %w", method, err)
Expand Down
1 change: 1 addition & 0 deletions client/jsonrpc/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import "time"
type Config struct {
URL string
PollInterval time.Duration
HTTPHeaders map[string]string
}
13 changes: 12 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log/slog"
"os"
"os/signal"
"strings"
stdsync "sync"
"syscall"
"time"
Expand Down Expand Up @@ -48,10 +49,20 @@ func main() {
var wg stdsync.WaitGroup
var rpcClient jsonrpc.BlockchainClient

rpcHTTPHeaders := make(map[string]string)
if cfg.RPCNode.ExtraHTTPHeader != "" {
pair := strings.Split(cfg.RPCNode.ExtraHTTPHeader, ",")
// We've validated this list has two elements
key := strings.Trim(pair[0], " ")
value := strings.Trim(pair[1], " ")
logger.Info("Adding extra HTTP header to RPC requests", "key", key, "value", value)
rpcHTTPHeaders[key] = value
}
switch cfg.RPCStack {
case models.OpStack:
rpcClient, err = jsonrpc.NewOpStackClient(logger, jsonrpc.Config{
URL: cfg.RPCNode.NodeURL,
URL: cfg.RPCNode.NodeURL,
HTTPHeaders: rpcHTTPHeaders,
})
default:
stdlog.Fatalf("unsupported RPC stack: %s", cfg.RPCStack)
Expand Down
11 changes: 10 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"errors"
"fmt"
"strings"
"time"

"github.com/duneanalytics/blockchain-ingester/models"
Expand All @@ -21,13 +23,20 @@ func (d DuneClient) HasError() error {
}

type RPCClient struct {
NodeURL string `long:"rpc-node-url" env:"RPC_NODE_URL" description:"URL for the blockchain node"`
NodeURL string `long:"rpc-node-url" env:"RPC_NODE_URL" description:"URL for the blockchain node"`
ExtraHTTPHeader string `long:"rpc-http-header" env:"RPC_HTTP_HEADER" description:"Extra HTTP header to send with RPC requests. On the form 'key,value'"` // nolint:lll
Copy link
Contributor

@adammilnesmith adammilnesmith Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're supporting a singular extra http header here which was what was immediately requested.
Whilst we are here, should we also allow for multiple to be set and make this a slice of strings instead? If so we could set an env-delim if we want to support this through environment variables still (meaning selecting that delimiter carefully and checking whether this library allows delimiter escaping - they don't have any unit tests for this case so probably not) or propose people use multiple args --rpc-http-header "Header1: value1" --rpc-http-header "Header2: value2"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually started implementing with a list, but then realized they only requested one. I don't think we can make the multiple args version work with an environment variable, unfortunately.

But a list would make sense!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could split on comma then? RPC_HTTP_HEADERS='Header1: value1, Header2: value2, ...'

Copy link
Member Author

@vegarsti vegarsti Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or should we not split on comma given a comma might be present in the header value, like the x-forwarded-for

Copy link
Contributor

@adammilnesmith adammilnesmith Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have said env-delim is specifically a directive in the library we are using.
Also, where , can appear in header values, something like | cannot in the http spec and might be a better delimiter.

}

func (r RPCClient) HasError() error {
if r.NodeURL == "" {
return errors.New("RPC node URL is required")
}
if r.ExtraHTTPHeader != "" {
header := strings.Split(r.ExtraHTTPHeader, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use a colon to separate so that configuration reads in the same way as the http spec/format? It's also familiar for users or curl. We also more easily support values that contain commas which are common for things like X-Forwarded-For

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's better! Thanks! Hadn't realized that was spec, but also nice to be consistent with curl

if len(header) != 2 {
return fmt.Errorf("invalid rpc http header: expected 'key,value', got '%s'", r.ExtraHTTPHeader)
}
}
return nil
}

Expand Down
Loading