diff --git a/cmd/postgres_exporter/postgres_exporter.go b/cmd/postgres_exporter/postgres_exporter.go index df970847..d59dff27 100644 --- a/cmd/postgres_exporter/postgres_exporter.go +++ b/cmd/postgres_exporter/postgres_exporter.go @@ -1086,8 +1086,9 @@ func getDataSource() string { pass = os.Getenv("DATA_SOURCE_PASS") } + ui := url.UserPassword(user, pass).String() uri := os.Getenv("DATA_SOURCE_URI") - dsn = "postgresql://" + user + ":" + pass + "@" + uri + dsn = "postgresql://" + ui + "@" + uri } return dsn diff --git a/cmd/postgres_exporter/postgres_exporter_test.go b/cmd/postgres_exporter/postgres_exporter_test.go index 6d6aecbf..c1907a1a 100644 --- a/cmd/postgres_exporter/postgres_exporter_test.go +++ b/cmd/postgres_exporter/postgres_exporter_test.go @@ -105,7 +105,7 @@ func (s *FunctionalSuite) TestEnvironmentSettingWithSecretsFiles(c *C) { c.Assert(err, IsNil) defer UnsetEnvironment(c, "DATA_SOURCE_URI") - var expected = "postgresql://custom_username:custom_password@localhost:5432/?sslmode=disable" + var expected = "postgresql://custom_username$&+,%2F%3A;=%3F%40:custom_password$&+,%2F%3A;=%3F%40@localhost:5432/?sslmode=disable" dsn := getDataSource() if dsn != expected { diff --git a/cmd/postgres_exporter/tests/username_file b/cmd/postgres_exporter/tests/username_file index 895ad19f..0650cfd3 100644 --- a/cmd/postgres_exporter/tests/username_file +++ b/cmd/postgres_exporter/tests/username_file @@ -1 +1 @@ -custom_username +custom_username$&+,/:;=?@ diff --git a/cmd/postgres_exporter/tests/userpass_file b/cmd/postgres_exporter/tests/userpass_file index 4251eaea..a9caa8de 100644 --- a/cmd/postgres_exporter/tests/userpass_file +++ b/cmd/postgres_exporter/tests/userpass_file @@ -1 +1 @@ -custom_password +custom_password$&+,/:;=?@