Skip to content

Commit

Permalink
Avoid raising errors on database passwords that contain a $ charact…
Browse files Browse the repository at this point in the history
…er (#14876)
  • Loading branch information
desertaxle authored Aug 12, 2024
1 parent ce00c80 commit 8f04edb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
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

0 comments on commit 8f04edb

Please sign in to comment.