|
1 | 1 | import logging |
2 | 2 | import os |
3 | 3 | from urllib.parse import quote_plus |
| 4 | +import re |
4 | 5 |
|
5 | 6 | # Set up logging |
6 | 7 | logging.basicConfig(level=logging.INFO) |
@@ -42,14 +43,29 @@ def make_celery(): |
42 | 43 | redis_host = os.getenv('REDIS_HOST', 'localhost') |
43 | 44 | redis_port = os.getenv('REDIS_PORT', '6379') |
44 | 45 | redis_password = os.getenv('REDIS_PASSWORD') |
45 | | - |
| 46 | + |
46 | 47 | if redis_password: |
47 | | - redis_password = quote_plus(redis_password) |
48 | | - redis_url = f'redis://:{redis_password}@{redis_host}:{redis_port}/0' |
| 48 | + # Password exists, quote it and build URL |
| 49 | + quoted_password = quote_plus(redis_password) |
| 50 | + redis_url = f'redis://:{quoted_password}@{redis_host}:{redis_port}/0' |
| 51 | + # Log with masking using the original password |
| 52 | + logger.info(f"Configuring Celery with Redis URL: {redis_url.replace(redis_password, '***')}") |
49 | 53 | else: |
| 54 | + # No password, build simple URL |
50 | 55 | redis_url = f'redis://{redis_host}:{redis_port}/0' |
51 | | - |
52 | | - logger.info(f"Configuring Celery with Redis URL: {redis_url.replace(redis_password if redis_password else '', '***')}") |
| 56 | + # Log without masking |
| 57 | + logger.info(f"Configuring Celery with Redis URL: {redis_url}") |
| 58 | + else: |
| 59 | + # REDIS_URL was provided, try to mask if password pattern is detected |
| 60 | + # Basic pattern matching for password in URL (adjust if needed) |
| 61 | + match = re.match(r"redis://:(?P<password>[^@]+)@", redis_url) |
| 62 | + if match: |
| 63 | + password_to_mask = match.group("password") |
| 64 | + masked_url = redis_url.replace(password_to_mask, "***") |
| 65 | + logger.info(f"Configuring Celery with Redis URL: {masked_url}") |
| 66 | + else: |
| 67 | + # Assume no password or couldn't detect it, log as is |
| 68 | + logger.info(f"Configuring Celery with Redis URL: {redis_url}") |
53 | 69 |
|
54 | 70 | celery = Celery( |
55 | 71 | 'worker', |
|
0 commit comments