diff --git a/cli/command/registry.go b/cli/command/registry.go index 452e2d7354cc..75b979e18561 100644 --- a/cli/command/registry.go +++ b/cli/command/registry.go @@ -144,8 +144,8 @@ func PromptUserForCredentials(ctx context.Context, cli Cli, argUser, argPassword } } - argPassword = strings.TrimSpace(argPassword) - if argPassword == "" { + isEmpty := strings.TrimSpace(argPassword) == "" + if isEmpty { restoreInput, err := prompt.DisableInputEcho(cli.In()) if err != nil { return registrytypes.AuthConfig{}, err diff --git a/cli/command/registry/login_test.go b/cli/command/registry/login_test.go index 9537f008b486..b392dab444a9 100644 --- a/cli/command/registry/login_test.go +++ b/cli/command/registry/login_test.go @@ -339,6 +339,56 @@ func TestRunLogin(t *testing.T) { }, }, }, + { + doc: "password with leading and trailing spaces", + priorCredentials: map[string]configtypes.AuthConfig{}, + input: loginOptions{ + serverAddress: "reg1", + user: "my-username", + password: " my password with spaces ", + }, + expectedCredentials: map[string]configtypes.AuthConfig{ + "reg1": { + Username: "my-username", + Password: " my password with spaces ", + ServerAddress: "reg1", + }, + }, + }, + { + doc: "password stdin with line-endings", + priorCredentials: map[string]configtypes.AuthConfig{}, + stdIn: " my password with spaces \r\n", + input: loginOptions{ + serverAddress: "reg1", + user: "my-username", + passwordStdin: true, + }, + expectedCredentials: map[string]configtypes.AuthConfig{ + "reg1": { + Username: "my-username", + Password: " my password with spaces ", + ServerAddress: "reg1", + }, + }, + }, + { + doc: "password stdin with multiple line-endings", + priorCredentials: map[string]configtypes.AuthConfig{}, + stdIn: " my password\nwith spaces \r\n\r\n", + input: loginOptions{ + serverAddress: "reg1", + user: "my-username", + passwordStdin: true, + }, + expectedCredentials: map[string]configtypes.AuthConfig{ + "reg1": { + Username: "my-username", + Password: " my password\nwith spaces \r\n", + ServerAddress: "reg1", + }, + }, + }, } for _, tc := range testCases {