Skip to content

Commit 314f90d

Browse files
authored
Merge pull request #274 from CodeForPhilly/271-secrets
Improved secrets handling for deployment
2 parents 8a064a1 + f50591a commit 314f90d

File tree

8 files changed

+88
-7
lines changed

8 files changed

+88
-7
lines changed

src/server/alembic.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ script_location = alembic
3636
# output_encoding = utf-8
3737

3838
# Container
39-
sqlalchemy.url = postgresql://postgres:thispasswordisverysecure@paws-compose-db/paws
39+
sqlalchemy.url = postgresql://postgres:PASSWORD@paws-compose-db/paws
4040

4141
# Local
4242
# sqlalchemy.url = postgresql://postgres:thispasswordisverysecure@localhost/paws

src/server/alembic/env.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from sqlalchemy import engine_from_config
44
from sqlalchemy import pool
55

6+
from os import environ
7+
68
from alembic import context
79

810
# this is the Alembic Config object, which provides
@@ -25,6 +27,13 @@
2527
# ... etc.
2628

2729

30+
PG_URL1 = 'postgresql://postgres:'
31+
PG_URL2 = environ['POSTGRES_PASSWORD']
32+
PG_URL3 = '@paws-compose-db/paws'
33+
34+
PG_URL = PG_URL1 + PG_URL2 + PG_URL3
35+
36+
2837
def run_migrations_offline():
2938
"""Run migrations in 'offline' mode.
3039
@@ -37,7 +46,8 @@ def run_migrations_offline():
3746
script output.
3847
3948
"""
40-
url = config.get_main_option("sqlalchemy.url")
49+
# url = config.get_main_option("sqlalchemy.url")
50+
url = PG_URL
4151
context.configure(
4252
url=url,
4353
target_metadata=target_metadata,
@@ -60,6 +70,7 @@ def run_migrations_online():
6070
config.get_section(config.config_ini_section),
6171
prefix="sqlalchemy.",
6272
poolclass=pool.NullPool,
73+
url=PG_URL,
6374
)
6475

6576
with connectable.connect() as connection:

src/server/api/common_api.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@
77
import time
88
from datetime import datetime
99
import dateutil.parser
10-
from secrets import SHELTERLUV_SECRET_TOKEN
10+
11+
12+
try:
13+
from secrets import SHELTERLUV_SECRET_TOKEN
14+
except ImportError:
15+
# Not running locally
16+
print("Couldn't get SL_TOKEN from file, trying environment **********")
17+
from os import environ
18+
19+
try:
20+
SHELTERLUV_SECRET_TOKEN = environ['SHELTERLUV_SECRET_TOKEN']
21+
except KeyError:
22+
# Nor in environment
23+
# You're SOL for now
24+
print("Couldn't get secrets from file or environment")
25+
26+
1127

1228
from api import jwt_ops
1329

src/server/app.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@
44

55
from flask_jwt_extended import JWTManager
66

7-
from secrets import JWT_SECRET, APP_SECRET_KEY
7+
try:
8+
from secrets import JWT_SECRET, APP_SECRET_KEY
9+
except ImportError:
10+
# Not running locally
11+
print("Could not get secrets from file, trying environment **********")
12+
from os import environ
13+
14+
try:
15+
JWT_SECRET = environ['JWT_SECRET']
16+
APP_SECRET_KEY = environ['APP_SECRET_KEY']
17+
except KeyError:
18+
# Nor in environment
19+
# You're SOL for now
20+
print("Couldn't get secrets from file or environment")
21+
822

923
app = Flask(__name__)
1024

src/server/bin/export_secrets.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set -o allexport
2+
while read k _ v;
3+
do
4+
eval $k=$v;
5+
export k;
6+
done < 'secrets.py'
7+
set +o allexport

src/server/bin/startServer.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
#!/bin/bash
12

23
# we may want to switch this to a script which logs output, etc?
34
echo "------------STARTING `date` ------------------"
45
set FLASK_APP=server/app.py
56
export FLASK_APP
6-
7+
source bin/export_secrets.sh
78
# This abomination ensures that the PG server has finished its restart cycle
89
echo "SLEEPING.. WAITING FOR DB"; sleep 5; echo "WAKING"; alembic upgrade head; echo "DB SETUP";
910
#; python -m flask run --host=0.0.0.0 --no-reload

src/server/pipeline/shelterluv_api_handler.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
import requests
22
import csv
33
from config import RAW_DATA_PATH
4-
from secrets import SHELTERLUV_SECRET_TOKEN
4+
5+
try:
6+
from secrets import SHELTERLUV_SECRET_TOKEN
7+
except ImportError:
8+
# Not running locally
9+
print("Couldn't get SL_TOKEN from file, trying environment **********")
10+
from os import environ
11+
12+
try:
13+
SHELTERLUV_SECRET_TOKEN = environ['SHELTERLUV_SECRET_TOKEN']
14+
except KeyError:
15+
# Nor in environment
16+
# You're SOL for now
17+
print("Couldn't get secrets from file or environment")
18+
519

620

721
def write_csv(json_data):

src/server/user_mgmt/base_users.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,25 @@
33
from api import user_api
44
import sqlalchemy as sa
55

6-
from secrets import BASEUSER_PW, BASEEDITOR_PW, BASEADMIN_PW
6+
try:
7+
from secrets import BASEUSER_PW, BASEEDITOR_PW, BASEADMIN_PW
8+
except ImportError:
9+
# Not running locally
10+
print("Couldn't get BASE user PWs from file, trying environment **********")
11+
from os import environ
12+
13+
try:
14+
BASEUSER_PW = environ['BASEUSER_PW']
15+
BASEEDITOR_PW = environ['BASEEDITOR_PW']
16+
BASEADMIN_PW = environ['BASEADMIN_PW']
17+
18+
except KeyError:
19+
# Nor in environment
20+
# You're SOL for now
21+
print("Couldn't get secrets from file or environment")
22+
23+
24+
725

826

927
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey

0 commit comments

Comments
 (0)