Skip to content
Open
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
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use (
./plugin
./plugins/credentialhelper
./plugins/pass
./plugins/stdinreader
./secrets
./store
./x
Expand Down
39 changes: 39 additions & 0 deletions plugins/pass/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ $ docker pass run -e DB_PASSWORD=se://POSTGRES_PASSWORD -- ./deploy.sh
`--env-file` files, and in `--env` flags, then starts the command with the
resolved values. It needs the Secrets Engine daemon to be running.

### Install a plugin

`docker pass install` builds a Secrets Engine plugin from Go source and copies
the binary into the engine's plugin directory:

```console
$ docker pass install github.com/docker/secrets-engine/plugins/stdinreader
$ docker pass install github.com/docker/secrets-engine/plugins/stdinreader@v0.1.0
$ docker pass install ./plugins/stdinreader
```

All three install the [stdinreader plugin](../stdinreader); the last builds it
from a checkout of this repository.

It needs the Go toolchain and accepts only binaries that use the
[`plugin`](../../plugin) SDK. The file name becomes the plugin name; change it
with `--name`. If the plugin directory is not writable, run the command with
`sudo`: the build still runs as the user who called `sudo`. The engine loads
plugins at startup, so restart it afterwards.

### Uninstall a plugin

`docker pass uninstall` deletes plugins from the engine's plugin directory by
file name:

```console
$ docker pass uninstall stdinreader
```

If the plugin directory is not writable, run the command with `sudo`. A deleted
plugin keeps running until the engine restarts, so restart it afterwards.

`install` and `uninstall` exist only in builds whose engine launches plugins
from a directory.

### Troubleshooting

- **Locked keychain (Linux):** log in to the desktop session, or run
Expand Down Expand Up @@ -149,3 +184,7 @@ 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.

`commands.InstallCommand(pluginDir)` and `commands.UninstallCommand(pluginDir)`
need neither the store nor the engine. Pass them the directory your engine
launches plugins from, and add them only where that directory exists.
253 changes: 253 additions & 0 deletions plugins/pass/commands/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
// Copyright 2026 Docker, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package commands

import (
"debug/buildinfo"
_ "embed"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/spf13/cobra"
)

const pluginSDKModule = "github.com/docker/secrets-engine/plugin"

const sudoPathHint = `If Go is installed, sudo dropped it from PATH. Rerun the command with sudo PATH="$PATH".`

//go:embed install_example.md
var installExample string

//go:embed install_long.md
var installLong string

type installOpts struct {
pluginDir string
name string
}

// InstallCommand builds a plugin from Go source and installs it into
// pluginDir, the directory the secrets engine launches plugins from. The
// engine scans that directory only at startup and takes each file name as the
// plugin name.
func InstallCommand(pluginDir string) (*cobra.Command, error) {
if !filepath.IsAbs(pluginDir) {
return nil, fmt.Errorf("plugin directory must be an absolute path, got %q", pluginDir)
}
return newInstallCommand(installOpts{pluginDir: pluginDir}), nil
}

func newInstallCommand(opts installOpts) *cobra.Command {
cmd := &cobra.Command{
Use: "install PACKAGE",
Short: "Build a plugin from Go source and install it into the plugin directory.",
Long: strings.Trim(installLong, "\n"),
Example: strings.Trim(installExample, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runInstall(cmd, opts, args[0])
},
}
cmd.Flags().StringVar(&opts.name, "name", "", "Install the plugin as `NAME` instead of the binary's default name.")
return cmd
}

func runInstall(cmd *cobra.Command, opts installOpts, arg string) error {
if err := checkPluginName(opts.name); err != nil {
return err
}
target, err := parseBuildTarget(arg)
if err != nil {
return err
}
stage, err := os.MkdirTemp(opts.pluginDir, ".install-*")
if errors.Is(err, fs.ErrPermission) {
err = fmt.Errorf("installing to %s: %w", opts.pluginDir, fs.ErrPermission)
}
if err != nil {
return withEscalationHint(err)
}
defer func() { _ = os.RemoveAll(stage) }()

gobin, err := os.MkdirTemp("", "docker-pass-install-*")
if err != nil {
return err
}
defer func() { _ = os.RemoveAll(gobin) }()

if err := goInstall(cmd, target, gobin); err != nil {
return fmt.Errorf("building %s: %w", arg, err)
}
built, err := os.ReadDir(gobin)
if err != nil {
return err
}
if len(built) == 0 {
return fmt.Errorf("%s is not a main package: go install produced no binary", arg)
}
name := pluginFileName(opts.name, built[0].Name())
staged := filepath.Join(stage, name)
if err := copyExecutable(filepath.Join(gobin, built[0].Name()), staged); err != nil {
return err
}
info, err := buildinfo.ReadFile(staged)
if err != nil {
return fmt.Errorf("reading build info of %s: %w", arg, err)
}
if !usesPluginSDK(info) {
return fmt.Errorf("%s is not a secrets engine plugin: it does not use %s", arg, pluginSDKModule)
}

dst := filepath.Join(opts.pluginDir, name)
if err := os.Rename(staged, dst); err != nil {
return err
}
_, err = fmt.Fprintf(cmd.OutOrStdout(), "Installed %s (%s %s, %s) to %s\nRestart the secrets engine to load it.\n",
name, info.Main.Path, info.Main.Version, info.GoVersion, dst)
return err
}

func withEscalationHint(err error) error {
if errors.Is(err, fs.ErrPermission) && !escalated() {
return fmt.Errorf("%w\n\n%s", err, escalationHint)
}
return err
}

type buildTarget struct {
dir string
pkg string
}

func parseBuildTarget(arg string) (buildTarget, error) {
if strings.Contains(arg, "...") {
return buildTarget{}, fmt.Errorf("%s: patterns are not supported, name one main package", arg)
}
if !filepath.IsAbs(arg) && !strings.HasPrefix(arg, ".") {
if !strings.Contains(arg, "@") {
arg += "@latest"
}
return buildTarget{pkg: arg}, nil
}
dir, err := filepath.Abs(arg)
if err != nil {
return buildTarget{}, err
}
fi, err := os.Stat(dir)
if err != nil {
return buildTarget{}, err
}
if !fi.IsDir() {
return buildTarget{}, fmt.Errorf("%s: not a directory", arg)
}
return buildTarget{dir: dir, pkg: "."}, nil
}

func goInstall(cmd *cobra.Command, target buildTarget, gobin string) error {
env := slices.DeleteFunc(os.Environ(), func(kv string) bool {
key, _, _ := strings.Cut(kv, "=")
return strings.EqualFold(key, "GOOS") || strings.EqualFold(key, "GOARCH")
})
Comment on lines +167 to +170

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. is it important that we read the whole os.Environ()?

goCmd := exec.CommandContext(cmd.Context(), "go", "install", target.pkg)
goCmd.Dir = target.dir
goCmd.Env = append(env, "GOBIN="+gobin)
goCmd.Stdout = cmd.ErrOrStderr()
goCmd.Stderr = cmd.ErrOrStderr()
if err := deescalate(goCmd, gobin); err != nil {
return err
}
err := goCmd.Run()
if errors.Is(err, exec.ErrNotFound) {
err = fmt.Errorf("the Go toolchain is required to build plugins: %w", err)
if underSudo() {
err = fmt.Errorf("%w\n\n%s", err, sudoPathHint)
}
}
return err
}

func underSudo() bool {
return os.Geteuid() == 0 && os.Getenv("SUDO_UID") != ""
}

func usesPluginSDK(info *buildinfo.BuildInfo) bool {
for _, dep := range info.Deps {
if dep.Path == pluginSDKModule {
return true
}
}
return info.Main.Path == pluginSDKModule
}

func checkPluginName(name string) error {
if name == "" || isFileName(name) {
return nil
}
return fmt.Errorf("invalid --name %q: want a file name without a directory", name)
}

func isFileName(name string) bool {
return name != "." && filepath.IsLocal(name) && name == filepath.Base(name)
}

func pluginFileName(name, built string) string {
if name == "" {
return built
}
return withExeSuffix(name)
}

func withExeSuffix(name string) string {
if runtime.GOOS == "windows" && !strings.EqualFold(filepath.Ext(name), ".exe") {
name += ".exe"
}
Comment on lines +221 to +223

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't go install determine this?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for the default name, and there the code already uses what go install wrote. withExeSuffix runs for the two names go install never sees.

return name
}

func copyExecutable(src, dst string) error {
// Copy rather than rename: os.Rename fails across filesystems (e.g. tmpfs
// to ext4). src is the go install output in the system temp directory,
// which is often tmpfs.
if err := copyFile(src, dst); err != nil {
return err
}
return os.Chmod(dst, 0o755)
}

func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer func() { _ = in.Close() }()

out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o755)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
_ = out.Close()
return err
}
return out.Close()
}
23 changes: 23 additions & 0 deletions plugins/pass/commands/install_example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Install the latest version of the stdinreader plugin:

```console
$ docker pass install github.com/docker/secrets-engine/plugins/stdinreader
```

### Install a specific version:

```console
$ docker pass install github.com/docker/secrets-engine/plugins/stdinreader@v0.1.0
```

### Build and install it from a checkout of docker/secrets-engine:

```console
$ docker pass install ./plugins/stdinreader
```

### Install it under a different plugin name:

```console
$ docker pass install --name stdin ./plugins/stdinreader
```
17 changes: 17 additions & 0 deletions plugins/pass/commands/install_long.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Builds a secrets engine plugin from Go source with `go install` and copies the
binary into the engine's plugin directory.

PACKAGE is a Go package path, optionally with a version
(`github.com/docker/secrets-engine/plugins/stdinreader@v0.1.0`; the default is
`@latest`). A PACKAGE that starts with `.` or `/` is a local directory that
holds a main package.

The build runs in a temporary directory. The binary is installed only if it is
a Go program that uses the plugin SDK
(`github.com/docker/secrets-engine/plugin`). The file name becomes the plugin
name; set it with `--name`.

Requires the Go toolchain. If the plugin directory is not writable, run the
command with `sudo`: the build still runs as the user who called `sudo`.

The engine loads plugins at startup, so restart it afterwards.
Loading
Loading