|
14 | 14 | from redis.client import Redis
|
15 | 15 | from redis.lock import Lock as RedisLock
|
16 | 16 |
|
| 17 | +from onyx.configs.app_configs import AWS_REGION_NAME |
17 | 18 | from onyx.configs.app_configs import REDIS_AUTH_KEY_PREFIX
|
18 | 19 | from onyx.configs.app_configs import REDIS_DB_NUMBER
|
19 | 20 | from onyx.configs.app_configs import REDIS_HEALTH_CHECK_INTERVAL
|
|
25 | 26 | from onyx.configs.app_configs import REDIS_SSL
|
26 | 27 | from onyx.configs.app_configs import REDIS_SSL_CA_CERTS
|
27 | 28 | from onyx.configs.app_configs import REDIS_SSL_CERT_REQS
|
| 29 | +from onyx.configs.app_configs import USE_REDIS_IAM_AUTH |
28 | 30 | from onyx.configs.constants import FASTAPI_USERS_AUTH_COOKIE_NAME
|
29 | 31 | from onyx.configs.constants import REDIS_SOCKET_KEEPALIVE_OPTIONS
|
| 32 | +from onyx.redis.iam_auth import configure_redis_iam_auth |
| 33 | +from onyx.redis.iam_auth import create_redis_ssl_context_if_iam |
30 | 34 | from onyx.utils.logger import setup_logger
|
31 | 35 | from shared_configs.configs import DEFAULT_REDIS_PREFIX
|
32 | 36 | from shared_configs.contextvars import get_current_tenant_id
|
@@ -186,12 +190,43 @@ def create_pool(
|
186 | 190 | ssl_cert_reqs: str = REDIS_SSL_CERT_REQS,
|
187 | 191 | ssl: bool = False,
|
188 | 192 | ) -> redis.BlockingConnectionPool:
|
| 193 | + """ |
| 194 | + Create a Redis connection pool with appropriate SSL configuration. |
| 195 | +
|
| 196 | + SSL Configuration Priority: |
| 197 | + 1. IAM Authentication (USE_REDIS_IAM_AUTH=true): Uses system CA certificates |
| 198 | + 2. Regular SSL (REDIS_SSL=true): Uses custom SSL configuration |
| 199 | + 3. No SSL: Standard connection without encryption |
| 200 | +
|
| 201 | + Note: IAM authentication automatically enables SSL and takes precedence |
| 202 | + over regular SSL configuration to ensure proper security. |
| 203 | + """ |
189 | 204 | """We use BlockingConnectionPool because it will block and wait for a connection
|
190 | 205 | rather than error if max_connections is reached. This is far more deterministic
|
191 | 206 | behavior and aligned with how we want to use Redis."""
|
192 | 207 |
|
193 | 208 | # Using ConnectionPool is not well documented.
|
194 | 209 | # Useful examples: https://github.com/redis/redis-py/issues/780
|
| 210 | + |
| 211 | + # Handle IAM authentication |
| 212 | + if USE_REDIS_IAM_AUTH: |
| 213 | + # For IAM authentication, we don't use password |
| 214 | + # and ensure SSL is enabled with proper context |
| 215 | + ssl_context = create_redis_ssl_context_if_iam() |
| 216 | + return redis.BlockingConnectionPool( |
| 217 | + host=host, |
| 218 | + port=port, |
| 219 | + db=db, |
| 220 | + password=None, # No password with IAM auth |
| 221 | + max_connections=max_connections, |
| 222 | + timeout=None, |
| 223 | + health_check_interval=REDIS_HEALTH_CHECK_INTERVAL, |
| 224 | + socket_keepalive=True, |
| 225 | + socket_keepalive_options=REDIS_SOCKET_KEEPALIVE_OPTIONS, |
| 226 | + connection_class=redis.SSLConnection, |
| 227 | + ssl_context=ssl_context, # Use IAM auth SSL context |
| 228 | + ) |
| 229 | + |
195 | 230 | if ssl:
|
196 | 231 | return redis.BlockingConnectionPool(
|
197 | 232 | host=host,
|
@@ -363,7 +398,17 @@ async def get_async_redis_connection() -> aioredis.Redis:
|
363 | 398 | "socket_keepalive_options": REDIS_SOCKET_KEEPALIVE_OPTIONS,
|
364 | 399 | }
|
365 | 400 |
|
366 |
| - if REDIS_SSL: |
| 401 | + # Handle SSL configuration with clear priority: |
| 402 | + # 1. IAM Authentication (takes precedence, handles SSL automatically) |
| 403 | + # 2. Regular SSL (only when IAM auth is disabled) |
| 404 | + if USE_REDIS_IAM_AUTH: |
| 405 | + # IAM authentication handles SSL configuration automatically |
| 406 | + # This ensures proper security with system CA certificates |
| 407 | + configure_redis_iam_auth( |
| 408 | + connection_kwargs, REDIS_HOST, REDIS_PORT, AWS_REGION_NAME |
| 409 | + ) |
| 410 | + elif REDIS_SSL: |
| 411 | + # Regular SSL configuration (only when not using IAM auth) |
367 | 412 | ssl_context = ssl.create_default_context()
|
368 | 413 |
|
369 | 414 | if REDIS_SSL_CA_CERTS:
|
|
0 commit comments