Skip to content

Commit 73be734

Browse files
committed
cli/command/container: --use-api-socket: support DOCKER_AUTH_CONFIG
With this patch, the `--use-api-socket` flag can obtain credentials from a validly formatted `DOCKER_AUTH_CONFIG` environment-variable. If the env-var is not set, or doesn't contain credentials, it falls back to attempting to read credentials from the CLI's configured credentials store. With this patch: # Make sure there's no auth on disk first mkdir -p tmpConfig export DOCKER_CONFIG=$PWD/tmpConfig rm -f $PWD/tmpConfig/config.json # no credentials docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json cat: can't open '/run/secrets/docker/config.json': No such file or directory # pass credentials through DOCKER_AUTH_CONFIG DOCKER_AUTH_CONFIG='{"auths": {"https://index.docker.io/v1/": {"auth": "am9lam9lOmhlbGxv"}}}' docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } } # credentials from file if no DOCKER_AUTH_CONFIG is set echo '{"auths": {"https://index.docker.io/v1/": {"auth": "am9lam9lOmhlbGxv"}}}' > "${DOCKER_CONFIG}/config.json" docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } } # same if DOCKER_AUTH_CONFIG is set, but doesn't contain credentials DOCKER_AUTH_CONFIG='{}' docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } } DOCKER_AUTH_CONFIG='{"auths": {}}' docker run --rm --use-api-socket alpine cat /run/secrets/docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "am9lam9lOmhlbGxv" } } } Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2002204 commit 73be734

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package container
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/docker/cli/cli/config"
9+
"github.com/docker/cli/cli/config/configfile"
10+
"github.com/docker/cli/cli/config/types"
11+
)
12+
13+
// readCredentials resolves auth-config from the current environment to be
14+
// applied to the container if the `--use-api-socket` flag is set.
15+
//
16+
// - If a valid "DOCKER_AUTH_CONFIG" env-var is found, and it contains
17+
// credentials, it's value is used.
18+
// - If no "DOCKER_AUTH_CONFIG" env-var is found, or it does not contain
19+
// credentials, it attempts to read from the CLI's credentials store.
20+
//
21+
// It returns an error if either the "DOCKER_AUTH_CONFIG" is incorrectly
22+
// formatted, or when failing to read from the credentials store.
23+
//
24+
// A nil value is returned if neither option contained any credentials.
25+
func readCredentials(dockerCLI config.Provider) (creds map[string]types.AuthConfig, _ error) {
26+
if v, ok := os.LookupEnv("DOCKER_AUTH_CONFIG"); ok && v != "" {
27+
// The results are expected to have been unmarshaled the same as
28+
// when reading from a config-file, which includes decoding the
29+
// base64-encoded "username:password" into the "UserName" and
30+
// "Password" fields.
31+
ac := &configfile.ConfigFile{}
32+
if err := ac.LoadFromReader(strings.NewReader(v)); err != nil {
33+
return nil, fmt.Errorf("failed to read credentials from DOCKER_AUTH_CONFIG: %w", err)
34+
}
35+
if len(ac.AuthConfigs) > 0 {
36+
return ac.AuthConfigs, nil
37+
}
38+
}
39+
40+
// Resolve this here for later, ensuring we error our before we create the container.
41+
creds, err := dockerCLI.ConfigFile().GetAllCredentials()
42+
if err != nil {
43+
return nil, fmt.Errorf("resolving credentials failed: %w", err)
44+
}
45+
return creds, nil
46+
}

cli/command/container/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
295295
// what they're doing and don't inject the creds.
296296
if !envvarPresent {
297297
// Resolve this here for later, ensuring we error our before we create the container.
298-
creds, err := dockerCli.ConfigFile().GetAllCredentials()
298+
creds, err := readCredentials(dockerCli)
299299
if err != nil {
300300
return "", fmt.Errorf("resolving credentials failed: %w", err)
301301
}

0 commit comments

Comments
 (0)