Skip to content

Commit

Permalink
Merge pull request #37 from pinax-network/fix/add_api_key_authenticat…
Browse files Browse the repository at this point in the history
…ion_to_firehose_fetch_client

add api key authentication to NewFirehoseFetchClient
  • Loading branch information
maoueh authored Mar 19, 2024
2 parents 421d7b8 + aa3e145 commit 1090f59
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ If you were at `firehose-core` version `1.0.0` and are bumping to `1.1.0`, you s
and is expanded as is.

* Added `Beacon` to known list of Block model.
* Added api key authentication to `NewFirehoseFetchClient`

## v1.2.3

Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/firehose/firehose.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func getFirehoseClientFromCmd[B firecore.Block, C any](cmd *cobra.Command, logge
if kind == "stream-client" {
rawClient, connClose, requestInfo.GRPCCallOpts, err = client.NewFirehoseClient(endpoint, jwt, apiKey, insecure, plaintext)
} else if kind == "fetch-client" {
rawClient, connClose, err = client.NewFirehoseFetchClient(endpoint, jwt, insecure, plaintext)
rawClient, connClose, requestInfo.GRPCCallOpts, err = client.NewFirehoseFetchClient(endpoint, jwt, apiKey, insecure, plaintext)
} else {
panic(fmt.Errorf("unsupported Firehose client kind: %s", kind))
}
Expand Down
16 changes: 10 additions & 6 deletions firehose/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (a *ApiKeyAuth) RequireTransportSecurity() bool {
return true
}

func NewFirehoseFetchClient(endpoint, jwt string, useInsecureTSLConnection, usePlainTextConnection bool) (cli pbfirehose.FetchClient, closeFunc func() error, err error) {
func NewFirehoseFetchClient(endpoint, jwt, apiKey string, useInsecureTSLConnection, usePlainTextConnection bool) (cli pbfirehose.FetchClient, closeFunc func() error, callOpts []grpc.CallOption, err error) {

if useInsecureTSLConnection && usePlainTextConnection {
return nil, nil, fmt.Errorf("option --insecure and --plaintext are mutually exclusive, they cannot be both specified at the same time")
return nil, nil, nil, fmt.Errorf("option --insecure and --plaintext are mutually exclusive, they cannot be both specified at the same time")
}

var dialOptions []grpc.DialOption
Expand All @@ -92,14 +92,18 @@ func NewFirehoseFetchClient(endpoint, jwt string, useInsecureTSLConnection, useP
dialOptions = []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{InsecureSkipVerify: true}))}
}

if jwt != "" && !usePlainTextConnection {
credentials := oauth.NewOauthAccess(&oauth2.Token{AccessToken: jwt, TokenType: "Bearer"})
dialOptions = append(dialOptions, grpc.WithPerRPCCredentials(credentials))
if !usePlainTextConnection {
if jwt != "" {
credentials := oauth.NewOauthAccess(&oauth2.Token{AccessToken: jwt, TokenType: "Bearer"})
callOpts = append(callOpts, grpc.PerRPCCredentials(credentials))
} else if apiKey != "" {
callOpts = append(callOpts, grpc.PerRPCCredentials(&ApiKeyAuth{ApiKey: apiKey}))
}
}

conn, err := dgrpc.NewExternalClient(endpoint, dialOptions...)
if err != nil {
return nil, nil, fmt.Errorf("unable to create external gRPC client: %w", err)
return nil, nil, nil, fmt.Errorf("unable to create external gRPC client: %w", err)
}
closeFunc = conn.Close
cli = pbfirehose.NewFetchClient(conn)
Expand Down

0 comments on commit 1090f59

Please sign in to comment.