Skip to content

Commit a07724e

Browse files
committed
Enhance database and RabbitMQ URL handling by URL-encoding credentials to support special characters. Updated related docstrings for clarity.
1 parent f562e19 commit a07724e

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

DocsManager/app/core/config.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pydantic_settings import BaseSettings, SettingsConfigDict
22
from pydantic import field_validator, model_validator
33
from typing import Optional
4+
from urllib.parse import quote_plus
45
import logging
56

67
logger = logging.getLogger(__name__)
@@ -70,27 +71,34 @@ def validate_and_build_urls(self):
7071

7172
# Build database_url if not provided
7273
if not self.database_url:
74+
# URL-encode username and password to handle special characters
75+
encoded_user = quote_plus(self.postgres_user)
76+
encoded_password = quote_plus(self.postgres_password)
7377
self.database_url = (
74-
f"postgresql://{self.postgres_user}:{self.postgres_password}"
78+
f"postgresql://{encoded_user}:{encoded_password}"
7579
f"@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"
7680
)
7781

7882
return self
7983

8084
@property
8185
def postgres_dsn(self) -> str:
82-
"""Returns the PostgreSQL DSN"""
86+
"""Returns the PostgreSQL DSN with URL-encoded credentials"""
87+
encoded_user = quote_plus(self.postgres_user)
88+
encoded_password = quote_plus(self.postgres_password)
8389
return (
84-
f"postgresql://{self.postgres_user}:{self.postgres_password}"
90+
f"postgresql://{encoded_user}:{encoded_password}"
8591
f"@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"
8692
)
8793

8894
@property
8995
def rabbitmq_url(self) -> str:
90-
"""Returns the RabbitMQ connection URL"""
91-
host = self.rabbitmq_host.strip() if self.rabbitmq_host else "localhost"
92-
url = f"amqp://{self.rabbitmq_user}:{self.rabbitmq_password}@{host}:{self.rabbitmq_port}/"
93-
logger.debug(f"RabbitMQ URL: amqp://{self.rabbitmq_user}:***@{host}:{self.rabbitmq_port}/")
96+
"""Returns the RabbitMQ connection URL with URL-encoded credentials"""
97+
# Use already-normalized host from normalize_rabbitmq_host validator
98+
encoded_user = quote_plus(self.rabbitmq_user)
99+
encoded_password = quote_plus(self.rabbitmq_password)
100+
url = f"amqp://{encoded_user}:{encoded_password}@{self.rabbitmq_host}:{self.rabbitmq_port}/"
101+
logger.debug(f"RabbitMQ URL: amqp://{encoded_user}:***@{self.rabbitmq_host}:{self.rabbitmq_port}/")
94102
return url
95103

96104

0 commit comments

Comments
 (0)