From df75fc62c8316bce058bbdda29f66af9dcc5573a Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 11 Jul 2019 09:25:38 -0400 Subject: Add support for -env-host This flag passes the host environment into the container. The basic idea is to leak all environment variables from the host into the container. Environment variables from the image, and passed in via --env and --env-file will override the host environment. Signed-off-by: Daniel J Walsh --- cmd/podman/shared/create.go | 10 ++++++++++ cmd/podman/shared/intermediate.go | 1 + 2 files changed, 11 insertions(+) (limited to 'cmd/podman/shared') diff --git a/cmd/podman/shared/create.go b/cmd/podman/shared/create.go index f401d3cf5..736a682eb 100644 --- a/cmd/podman/shared/create.go +++ b/cmd/podman/shared/create.go @@ -483,6 +483,16 @@ func ParseCreateOpts(ctx context.Context, c *GenericCLIResults, runtime *libpod. // ENVIRONMENT VARIABLES env := EnvVariablesFromData(data) + if c.Bool("env-host") { + for _, e := range os.Environ() { + pair := strings.SplitN(e, "=", 2) + if _, ok := env[pair[0]]; !ok { + if len(pair) > 1 { + env[pair[0]] = pair[1] + } + } + } + } if err := parse.ReadKVStrings(env, c.StringSlice("env-file"), c.StringArray("env")); err != nil { return nil, errors.Wrapf(err, "unable to process environment variables") } diff --git a/cmd/podman/shared/intermediate.go b/cmd/podman/shared/intermediate.go index eecd1604c..855f84086 100644 --- a/cmd/podman/shared/intermediate.go +++ b/cmd/podman/shared/intermediate.go @@ -393,6 +393,7 @@ func NewIntermediateLayer(c *cliconfig.PodmanCommand, remote bool) GenericCLIRes m["dns-search"] = newCRStringSlice(c, "dns-search") m["entrypoint"] = newCRString(c, "entrypoint") m["env"] = newCRStringArray(c, "env") + m["env-host"] = newCRBool(c, "env-host") m["env-file"] = newCRStringSlice(c, "env-file") m["expose"] = newCRStringSlice(c, "expose") m["gidmap"] = newCRStringSlice(c, "gidmap") -- cgit v1.2.3-54-g00ecf From efe9c5b0e7968473b261eae4641e422e4a0f69a2 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Thu, 11 Jul 2019 13:39:31 -0400 Subject: Add glob parsing for --env flag Sometimes you want to add a few environmen variables based on the last field being a "*". Signed-off-by: Daniel J Walsh --- cmd/podman/shared/parse/parse.go | 19 ++++++++++++++++--- docs/podman-create.1.md | 14 ++++++++------ docs/podman-run.1.md | 14 ++++++++------ 3 files changed, 32 insertions(+), 15 deletions(-) (limited to 'cmd/podman/shared') diff --git a/cmd/podman/shared/parse/parse.go b/cmd/podman/shared/parse/parse.go index 7bc2652cb..a77002235 100644 --- a/cmd/podman/shared/parse/parse.go +++ b/cmd/podman/shared/parse/parse.go @@ -112,9 +112,22 @@ func parseEnv(env map[string]string, line string) error { if len(data) > 1 { env[name] = data[1] } else { - // if only a pass-through variable is given, clean it up. - val, _ := os.LookupEnv(name) - env[name] = val + if strings.HasSuffix(name, "*") { + name = strings.TrimSuffix(name, "*") + for _, e := range os.Environ() { + part := strings.SplitN(e, "=", 2) + if len(part) < 2 { + continue + } + if strings.HasPrefix(part[0], name) { + env[part[0]] = part[1] + } + } + } else { + // if only a pass-through variable is given, clean it up. + val, _ := os.LookupEnv(name) + env[name] = val + } } return nil } diff --git a/docs/podman-create.1.md b/docs/podman-create.1.md index 00b706d4a..8f7577a86 100644 --- a/docs/podman-create.1.md +++ b/docs/podman-create.1.md @@ -245,7 +245,9 @@ You need to specify multi option commands in the form of a json string. Set environment variables -This option allows you to specify arbitrary environment variables that are available for the process that will be launched inside of the container. If you specify a environment variable without a value, podman will check the host environment for a value or set the environment to "". See **Environment** note below for precedence. +This option allows you to specify arbitrary environment variables that are available for the process that will be launched inside of the container. If you specify a environment variable without a value, podman will check the host environment for a value or set the environment to "". If you specify a environment variable ending in --*--, podman will search the host environment for variables starting with the prefix and add them to the container. If you want to add an environment variable with a ***** following it, then you need to set a value. + +See **Environment** note below for precedence. **--env-host**=*true|false* @@ -905,16 +907,16 @@ required for VPN, without it containers need to be run with the --net=host flag. ## ENVIRONMENT -Environment variables within containers can be set using multiple different options: This section describes the presidence. +Environment variables within containers can be set using multiple different options: This section describes the precedence. -Presidence Order: +Precedence Order: **--env-host** : Host environment of the process executing podman is added. - Container image : Any enviroment variables specified in the contianer image. + Container image : Any enviroment variables specified in the container image. - **--env-file** : Any environment variables specfied via env-files. If multiple files specified, then they override each other in order of entry. + **--env-file** : Any environment variables specified via env-files. If multiple files specified, then they override each other in order of entry. - **--env** : Any environment variables specified will overide previous settings. + **--env** : Any environment variables specified will override previous settings. ## FILES diff --git a/docs/podman-run.1.md b/docs/podman-run.1.md index ea1670fac..dd52958ac 100644 --- a/docs/podman-run.1.md +++ b/docs/podman-run.1.md @@ -252,7 +252,9 @@ You need to specify multi option commands in the form of a json string. Set environment variables -This option allows you to specify arbitrary environment variables that are available for the process that will be launched inside of the container. If you specify a environment variable without a value, podman will check the host environment for a value or set the environment to "". See **Environment** note below for precedence. +This option allows you to specify arbitrary environment variables that are available for the process that will be launched inside of the container. If you specify a environment variable without a value, podman will check the host environment for a value or set the environment to "". If you specify a environment variable ending in --*--, podman will search the host environment for variables starting with the prefix and add them to the container. If you want to add an environment variable with a ***** following it, then you need to set a value. + +See **Environment** note below for precedence. **--env-host**=*true|false* @@ -1189,17 +1191,17 @@ required for VPN, without it containers need to be run with the --net=host flag. ## ENVIRONMENT -Environment variables within containers can be set using multiple different options: This section describes the presidence. +Environment variables within containers can be set using multiple different options: This section describes the precedence. -Presidence Order: +Precedence Order: **--env-host** : Host environment of the process executing podman is added. - Container image : Any enviroment variables specified in the contianer image. + Container image : Any enviroment variables specified in the container image. - **--env-file** : Any environment variables specfied via env-files. If multiple files specified, then they override each other in order of entry. + **--env-file** : Any environment variables specified via env-files. If multiple files specified, then they override each other in order of entry. - **--env** : Any environment variables specified will overide previous settings. + **--env** : Any environment variables specified will override previous settings. ## FILES -- cgit v1.2.3-54-g00ecf