Skip to content

Commit

Permalink
Merge pull request #491 from mialinx/master
Browse files Browse the repository at this point in the history
More precise .pgpass handling
  • Loading branch information
jackc authored Dec 15, 2018
2 parents c59c9ca + af319b2 commit 4618730
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
41 changes: 31 additions & 10 deletions pgpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

func parsepgpass(cfg *ConnConfig, line string) *string {
func parsepgpass(line, cfgHost, cfgPort, cfgDatabase, cfgUsername string) *string {
const (
backslash = "\r"
colon = "\n"
Expand All @@ -21,6 +21,9 @@ func parsepgpass(cfg *ConnConfig, line string) *string {
username
pw
)
if strings.HasPrefix(line, "#") {
return nil
}
line = strings.Replace(line, `\:`, colon, -1)
line = strings.Replace(line, `\\`, backslash, -1)
parts := strings.Split(line, `:`)
Expand All @@ -34,23 +37,19 @@ func parsepgpass(cfg *ConnConfig, line string) *string {
parts[i] = strings.Replace(strings.Replace(parts[i], backslash, `\`, -1), colon, `:`, -1)
switch i {
case host:
if parts[i] != cfg.Host {
if parts[i] != cfgHost {
return nil
}
case port:
portstr := fmt.Sprintf(`%v`, cfg.Port)
if portstr == "0" {
portstr = "5432"
}
if parts[i] != portstr {
if parts[i] != cfgPort {
return nil
}
case database:
if parts[i] != cfg.Database {
if parts[i] != cfgDatabase {
return nil
}
case username:
if parts[i] != cfg.User {
if parts[i] != cfgUsername {
return nil
}
}
Expand All @@ -72,10 +71,32 @@ func pgpass(cfg *ConnConfig) (found bool) {
return
}
defer f.Close()

host := cfg.Host
if _, err := os.Stat(host); err == nil {
host = "localhost"
}
port := fmt.Sprintf(`%v`, cfg.Port)
if port == "0" {
port = "5432"
}
username := cfg.User
if username == "" {
user, err := user.Current()
if err != nil {
return
}
username = user.Username
}
database := cfg.Database
if database == "" {
database = username
}

scanner := bufio.NewScanner(f)
var pw *string
for scanner.Scan() {
pw = parsepgpass(cfg, scanner.Text())
pw = parsepgpass(scanner.Text(), host, port, database, username)
if pw != nil {
cfg.Password = *pw
return true
Expand Down
35 changes: 34 additions & 1 deletion pgpass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
"strings"
"testing"
)
Expand All @@ -20,16 +21,29 @@ var passfile = [][]string{
{"test1", "5432", "curlydb", "curly", "nyuknyuknyuk"},
{"test2", "5432", "*", "shemp", "heymoe"},
{"test2", "5432", "*", "*", `test\\ing\:`},
{"localhost", "*", "*", "*", "sesam"},
{"test3", "*", "", "", "swordfish"}, // user will be filled later
}

func TestPGPass(t *testing.T) {
tf, err := ioutil.TempFile("", "")
if err != nil {
t.Fatal(err)
}
user, err := user.Current()
if err != nil {
t.Fatal(err)
}
passfile[len(passfile)-1][2] = user.Username
passfile[len(passfile)-1][3] = user.Username

defer tf.Close()
defer os.Remove(tf.Name())
os.Setenv("PGPASSFILE", tf.Name())
_, err = fmt.Fprintln(tf, "#some comment\n\n#more comment")
if err != nil {
t.Fatal(err)
}
for _, l := range passfile {
_, err := fmt.Fprintln(tf, strings.Join(l, `:`))
if err != nil {
Expand All @@ -48,9 +62,28 @@ func TestPGPass(t *testing.T) {
if cfg.Password != unescape(l[4]) {
t.Fatalf(`Password mismatch entry %v want %s got %s`, i, unescape(l[4]), cfg.Password)
}
if l[0] == "localhost" {
// using some existing path as socket
cfg := ConnConfig{Host: tf.Name(), Database: l[2], User: l[3]}
found := pgpass(&cfg)
if !found {
t.Fatalf("Entry %v not found", i)
}
if cfg.Password != unescape(l[4]) {
t.Fatalf(`Password mismatch entry %v want %s got %s`, i, unescape(l[4]), cfg.Password)
}
}
}
cfg := ConnConfig{Host: "derp", Database: "herp", User: "joe"}
cfg := ConnConfig{Host: "test3"}
found := pgpass(&cfg)
if !found {
t.Fatalf("Entry for default user name")
}
if cfg.Password != "swordfish" {
t.Fatalf(`Password mismatch for default user entry, want %s got %s`, "swordfish", cfg.Password)
}
cfg = ConnConfig{Host: "derp", Database: "herp", User: "joe"}
found = pgpass(&cfg)
if found {
t.Fatal("bad found")
}
Expand Down

0 comments on commit 4618730

Please sign in to comment.