2016-10-19 16:22:02 +00:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-20 01:41:11 +00:00
|
|
|
"io"
|
2016-10-19 16:22:02 +00:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
2016-11-03 21:01:54 +00:00
|
|
|
"github.com/docker/docker/opts"
|
2016-11-20 01:41:11 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-11-03 21:01:54 +00:00
|
|
|
runconfigopts "github.com/docker/docker/runconfig/opts"
|
2017-03-09 18:23:45 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-10-19 16:22:02 +00:00
|
|
|
"github.com/spf13/cobra"
|
2016-11-10 00:59:01 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-10-19 16:22:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
2016-11-03 21:01:54 +00:00
|
|
|
name string
|
2016-11-20 01:41:11 +00:00
|
|
|
file string
|
2016-11-03 21:01:54 +00:00
|
|
|
labels opts.ListOpts
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newSecretCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-11-03 21:01:54 +00:00
|
|
|
createOpts := createOptions{
|
2016-12-23 19:09:12 +00:00
|
|
|
labels: opts.NewListOpts(opts.ValidateEnv),
|
2016-11-03 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2017-01-06 20:06:02 +00:00
|
|
|
Use: "create [OPTIONS] SECRET file|-",
|
2016-11-20 01:41:11 +00:00
|
|
|
Short: "Create a secret from a file or STDIN as content",
|
2017-01-06 20:06:02 +00:00
|
|
|
Args: cli.ExactArgs(2),
|
2016-10-19 16:22:02 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-11-03 21:01:54 +00:00
|
|
|
createOpts.name = args[0]
|
2017-01-06 20:06:02 +00:00
|
|
|
createOpts.file = args[1]
|
2016-11-03 21:01:54 +00:00
|
|
|
return runSecretCreate(dockerCli, createOpts)
|
2016-10-19 16:22:02 +00:00
|
|
|
},
|
|
|
|
}
|
2016-11-03 21:01:54 +00:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.VarP(&createOpts.labels, "label", "l", "Secret labels")
|
|
|
|
|
|
|
|
return cmd
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-03 21:01:54 +00:00
|
|
|
func runSecretCreate(dockerCli *command.DockerCli, options createOptions) error {
|
2016-10-19 16:22:02 +00:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2016-11-20 01:41:11 +00:00
|
|
|
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)
|
2016-10-19 16:22:02 +00:00
|
|
|
if err != nil {
|
2017-03-09 18:23:45 +00:00
|
|
|
return errors.Errorf("Error reading content from %q: %v", options.file, err)
|
2016-10-19 16:22:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spec := swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
2016-11-03 21:01:54 +00:00
|
|
|
Name: options.name,
|
|
|
|
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
|
2016-10-19 16:22:02 +00:00
|
|
|
},
|
|
|
|
Data: secretData,
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := client.SecretCreate(ctx, spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), r.ID)
|
|
|
|
return nil
|
|
|
|
}
|