Files
coder/enterprise/coderd/x/chatd/chatd.go
T
Michael Suchacz 8b6f2c656c refactor(enterprise/coderd/x/chatd): remove the relay dialer's DialerFn bypass (#30920)
Removes `StreamPartsDialerConfig.DialerFn`, which replaced the whole
relay dial when set, and `TestStreamPartsDialerUsesConfiguredDialer`
with its `fakePartsSession`, the only code that set it. Production
builds the dialer in `enterprise/coderd/coderd.go` from the replica
resolver, HTTP client, and replica ID, and never sets `DialerFn`.

## Why it is safe

`git grep DialerFn` finds nothing after this change. The deleted test
handed its input to its own function and got its own fake session back,
so it never reached `dialRelayParts`. Its checks already run against the
real dial in `TestStreamPartsDialerDialsPartsEndpoint`, which dials a
WebSocket served by `httptest`. Each mutant below breaks one of those
checks in `dialRelayParts`, and the remaining test fails on each:

| Check of the deleted test | Real-dial check | Mutant | Result |
|---|---|---|---|
| chat ID reaches the dial | request path | `buildRelayURL(address,
uuid.Nil)` | fails |
| worker ID reaches the dial | resolver argument |
`ResolveReplicaAddress(ctx, uuid.Nil)` | fails |
| session token reaches the dial | `Coder-Session-Token` header |
`extractSessionToken(nil)` | fails |
| the dial returns the session | `require.NotNil(session)` | `return
nil, nil` | fails |

`TestStreamPartsDialerClassifiesHTTPFailures` and
`TestRelayDialErrorIsUnrecoverable` are unchanged.

## Review focus

`StreamPartsDialerConfig` is exported, but the package sits under `x/`
and its only importer is `enterprise/coderd`.

## Validation

- `go test ./enterprise/coderd/x/chatd/ -count=1 -race`: pass, before
and after.
- Mutants: `go test -overlay=<mutant> ./enterprise/coderd/x/chatd/ -run
'TestStreamPartsDialer|TestRelayDialError'` fails
`TestStreamPartsDialerDialsPartsEndpoint` for each row above. A copy
with a syntax error fails to build, which shows the overlay applies.
- `golangci-lint run ./enterprise/coderd/x/chatd/...`: 0 issues. The
pre-commit hook (`make pre-commit`: gen, fmt, lint, slim build) passed.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
2026-10-27 10:14:02 +01:00

148 lines
4.5 KiB
Go

package chatd
import (
"context"
"net/http"
"net/url"
"strings"
"github.com/google/uuid"
"golang.org/x/xerrors"
osschatd "github.com/coder/coder/v2/coderd/x/chatd"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/websocket"
)
// RelaySourceHeader marks replica-relayed stream requests.
const RelaySourceHeader = "X-Coder-Relay-Source-Replica"
const (
authorizationHeader = "Authorization"
cookieHeader = "Cookie"
)
// RelayDialError wraps a failed relay handshake. HTTPStatus is 0
// when the failure happened before a response.
type RelayDialError struct {
HTTPStatus int
Err error
}
func (e *RelayDialError) Error() string { return e.Err.Error() }
func (e *RelayDialError) Unwrap() error { return e.Err }
// IsUnrecoverable reports whether retrying with the same captured
// session token is futile.
func (e *RelayDialError) IsUnrecoverable() bool {
return e.HTTPStatus == http.StatusUnauthorized ||
e.HTTPStatus == http.StatusForbidden
}
// StreamPartsDialerConfig holds dependencies for multi-replica stream parts.
type StreamPartsDialerConfig struct {
ResolveReplicaAddress func(context.Context, uuid.UUID) (string, bool)
ReplicaHTTPClient *http.Client
ReplicaIDFn func() uuid.UUID
}
// NewStreamPartsDialer returns a dialer for the owning replica's parts endpoint.
func NewStreamPartsDialer(cfg StreamPartsDialerConfig) osschatd.StreamPartsDialer {
return func(ctx context.Context, input osschatd.StreamPartsDialInput) (osschatd.StreamPartsSession, error) {
return dialRelayParts(ctx, input, cfg)
}
}
func dialRelayParts(
ctx context.Context,
input osschatd.StreamPartsDialInput,
cfg StreamPartsDialerConfig,
) (osschatd.StreamPartsSession, error) {
if cfg.ResolveReplicaAddress == nil {
return nil, &RelayDialError{Err: xerrors.New("dial relay stream parts: resolver not configured")}
}
address, ok := cfg.ResolveReplicaAddress(ctx, input.WorkerID)
if !ok {
return nil, &RelayDialError{Err: xerrors.New("dial relay stream parts: worker replica not found")}
}
wsURL, err := buildRelayURL(address, input.ChatID)
if err != nil {
return nil, &RelayDialError{Err: xerrors.Errorf("dial relay stream parts: %w", err)}
}
if cfg.ReplicaIDFn == nil {
return nil, &RelayDialError{Err: xerrors.New("dial relay stream parts: replica ID function not configured")}
}
replicaID := cfg.ReplicaIDFn()
if replicaID == uuid.Nil {
return nil, &RelayDialError{Err: xerrors.New("dial relay stream parts: replica ID is nil")}
}
headers := make(http.Header, 2)
headers.Set(codersdk.SessionTokenHeader, extractSessionToken(input.RequestHeader))
headers.Set(RelaySourceHeader, replicaID.String())
conn, resp, dialErr := websocket.Dial(ctx, wsURL, &websocket.DialOptions{
HTTPClient: cfg.ReplicaHTTPClient,
HTTPHeader: headers,
CompressionMode: websocket.CompressionDisabled,
})
status := 0
if resp != nil {
status = resp.StatusCode
if dialErr != nil && resp.Body != nil {
_ = resp.Body.Close()
}
}
if dialErr != nil {
return nil, &RelayDialError{
HTTPStatus: status,
Err: xerrors.Errorf("dial relay stream parts: %w", dialErr),
}
}
conn.SetReadLimit(1 << 22)
return osschatd.NewStreamPartsJSONSession(ctx, conn), nil
}
// buildRelayURL builds the websocket URL for the chat stream parts endpoint on
// a peer replica. It maps http(s) schemes to ws(s).
func buildRelayURL(address string, chatID uuid.UUID) (string, error) {
u, err := url.Parse(address)
if err != nil {
return "", xerrors.Errorf("parse relay address %q: %w", address, err)
}
switch u.Scheme {
case "http":
u.Scheme = "ws"
case "https":
u.Scheme = "wss"
case "ws", "wss":
default:
return "", xerrors.Errorf("unsupported relay address scheme %q", u.Scheme)
}
u.Path = "/api/v2/chats/" + chatID.String() + "/stream/parts"
u.RawQuery = ""
return u.String(), nil
}
// extractSessionToken returns the session token carried by the given request
// headers. It mirrors the priority order used by apiKeyMiddleware: cookie,
// then Coder-Session-Token header, then Authorization: Bearer header.
func extractSessionToken(header http.Header) string {
if header == nil {
return ""
}
if raw := header.Get(cookieHeader); raw != "" {
r := &http.Request{Header: http.Header{cookieHeader: {raw}}}
if c, err := r.Cookie(codersdk.SessionTokenCookie); err == nil && c.Value != "" {
return c.Value
}
}
if v := header.Get(codersdk.SessionTokenHeader); v != "" {
return v
}
if v := header.Get(authorizationHeader); len(v) > 7 && strings.EqualFold(v[:7], "bearer ") {
return strings.TrimSpace(v[7:])
}
return ""
}