Skip to content

Commit 3302212

Browse files
authored
Merge pull request #6163 from Benehiko/env-credential-warn
registry: warn of `DOCKER_AUTH_CONFIG` usage in login and logout
2 parents 28f19a9 + ccd5bd8 commit 3302212

5 files changed

Lines changed: 63 additions & 7 deletions

File tree

cli/command/registry/login.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func runLogin(ctx context.Context, dockerCLI command.Cli, opts loginOptions) err
110110
if err := verifyLoginOptions(dockerCLI, &opts); err != nil {
111111
return err
112112
}
113+
114+
maybePrintEnvAuthWarning(dockerCLI)
115+
113116
var (
114117
serverAddress string
115118
msg string

cli/command/registry/logout.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func NewLogoutCommand(dockerCli command.Cli) *cobra.Command {
3636
}
3737

3838
func runLogout(ctx context.Context, dockerCLI command.Cli, serverAddress string) error {
39+
maybePrintEnvAuthWarning(dockerCLI)
40+
3941
var isDefaultRegistry bool
4042

4143
if serverAddress == "" {

cli/command/registry/warning.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package registry
2+
3+
import (
4+
"os"
5+
6+
"github.com/docker/cli/cli/command"
7+
"github.com/docker/cli/cli/config/configfile"
8+
"github.com/docker/cli/internal/tui"
9+
)
10+
11+
// maybePrintEnvAuthWarning if the `DOCKER_AUTH_CONFIG` environment variable is
12+
// set this function will output a warning to stdErr
13+
func maybePrintEnvAuthWarning(out command.Streams) {
14+
if os.Getenv(configfile.DockerEnvConfigKey) != "" {
15+
tui.NewOutput(out.Err()).
16+
PrintWarning("%[1]s is set and takes precedence.\nUnset %[1]s to restore the CLI auth behaviour.\n", configfile.DockerEnvConfigKey)
17+
}
18+
}

cli/config/configfile/file.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ type configEnv struct {
5656
AuthConfigs map[string]configEnvAuth `json:"auths"`
5757
}
5858

59-
// dockerEnvConfig is an environment variable that contains a JSON encoded
59+
// DockerEnvConfigKey is an environment variable that contains a JSON encoded
6060
// credential config. It only supports storing the credentials as a base64
6161
// encoded string in the format base64("username:pat").
6262
//
@@ -71,7 +71,7 @@ type configEnv struct {
7171
// }
7272
// }
7373
// }
74-
const dockerEnvConfig = "DOCKER_AUTH_CONFIG"
74+
const DockerEnvConfigKey = "DOCKER_AUTH_CONFIG"
7575

7676
// ProxyConfig contains proxy configuration settings
7777
type ProxyConfig struct {
@@ -296,7 +296,7 @@ func (configFile *ConfigFile) GetCredentialsStore(registryHostname string) crede
296296
store = newNativeStore(configFile, helper)
297297
}
298298

299-
envConfig := os.Getenv(dockerEnvConfig)
299+
envConfig := os.Getenv(DockerEnvConfigKey)
300300
if envConfig == "" {
301301
return store
302302
}

internal/tui/note.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,39 @@ var InfoHeader = Str{
1515
Fancy: aec.Bold.Apply(aec.LightCyanB.Apply(aec.BlackF.Apply("i")) + " " + aec.LightCyanF.Apply("Info → ")),
1616
}
1717

18-
func (o Output) PrintNote(format string, args ...any) {
18+
type options struct {
19+
header Str
20+
}
21+
22+
type noteOptions func(o *options)
23+
24+
func withHeader(header Str) noteOptions {
25+
return func(o *options) {
26+
o.header = header
27+
}
28+
}
29+
30+
func (o Output) printNoteWithOptions(format string, args []any, opts ...noteOptions) {
1931
if o.isTerminal {
2032
// TODO: Handle all flags
2133
format = strings.ReplaceAll(format, "--platform", ColorFlag.Apply("--platform"))
2234
}
2335

24-
header := o.Sprint(InfoHeader)
36+
opt := &options{
37+
header: InfoHeader,
38+
}
39+
40+
for _, override := range opts {
41+
override(opt)
42+
}
43+
44+
h := o.Sprint(opt.header)
2545

26-
_, _ = fmt.Fprint(o, "\n", header)
46+
_, _ = fmt.Fprint(o, "\n", h)
2747
s := fmt.Sprintf(format, args...)
2848
for idx, line := range strings.Split(s, "\n") {
2949
if idx > 0 {
30-
_, _ = fmt.Fprint(o, strings.Repeat(" ", Width(header)))
50+
_, _ = fmt.Fprint(o, strings.Repeat(" ", Width(h)))
3151
}
3252

3353
l := line
@@ -37,3 +57,16 @@ func (o Output) PrintNote(format string, args ...any) {
3757
_, _ = fmt.Fprintln(o, l)
3858
}
3959
}
60+
61+
func (o Output) PrintNote(format string, args ...any) {
62+
o.printNoteWithOptions(format, args, withHeader(InfoHeader))
63+
}
64+
65+
var warningHeader = Str{
66+
Plain: " Warn -> ",
67+
Fancy: aec.Bold.Apply(aec.LightYellowB.Apply(aec.BlackF.Apply("w")) + " " + ColorWarning.Apply("Warn → ")),
68+
}
69+
70+
func (o Output) PrintWarning(format string, args ...any) {
71+
o.printNoteWithOptions(format, args, withHeader(warningHeader))
72+
}

0 commit comments

Comments
 (0)