-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.py
66 lines (52 loc) · 1.58 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import json
from addict import Dict
import boto3
SETTINGS = None
REGION = None
def set_region(region):
global REGION
REGION = region
def load():
"""
Loads the correct settings file based on FLASK_ENV.
"""
flask_env = os.environ["FLASK_ENV"]
with open(f"settings.{flask_env}.json", "r") as f:
return Dict(json.load(f))
def get_ssm_client():
region = get_value("aws_region")
if "AWS_SECRET_ACCESS_KEY" in os.environ:
ssm = boto3.client(
"ssm",
aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
aws_session_token=os.environ["AWS_SESSION_TOKEN"],
region_name=region,
)
else:
print("aws_secret_key not in env, setting to none")
ssm = None
return ssm
def get_value(name):
settings = load()
setting = settings.get(name, Dict({"source": None}))
value = None
if "source" in setting:
if setting.source == "env":
value = os.environ[setting.name]
elif setting.source == "ssm":
ssm = get_ssm_client()
if ssm:
response = ssm.get_parameter(Name=setting.name, WithDecryption=True)
param = response["Parameter"]
value = param["Value"]
else:
value = None
elif setting.source == "this":
value = setting.name
return value
def get_setting(name):
settings = load()
setting = settings.get(name, {"source": None})
return setting