Skip to content

Commit

Permalink
[postgres] urlquote usernames as well (#7430)
Browse files Browse the repository at this point in the history
sometimes users will have `name@place` as their db username which can cause issues if not properly urlquoted

https://www.postgresql.org/docs/current/libpq-connect.html#AEN39024

## Test Plan

added test case
  • Loading branch information
alangenfeld committed Apr 13, 2022
1 parent 8e75a98 commit cb642cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def pg_url_from_config(config_value):


def get_conn_string(username, password, hostname, db_name, port="5432", params=None):
uri = f"postgresql://{username}:{urlquote(password)}@{hostname}:{port}/{db_name}"
uri = f"postgresql://{urlquote(username)}:{urlquote(password)}@{hostname}:{port}/{db_name}"

if params:
query_string = f"{urlencode(params, quote_via=quote)}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import tempfile
from urllib.parse import unquote, urlparse

import pytest
import sqlalchemy as db
import yaml
from dagster_postgres.utils import get_conn
from dagster_postgres.utils import get_conn, get_conn_string

from dagster.core.instance import DagsterInstance, InstanceRef
from dagster.core.test_utils import instance_for_test
Expand Down Expand Up @@ -222,3 +223,25 @@ def test_specify_pg_params(hostname):
assert instance._run_storage.postgres_url == postgres_url
assert instance._schedule_storage.postgres_url == postgres_url
# pylint: enable=protected-access


def test_conn_str():
username = "has@init"
password = "full:of:junk!@?"
db_name = "dagster"
hostname = "database-city.com"

conn_str = get_conn_string(
username=username,
password=password,
db_name=db_name,
hostname=hostname,
)
assert (
conn_str
== r"postgresql://has%40init:full%3Aof%3Ajunk%21%40%3F@database-city.com:5432/dagster"
)
parsed = urlparse(conn_str)
assert unquote(parsed.username) == username
assert unquote(parsed.password) == password
assert parsed.hostname == hostname

0 comments on commit cb642cb

Please sign in to comment.