Skip to content

Commit

Permalink
Allow connection to redis using custom username and password (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiProjects authored Aug 11, 2024
1 parent 5110dda commit bde25cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions pgsync/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@

# Redis:
REDIS_AUTH = env.str("REDIS_AUTH", default=None)
REDIS_USER = env.str("REDIS_USER", default=None)
REDIS_DB = env.int("REDIS_DB", default=0)
REDIS_HOST = env.str("REDIS_HOST", default="localhost")
# redis poll interval (in secs)
Expand Down
10 changes: 9 additions & 1 deletion pgsync/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PG_PORT,
PG_USER,
REDIS_AUTH,
REDIS_USER,
REDIS_DB,
REDIS_HOST,
REDIS_PORT,
Expand Down Expand Up @@ -117,6 +118,7 @@ def get_postgres_url(
def get_redis_url(
scheme: t.Optional[str] = None,
host: t.Optional[str] = None,
username: t.Optional[str] = None,
password: t.Optional[str] = None,
port: t.Optional[int] = None,
db: t.Optional[str] = None,
Expand All @@ -127,6 +129,7 @@ def get_redis_url(
Args:
scheme (Optional[str]): The scheme to use for the Redis connection. Defaults to None.
host (Optional[str]): The Redis host to connect to. Defaults to None.
username (Optional[str]): The Redis username to use for authentication. Defaults to None.
password (Optional[str]): The Redis password to use for authentication. Defaults to None.
port (Optional[int]): The Redis port to connect to. Defaults to None.
db (Optional[str]): The Redis database to connect to. Defaults to None.
Expand All @@ -135,11 +138,16 @@ def get_redis_url(
str: The Redis connection URL.
"""
host = host or REDIS_HOST
username = username or REDIS_USER
password = _get_auth("REDIS_AUTH") or password or REDIS_AUTH
port = port or REDIS_PORT
db = db or REDIS_DB
scheme = scheme or REDIS_SCHEME
if password:
if username and password:
logger.debug("Connecting to Redis with custom username and password")
return f"{scheme}://{quote_plus(username)}:{quote_plus(password)}@{host}:{port}/{db}"
elif password:
logger.debug("Connecting to Redis with default username and password")
return f"{scheme}://:{quote_plus(password)}@{host}:{port}/{db}"
logger.debug("Connecting to Redis without password.")
return f"{scheme}://{host}:{port}/{db}"

0 comments on commit bde25cb

Please sign in to comment.