Skip to content

Commit 8f20c9a

Browse files
authored
Merge pull request #5259 from thaJeztah/move_file_warning
cli/config/credentials: move warning to fileStore
2 parents d5f90ed + ab80ea3 commit 8f20c9a

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

cli/command/registry/login.go

-22
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ import (
1818
"github.com/spf13/cobra"
1919
)
2020

21-
// unencryptedWarning warns the user when using an insecure credential storage.
22-
// After a deprecation period, user will get prompted if stdin and stderr are a terminal.
23-
// Otherwise, we'll assume they want it (sadly), because people may have been scripting
24-
// insecure logins and we don't want to break them. Maybe they'll see the warning in their
25-
// logs and fix things.
26-
const unencryptedWarning = `
27-
WARNING! Your credentials are stored unencrypted in '%s'.
28-
Configure a credential helper to remove this warning. See
29-
https://docs.docker.com/go/credential-store/
30-
`
31-
3221
type loginOptions struct {
3322
serverAddress string
3423
user string
@@ -66,11 +55,6 @@ func NewLoginCommand(dockerCli command.Cli) *cobra.Command {
6655
return cmd
6756
}
6857

69-
type isFileStore interface {
70-
IsFileStore() bool
71-
GetFilename() string
72-
}
73-
7458
func verifyloginOptions(dockerCli command.Cli, opts *loginOptions) error {
7559
if opts.password != "" {
7660
fmt.Fprintln(dockerCli.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
@@ -137,16 +121,10 @@ func runLogin(ctx context.Context, dockerCli command.Cli, opts loginOptions) err
137121
}
138122

139123
creds := dockerCli.ConfigFile().GetCredentialsStore(serverAddress)
140-
141124
if err := creds.Store(configtypes.AuthConfig(authConfig)); err != nil {
142125
return errors.Errorf("Error saving credentials: %v", err)
143126
}
144127

145-
if store, isDefault := creds.(isFileStore); isDefault && authConfig.Password != "" {
146-
// Display a warning if we're storing the users password (not a token)
147-
_, _ = fmt.Fprintln(dockerCli.Err(), fmt.Sprintf(unencryptedWarning, store.GetFilename()))
148-
}
149-
150128
if response.Status != "" {
151129
fmt.Fprintln(dockerCli.Out(), response.Status)
152130
}

cli/config/credentials/file_store.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package credentials
22

33
import (
4+
"fmt"
45
"net"
56
"net/url"
7+
"os"
68
"strings"
79

810
"github.com/docker/cli/cli/config/types"
@@ -52,19 +54,33 @@ func (c *fileStore) GetAll() (map[string]types.AuthConfig, error) {
5254
return c.file.GetAuthConfigs(), nil
5355
}
5456

57+
// unencryptedWarning warns the user when using an insecure credential storage.
58+
// After a deprecation period, user will get prompted if stdin and stderr are a terminal.
59+
// Otherwise, we'll assume they want it (sadly), because people may have been scripting
60+
// insecure logins and we don't want to break them. Maybe they'll see the warning in their
61+
// logs and fix things.
62+
const unencryptedWarning = `
63+
WARNING! Your credentials are stored unencrypted in '%s'.
64+
Configure a credential helper to remove this warning. See
65+
https://docs.docker.com/go/credential-store/
66+
`
67+
5568
// Store saves the given credentials in the file store.
5669
func (c *fileStore) Store(authConfig types.AuthConfig) error {
5770
authConfigs := c.file.GetAuthConfigs()
5871
authConfigs[authConfig.ServerAddress] = authConfig
59-
return c.file.Save()
60-
}
72+
if err := c.file.Save(); err != nil {
73+
return err
74+
}
6175

62-
func (c *fileStore) GetFilename() string {
63-
return c.file.GetFilename()
64-
}
76+
if authConfig.Password != "" {
77+
// Display a warning if we're storing the users password (not a token).
78+
//
79+
// FIXME(thaJeztah): make output configurable instead of hardcoding to os.Stderr
80+
_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf(unencryptedWarning, c.file.GetFilename()))
81+
}
6582

66-
func (c *fileStore) IsFileStore() bool {
67-
return true
83+
return nil
6884
}
6985

7086
// ConvertToHostname converts a registry url which has http|https prepended

0 commit comments

Comments
 (0)