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

Fix handlePgpass #1120

Merged
merged 1 commit into from
Apr 26, 2023
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
fix handle pgpass
  • Loading branch information
acoshift committed Apr 16, 2023
commit ce86388f86389973a9da0364b645eb0bc6a04b6e
17 changes: 10 additions & 7 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@
scanner := bufio.NewScanner(io.Reader(file))
// From: https://github.com/tg/pgpass/blob/master/reader.go
for scanner.Scan() {
scanText(scanner.Text(), o)
if scanText(scanner.Text(), o) {
break
}
}
}

Expand Down Expand Up @@ -291,23 +293,24 @@
}

// ScanText assists HandlePgpass in it's objective.
func scanText(line string, o values) {
func scanText(line string, o values) bool {
hostname := o["host"]
ntw, _ := network(o)
port := o["port"]
db := o["dbname"]
username := o["user"]
if len(line) != 0 || line[0] != '#' {
return
if len(line) == 0 || line[0] == '#' {
return false
}
split := getFields(line)
if len(split) == 5 {
return
if len(split) != 5 {
return false
}
if (split[0] == "*" || split[0] == hostname || (split[0] == "localhost" && (hostname == "" || ntw == "unix"))) && (split[1] == "*" || split[1] == port) && (split[2] == "*" || split[2] == db) && (split[3] == "*" || split[3] == username) {
o["password"] = split[4]
return
return true
}
return false
}

func (cn *conn) writeBuf(b byte) *writeBuf {
Expand Down Expand Up @@ -2086,7 +2089,7 @@
// All Conn implementations should implement the following interfaces: Pinger, SessionResetter, and Validator.
var _ driver.Pinger = &conn{}
var _ driver.SessionResetter = &conn{}
var _ driver.Validator = &conn{}

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (13, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (13, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (12, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (12, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (11, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (11, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (10, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (10, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.14)

undefined: driver.Validator

Check failure on line 2092 in conn.go

View workflow job for this annotation

GitHub Actions / test (9.6, 1.14)

undefined: driver.Validator

func (cn *conn) ResetSession(ctx context.Context) error {
// Ensure bad connections are reported: From database/sql/driver:
Expand Down
4 changes: 0 additions & 4 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ func TestOpenURL(t *testing.T) {
const pgpassFile = "/tmp/pqgotest_pgpass"

func TestPgpass(t *testing.T) {
if os.Getenv("TRAVIS") != "true" {
t.Skip("not running under Travis, skipping pgpass tests")
}

testAssert := func(conninfo string, expected string, reason string) {
conn, err := openTestConnConninfo(conninfo)
if err != nil {
Expand Down
Loading