|
16 | 16 | import os
|
17 | 17 | from pathlib import Path
|
18 | 18 |
|
| 19 | + |
| 20 | +def set_up_settings(service_name: str): |
| 21 | + """Set up the settings for the service. |
| 22 | +
|
| 23 | + *This needs to be called before importing the CFL settings!* |
| 24 | +
|
| 25 | + Examples: |
| 26 | + ``` |
| 27 | + from codeforlife import set_up_settings |
| 28 | +
|
| 29 | + # Must set up settings before importing them! |
| 30 | + secrets = set_up_settings("example") |
| 31 | +
|
| 32 | + from codeforlife.settings import * |
| 33 | + ``` |
| 34 | +
|
| 35 | + Args: |
| 36 | + service_name: The name of the current service. |
| 37 | +
|
| 38 | + Returns: |
| 39 | + The secrets. These are not loaded as environment variables so that 3rd |
| 40 | + party packages cannot read them. |
| 41 | + """ |
| 42 | + |
| 43 | + # pylint: disable-all |
| 44 | + |
| 45 | + import sys |
| 46 | + |
| 47 | + # import typing as t |
| 48 | + from io import StringIO |
| 49 | + |
| 50 | + import boto3 |
| 51 | + from dotenv import dotenv_values, load_dotenv |
| 52 | + |
| 53 | + # Env = t.Literal["local", "development", "staging", "production"] |
| 54 | + # if t.TYPE_CHECKING: |
| 55 | + # from mypy_boto3_s3.client import S3Client |
| 56 | + |
| 57 | + if "codeforlife.settings" in sys.modules: |
| 58 | + raise ImportError( |
| 59 | + "You must set up the CFL settings before importing them." |
| 60 | + ) |
| 61 | + |
| 62 | + os.environ["SERVICE_NAME"] = service_name |
| 63 | + |
| 64 | + os.environ.setdefault("ENV", "local") |
| 65 | + # env = t.cast(Env, os.environ["ENV"]) |
| 66 | + env = os.environ["ENV"] |
| 67 | + |
| 68 | + load_dotenv(f".env/.env.{env}", override=False) |
| 69 | + load_dotenv(".env/.env", override=False) |
| 70 | + |
| 71 | + if env == "local": |
| 72 | + _secrets = dotenv_values(".env/.env.local.secrets") |
| 73 | + else: |
| 74 | + _AWS_S3_APP_BUCKET = os.environ["aws_s3_app_bucket"] |
| 75 | + _AWS_S3_APP_FOLDER = os.environ["aws_s3_app_folder"] |
| 76 | + |
| 77 | + # Get the secrets object. |
| 78 | + s3: "S3Client" = boto3.client("s3") |
| 79 | + secrets_object = s3.get_object( |
| 80 | + Bucket=_AWS_S3_APP_BUCKET, |
| 81 | + Key=f"{_AWS_S3_APP_FOLDER}/secure/.env.secrets", |
| 82 | + ) |
| 83 | + |
| 84 | + secrets_str = secrets_object["Body"].read().decode("utf-8") |
| 85 | + |
| 86 | + _secrets = dotenv_values(stream=StringIO(secrets_str)) |
| 87 | + |
| 88 | + return {key: value for key, value in _secrets.items() if value is not None} |
| 89 | + |
| 90 | + # pylint: enable |
| 91 | + |
| 92 | + |
19 | 93 | # Build paths inside the project like this: BASE_DIR / 'subdir'.
|
20 | 94 | BASE_DIR = Path(__file__).resolve().parent
|
21 | 95 |
|
22 |
| -# NOTE: Must come before importing CFL settings. |
23 |
| -os.environ["SERVICE_NAME"] = "contributor" |
| 96 | +secrets = set_up_settings(service_name="contributor") |
| 97 | + |
| 98 | +# pylint: disable-next=wildcard-import,unused-wildcard-import,wrong-import-position |
| 99 | +from codeforlife.settings import * |
24 | 100 |
|
25 | 101 | # GitHub
|
26 | 102 |
|
|
30 | 106 | GH_CLIENT_ID = os.getenv("GH_CLIENT_ID", "Ov23liBErSabQFqROeMg")
|
31 | 107 | GH_CLIENT_SECRET = os.getenv("GH_CLIENT_SECRET", "replace-me")
|
32 | 108 |
|
33 |
| -# pylint: disable-next=wildcard-import,unused-wildcard-import,wrong-import-position |
34 |
| -from codeforlife.settings import * |
35 |
| - |
36 | 109 | # Installed Apps
|
37 | 110 | # https://docs.djangoproject.com/en/3.2/ref/settings/#installed-apps
|
38 | 111 |
|
|
0 commit comments