DockerCLI/cli/command/image
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
..
build Merge pull request #1111 from tiborvass/experimental-buildkit 2018-06-13 18:21:41 -07:00
testdata Add option to pull images quietly 2018-12-19 13:48:41 +01:00
build.go Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
build_buildkit.go build: update session support detection 2018-10-22 20:52:56 +00:00
build_session.go build: update session support detection 2018-10-22 20:52:56 +00:00
build_test.go 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
client_test.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
cmd.go Move credential getting functions to the ConfigFile. 2017-06-27 13:46:47 +02:00
formatter_history.go formatter package heavy refactoring 2018-10-23 17:05:44 +02:00
formatter_history_test.go formatter package heavy refactoring 2018-10-23 17:05:44 +02:00
history.go formatter package heavy refactoring 2018-10-23 17:05:44 +02:00
history_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
import.go LCOW: --platform on import (already in API) 2018-09-13 15:04:04 -07:00
import_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
inspect.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
inspect_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
list.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
list_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
load.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
load_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
prune.go prune: use filters.Args.Clone() 2018-12-10 16:48:59 +00:00
prune_test.go Fix panic when pruning images with label-filter 2018-12-07 17:56:31 +01:00
pull.go Add option to pull images quietly 2018-12-19 13:48:41 +01:00
pull_test.go Add option to pull images quietly 2018-12-19 13:48:41 +01:00
push.go Export Push and Save 2018-06-14 15:48:21 +02:00
push_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
remove.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
remove_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
save.go Prevent overwriting irregular files (cp, save, export commands) 2019-02-07 09:17:35 +01:00
save_test.go Prevent overwriting irregular files (cp, save, export commands) 2019-02-07 09:17:35 +01:00
tag.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
tag_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
trust.go 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_test.go Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00