DockerCLI/cli/command
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
..
builder Hide "builder" and "network" commands on old API versions 2019-01-30 01:06:09 +01:00
bundlefile Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
checkpoint formatter package heavy refactoring 2018-10-23 17:05:44 +02:00
commands Merge pull request #1602 from thaJeztah/hide_experimental_deploy 2019-02-08 13:53:12 -08:00
config Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
container Merge pull request #1700 from thaJeztah/update_engine 2019-03-12 10:41:51 -07:00
context Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
engine Remove e-mail from trial flow 2019-01-08 00:35:24 +01:00
formatter Fast Context Switch: commands 2019-01-10 22:25:43 +01:00
idresolver Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
image Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
inspect Update tests to use gotest.tools 👼 2018-06-08 18:24:26 +02:00
manifest Add bash completion for `manifest` command family 2018-08-30 08:54:49 +02:00
network Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
node Fix tests with missing mocks 2018-11-08 11:37:49 +01:00
plugin Fix tests with missing mocks 2018-11-08 11:37:49 +01:00
registry Remove docker api dependency from cli/config 2019-01-31 21:25:43 +00:00
secret Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
service Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
stack Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
swarm 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
system Make system prune warning filters human-readable 2019-03-18 13:57:18 +01:00
task formatter package heavy refactoring 2018-10-23 17:05:44 +02:00
testdata Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
trust Fix tests with missing mocks 2018-11-08 11:37:49 +01:00
volume Fix labels copying value from environment variables 2019-03-19 03:01:57 +01:00
cli.go Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
cli_options.go Add context store config options and expose context commands 2019-01-29 11:19:54 +01:00
cli_options_test.go Unit test for WithContentTrustFromEnv 2019-01-29 11:26:40 +00:00
cli_test.go Cover the changes with unit test 2019-03-13 14:18:41 +01:00
context.go Fast Context Switch: commands 2019-01-10 22:25:43 +01:00
defaultcontextstore.go Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
defaultcontextstore_test.go Make default context behaves like a real context: 2019-03-18 11:45:46 +01:00
events_utils.go updated vendoring 2017-09-01 19:41:06 -04:00
orchestrator.go Fast Context Switch: commands 2019-01-10 22:25:43 +01:00
orchestrator_test.go Introduce docker context store 2019-01-04 17:06:51 +01:00
registry.go Remove docker api dependency from cli/config 2019-01-31 21:25:43 +00:00
registry_test.go Remove docker api dependency from cli/config 2019-01-31 21:25:43 +00:00
streams.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.go Refactor content_trust cli/flags handling 2018-03-08 15:00:43 -05:00
utils.go Merge pull request #1515 from sw-pschmied/1514-prevent-replacing-irregular-files 2019-02-07 10:05:02 +01:00