Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: don't show password in \c metacommand #87298

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/cli/clisqlshell/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ func (c *cliState) handleConnectInternal(cmd []string) error {
if dbName == "" {
dbName = currURL.GetDatabase()
}
fmt.Fprintf(c.iCtx.stdout, "Connection string: %s\n", currURL.ToPQ())
fmt.Fprintf(c.iCtx.stdout, "Connection string: %s\n", currURL.ToPQRedacted())
fmt.Fprintf(c.iCtx.stdout, "You are connected to database %q as user %q.\n", dbName, currURL.GetUsername())
return nil

Expand Down
16 changes: 16 additions & 0 deletions pkg/cli/interactive_tests/test_connect_cmd.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ send "create user foo with password 'abc';\r"
eexpect "CREATE ROLE"
eexpect root@
eexpect "/t>"
end_test

start_test "Check that the client-side connect cmd prints the current conn details"
send "\\c\r"
eexpect "Connection string:"
eexpect "You are connected to database \"t\" as user \"root\""
eexpect root@
eexpect "/t>"
end_test

start_test "Check that the client-side connect cmd can change databases"
send "\\c postgres\r"
Expand Down Expand Up @@ -164,6 +166,20 @@ end_test
send "\\q\r"
eexpect eof

start_test "Check that the client-side connect cmd prints the current conn details with password redacted"

spawn $argv sql --certs-dir=$certs_dir --url=postgres://foo:abc@localhost:26257/defaultdb
eexpect foo@
send "\\c\r"
eexpect "Connection string: postgresql://foo:~~~~~~@"
eexpect "You are connected to database \"defaultdb\" as user \"foo\""
eexpect foo@
eexpect "/defaultdb>"
end_test

send "\\q\r"
eexpect eof

stop_server $argv

# Some more tests with the insecure mode.
Expand Down
20 changes: 20 additions & 0 deletions pkg/server/pgurl/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ func (u *URL) ToPQ() *url.URL {
return nu
}

// ToPQRedacted converts the URL to a connection string supported
// by drivers using libpq or compatible, with the password redacted.
func (u *URL) ToPQRedacted() *url.URL {
nu, opts := u.baseURL()

if u.username != "" {
nu.User = url.User(u.username)
}
switch u.authn {
case authnPassword, authnPasswordWithClientCert:
if u.hasPassword {
// Use '~' since it does not need to be escaped.
nu.User = url.UserPassword(u.username, "~~~~~~")
}
}

nu.RawQuery = opts.Encode()
return nu
}

// String makes URL printable.
func (u *URL) String() string { return u.ToPQ().String() }

Expand Down