Skip to content
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
19 changes: 19 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package client

import (
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net/http"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -113,6 +115,8 @@ type Client struct {

state int32

tlsConfig *tls.Config

mut sync.RWMutex
}

Expand All @@ -125,6 +129,21 @@ func WithLogger(log *slog.Logger) Option {
}
}

// WithTLSConfig lets the caller set an optional TLS configuration for connections
// to Mattermost. This is needed when the server uses a self-signed or private CA
// certificate. The config is applied to both the HTTP API client and the WebSocket
// connection.
func WithTLSConfig(tlsConfig *tls.Config) Option {
return func(c *Client) error {
transport := http.DefaultTransport.(*http.Transport).Clone()
Comment thread
bgardner8008 marked this conversation as resolved.
transport.TLSClientConfig = tlsConfig
transport.ForceAttemptHTTP2 = true
Comment thread
bgardner8008 marked this conversation as resolved.
c.apiClient.HTTPClient = &http.Client{Transport: transport}
c.tlsConfig = tlsConfig
return nil
}
}

// New initializes and returns a new Calls client.
func New(cfg Config, opts ...Option) (*Client, error) {
if err := cfg.Parse(); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,15 @@ func (c *Client) handleWSMsg(msg ws.Message) error {
}

func (c *Client) wsOpen() error {
wsOpts := []ws.ClientOption{ws.WithLogger(c.log)}
if c.tlsConfig != nil {
wsOpts = append(wsOpts, ws.WithTLSConfig(c.tlsConfig))
}
ws, err := ws.NewClient(ws.ClientConfig{
URL: c.cfg.wsURL,
AuthToken: c.cfg.AuthToken,
AuthType: ws.BearerClientAuthType,
}, ws.WithLogger(c.log))
}, wsOpts...)
if err != nil {
return fmt.Errorf("failed to create websocket client: %w", err)
}
Expand Down
22 changes: 17 additions & 5 deletions client/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package client

import (
"fmt"
"net"
"testing"
"time"

Expand Down Expand Up @@ -115,12 +117,21 @@ func TestClientWSReconnectTimeout(t *testing.T) {
require.Fail(t, "timed out waiting for connect event")
}

th.userClient.cfg.wsURL = "ws://localhost:8080"
// Bind a listener to get an unused port, then close it so the port gives
// immediate ECONNREFUSED on reconnect (avoids slow TCP timeouts from
// non-routable IPs, and avoids accidentally hitting a real server).
ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
unusedAddr := ln.Addr().String()
ln.Close()
th.userClient.cfg.wsURL = fmt.Sprintf("ws://%s", unusedAddr)
Comment thread
bgardner8008 marked this conversation as resolved.

errorCh := make(chan struct{})
errorCh := make(chan error, 1)
err = th.userClient.On(ErrorEvent, func(ctx any) error {
close(errorCh)
require.EqualError(t, ctx.(error), "ws reconnection timeout reached")
select {
case errorCh <- ctx.(error):
default:
}
return nil
})
require.NoError(t, err)
Expand All @@ -136,7 +147,8 @@ func TestClientWSReconnectTimeout(t *testing.T) {
require.NoError(t, err)

select {
case <-errorCh:
case err := <-errorCh:
require.EqualError(t, err, "ws reconnection timeout reached")
case <-time.After(wsReconnectionTimeout * 2):
require.Fail(t, "timed out waiting for error event")
}
Expand Down
5 changes: 5 additions & 0 deletions service/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package ws

import (
"crypto/tls"
"fmt"
"log/slog"
"net/http"
Expand Down Expand Up @@ -33,6 +34,7 @@ type Client struct {
wg sync.WaitGroup
connState int32
dialFn DialContextFn
tlsConfig *tls.Config
pingHandlerFn func(msg string) error
log *slog.Logger
}
Expand Down Expand Up @@ -73,6 +75,9 @@ func NewClient(cfg ClientConfig, opts ...ClientOption) (*Client, error) {
if c.dialFn != nil {
dialer.NetDialContext = c.dialFn
}
if c.tlsConfig != nil {
dialer.TLSClientConfig = c.tlsConfig
}
ws, _, err := dialer.Dial(cfg.URL, header)
if err != nil {
return nil, fmt.Errorf("failed to dial: %w", err)
Expand Down
11 changes: 11 additions & 0 deletions service/ws/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ws

import (
"context"
"crypto/tls"
"log/slog"
"net"
)
Expand Down Expand Up @@ -38,3 +39,13 @@ func WithLogger(log *slog.Logger) ClientOption {
return nil
}
}

// WithTLSConfig lets the caller set an optional TLS configuration for the
// WebSocket connection. This is needed when connecting to a server using a
// self-signed or private CA certificate.
func WithTLSConfig(tlsConfig *tls.Config) ClientOption {
return func(c *Client) error {
c.tlsConfig = tlsConfig
return nil
}
}
Loading