Skip to content

Commit

Permalink
fix handle pgpass (#1120)
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Apr 26, 2023
1 parent c10fcfe commit d8d93a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
17 changes: 10 additions & 7 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ func (cn *conn) handlePgpass(o values) {
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 @@ func getFields(s string) []string {
}

// 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
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

0 comments on commit d8d93a3

Please sign in to comment.