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

fix(jsonrpc): enable HTTP basic auth in WS client (backport #2434) #2451

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(jsonrpc): enable HTTP basic auth in WS client (#2434)
Hello,

I noticed that the JSON-RPC client is compatible with URLs containing
**HTTP basic auth**, but not on the WebSocket side.

For instance:

```golang
client, err := http.New("https://foo:bar@example.org:443", "/websocket")

client.Block(ctx, nil) // works

client.Start() // websocket error: the client doesn't forward auth headers
```

The HTTP client handle the username/password from the given endpoint
correctly:
-
https://github.com/cometbft/cometbft/blob/v0.38.5/rpc/jsonrpc/client/http_json_client.go#L140-L141
-
https://github.com/cometbft/cometbft/blob/v0.38.5/rpc/jsonrpc/client/http_json_client.go#L184-L185

So this PR brings the same logic for the WS client.

---

#### PR checklist

- [ ] Tests written/updated
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

(cherry picked from commit 8bf81d4)

# Conflicts:
#	rpc/jsonrpc/client/ws_client.go
  • Loading branch information
MattKetmo authored and mergify[bot] committed Feb 27, 2024
commit ca5a5032efd9da77f93f3825dfd4e36c66f1eed6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `[jsonrpc]` enable HTTP basic auth in websocket client ([#2434](https://github.com/cometbft/cometbft/pull/2434))
26 changes: 25 additions & 1 deletion rpc/jsonrpc/client/ws_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net"
Expand Down Expand Up @@ -35,7 +36,10 @@

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,27 @@
parsedURL.Scheme = protoWS
}

<<<<<<< HEAD

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected statement, found '<<'

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (04)

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (01)

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (00)

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (03)

syntax error: unexpected <<, expected }

Check failure on line 103 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (05)

syntax error: unexpected <<, expected }
dialFn, err := makeHTTPDialer(remoteAddr)
=======
// extract username and password from URL if any
username := ""
password := ""
if parsedURL.User.String() != "" {
username = parsedURL.User.Username()

Check failure on line 110 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: username
password, _ = parsedURL.User.Password()

Check failure on line 111 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: password
}

dialFn, err := MakeHTTPDialer(remoteAddr)

Check failure on line 114 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: MakeHTTPDialer
>>>>>>> 8bf81d423 (fix(jsonrpc): enable HTTP basic auth in WS client (#2434))

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

expected statement, found '>>'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

illegal character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

invalid character U+0023 '#') (typecheck)

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / golangci-lint

invalid character U+0023 '#') (typecheck)

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / Build (arm, linux)

invalid character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / Build (amd64, linux)

invalid character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (04)

invalid character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (01)

invalid character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (00)

invalid character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (03)

invalid character U+0023 '#'

Check failure on line 115 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / tests (05)

invalid character U+0023 '#'
if err != nil {
return nil, err
}

c := &WSClient{
Address: parsedURL.GetTrimmedHostWithPath(),
Username: username,

Check failure on line 122 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: username
Password: password,

Check failure on line 123 in rpc/jsonrpc/client/ws_client.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: password
Dialer: dialFn,
Endpoint: endpoint,
PingPongLatencyTimer: metrics.NewTimer(),
Expand Down Expand Up @@ -267,6 +285,12 @@
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
Loading