From 8d16f7ae18b1ae660e8c67ef1617e198d6662cdd Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:49:55 +0200 Subject: [PATCH 1/7] feat(pass): add WithClient option to commands Lets callers pass an existing client.Client to the pass commands instead of having one built from the socket path and timeout options. --- plugins/pass/README.md | 4 +++- plugins/pass/commands/client.go | 14 ++++++++++++++ plugins/pass/commands/run_test.go | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/plugins/pass/README.md b/plugins/pass/README.md index 951c3fde..4dd3082d 100644 --- a/plugins/pass/README.md +++ b/plugins/pass/README.md @@ -148,4 +148,6 @@ back to another store there if your application supports one. `set --force`, `get --reveal`, `rm`, and `run` call the Secrets Engine. They connect to Docker Desktop's socket by default; pass `commands.WithSocketPath` to use another one, and `commands.WithTimeout` or -`commands.WithResponseTimeout` to change the timeouts. +`commands.WithResponseTimeout` to change the timeouts. Pass +`commands.WithClient` to use a `client.Client` you already have; the socket +path and timeout options are then ignored. diff --git a/plugins/pass/commands/client.go b/plugins/pass/commands/client.go index a578cafa..3df54f40 100644 --- a/plugins/pass/commands/client.go +++ b/plugins/pass/commands/client.go @@ -60,6 +60,7 @@ type clientOpts struct { timeout time.Duration responseTimeout time.Duration socketPath string + client client.Client } func (o clientOpts) isUnbound() bool { @@ -121,6 +122,16 @@ func WithSocketPath(socketPath string) ClientOption { } } +func WithClient(c client.Client) ClientOption { + return func(o *clientOpts) error { + if c == nil { + return errors.New("no client provided") + } + o.client = c + return nil + } +} + func parseClientOptions(options ...ClientOption) (clientOpts, error) { opts := clientOpts{ timeout: config.DefaultClientRequestTimeout, @@ -135,6 +146,9 @@ func parseClientOptions(options ...ClientOption) (clientOpts, error) { } func newClient(opts clientOpts) (client.Client, error) { + if opts.client != nil { + return opts.client, nil + } socketPath := opts.socketPath if socketPath == "" { socketPath = config.DesktopSocketPath() diff --git a/plugins/pass/commands/run_test.go b/plugins/pass/commands/run_test.go index 1f9f98c1..2e7c6724 100644 --- a/plugins/pass/commands/run_test.go +++ b/plugins/pass/commands/run_test.go @@ -367,6 +367,11 @@ func TestRunCommandOptions(t *testing.T) { options: []ClientOption{WithSocketPath("")}, wantErr: "no path provided", }, + { + name: "nil client", + options: []ClientOption{WithClient(nil)}, + wantErr: "no client provided", + }, { name: "negative request timeout", options: []ClientOption{WithTimeout(-time.Second)}, From afb65632c9df6fc0dbda146ca8f9f92c8b4f9716 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:50:43 +0200 Subject: [PATCH 2/7] docs(pass): document WithClient option --- plugins/pass/README.md | 22 +++++++++++++++++++--- plugins/pass/commands/client.go | 4 ++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/plugins/pass/README.md b/plugins/pass/README.md index 4dd3082d..4de22ce8 100644 --- a/plugins/pass/README.md +++ b/plugins/pass/README.md @@ -148,6 +148,22 @@ back to another store there if your application supports one. `set --force`, `get --reveal`, `rm`, and `run` call the Secrets Engine. They connect to Docker Desktop's socket by default; pass `commands.WithSocketPath` to use another one, and `commands.WithTimeout` or -`commands.WithResponseTimeout` to change the timeouts. Pass -`commands.WithClient` to use a `client.Client` you already have; the socket -path and timeout options are then ignored. +`commands.WithResponseTimeout` to change the timeouts. + +To share a Secrets Engine client your application already has, pass it with +`commands.WithClient`. The commands then use that client as is: its own socket +path and timeouts apply, and `WithSocketPath`, `WithTimeout`, and +`WithResponseTimeout` do not change how it connects. `WithClient(nil)` +returns an error. + +```go +c, err := client.New(client.WithSocketPath("/run/secrets-engine.sock")) +if err != nil { + log.Fatal(err) +} + +run, err := commands.RunCommand(commands.WithClient(c)) +if err != nil { + log.Fatal(err) +} +``` diff --git a/plugins/pass/commands/client.go b/plugins/pass/commands/client.go index 3df54f40..46478b88 100644 --- a/plugins/pass/commands/client.go +++ b/plugins/pass/commands/client.go @@ -122,6 +122,10 @@ func WithSocketPath(socketPath string) ClientOption { } } +// WithClient makes the command use c to talk to the Secrets Engine instead of +// creating its own client. The socket path and timeouts come from c and +// [WithSocketPath], [WithTimeout], and [WithResponseTimeout] do not configure +// it; WithTimeout(0) still turns on the preflight ping against c. func WithClient(c client.Client) ClientOption { return func(o *clientOpts) error { if c == nil { From 0f08f5bd6b72293336c65972d2139243d4deba93 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:52:34 +0200 Subject: [PATCH 3/7] feat(client): add WithClient option Lets callers pass their own *http.Client to client.New. Drops the WithClient option from the pass commands. --- client/client.go | 33 +++++++++++++++++++++++++++++-- client/client_test.go | 32 ++++++++++++++++++++++++++++++ plugins/pass/README.md | 18 ----------------- plugins/pass/commands/client.go | 18 ----------------- plugins/pass/commands/run_test.go | 5 ----- 5 files changed, 63 insertions(+), 43 deletions(-) diff --git a/client/client.go b/client/client.go index 0536ddfc..ea7708a4 100644 --- a/client/client.go +++ b/client/client.go @@ -115,6 +115,24 @@ func WithResponseTimeout(responseTimeout time.Duration) Option { } } +// WithClient makes the client send its requests through httpClient instead of +// the HTTP client it builds by default. +// +// httpClient must reach the secrets engine on its own, typically through a +// Transport that dials the engine's socket, so WithClient cannot be combined +// with [WithSocketPath] or [WithDialContext]. The request and response +// timeouts come from httpClient; [WithTimeout] and [WithResponseTimeout] do +// not apply to it. +func WithClient(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 +140,7 @@ type config struct { dialContext dial requestTimeout time.Duration responseTimeout time.Duration + httpClient *http.Client } var ( @@ -274,6 +293,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 +324,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..30df178c 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -546,6 +546,38 @@ func Test_Version(t *testing.T) { }) } +func TestWithClient(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(WithClient(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(WithClient(nil)) + require.EqualError(t, err, "no http client provided") + }) + t.Run("cannot be combined with a socket path", func(t *testing.T) { + _, err := New(WithClient(&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())), WithClient(&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(WithClient(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)) diff --git a/plugins/pass/README.md b/plugins/pass/README.md index 4de22ce8..951c3fde 100644 --- a/plugins/pass/README.md +++ b/plugins/pass/README.md @@ -149,21 +149,3 @@ back to another store there if your application supports one. connect to Docker Desktop's socket by default; pass `commands.WithSocketPath` to use another one, and `commands.WithTimeout` or `commands.WithResponseTimeout` to change the timeouts. - -To share a Secrets Engine client your application already has, pass it with -`commands.WithClient`. The commands then use that client as is: its own socket -path and timeouts apply, and `WithSocketPath`, `WithTimeout`, and -`WithResponseTimeout` do not change how it connects. `WithClient(nil)` -returns an error. - -```go -c, err := client.New(client.WithSocketPath("/run/secrets-engine.sock")) -if err != nil { - log.Fatal(err) -} - -run, err := commands.RunCommand(commands.WithClient(c)) -if err != nil { - log.Fatal(err) -} -``` diff --git a/plugins/pass/commands/client.go b/plugins/pass/commands/client.go index 46478b88..a578cafa 100644 --- a/plugins/pass/commands/client.go +++ b/plugins/pass/commands/client.go @@ -60,7 +60,6 @@ type clientOpts struct { timeout time.Duration responseTimeout time.Duration socketPath string - client client.Client } func (o clientOpts) isUnbound() bool { @@ -122,20 +121,6 @@ func WithSocketPath(socketPath string) ClientOption { } } -// WithClient makes the command use c to talk to the Secrets Engine instead of -// creating its own client. The socket path and timeouts come from c and -// [WithSocketPath], [WithTimeout], and [WithResponseTimeout] do not configure -// it; WithTimeout(0) still turns on the preflight ping against c. -func WithClient(c client.Client) ClientOption { - return func(o *clientOpts) error { - if c == nil { - return errors.New("no client provided") - } - o.client = c - return nil - } -} - func parseClientOptions(options ...ClientOption) (clientOpts, error) { opts := clientOpts{ timeout: config.DefaultClientRequestTimeout, @@ -150,9 +135,6 @@ func parseClientOptions(options ...ClientOption) (clientOpts, error) { } func newClient(opts clientOpts) (client.Client, error) { - if opts.client != nil { - return opts.client, nil - } socketPath := opts.socketPath if socketPath == "" { socketPath = config.DesktopSocketPath() diff --git a/plugins/pass/commands/run_test.go b/plugins/pass/commands/run_test.go index 2e7c6724..1f9f98c1 100644 --- a/plugins/pass/commands/run_test.go +++ b/plugins/pass/commands/run_test.go @@ -367,11 +367,6 @@ func TestRunCommandOptions(t *testing.T) { options: []ClientOption{WithSocketPath("")}, wantErr: "no path provided", }, - { - name: "nil client", - options: []ClientOption{WithClient(nil)}, - wantErr: "no client provided", - }, { name: "negative request timeout", options: []ClientOption{WithTimeout(-time.Second)}, From 997b3aec03ccfb856eda0c4d013993dc8be1b62d Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:53:44 +0200 Subject: [PATCH 4/7] refactor(client): rename WithClient to WithHTTPClient --- client/client.go | 6 +++--- client/client_test.go | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/client/client.go b/client/client.go index ea7708a4..0027fe6b 100644 --- a/client/client.go +++ b/client/client.go @@ -115,15 +115,15 @@ func WithResponseTimeout(responseTimeout time.Duration) Option { } } -// WithClient makes the client send its requests through httpClient instead of +// WithHTTPClient makes the client send its requests through httpClient instead of // the HTTP client it builds by default. // // httpClient must reach the secrets engine on its own, typically through a -// Transport that dials the engine's socket, so WithClient cannot be combined +// Transport that dials the engine's socket, so WithHTTPClient cannot be combined // with [WithSocketPath] or [WithDialContext]. The request and response // timeouts come from httpClient; [WithTimeout] and [WithResponseTimeout] do // not apply to it. -func WithClient(httpClient *http.Client) Option { +func WithHTTPClient(httpClient *http.Client) Option { return func(s *config) error { if httpClient == nil { return errors.New("no http client provided") diff --git a/client/client_test.go b/client/client_test.go index 30df178c..6372646e 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -546,32 +546,32 @@ func Test_Version(t *testing.T) { }) } -func TestWithClient(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(WithClient(httpClient)) + 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(WithClient(nil)) + _, 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(WithClient(&http.Client{}), WithSocketPath(randomShortSocketName())) + _, 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())), WithClient(&http.Client{})) + _, 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(WithClient(httpClient)) + c, err := New(WithHTTPClient(httpClient)) require.NoError(t, err) _, err = c.Version(t.Context()) require.ErrorIs(t, err, ErrSecretsEngineNotRunning) From 07926c8fdd7eade24144c76aa418d24c8f8132d0 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:55:05 +0200 Subject: [PATCH 5/7] docs(client): shorten WithHTTPClient godoc --- client/client.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/client/client.go b/client/client.go index 0027fe6b..2bbb584c 100644 --- a/client/client.go +++ b/client/client.go @@ -115,14 +115,7 @@ func WithResponseTimeout(responseTimeout time.Duration) Option { } } -// WithHTTPClient makes the client send its requests through httpClient instead of -// the HTTP client it builds by default. -// -// httpClient must reach the secrets engine on its own, typically through a -// Transport that dials the engine's socket, so WithHTTPClient cannot be combined -// with [WithSocketPath] or [WithDialContext]. The request and response -// timeouts come from httpClient; [WithTimeout] and [WithResponseTimeout] do -// not apply to it. +// WithHTTPClient sets the HTTP client used to reach the secrets engine. func WithHTTPClient(httpClient *http.Client) Option { return func(s *config) error { if httpClient == nil { From 7e086d96f05c2ff7eb0e9d0f6076b1cc93b8759f Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:57:14 +0200 Subject: [PATCH 6/7] docs(client): note caller owns DialContext in WithHTTPClient --- client/client.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/client.go b/client/client.go index 2bbb584c..056bf2d1 100644 --- a/client/client.go +++ b/client/client.go @@ -116,6 +116,8 @@ 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. func WithHTTPClient(httpClient *http.Client) Option { return func(s *config) error { if httpClient == nil { From e47d49df8871a9fa3d7f7e186f7b87831c5401c7 Mon Sep 17 00:00:00 2001 From: Alano Terblanche <18033717+Benehiko@users.noreply.github.com> Date: Fri, 4 Dec 2026 10:57:42 +0200 Subject: [PATCH 7/7] docs(client): note WithHTTPClient ignores timeout options --- client/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/client.go b/client/client.go index 056bf2d1..59805dad 100644 --- a/client/client.go +++ b/client/client.go @@ -117,7 +117,7 @@ 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. +// correct socket path. [WithTimeout] and [WithResponseTimeout] are ignored. func WithHTTPClient(httpClient *http.Client) Option { return func(s *config) error { if httpClient == nil {