DockerCLI/cli
Sebastiaan van Stijn f2424bd375
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`;
2b17f4c8a8, 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
abb5e9a077 (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 31dc5c0a9a),
  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-03-19 03:01:57 +01:00
..
command Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
compose Update compose 3.8 to disallow additional properties 2019-03-18 12:56:16 +01:00
config Merge pull request #1712 from thaJeztah/fix_test_for_go_1.12 2019-03-13 13:38:57 -07:00
connhelper connhelper: export functions for other projects 2019-03-02 21:11:49 +09:00
context Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
debug updated vendoring 2017-09-01 19:41:06 -04:00
flags Introduce docker context store 2019-01-04 17:06:51 +01:00
manifest Fix some spelling errors 2019-01-11 15:49:02 +08:00
registry/client cli/registry: fix a Debugf statement 2018-10-11 01:13:13 -07:00
streams Extract streams helpers from command package to their own package to remove a cyclic dependency from command to internal/containerizedengine 2019-01-28 14:36:00 +01:00
trust Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
version Move versioning variables to a separate package. 2019-01-29 11:26:40 +00:00
winresources Add windows resources to binary. 2017-05-15 18:03:03 -04:00
cobra.go Use a copy of root flagset in `HandleGlobalFlags` 2019-03-13 11:28:17 +00:00
cobra_test.go Refactor plugins' vendor location on --help 2019-02-21 17:54:11 +01:00
error.go Import docker/docker/cli 2017-04-17 17:40:59 -04:00
required.go Singularize / pluralize "argument(s)" in error message 2017-08-12 18:25:38 +02:00
required_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00