mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
c6f0b7f448
This fix tries to address the issue raised in 28581 and 28927 where it is not possible to create a secret from a file (only through STDIN). This fix add a flag `--file` to `docker secret create` so that it is possible to create a secret from a file with: ``` docker secret create --file secret.in secret.name ``` or ``` echo TEST | docker secret create --file - secret.name ``` Related docs has been updated. An integration test has been added to cover the changes. This fix fixes 28581. This fix is related to 28927. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package secret
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/docker/cli/command"
|
|
"github.com/docker/docker/opts"
|
|
"github.com/docker/docker/pkg/system"
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
type createOptions struct {
|
|
name string
|
|
file string
|
|
labels opts.ListOpts
|
|
}
|
|
|
|
func newSecretCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
createOpts := createOptions{
|
|
labels: opts.NewListOpts(runconfigopts.ValidateEnv),
|
|
}
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "create [OPTIONS] SECRET",
|
|
Short: "Create a secret from a file or STDIN as content",
|
|
Args: cli.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
createOpts.name = args[0]
|
|
return runSecretCreate(dockerCli, createOpts)
|
|
},
|
|
}
|
|
flags := cmd.Flags()
|
|
flags.VarP(&createOpts.labels, "label", "l", "Secret labels")
|
|
flags.StringVarP(&createOpts.file, "file", "f", "", "Read from a file or STDIN ('-')")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runSecretCreate(dockerCli *command.DockerCli, options createOptions) error {
|
|
client := dockerCli.Client()
|
|
ctx := context.Background()
|
|
|
|
if options.file == "" {
|
|
return fmt.Errorf("Please specify either a file name or STDIN ('-') with --file")
|
|
}
|
|
|
|
var in io.Reader = dockerCli.In()
|
|
if options.file != "-" {
|
|
file, err := system.OpenSequential(options.file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
in = file
|
|
defer file.Close()
|
|
}
|
|
|
|
secretData, err := ioutil.ReadAll(in)
|
|
if err != nil {
|
|
return fmt.Errorf("Error reading content from %q: %v", options.file, err)
|
|
}
|
|
|
|
spec := swarm.SecretSpec{
|
|
Annotations: swarm.Annotations{
|
|
Name: options.name,
|
|
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
|
|
},
|
|
Data: secretData,
|
|
}
|
|
|
|
r, err := client.SecretCreate(ctx, spec)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
fmt.Fprintln(dockerCli.Out(), r.ID)
|
|
return nil
|
|
}
|