Skip to content

Commit c012702

Browse files
authored
feat: read argocd password from stdin (#23520)
Signed-off-by: nitishfy <[email protected]>
1 parent fb94cad commit c012702

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

cmd/argocd/commands/login.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ argocd login cd.argoproj.io --core`,
185185
command.Flags().StringVar(&password, "password", "", "The password of an account to authenticate")
186186
command.Flags().BoolVar(&sso, "sso", false, "Perform SSO login")
187187
command.Flags().IntVar(&ssoPort, "sso-port", DefaultSSOLocalPort, "Port to run local OAuth2 login application")
188-
command.Flags().
189-
BoolVar(&skipTestTLS, "skip-test-tls", false, "Skip testing whether the server is configured with TLS (this can help when the command hangs for no apparent reason)")
188+
command.Flags().BoolVar(&skipTestTLS, "skip-test-tls", false, "Skip testing whether the server is configured with TLS (this can help when the command hangs for no apparent reason)")
190189
command.Flags().BoolVar(&ssoLaunchBrowser, "sso-launch-browser", true, "Automatically launch the system default browser when performing SSO login")
191190
return command
192191
}

util/cli/cli.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,21 @@ func PromptMessage(message, value string) string {
102102
return value
103103
}
104104

105-
// PromptPassword prompts the user for a password, without local echo. (unless already supplied)
105+
// PromptPassword prompts the user for a password, without local echo (unless already supplied).
106+
// If terminal.ReadPassword fails — often due to stdin not being a terminal (e.g., when input is piped),
107+
// we fall back to reading from standard input using bufio.Reader.
106108
func PromptPassword(password string) string {
107109
for password == "" {
108110
fmt.Print("Password: ")
109111
passwordRaw, err := terminal.ReadPassword(int(os.Stdin.Fd()))
110-
errors.CheckError(err)
112+
if err != nil {
113+
// Fallback: handle cases where stdin is not a terminal (e.g., piped input)
114+
reader := bufio.NewReader(os.Stdin)
115+
input, err := reader.ReadString('\n')
116+
errors.CheckError(err)
117+
password = strings.TrimSpace(input)
118+
return password
119+
}
111120
password = string(passwordRaw)
112121
fmt.Print("\n")
113122
}

util/cli/cli_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cli
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
const pwd = "test-password"
11+
12+
func TestPromptPassword_Fallback(t *testing.T) {
13+
oldStdin := os.Stdin
14+
defer func() {
15+
os.Stdin = oldStdin
16+
}()
17+
18+
r, w, err := os.Pipe()
19+
if err != nil {
20+
t.Fatalf("Failed to create pipe: %v", err)
21+
}
22+
_, err = w.WriteString(pwd + "\n")
23+
if err != nil {
24+
t.Fatalf("Failed to write to pipe: %v", err)
25+
}
26+
w.Close()
27+
28+
os.Stdin = r
29+
password := PromptPassword("")
30+
require.Equal(t, pwd, password)
31+
}

0 commit comments

Comments
 (0)