Skip to content

DSN validation fails if password contains '=' sign #292

@keierk01

Description

@keierk01

If password contains = sign, dsn validation fails. Postgres exporter should accept all special chars in password files, including = sign. I was using release v0.5.1 but this bug is already in v0.5.0.

Reason is that dsn is firstly splitted by spaces and then each items are splitted by = sign. DSN is malformed if split result len is different than 2.

func parseFingerprint(url string) (string, error) {
	dsn, err := pq.ParseURL(url)
	if err != nil {
		dsn = url
	}

	pairs := strings.Split(dsn, " ")
	kv := make(map[string]string, len(pairs))
	for _, pair := range pairs {
		splitted := strings.Split(pair, "=")
		if len(splitted) != 2 {
			return "", fmt.Errorf("malformed dsn %q", dsn)
		}
		kv[splitted[0]] = splitted[1]
	}
...

When I changed url in GOPATH/src/github.com/wrouesnel/postgres_exporter/cmd/postgres_exporter/postgres_exporter_test.go to "postgresql://userDsn:password=Dsn@localhost:55432/?sslmode=disabled" the tests started to fail.

¨ERRO[0000] Wrong constant labels format " xyz", should be "key=value"  source="postgres_exporter.go:950"

----------------------------------------------------------------------
FAIL: postgres_exporter_test.go:186: FunctionalSuite.TestParseFingerprint

postgres_exporter_test.go:213:
    c.Assert(err, IsNil)
... value *errors.errorString = &errors.errorString{s:"malformed dsn \"host=localhost password=password=Dsn port=55432 sslmode=disabled user=userDsn\""} ("malformed dsn \"host=localhost password=password=Dsn port=55432 sslmode=disabled user=userDsn\"")

OOPS: 9 passed, 1 FAILED
--- FAIL: Test (0.00s)
FAIL
exit status 1
FAIL	github.com/wrouesnel/postgres_exporter/cmd/postgres_exporter	0.014s

With original test file

go test
ERRO[0000] Wrong constant labels format " xyz", should be "key=value"  source="postgres_exporter.go:950"
OK: 10 passed
PASS
ok  	github.com/wrouesnel/postgres_exporter/cmd/postgres_exporter	0.014s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions