Skip to content

Commit b89b19b

Browse files
SFR-2472: Load secrets when loading env (#508)
1 parent f9f31ea commit b89b19b

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

load_env.py

+37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22
from typing import Optional
33
import yaml
44

5+
from services.ssm_service import SSMService
6+
7+
8+
ENV_VAR_TO_SSM_NAME = {
9+
'CONTENT_CAFE_USER': 'contentcafe/user',
10+
'CONTENT_CAFE_PSWD': 'contentcafe/pswd',
11+
'ELASTICSEARCH_PSWD': 'elasticsearch/pswd',
12+
'ELASTICSEARCH_USER': 'elasticsearch/user',
13+
'GITHUB_API_KEY': 'github-key',
14+
'GOOGLE_BOOKS_KEY': 'google-books/api-key',
15+
'HATHI_API_KEY': 'hathitrust/api-key',
16+
'HATHI_API_SECRET': 'hathitrust/api-secret',
17+
'NEW_RELIC_LICENSE_KEY': 'newrelic/key',
18+
'NYPL_API_CLIENT_ID': 'nypl-api/client-id',
19+
'NYPL_API_CLIENT_PUBLIC_KEY': 'nypl-api/public-key',
20+
'NYPL_API_CLIENT_SECRET': 'nypl-api/client-secret',
21+
'NYPL_BIB_PSWD': 'postgres/nypl-pswd',
22+
'NYPL_BIB_USER': 'postgres/nypl-user',
23+
'OCLC_METADATA_ID': 'oclc-metadata-clientid',
24+
'OCLC_METADATA_SECRET': 'oclc-metadata-secret',
25+
'OCLC_CLIENT_ID': 'oclc-search-clientid',
26+
'OCLC_CLIENT_SECRET': 'oclc-search-secret',
27+
'POSTGRES_PSWD': 'postgres/pswd',
28+
'POSTGRES_USER': 'postgres/user',
29+
'RABBIT_PSWD': 'rabbit-pswd',
30+
'RABBIT_USER': 'rabbit-user',
31+
}
32+
533

634
def load_env_file(run_type: str, file_string: Optional[str]=None) -> None:
735
"""Loads configuration details from a specific yaml file.
@@ -37,3 +65,12 @@ def load_env_file(run_type: str, file_string: Optional[str]=None) -> None:
3765
if env_dict:
3866
for key, value in env_dict.items():
3967
os.environ[key] = value
68+
69+
load_secrets()
70+
71+
def load_secrets():
72+
ssm_service = SSMService()
73+
74+
for env_var, param_name in ENV_VAR_TO_SSM_NAME.items():
75+
if os.environ.get(env_var, None) is None:
76+
os.environ[env_var] = ssm_service.get_parameter(param_name)

services/ssm_service.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def __init__(self):
1515
region_name=os.environ.get('AWS_REGION', None)
1616
)
1717

18-
self.environment = 'production' if os.environ['ENVIRONMENT'] == 'production' else 'qa'
18+
self.environment = 'production' if os.environ.get('ENVIRONMENT', 'qa') == 'production' else 'qa'
1919

2020
def get_parameter(self, parameter_name: str) -> Optional[dict]:
2121
try:

0 commit comments

Comments
 (0)