pax_global_header00006660000000000000000000000064134477122360014523gustar00rootroot0000000000000052 comment=99d8e8e28945ffceaf75b0299fcb2bb656b8a683 pgpassfile-1.0.0/000077500000000000000000000000001344771223600136565ustar00rootroot00000000000000pgpassfile-1.0.0/.travis.yml000066400000000000000000000001131344771223600157620ustar00rootroot00000000000000language: go go: - 1.x - tip matrix: allow_failures: - go: tip pgpassfile-1.0.0/LICENSE000066400000000000000000000020611344771223600146620ustar00rootroot00000000000000Copyright (c) 2019 Jack Christensen MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. pgpassfile-1.0.0/README.md000066400000000000000000000005641344771223600151420ustar00rootroot00000000000000[![](https://godoc.org/github.com/jackc/pgpassfile?status.svg)](https://godoc.org/github.com/jackc/pgpassfile) [![Build Status](https://travis-ci.org/jackc/pgpassfile.svg)](https://travis-ci.org/jackc/pgpassfile) # pgpassfile Package pgpassfile is a parser PostgreSQL .pgpass files. Extracted and rewritten from original implementation in https://github.com/jackc/pgx. pgpassfile-1.0.0/go.mod000066400000000000000000000001301344771223600147560ustar00rootroot00000000000000module github.com/jackc/pgpassfile go 1.12 require github.com/stretchr/testify v1.3.0 pgpassfile-1.0.0/go.sum000066400000000000000000000011401344771223600150050ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= pgpassfile-1.0.0/pgpass.go000066400000000000000000000052251344771223600155060ustar00rootroot00000000000000// Package pgpassfile is a parser PostgreSQL .pgpass files. package pgpassfile import ( "bufio" "io" "os" "regexp" "strings" ) // Entry represents a line in a PG passfile. type Entry struct { Hostname string Port string Database string Username string Password string } // Passfile is the in memory data structure representing a PG passfile. type Passfile struct { Entries []*Entry } // ReadPassfile reads the file at path and parses it into a Passfile. func ReadPassfile(path string) (*Passfile, error) { f, err := os.Open(path) if err != nil { return nil, err } defer f.Close() return ParsePassfile(f) } // ParsePassfile reads r and parses it into a Passfile. func ParsePassfile(r io.Reader) (*Passfile, error) { passfile := &Passfile{} scanner := bufio.NewScanner(r) for scanner.Scan() { entry := parseLine(scanner.Text()) if entry != nil { passfile.Entries = append(passfile.Entries, entry) } } return passfile, scanner.Err() } // Match (not colons or escaped colon or escaped backslash)+. Essentially gives a split on unescaped // colon. var colonSplitterRegexp = regexp.MustCompile("(([^:]|(\\:)))+") // var colonSplitterRegexp = regexp.MustCompile("((?:[^:]|(?:\\:)|(?:\\\\))+)") // parseLine parses a line into an *Entry. It returns nil on comment lines or any other unparsable // line. func parseLine(line string) *Entry { const ( tmpBackslash = "\r" tmpColon = "\n" ) line = strings.TrimSpace(line) if strings.HasPrefix(line, "#") { return nil } line = strings.Replace(line, `\\`, tmpBackslash, -1) line = strings.Replace(line, `\:`, tmpColon, -1) parts := strings.Split(line, ":") if len(parts) != 5 { return nil } // Unescape escaped colons and backslashes for i := range parts { parts[i] = strings.Replace(parts[i], tmpBackslash, `\`, -1) parts[i] = strings.Replace(parts[i], tmpColon, `:`, -1) } return &Entry{ Hostname: parts[0], Port: parts[1], Database: parts[2], Username: parts[3], Password: parts[4], } } // FindPassword finds the password for the provided hostname, port, database, and username. For a // Unix domain socket hostname must be set to "localhost". An empty string will be returned if no // match is found. // // See https://www.postgresql.org/docs/current/libpq-pgpass.html for more password file information. func (pf *Passfile) FindPassword(hostname, port, database, username string) (password string) { for _, e := range pf.Entries { if (e.Hostname == "*" || e.Hostname == hostname) && (e.Port == "*" || e.Port == port) && (e.Database == "*" || e.Database == database) && (e.Username == "*" || e.Username == username) { return e.Password } } return "" } pgpassfile-1.0.0/pgpass_test.go000066400000000000000000000032461344771223600165460ustar00rootroot00000000000000package pgpassfile import ( "bytes" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func unescape(s string) string { s = strings.Replace(s, `\:`, `:`, -1) s = strings.Replace(s, `\\`, `\`, -1) return s } var passfile = [][]string{ {"test1", "5432", "larrydb", "larry", "whatstheidea"}, {"test1", "5432", "moedb", "moe", "imbecile"}, {"test1", "5432", "curlydb", "curly", "nyuknyuknyuk"}, {"test2", "5432", "*", "shemp", "heymoe"}, {"test2", "5432", "*", "*", `test\\ing\:`}, {"localhost", "*", "*", "*", "sesam"}, {"test3", "*", "", "", "swordfish"}, // user will be filled later } func TestParsePassFile(t *testing.T) { buf := bytes.NewBufferString(`# A comment test1:5432:larrydb:larry:whatstheidea test1:5432:moedb:moe:imbecile test1:5432:curlydb:curly:nyuknyuknyuk test2:5432:*:shemp:heymoe test2:5432:*:*:test\\ing\: localhost:*:*:*:sesam `) passfile, err := ParsePassfile(buf) require.Nil(t, err) assert.Len(t, passfile.Entries, 6) assert.Equal(t, "whatstheidea", passfile.FindPassword("test1", "5432", "larrydb", "larry")) assert.Equal(t, "imbecile", passfile.FindPassword("test1", "5432", "moedb", "moe")) assert.Equal(t, `test\ing:`, passfile.FindPassword("test2", "5432", "something", "else")) assert.Equal(t, "sesam", passfile.FindPassword("localhost", "9999", "foo", "bare")) assert.Equal(t, "", passfile.FindPassword("wrong", "5432", "larrydb", "larry")) assert.Equal(t, "", passfile.FindPassword("test1", "wrong", "larrydb", "larry")) assert.Equal(t, "", passfile.FindPassword("test1", "5432", "wrong", "larry")) assert.Equal(t, "", passfile.FindPassword("test1", "5432", "larrydb", "wrong")) }