Skip to content

Commit 90878ef

Browse files
committed
Escape url parameters in sqlalchemy connection strings
1 parent ecb53be commit 90878ef

File tree

5 files changed

+261
-29
lines changed

5 files changed

+261
-29
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,14 @@ Attributes can also be passed in the connection string.
100100

101101
```python
102102
from sqlalchemy import create_engine
103+
from trino.sqlalchemy import URL
103104

104105
engine = create_engine(
105-
'trino://user@localhost:8080/system',
106+
URL(
107+
host="localhost",
108+
port=8080,
109+
catalog="system"
110+
),
106111
connect_args={
107112
"session_properties": {'query_max_run_time': '1d'},
108113
"client_tags": ["tag1", "tag2"],
@@ -117,6 +122,14 @@ engine = create_engine(
117122
'&client_tags=["tag1", "tag2"]'
118123
'&experimental_python_types=true',
119124
)
125+
126+
# or using the URL factory method
127+
engine = create_engine(URL(
128+
host="localhost",
129+
port=8080,
130+
client_tags=["tag1", "tag2"],
131+
experimental_python_types=True
132+
))
120133
```
121134

122135
## Authentication mechanisms

tests/unit/sqlalchemy/test_dialect.py

Lines changed: 135 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,43 @@
99
from trino.dbapi import Connection
1010
from trino.sqlalchemy.dialect import CertificateAuthentication, JWTAuthentication, TrinoDialect
1111
from trino.transaction import IsolationLevel
12+
from trino.sqlalchemy import URL as trino_url
1213

1314

1415
class TestTrinoDialect:
1516
def setup(self):
1617
self.dialect = TrinoDialect()
1718

1819
@pytest.mark.parametrize(
19-
"url, expected_args, expected_kwargs",
20+
"url, generated_url, expected_args, expected_kwargs",
2021
[
2122
(
22-
make_url("trino://user@localhost"),
23+
make_url(trino_url(
24+
user="user",
25+
host="localhost",
26+
)),
27+
'trino://user@localhost:8080?source=trino-sqlalchemy',
2328
list(),
24-
dict(host="localhost", catalog="system", user="user", source="trino-sqlalchemy"),
29+
dict(host="localhost", catalog="system", user="user", port=8080, source="trino-sqlalchemy"),
2530
),
2631
(
27-
make_url("trino://user@localhost:8080"),
32+
make_url(trino_url(
33+
user="user",
34+
host="localhost",
35+
port=443,
36+
)),
37+
'trino://user@localhost:443?source=trino-sqlalchemy',
2838
list(),
29-
dict(host="localhost", port=8080, catalog="system", user="user", source="trino-sqlalchemy"),
39+
dict(host="localhost", port=443, catalog="system", user="user", source="trino-sqlalchemy"),
3040
),
3141
(
32-
make_url("trino://user:pass@localhost:8080?source=trino-rulez"),
42+
make_url(trino_url(
43+
user="user",
44+
password="pass",
45+
host="localhost",
46+
source="trino-rulez",
47+
)),
48+
'trino://user:***@localhost:8080?source=trino-rulez',
3349
list(),
3450
dict(
3551
host="localhost",
@@ -42,13 +58,64 @@ def setup(self):
4258
),
4359
),
4460
(
45-
make_url(
46-
'trino://user@localhost:8080?'
47-
'session_properties={"query_max_run_time": "1d"}'
48-
'&http_headers={"trino": 1}'
49-
'&extra_credential=[("a", "b"), ("c", "d")]'
50-
'&client_tags=[1, "sql"]'
51-
'&experimental_python_types=true'),
61+
make_url(trino_url(
62+
user="user",
63+
host="localhost",
64+
cert="/my/path/to/cert",
65+
key="afdlsdfk%4#'",
66+
)),
67+
'trino://user@localhost:8080'
68+
'?cert=%2Fmy%2Fpath%2Fto%2Fcert'
69+
'&key=afdlsdfk%254%23%27'
70+
'&source=trino-sqlalchemy',
71+
list(),
72+
dict(
73+
host="localhost",
74+
port=8080,
75+
catalog="system",
76+
user="user",
77+
auth=CertificateAuthentication("/my/path/to/cert", "afdlsdfk%4#'"),
78+
http_scheme="https",
79+
source="trino-sqlalchemy"
80+
),
81+
),
82+
(
83+
make_url(trino_url(
84+
user="user",
85+
host="localhost",
86+
access_token="afdlsdfk%4#'",
87+
)),
88+
'trino://user@localhost:8080'
89+
'?access_token=afdlsdfk%254%23%27'
90+
'&source=trino-sqlalchemy',
91+
list(),
92+
dict(
93+
host="localhost",
94+
port=8080,
95+
catalog="system",
96+
user="user",
97+
auth=JWTAuthentication("afdlsdfk%4#'"),
98+
http_scheme="https",
99+
source="trino-sqlalchemy"
100+
),
101+
),
102+
(
103+
make_url(trino_url(
104+
user="user",
105+
host="localhost",
106+
session_properties={"query_max_run_time": "1d"},
107+
http_headers={"trino": 1},
108+
extra_credential=[("a", "b"), ("c", "d")],
109+
client_tags=["1", "sql"],
110+
experimental_python_types=True,
111+
)),
112+
'trino://user@localhost:8080'
113+
'?client_tags=%5B%221%22%2C+%22sql%22%5D'
114+
'&experimental_python_types=true'
115+
'&extra_credential=%5B%28%27a%27%2C+%27b%27%29%2C+%28%27c%27%2C+%27d%27%29%5D'
116+
'&http_headers=%7B%22trino%22%3A+1%7D'
117+
'&session_properties=%7B%22query_max_run_time%22%3A+%221d%22%7D'
118+
'&source=trino-sqlalchemy',
52119
list(),
53120
dict(
54121
host="localhost",
@@ -59,13 +126,66 @@ def setup(self):
59126
session_properties={"query_max_run_time": "1d"},
60127
http_headers={"trino": 1},
61128
extra_credential=[("a", "b"), ("c", "d")],
62-
client_tags=[1, "sql"],
129+
client_tags=["1", "sql"],
63130
experimental_python_types=True,
64131
),
65132
),
133+
# url encoding
134+
(
135+
make_url(trino_url(
136+
user="[email protected]/my_role",
137+
password="pass /*&",
138+
host="localhost",
139+
session_properties={"query_max_run_time": "1d"},
140+
http_headers={"trino": 1},
141+
extra_credential=[
142+
("[email protected]/my_role", "[email protected]/my_role"),
143+
("[email protected]/my_role", "[email protected]/my_role")],
144+
experimental_python_types=True,
145+
client_tags=["1 @& /\"", "sql"],
146+
verify=False,
147+
)),
148+
'trino://user%40test.org%2Fmy_role:***@localhost:8080'
149+
'?client_tags=%5B%221+%40%26+%2F%5C%22%22%2C+%22sql%22%5D'
150+
'&experimental_python_types=true'
151+
'&extra_credential=%5B%28%27user1%40test.org%2Fmy_role%27%2C'
152+
'+%27user2%40test.org%2Fmy_role%27%29%2C'
153+
'+%28%27user3%40test.org%2Fmy_role%27%2C'
154+
'+%27user36%40test.org%2Fmy_role%27%29%5D'
155+
'&http_headers=%7B%22trino%22%3A+1%7D'
156+
'&session_properties=%7B%22query_max_run_time%22%3A+%221d%22%7D'
157+
'&source=trino-sqlalchemy'
158+
'&verify=false',
159+
list(),
160+
dict(
161+
host="localhost",
162+
port=8080,
163+
catalog="system",
164+
user="[email protected]/my_role",
165+
auth=BasicAuthentication("[email protected]/my_role", "pass /*&"),
166+
http_scheme="https",
167+
source="trino-sqlalchemy",
168+
session_properties={"query_max_run_time": "1d"},
169+
http_headers={"trino": 1},
170+
extra_credential=[
171+
("[email protected]/my_role", "[email protected]/my_role"),
172+
("[email protected]/my_role", "[email protected]/my_role")],
173+
experimental_python_types=True,
174+
client_tags=["1 @& /\"", "sql"],
175+
verify=False,
176+
),
177+
),
66178
],
67179
)
68-
def test_create_connect_args(self, url: URL, expected_args: List[Any], expected_kwargs: Dict[str, Any]):
180+
def test_create_connect_args(
181+
self,
182+
url: URL,
183+
generated_url: str,
184+
expected_args: List[Any],
185+
expected_kwargs: Dict[str, Any]
186+
):
187+
assert repr(url) == generated_url
188+
69189
actual_args, actual_kwargs = self.dialect.create_connect_args(url)
70190

71191
assert actual_args == expected_args

trino/sqlalchemy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
from sqlalchemy.dialects import registry
13+
from .util import _url as URL # noqa
1314

1415
registry.register("trino", "trino.sqlalchemy.dialect", "TrinoDialect")

trino/sqlalchemy/dialect.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ast import literal_eval
1414
from textwrap import dedent
1515
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple
16+
from urllib.parse import unquote_plus
1617

1718
from sqlalchemy import exc, sql
1819
from sqlalchemy.engine.base import Connection
@@ -80,49 +81,52 @@ def create_connect_args(self, url: URL) -> Tuple[Sequence[Any], Mapping[str, Any
8081

8182
db_parts = (url.database or "system").split("/")
8283
if len(db_parts) == 1:
83-
kwargs["catalog"] = db_parts[0]
84+
kwargs["catalog"] = unquote_plus(db_parts[0])
8485
elif len(db_parts) == 2:
85-
kwargs["catalog"] = db_parts[0]
86-
kwargs["schema"] = db_parts[1]
86+
kwargs["catalog"] = unquote_plus(db_parts[0])
87+
kwargs["schema"] = unquote_plus(db_parts[1])
8788
else:
8889
raise ValueError(f"Unexpected database format {url.database}")
8990

9091
if url.username:
91-
kwargs["user"] = url.username
92+
kwargs["user"] = unquote_plus(url.username)
9293

9394
if url.password:
9495
if not url.username:
9596
raise ValueError("Username is required when specify password in connection URL")
9697
kwargs["http_scheme"] = "https"
97-
kwargs["auth"] = BasicAuthentication(url.username, url.password)
98+
kwargs["auth"] = BasicAuthentication(unquote_plus(url.username), unquote_plus(url.password))
9899

99100
if "access_token" in url.query:
100101
kwargs["http_scheme"] = "https"
101-
kwargs["auth"] = JWTAuthentication(url.query["access_token"])
102+
kwargs["auth"] = JWTAuthentication(unquote_plus(url.query["access_token"]))
102103

103104
if "cert" and "key" in url.query:
104105
kwargs["http_scheme"] = "https"
105-
kwargs["auth"] = CertificateAuthentication(url.query['cert'], url.query['key'])
106+
kwargs["auth"] = CertificateAuthentication(unquote_plus(url.query['cert']), unquote_plus(url.query['key']))
106107

107108
if "source" in url.query:
108-
kwargs["source"] = url.query["source"]
109+
kwargs["source"] = unquote_plus(url.query["source"])
109110
else:
110111
kwargs["source"] = "trino-sqlalchemy"
111112

112113
if "session_properties" in url.query:
113-
kwargs["session_properties"] = json.loads(url.query["session_properties"])
114+
kwargs["session_properties"] = json.loads(unquote_plus(url.query["session_properties"]))
114115

115116
if "http_headers" in url.query:
116-
kwargs["http_headers"] = json.loads(url.query["http_headers"])
117+
kwargs["http_headers"] = json.loads(unquote_plus(url.query["http_headers"]))
117118

118119
if "extra_credential" in url.query:
119-
kwargs["extra_credential"] = literal_eval(url.query["extra_credential"])
120+
kwargs["extra_credential"] = literal_eval(unquote_plus(url.query["extra_credential"]))
120121

121122
if "client_tags" in url.query:
122-
kwargs["client_tags"] = json.loads(url.query["client_tags"])
123+
kwargs["client_tags"] = json.loads(unquote_plus(url.query["client_tags"]))
123124

124125
if "experimental_python_types" in url.query:
125-
kwargs["experimental_python_types"] = json.loads(url.query["experimental_python_types"])
126+
kwargs["experimental_python_types"] = json.loads(unquote_plus(url.query["experimental_python_types"]))
127+
128+
if "verify" in url.query:
129+
kwargs["verify"] = json.loads(unquote_plus(url.query["verify"]))
126130

127131
return args, kwargs
128132

trino/sqlalchemy/util.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import json
2+
from urllib.parse import quote_plus
3+
4+
from typing import Optional, Dict, List, Union, Tuple
5+
from sqlalchemy import exc
6+
from sqlalchemy.engine.url import _rfc_1738_quote # noqa
7+
8+
9+
def _url(
10+
host: str,
11+
port: Optional[int] = 8080,
12+
user: Optional[str] = None,
13+
password: Optional[str] = None,
14+
catalog: Optional[str] = None,
15+
schema: Optional[str] = None,
16+
source: Optional[str] = "trino-sqlalchemy",
17+
session_properties: Dict[str, str] = None,
18+
http_headers: Dict[str, Union[str, int]] = None,
19+
extra_credential: Optional[List[Tuple[str, str]]] = None,
20+
client_tags: Optional[List[str]] = None,
21+
experimental_python_types: Optional[bool] = None,
22+
access_token: Optional[str] = None,
23+
cert: Optional[str] = None,
24+
key: Optional[str] = None,
25+
verify: Optional[bool] = None,
26+
) -> str:
27+
"""
28+
Composes a SQLAlchemy connection string from the given database connection
29+
parameters.
30+
Parameters containing special characters (e.g., '@', '%') need to be encoded to be parsed correctly.
31+
"""
32+
33+
trino_url = "trino://"
34+
35+
if user is not None:
36+
trino_url += _rfc_1738_quote(user)
37+
38+
if password is not None:
39+
if user is None:
40+
raise exc.ArgumentError("user must be specified when specifying a password.")
41+
trino_url += f":{_rfc_1738_quote(password)}"
42+
43+
if user is not None:
44+
trino_url += "@"
45+
46+
if not host:
47+
raise exc.ArgumentError("host must be specified.")
48+
49+
trino_url += host
50+
51+
if not port:
52+
raise exc.ArgumentError("port must be specified.")
53+
54+
trino_url += f":{port}"
55+
56+
if catalog is not None:
57+
trino_url += f"/{quote_plus(catalog)}"
58+
59+
if schema is not None:
60+
if catalog is None:
61+
raise exc.ArgumentError("catalog must be specified when specifying a default schema.")
62+
trino_url += f"/{quote_plus(schema)}"
63+
64+
assert source
65+
trino_url += f"?source={quote_plus(source)}"
66+
67+
if session_properties is not None:
68+
trino_url += f"&session_properties={quote_plus(json.dumps(session_properties))}"
69+
70+
if http_headers is not None:
71+
trino_url += f"&http_headers={quote_plus(json.dumps(http_headers))}"
72+
73+
if extra_credential is not None:
74+
trino_url += f"&extra_credential={quote_plus(repr(extra_credential))}"
75+
76+
if client_tags is not None:
77+
trino_url += f"&client_tags={quote_plus(json.dumps(client_tags))}"
78+
79+
if experimental_python_types is not None:
80+
trino_url += f"&experimental_python_types={json.dumps(experimental_python_types)}"
81+
82+
if access_token is not None:
83+
trino_url += f"&access_token={quote_plus(access_token)}"
84+
85+
if cert is not None:
86+
trino_url += f"&cert={quote_plus(cert)}"
87+
88+
if key is not None:
89+
trino_url += f"&key={quote_plus(key)}"
90+
91+
if verify is not None:
92+
trino_url += f"&verify={json.dumps(verify)}"
93+
94+
return trino_url

0 commit comments

Comments
 (0)