Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urlsplit password issues #17

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/dependbot.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
version: 2
updates:
- package-ecosystem: "pip"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
- package-ecosystem: "github-actions"
interval: "daily"
commit-message:
prefix: ⬆
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: monthly
interval: "daily"
commit-message:
prefix: ⬆
16 changes: 8 additions & 8 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
image: mysql:5.7
env:
MYSQL_USER: username
MYSQL_PASSWORD: password
MYSQL_PASSWORD: passwsss*1348394#
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: testsuite
ports:
Expand All @@ -34,7 +34,7 @@ jobs:
image: postgres:14
env:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
POSTGRES_PASSWORD: passwsss*1348394#
POSTGRES_DB: testsuite
ports:
- 5432:5432
Expand Down Expand Up @@ -80,12 +80,12 @@ jobs:
TEST_DATABASE_URLS: |
sqlite:///testsuite,
sqlite+aiosqlite:///testsuite,
mysql://username:password@localhost:3306/testsuite,
mysql+aiomysql://username:password@localhost:3306/testsuite,
mysql+asyncmy://username:password@localhost:3306/testsuite,
postgresql://username:password@localhost:5432/testsuite,
postgresql+aiopg://username:password@127.0.0.1:5432/testsuite,
postgresql+asyncpg://username:password@localhost:5432/testsuite,
mysql://username:passwsss*1348394#@localhost:3306/testsuite,
mysql+aiomysql://username:passwsss*1348394#@localhost:3306/testsuite,
mysql+asyncmy://username:passwsss*1348394#@localhost:3306/testsuite,
postgresql://username:passwsss*1348394#@localhost:5432/testsuite,
postgresql+aiopg://username:passwsss*1348394#@127.0.0.1:5432/testsuite,
postgresql+asyncpg://username:passwsss*1348394#@localhost:5432/testsuite,
mssql://sa:Mssql123mssql-@localhost:1433/master?driver=ODBC+Driver+17+for+SQL+Server,
mssql+pyodbc://sa:Mssql123mssql-@localhost:1433/master?driver=ODBC+Driver+17+for+SQL+Server,
mssql+aioodbc://sa:Mssql123mssql-@localhost:1433/master?driver=ODBC+Driver+17+for+SQL+Server
Expand Down
2 changes: 1 addition & 1 deletion databasez/backends/aiopg.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _get_connection_kwargs(self) -> dict:
if max_size is not None:
kwargs["maxsize"] = int(max_size)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
kwargs["ssl"] = {"true": True, "false": False}.get(ssl.lower(), ssl.lower())

for key, value in self._options.items():
# Coerce 'min_size' and 'max_size' for consistency.
Expand Down
2 changes: 1 addition & 1 deletion databasez/backends/asyncmy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _get_connection_kwargs(self) -> dict:
if pool_recycle is not None:
kwargs["pool_recycle"] = int(pool_recycle)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
kwargs["ssl"] = {"true": True, "false": False}.get(ssl.lower(), ssl.lower())

for key, value in self._options.items():
# Coerce 'min_size' and 'max_size' for consistency.
Expand Down
2 changes: 1 addition & 1 deletion databasez/backends/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _get_connection_kwargs(self) -> dict:
if pool_recycle is not None:
kwargs["pool_recycle"] = int(pool_recycle)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
kwargs["ssl"] = {"true": True, "false": False}.get(ssl.lower(), ssl.lower())

kwargs.update(
{
Expand Down
2 changes: 1 addition & 1 deletion databasez/backends/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _get_connection_kwargs(self) -> dict:
if pool_recycle is not None:
kwargs["pool_recycle"] = int(pool_recycle)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
kwargs["ssl"] = {"true": True, "false": False}.get(ssl.lower(), ssl.lower())

for key, value in self._options.items():
# Coerce 'min_size' and 'max_size' for consistency.
Expand Down
2 changes: 1 addition & 1 deletion databasez/backends/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _get_connection_kwargs(self) -> dict:
if max_size is not None:
kwargs["max_size"] = int(max_size)
if ssl is not None:
kwargs["ssl"] = {"true": True, "false": False}[ssl.lower()]
kwargs["ssl"] = {"true": True, "false": False}.get(ssl.lower(), ssl.lower())

kwargs.update(self._options)

Expand Down
20 changes: 18 additions & 2 deletions databasez/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import weakref
from contextvars import ContextVar
from types import TracebackType
from urllib.parse import SplitResult, parse_qsl, unquote, urlencode, urlsplit
from urllib.parse import SplitResult, parse_qsl, quote_plus, unquote, urlencode, urlsplit

from sqlalchemy import text
from sqlalchemy.engine import URL, make_url
from sqlalchemy.sql import ClauseElement

from databasez.importer import import_from_string
Expand Down Expand Up @@ -568,10 +569,25 @@ def __init__(self, url: typing.Union[str, "DatabaseURL"]):
f"Invalid type for DatabaseURL. Expected str or DatabaseURL, got {type(url)}"
)

@classmethod
def _sanitize_password(cls, url: URL) -> URL:
"""
Making sure all the passwords are allowed.
"""
password = url.password
if not password or password is None:
return url

quoted_password = quote_plus(password)
url = url._replace(password=quoted_password)
return url

@property
def components(self) -> SplitResult:
if not hasattr(self, "_components"):
self._components = urlsplit(self._url)
raw_url = make_url(self._url)
url = DatabaseURL._sanitize_password(raw_url)
self._components = urlsplit(url.render_as_string(hide_password=False))
return self._components

@property
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_USER: "postgres"
POSTGRES_PASSWORD: "postgres"
POSTGRES_PASSWORD: "passwsss*1348394#"
POSTGRES_DB: "testsuite"
expose:
- "5432"
Expand All @@ -19,7 +19,7 @@ services:
environment:
MYSQL_USER: "mysql"
MYSQL_PASSWORD: "mysql"
MYSQL_DATABASE: "testsuite"
MYSQL_DATABASE: "passwsss*1348394#"
MYSQL_ROOT_PASSWORD: "password"
expose:
- "3306"
Expand Down
8 changes: 6 additions & 2 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@

import pytest
import sqlalchemy
from sqlalchemy.engine import URL, make_url

from databasez import Database, DatabaseURL

assert "TEST_DATABASE_URLS" in os.environ, "TEST_DATABASE_URLS is not set."

DATABASE_URLS = [url.strip() for url in os.environ["TEST_DATABASE_URLS"].split(",")]


DATABASE_CONFIG_URLS = []
for value in DATABASE_URLS:
spliter = urlsplit(value)
raw_url = make_url(value)
url: URL = DatabaseURL._sanitize_password(raw_url)
spliter = urlsplit(url.render_as_string(hide_password=False))
DATABASE_CONFIG_URLS.append(
{
"connection": {
Expand All @@ -39,7 +43,7 @@

class AsyncMock(MagicMock):
async def __call__(self, *args, **kwargs):
return super(AsyncMock, self).__call__(*args, **kwargs)
return super().__call__(*args, **kwargs)


class MyEpochType(sqlalchemy.types.TypeDecorator):
Expand Down