Skip to content

Commit e7af181

Browse files
committed
cli/command/registry: login: improve flag validation
Before this change, some errors could be ambiguous as they did not distinguish a flag to be omitted, or set, but with an empty value. For example, if a user would try to loging but with an empty username, the error would suggest that the `--username` flag was not set (which it was); I don't have `MY_USERNAME` set in this shell; printenv MY_USERNAME || echo 'variable not set' variable not set Now, attempting to do a non-interactive login would result in an ambiguous error; echo "supersecret" | docker login --password-stdin --username "$MY_USERNAME" Must provide --username with --password-stdin With this patch applied, the error indicates that the username was empty, or not set; echo "supersecret" | docker login --password-stdin --username "$MY_USERNAME" username is empty echo "supersecret" | docker login --password-stdin the --password-stdin option requires --username to be set echo "supersecret" | docker login --password-stdin --password "supersecret" conflicting options: cannot specify both --password and --password-stdin Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 8845ccd commit e7af181

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

cli/command/registry/login.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/docker/docker/registry"
2222
"github.com/pkg/errors"
2323
"github.com/spf13/cobra"
24+
"github.com/spf13/pflag"
2425
)
2526

2627
type loginOptions struct {
@@ -43,6 +44,9 @@ func NewLoginCommand(dockerCLI command.Cli) *cobra.Command {
4344
if len(args) > 0 {
4445
opts.serverAddress = args[0]
4546
}
47+
if err := verifyLoginFlags(cmd.Flags(), opts); err != nil {
48+
return err
49+
}
4650
return runLogin(cmd.Context(), dockerCLI, opts)
4751
},
4852
Annotations: map[string]string{
@@ -60,17 +64,35 @@ func NewLoginCommand(dockerCLI command.Cli) *cobra.Command {
6064
return cmd
6165
}
6266

63-
func verifyLoginOptions(dockerCLI command.Cli, opts *loginOptions) error {
67+
// verifyLoginFlags validates flags set on the command.
68+
//
69+
// TODO(thaJeztah); combine with verifyLoginOptions, but this requires rewrites of many tests.
70+
func verifyLoginFlags(flags *pflag.FlagSet, opts loginOptions) error {
71+
if flags.Changed("password-stdin") {
72+
if flags.Changed("password") {
73+
return errors.New("conflicting options: cannot specify both --password and --password-stdin")
74+
}
75+
if !flags.Changed("username") {
76+
return errors.New("the --password-stdin option requires --username to be set")
77+
}
78+
}
79+
if flags.Changed("username") && opts.user == "" {
80+
return errors.New("username is empty")
81+
}
82+
if flags.Changed("password") && opts.password == "" {
83+
return errors.New("password is empty")
84+
}
85+
return nil
86+
}
87+
88+
func verifyLoginOptions(dockerCLI command.Streams, opts *loginOptions) error {
6489
if opts.password != "" {
6590
_, _ = fmt.Fprintln(dockerCLI.Err(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
66-
if opts.passwordStdin {
67-
return errors.New("--password and --password-stdin are mutually exclusive")
68-
}
6991
}
7092

7193
if opts.passwordStdin {
7294
if opts.user == "" {
73-
return errors.New("Must provide --username with --password-stdin")
95+
return errors.New("username is empty")
7496
}
7597

7698
contents, err := io.ReadAll(dockerCLI.In())

cli/command/registry/login_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,12 +550,17 @@ func TestLoginValidateFlags(t *testing.T) {
550550
{
551551
name: "--password-stdin without --username",
552552
args: []string{"--password-stdin"},
553-
expectedErr: `Must provide --username with --password-stdin`,
553+
expectedErr: `the --password-stdin option requires --username to be set`,
554554
},
555555
{
556556
name: "--password-stdin with empty --username",
557557
args: []string{"--password-stdin", "--username", ""},
558-
expectedErr: `Must provide --username with --password-stdin`,
558+
expectedErr: `username is empty`,
559+
},
560+
{
561+
name: "empty --username",
562+
args: []string{"--username", ""},
563+
expectedErr: `username is empty`,
559564
},
560565
{
561566
name: "--username without value",
@@ -565,7 +570,12 @@ func TestLoginValidateFlags(t *testing.T) {
565570
{
566571
name: "conflicting options --password-stdin and --password",
567572
args: []string{"--password-stdin", "--password", ""},
568-
expectedErr: `Must provide --username with --password-stdin`,
573+
expectedErr: `conflicting options: cannot specify both --password and --password-stdin`,
574+
},
575+
{
576+
name: "empty --password",
577+
args: []string{"--password", ""},
578+
expectedErr: `password is empty`,
569579
},
570580
{
571581
name: "--password without value",

0 commit comments

Comments
 (0)