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

Avoid raising errors on database passwords that contain a $ character #14876

Merged
merged 2 commits into from
Aug 12, 2024
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
6 changes: 5 additions & 1 deletion src/prefect/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,11 @@ def templater(settings, value):
setting.name: setting.value_from(settings) for setting in upstream_settings
}
template = string.Template(str(value))
return original_type(template.substitute(template_values))
# Note the use of `safe_substitute` to avoid raising an exception if a
# template value is missing. In this case, template values will be left
# as-is in the string. Using `safe_substitute` prevents us raising when
# the DB password contains a `$` character.
return original_type(template.safe_substitute(template_values))

return templater

Expand Down
25 changes: 25 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,31 @@ def test_unknown_driver_raises(self):
):
pass

def test_connection_string_with_dollar_sign(self):
"""
Regression test for https://github.com/PrefectHQ/prefect/issues/11067.

This test ensures that passwords with dollar signs do not cause issues when
templating the connection string.
"""
with temporary_settings(
{
PREFECT_API_DATABASE_CONNECTION_URL: (
"postgresql+asyncpg://"
"the-user:the-$password@"
"the-database-server.example.com:5432"
"/the-database"
),
PREFECT_API_DATABASE_USER: "the-user",
}
):
assert PREFECT_API_DATABASE_CONNECTION_URL.value() == (
"postgresql+asyncpg://"
"the-user:the-$password@"
"the-database-server.example.com:5432"
"/the-database"
)


class TestTemporarySettings:
def test_temporary_settings(self):
Expand Down