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
28 changes: 26 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,27 @@ 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 {
socketPath string
dialContext dial
requestTimeout time.Duration
responseTimeout time.Duration
httpClient *http.Client
}

var (
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
32 changes: 32 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading