Skip to content

Commit

Permalink
fix(jsonrpc): enable HTTP basic auth in WS client (backport #2434) (#…
Browse files Browse the repository at this point in the history
…2451)

This is an automatic backport of pull request #2434 done by
[Mergify](https://mergify.com).
Cherry-pick of 8bf81d4 has failed:
```
On branch mergify/bp/v0.38.x/pr-2434
Your branch is up to date with 'origin/v0.38.x'.

You are currently cherry-picking commit 8bf81d4.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	new file:   .changelog/unreleased/improvements/2434-jsonrpc-websocket-basic-auth.md

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   rpc/jsonrpc/client/ws_client.go

```


To fix up this pull request, you can check it out locally. See
documentation:
https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the
[documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on
`<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com) you
can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>

---------

Co-authored-by: Matt Ketmo <matthieu@moquet.net>
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
  • Loading branch information
3 people committed Feb 27, 2024
1 parent 8cd4a69 commit 851fd2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
@@ -0,0 +1 @@
- `[jsonrpc]` enable HTTP basic auth in websocket client ([#2434](https://github.com/cometbft/cometbft/pull/2434))
22 changes: 21 additions & 1 deletion rpc/jsonrpc/client/ws_client.go
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net"
Expand Down Expand Up @@ -35,7 +36,10 @@ type WSClient struct { //nolint: maligned

Address string // IP:PORT or /path/to/socket
Endpoint string // /websocket/url/endpoint
Dialer func(string, string) (net.Conn, error)
Username string
Password string

Dialer func(string, string) (net.Conn, error)

// Single user facing channel to read RPCResponses from, closed only when the
// client is being stopped.
Expand Down Expand Up @@ -96,13 +100,23 @@ func NewWS(remoteAddr, endpoint string, options ...func(*WSClient)) (*WSClient,
parsedURL.Scheme = protoWS
}

// extract username and password from URL if any
username := ""
password := ""
if parsedURL.User.String() != "" {
username = parsedURL.User.Username()
password, _ = parsedURL.User.Password()
}

dialFn, err := makeHTTPDialer(remoteAddr)
if err != nil {
return nil, err
}

c := &WSClient{
Address: parsedURL.GetTrimmedHostWithPath(),
Username: username,
Password: password,
Dialer: dialFn,
Endpoint: endpoint,
PingPongLatencyTimer: metrics.NewTimer(),
Expand Down Expand Up @@ -267,6 +281,12 @@ func (c *WSClient) dial() error {
Proxy: http.ProxyFromEnvironment,
}
rHeader := http.Header{}

// Set basic auth header if username and password are provided
if c.Username != "" && c.Password != "" {
rHeader.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(c.Username+":"+c.Password)))
}

conn, _, err := dialer.Dial(c.protocol+"://"+c.Address+c.Endpoint, rHeader) //nolint:bodyclose
if err != nil {
return err
Expand Down

0 comments on commit 851fd2b

Please sign in to comment.