Skip to content

Commit eab2c09

Browse files
fix(deploy): report invalid Postgres port cleanly
1 parent 01cc02e commit eab2c09

2 files changed

Lines changed: 36 additions & 18 deletions

File tree

‎scripts/check_deploy_credentials.py‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,36 @@ def audit_credentials(env: Mapping[str, str]) -> list[str]:
164164
# Resolved exactly the way Settings resolves them: an explicit URL wins, otherwise the
165165
# URL is assembled from the POSTGRES_* components with the credentials escaped. Auditing
166166
# the raw variables would either miss the component form or misread an escaped password.
167-
app_url = (
168-
resolve_postgres_url(
169-
env,
170-
url_var="DATABASE_URL",
171-
user_var="POSTGRES_USER",
172-
password_var="POSTGRES_PASSWORD",
173-
default_user=DEFAULT_POSTGRES_USER,
167+
try:
168+
app_url = (
169+
resolve_postgres_url(
170+
env,
171+
url_var="DATABASE_URL",
172+
user_var="POSTGRES_USER",
173+
password_var="POSTGRES_PASSWORD",
174+
default_user=DEFAULT_POSTGRES_USER,
175+
)
176+
or ""
174177
)
175-
or ""
176-
)
177-
readonly_url = (
178-
resolve_postgres_url(
179-
env,
180-
url_var="DATABASE_READONLY_URL",
181-
user_var="POSTGRES_READONLY_USER",
182-
password_var="POSTGRES_READONLY_PASSWORD",
183-
default_user=DEFAULT_READONLY_USER,
178+
except ValueError:
179+
app_url = ""
180+
problems.append("POSTGRES_PORT is not a valid integer.")
181+
182+
try:
183+
readonly_url = (
184+
resolve_postgres_url(
185+
env,
186+
url_var="DATABASE_READONLY_URL",
187+
user_var="POSTGRES_READONLY_USER",
188+
password_var="POSTGRES_READONLY_PASSWORD",
189+
default_user=DEFAULT_READONLY_USER,
190+
)
191+
or ""
184192
)
185-
or ""
186-
)
193+
except ValueError:
194+
readonly_url = ""
195+
if "POSTGRES_PORT is not a valid integer." not in problems:
196+
problems.append("POSTGRES_PORT is not a valid integer.")
187197
raw_redis = (env.get("REDIS_URL") or "").strip()
188198
redis_url, redis_problems = _resolve_redis_url(raw_redis, env.get("REDIS_PASSWORD"))
189199
problems.extend(redis_problems)

‎tests/unit/test_deploy_credentials.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ def test_malformed_redis_url_is_reported_instead_of_raising(url: str) -> None:
174174
assert problems == ["REDIS_URL is not a valid connection URL."]
175175

176176

177+
def test_invalid_postgres_port_is_reported_without_traceback() -> None:
178+
"""Malformed component ports become one stable operator-facing problem."""
179+
problems = audit_credentials(
180+
_env(DATABASE_URL=None, DATABASE_READONLY_URL=None, POSTGRES_PORT="abc")
181+
)
182+
assert problems.count("POSTGRES_PORT is not a valid integer.") == 1
183+
184+
177185
def test_problem_messages_never_echo_the_secret() -> None:
178186
"""The guard reports variable names, never the credential it rejected."""
179187
secret = "hunter2"

0 commit comments

Comments
 (0)