|
7 | 7 |
|
8 | 8 | import environ |
9 | 9 | import pytz |
| 10 | +from azure.identity import DefaultAzureCredential |
10 | 11 | from corsheaders.defaults import default_headers |
11 | 12 | from django.utils.translation import gettext_lazy as _ |
12 | 13 | from urllib3.util.retry import Retry |
|
24 | 25 | DOCKER_HOST_IP=(str, None), |
25 | 26 | DJANGO_SECRET_KEY=str, |
26 | 27 | DJANGO_MEDIA_URL=(str, "/media/"), |
27 | | - DJANGO_MEDIA_ROOT=(str, os.path.join(BASE_DIR, "media")), |
28 | 28 | DJANGO_STATIC_URL=(str, "/static/"), |
29 | | - DJANGO_STATIC_ROOT=(str, os.path.join(BASE_DIR, "static")), |
30 | 29 | DJANGO_ADDITIONAL_ALLOWED_HOSTS=(list, []), # Eg: api.go.ifrc.org, goadmin.ifrc.org, dsgocdnapi.azureedge.net |
31 | 30 | GO_ENVIRONMENT=(str, "development"), # staging, production |
32 | 31 | # |
|
39 | 38 | DJANGO_DB_PASS=str, |
40 | 39 | DJANGO_DB_HOST=str, |
41 | 40 | DJANGO_DB_PORT=(int, 5432), |
42 | | - # Azure storage |
| 41 | + # Storage |
| 42 | + # -- Azure storage |
| 43 | + AZURE_STORAGE_ENABLED=(bool, False), |
| 44 | + AZURE_STORAGE_CONNECTION_STRING=(str, None), |
43 | 45 | AZURE_STORAGE_ACCOUNT=(str, None), |
44 | 46 | AZURE_STORAGE_KEY=(str, None), |
| 47 | + AZURE_STORAGE_TOKEN_CREDENTIAL=(str, None), |
| 48 | + AZURE_STORAGE_MANAGED_IDENTITY=(bool, False), |
| 49 | + # -- Filesystem (default) XXX: Don't use for production |
| 50 | + DJANGO_MEDIA_ROOT=(str, os.path.join(BASE_DIR, "media")), |
| 51 | + DJANGO_STATIC_ROOT=(str, os.path.join(BASE_DIR, "static")), |
45 | 52 | # Email |
46 | 53 | EMAIL_USE_TLS=(bool, True), |
47 | 54 | FORCE_USE_SMTP=(bool, False), |
|
132 | 139 |
|
133 | 140 | # Requires uppercase variable https://docs.djangoproject.com/en/2.1/topics/settings/#creating-your-own-settings |
134 | 141 |
|
| 142 | + |
135 | 143 | def find_env_with_value(*keys: str) -> None | str: |
136 | 144 | for key in keys: |
137 | 145 | if env(key): |
@@ -437,39 +445,65 @@ def parse_domain(*env_keys: str) -> str: |
437 | 445 | IFRC_TRANSLATION_DOMAIN = env("IFRC_TRANSLATION_DOMAIN") |
438 | 446 | IFRC_TRANSLATION_HEADER_API_KEY = env("IFRC_TRANSLATION_HEADER_API_KEY") |
439 | 447 |
|
440 | | -MEDIA_URL = env("DJANGO_MEDIA_URL") |
441 | | -MEDIA_ROOT = env("DJANGO_MEDIA_ROOT") |
| 448 | +# Needed to generate correct https links when running behind a reverse proxy |
| 449 | +# when SSL is terminated at the proxy |
| 450 | +USE_X_FORWARDED_HOST = True |
| 451 | +SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_SCHEME", "https") |
442 | 452 |
|
| 453 | +# Storage |
| 454 | +MEDIA_URL = env("DJANGO_MEDIA_URL") |
443 | 455 | STATIC_URL = env("DJANGO_STATIC_URL") |
444 | | -STATIC_ROOT = env("DJANGO_STATIC_ROOT") |
445 | 456 |
|
446 | 457 | STATICFILES_DIRS = [ |
447 | 458 | os.path.join(BASE_DIR, "go-static"), |
448 | 459 | ] |
449 | 460 |
|
450 | | -# Needed to generate correct https links when running behind a reverse proxy |
451 | | -# when SSL is terminated at the proxy |
452 | | -USE_X_FORWARDED_HOST = True |
453 | | -SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_SCHEME", "https") |
454 | | - |
| 461 | +# NOTE: This is used by api/logger.py which sends logs to azure storage |
| 462 | +# FIXME: Do we need this? We are also using loki for log collections |
455 | 463 | AZURE_STORAGE_ACCOUNT = env("AZURE_STORAGE_ACCOUNT") |
456 | 464 | AZURE_STORAGE_KEY = env("AZURE_STORAGE_KEY") |
457 | 465 |
|
458 | | -AZURE_STORAGE = { |
459 | | - "CONTAINER": "api", |
460 | | - "ACCOUNT_NAME": AZURE_STORAGE_ACCOUNT, |
461 | | - "ACCOUNT_KEY": AZURE_STORAGE_KEY, |
462 | | - "CDN_HOST": None, |
463 | | - "USE_SSL": False, |
464 | | -} |
465 | | -# instead of: if AZURE_STORAGE_ACCOUNT: DEFAULT_FILE_STORAGE = "api.storage.AzureStorage" |
466 | | -# > https://django-storages.readthedocs.io/en/latest/backends/azure.html |
467 | | - |
468 | | -AZURE_ACCOUNT_NAME = env("AZURE_STORAGE_ACCOUNT") |
469 | | -AZURE_ACCOUNT_KEY = env("AZURE_STORAGE_KEY") |
470 | | -AZURE_CONTAINER = "api" |
471 | | -if AZURE_STORAGE_ACCOUNT: |
472 | | - DEFAULT_FILE_STORAGE = "storages.backends.azure_storage.AzureStorage" |
| 466 | +if env("AZURE_STORAGE_ENABLED"): |
| 467 | + |
| 468 | + AZURE_STORAGE_CONFIG_OPTIONS = { |
| 469 | + "connection_string": env("AZURE_STORAGE_CONNECTION_STRING"), |
| 470 | + "overwrite_files": False, |
| 471 | + } |
| 472 | + |
| 473 | + if not env("AZURE_STORAGE_CONNECTION_STRING"): |
| 474 | + AZURE_STORAGE_CONFIG_OPTIONS.update( |
| 475 | + { |
| 476 | + "account_name": env("AZURE_STORAGE_ACCOUNT"), |
| 477 | + "account_key": env("AZURE_STORAGE_KEY"), |
| 478 | + "token_credential": env("AZURE_STORAGE_TOKEN_CREDENTIAL"), |
| 479 | + } |
| 480 | + ) |
| 481 | + |
| 482 | + if env("AZURE_STORAGE_MANAGED_IDENTITY"): |
| 483 | + AZURE_STORAGE_CONFIG_OPTIONS["token_credential"] = DefaultAzureCredential() |
| 484 | + |
| 485 | + STORAGES = { |
| 486 | + "default": { |
| 487 | + "BACKEND": "storages.backends.azure_storage.AzureStorage", |
| 488 | + "OPTIONS": { |
| 489 | + **AZURE_STORAGE_CONFIG_OPTIONS, |
| 490 | + "azure_container": "api", |
| 491 | + }, |
| 492 | + }, |
| 493 | + # TODO: Use this instead of nginx for staticfiles |
| 494 | + # "staticfiles": { |
| 495 | + # "BACKEND": "storages.backends.azure_storage.AzureStorage", |
| 496 | + # "OPTIONS": { |
| 497 | + # **AZURE_STORAGE_CONFIG_OPTIONS, |
| 498 | + # "azure_container": env("AZURE_STORAGE_STATIC_CONTAINER"), |
| 499 | + # "overwrite_files": True, |
| 500 | + # }, |
| 501 | + # }, |
| 502 | + } |
| 503 | +else: |
| 504 | + # Filesystem |
| 505 | + MEDIA_ROOT = env("DJANGO_MEDIA_ROOT") |
| 506 | + STATIC_ROOT = env("DJANGO_STATIC_ROOT") |
473 | 507 |
|
474 | 508 | # Email config |
475 | 509 | FORCE_USE_SMTP = env("FORCE_USE_SMTP") |
|
0 commit comments