2016-10-19 12:22:02 -04:00
|
|
|
package secret
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-10-19 12:22:02 -04:00
|
|
|
"fmt"
|
2016-11-19 20:41:11 -05:00
|
|
|
"io"
|
2016-10-19 12:22:02 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-11-19 20:41:11 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-10-19 12:22:02 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type createOptions struct {
|
2018-02-21 13:45:34 -05:00
|
|
|
name string
|
|
|
|
driver string
|
|
|
|
templateDriver string
|
|
|
|
file string
|
|
|
|
labels opts.ListOpts
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-04-01 03:07:22 -04:00
|
|
|
func newSecretCreateCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := createOptions{
|
Fix labels copying value from environment variables
This patch fixes a bug where labels use the same behavior as `--env`, resulting
in a value to be copied from environment variables with the same name as the
label if no value is set (i.e. a simple key, no `=` sign, no value).
An earlier pull request addressed similar cases for `docker run`;
2b17f4c8a8caad552025edb05a73db683fb8a5c6, but this did not address the
same situation for (e.g.) `docker service create`.
Digging in history for this bug, I found that use of the `ValidateEnv`
function for labels was added in the original implementation of the labels feature in
https://github.com/docker/docker/commit/abb5e9a0777469e64fe2c7ecfa66ea01083d2071#diff-ae476143d40e21ac0918630f7365ed3cR34
However, the design never intended it to expand environment variables,
and use of this function was either due to either a "copy/paste" of the
equivalent `--env` flags, or a misunderstanding (the name `ValidateEnv` does
not communicate that it also expands environment variables), and the existing
`ValidateLabel` was designed for _engine_ labels (which required a value to
be set).
Following the initial implementation, other parts of the code followed
the same (incorrect) approach, therefore leading the bug to be introduced
in services as well.
This patch:
- updates the `ValidateLabel` to match the expected validation
rules (this function is no longer used since 31dc5c0a9a8bdc11c7ad335aebb753ed527caa5a),
and the daemon has its own implementation)
- corrects various locations in the code where `ValidateEnv` was used instead of `ValidateLabel`.
Before this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker service inspect --format '{{json .Spec.Labels}}' test
{"SOME_ENV_VAR":"I_AM_SOME_ENV_VAR"}
```
After this patch:
```bash
export SOME_ENV_VAR=I_AM_SOME_ENV_VAR
docker service create --label SOME_ENV_VAR --tty --name test busybox
docker container inspect --format '{{json .Config.Labels}}' test
{"SOME_ENV_VAR":""}
```
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2019-02-13 10:47:30 -05:00
|
|
|
labels: opts.NewListOpts(opts.ValidateLabel),
|
2016-11-03 17:01:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2017-07-30 07:01:39 -04:00
|
|
|
Use: "create [OPTIONS] SECRET [file|-]",
|
2016-11-19 20:41:11 -05:00
|
|
|
Short: "Create a secret from a file or STDIN as content",
|
2017-07-30 07:01:39 -04:00
|
|
|
Args: cli.RequiresRangeArgs(1, 2),
|
2016-10-19 12:22:02 -04:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.name = args[0]
|
2017-07-30 07:01:39 -04:00
|
|
|
if len(args) == 2 {
|
|
|
|
options.file = args[1]
|
|
|
|
}
|
2017-05-15 08:45:19 -04:00
|
|
|
return runSecretCreate(dockerCli, options)
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
}
|
2016-11-03 17:01:54 -04:00
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.VarP(&options.labels, "label", "l", "Secret labels")
|
2017-07-30 07:01:39 -04:00
|
|
|
flags.StringVarP(&options.driver, "driver", "d", "", "Secret driver")
|
|
|
|
flags.SetAnnotation("driver", "version", []string{"1.31"})
|
2018-02-21 13:45:34 -05:00
|
|
|
flags.StringVar(&options.templateDriver, "template-driver", "", "Template driver")
|
2019-03-28 11:18:50 -04:00
|
|
|
flags.SetAnnotation("template-driver", "version", []string{"1.37"})
|
2016-11-03 17:01:54 -04:00
|
|
|
|
|
|
|
return cmd
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
|
2017-04-01 03:07:22 -04:00
|
|
|
func runSecretCreate(dockerCli command.Cli, options createOptions) error {
|
2016-10-19 12:22:02 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2017-07-30 07:01:39 -04:00
|
|
|
if options.driver != "" && options.file != "" {
|
|
|
|
return errors.Errorf("When using secret driver secret data must be empty")
|
2016-11-19 20:41:11 -05:00
|
|
|
}
|
|
|
|
|
2017-07-30 07:01:39 -04:00
|
|
|
secretData, err := readSecretData(dockerCli.In(), options.file)
|
2016-10-19 12:22:02 -04:00
|
|
|
if err != nil {
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("Error reading content from %q: %v", options.file, err)
|
2016-10-19 12:22:02 -04:00
|
|
|
}
|
|
|
|
spec := swarm.SecretSpec{
|
|
|
|
Annotations: swarm.Annotations{
|
2016-11-03 17:01:54 -04:00
|
|
|
Name: options.name,
|
2017-06-05 18:23:21 -04:00
|
|
|
Labels: opts.ConvertKVStringsToMap(options.labels.GetAll()),
|
2016-10-19 12:22:02 -04:00
|
|
|
},
|
|
|
|
Data: secretData,
|
|
|
|
}
|
2017-07-30 07:01:39 -04:00
|
|
|
if options.driver != "" {
|
|
|
|
spec.Driver = &swarm.Driver{
|
|
|
|
Name: options.driver,
|
|
|
|
}
|
|
|
|
}
|
2018-02-21 13:45:34 -05:00
|
|
|
if options.templateDriver != "" {
|
|
|
|
spec.Templating = &swarm.Driver{
|
|
|
|
Name: options.templateDriver,
|
|
|
|
}
|
|
|
|
}
|
2016-10-19 12:22:02 -04:00
|
|
|
r, err := client.SecretCreate(ctx, spec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), r.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-30 07:01:39 -04:00
|
|
|
|
|
|
|
func readSecretData(in io.ReadCloser, file string) ([]byte, error) {
|
|
|
|
// Read secret value from external driver
|
|
|
|
if file == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if file != "-" {
|
|
|
|
var err error
|
|
|
|
in, err = system.OpenSequential(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
}
|
2022-02-25 08:30:27 -05:00
|
|
|
data, err := io.ReadAll(in)
|
2017-07-30 07:01:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|