diff --git a/client/client.go b/client/client.go index 0536ddfc..59805dad 100644 --- a/client/client.go +++ b/client/client.go @@ -115,6 +115,19 @@ func WithResponseTimeout(responseTimeout time.Duration) Option { } } +// WithHTTPClient sets the HTTP client used to reach the secrets engine. +// The caller is responsible for setting up its DialContext against the +// correct socket path. [WithTimeout] and [WithResponseTimeout] are ignored. +func WithHTTPClient(httpClient *http.Client) Option { + return func(s *config) error { + if httpClient == nil { + return errors.New("no http client provided") + } + s.httpClient = httpClient + return nil + } +} + type dial func(ctx context.Context, network, addr string) (net.Conn, error) type config struct { @@ -122,6 +135,7 @@ type config struct { dialContext dial requestTimeout time.Duration responseTimeout time.Duration + httpClient *http.Client } var ( @@ -274,6 +288,12 @@ func New(options ...Option) (Client, error) { return nil, err } } + if cfg.httpClient != nil { + if cfg.dialContext != nil { + return nil, errors.New("cannot set http client and socket path or dial") + } + return newClient(cfg.httpClient, ""), nil + } if cfg.dialContext == nil { cfg.socketPath = clientconfig.StandaloneSocketPath() cfg.dialContext = dialFromPath(cfg.socketPath) @@ -299,13 +319,17 @@ func New(options ...Option) (Client, error) { // it can be overwritten with [WithTimeout] Timeout: cfg.requestTimeout, } - callEngine := call(rpc.NewClient(c, baseURL), cfg.socketPath) + return newClient(c, cfg.socketPath), nil +} + +func newClient(httpClient *http.Client, socketPath string) *client { + callEngine := call(rpc.NewClient(httpClient, baseURL), socketPath) return &client{ resolverClient: resolverv1rpc.NewResolverServiceClient(callEngine), authorizerClient: resolverv1rpc.NewAuthorizerServiceClient(callEngine), versionClient: healthv1rpc.NewVersionServiceClient(callEngine), pluginsClient: pluginsv1rpc.NewPluginManagementServiceClient(callEngine), - }, nil + } } func (c client) ListPlugins(ctx context.Context) ([]PluginInfo, error) { diff --git a/client/client_test.go b/client/client_test.go index a623661c..6372646e 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -546,6 +546,38 @@ func Test_Version(t *testing.T) { }) } +func TestWithHTTPClient(t *testing.T) { + t.Parallel() + t.Run("sends requests through the given http client", func(t *testing.T) { + socket := mockVersionEngine(t, "v1.2.3", "2026-03-26", "abc1234") + httpClient := &http.Client{Transport: &http.Transport{DialContext: dialFromPath(socket)}} + c, err := New(WithHTTPClient(httpClient)) + require.NoError(t, err) + dv, err := c.Version(t.Context()) + require.NoError(t, err) + assert.Equal(t, "v1.2.3", dv.Version.String()) + }) + t.Run("nil http client", func(t *testing.T) { + _, err := New(WithHTTPClient(nil)) + require.EqualError(t, err, "no http client provided") + }) + t.Run("cannot be combined with a socket path", func(t *testing.T) { + _, err := New(WithHTTPClient(&http.Client{}), WithSocketPath(randomShortSocketName())) + require.EqualError(t, err, "cannot set http client and socket path or dial") + }) + t.Run("cannot be combined with dial", func(t *testing.T) { + _, err := New(WithDialContext(dialFromPath(randomShortSocketName())), WithHTTPClient(&http.Client{})) + require.EqualError(t, err, "cannot set http client and socket path or dial") + }) + t.Run("reports an unreachable engine as not running", func(t *testing.T) { + httpClient := &http.Client{Transport: &http.Transport{DialContext: dialFromPath(randomShortSocketName())}} + c, err := New(WithHTTPClient(httpClient)) + require.NoError(t, err) + _, err = c.Version(t.Context()) + require.ErrorIs(t, err, ErrSecretsEngineNotRunning) + }) +} + func TestSecretsEngineUnavailable(t *testing.T) { socketPath := randomShortSocketName() client, err := New(WithSocketPath(socketPath))