mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-08 22:22:24 -05:00
Update config
Source the configuration from an environment file, this way it is similar to how the aws environment works
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -60,4 +60,5 @@ target/
|
||||
.idea/
|
||||
|
||||
# Mac
|
||||
*.DS_Store
|
||||
*.DS_Store
|
||||
environment.sh
|
||||
16
README.md
16
README.md
@@ -9,3 +9,19 @@ Read and write notifications/status queue.
|
||||
Get and update notification status.
|
||||
|
||||
mkvirtualenv -p /usr/local/bin/python3 notifications-api
|
||||
|
||||
|
||||
```
|
||||
export ADMIN_CLIENT_USER_NAME = 'dev-notify-admin'
|
||||
export ADMIN_CLIENT_SECRET = 'dev-notify-secret-key'
|
||||
export AWS_REGION='eu-west-1'
|
||||
export DANGEROUS_SALT = 'dangerous-salt'
|
||||
export DELIVERY_CLIENT_USER_NAME='dev-notify-delivery'
|
||||
export DELIVERY_CLIENT_SECRET='dev-notify-secret-key'
|
||||
|
||||
export NOTIFY_JOB_QUEUE='notify-jobs-queue-[-unique-to-environment]'
|
||||
export NOTIFICATION_QUEUE_PREFIX='notification_development[-unique-to-environment]'
|
||||
|
||||
export SECRET_KEY = 'secret-key'
|
||||
export SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/notification_api'
|
||||
```
|
||||
@@ -8,7 +8,6 @@ from flask import Flask, _request_ctx_stack
|
||||
from flask.ext.sqlalchemy import SQLAlchemy
|
||||
from flask_marshmallow import Marshmallow
|
||||
from werkzeug.local import LocalProxy
|
||||
from config import configs
|
||||
from utils import logging
|
||||
|
||||
|
||||
@@ -18,15 +17,14 @@ ma = Marshmallow()
|
||||
api_user = LocalProxy(lambda: _request_ctx_stack.top.api_user)
|
||||
|
||||
|
||||
def create_app(config_name, config_overrides=None):
|
||||
def create_app():
|
||||
application = Flask(__name__)
|
||||
|
||||
application.config['NOTIFY_API_ENVIRONMENT'] = config_name
|
||||
application.config.from_object(configs[config_name])
|
||||
application.config.from_object(os.environ['NOTIFY_API_ENVIRONMENT'])
|
||||
|
||||
db.init_app(application)
|
||||
ma.init_app(application)
|
||||
init_app(application, config_overrides)
|
||||
init_app(application)
|
||||
logging.init_app(application)
|
||||
|
||||
from app.service.rest import service as service_blueprint
|
||||
@@ -46,16 +44,7 @@ def create_app(config_name, config_overrides=None):
|
||||
return application
|
||||
|
||||
|
||||
def init_app(app, config_overrides):
|
||||
for key, value in app.config.items():
|
||||
if key in os.environ:
|
||||
app.config[key] = convert_to_boolean(os.environ[key])
|
||||
|
||||
if config_overrides:
|
||||
for key in app.config.keys():
|
||||
if key in config_overrides:
|
||||
app.config[key] = config_overrides[key]
|
||||
|
||||
def init_app(app):
|
||||
@app.before_request
|
||||
def required_authentication():
|
||||
if request.path != url_for('status.show_status'):
|
||||
@@ -72,51 +61,6 @@ def init_app(app, config_overrides):
|
||||
return response
|
||||
|
||||
|
||||
def convert_to_boolean(value):
|
||||
"""Turn strings to bools if they look like them
|
||||
|
||||
Truthy things should be True
|
||||
>>> for truthy in ['true', 'on', 'yes', '1']:
|
||||
... assert convert_to_boolean(truthy) == True
|
||||
|
||||
Falsey things should be False
|
||||
>>> for falsey in ['false', 'off', 'no', '0']:
|
||||
... assert convert_to_boolean(falsey) == False
|
||||
|
||||
Other things should be unchanged
|
||||
>>> for value in ['falsey', 'other', True, 0]:
|
||||
... assert convert_to_boolean(value) == value
|
||||
"""
|
||||
if isinstance(value, string_types):
|
||||
if value.lower() in ['t', 'true', 'on', 'yes', '1']:
|
||||
return True
|
||||
elif value.lower() in ['f', 'false', 'off', 'no', '0']:
|
||||
return False
|
||||
|
||||
return value
|
||||
|
||||
|
||||
def convert_to_number(value):
|
||||
"""Turns numeric looking things into floats or ints
|
||||
|
||||
Integery things should be integers
|
||||
>>> for inty in ['0', '1', '2', '99999']:
|
||||
... assert isinstance(convert_to_number(inty), int)
|
||||
|
||||
Floaty things should be floats
|
||||
>>> for floaty in ['0.99', '1.1', '1000.0000001']:
|
||||
... assert isinstance(convert_to_number(floaty), float)
|
||||
|
||||
Other things should be unchanged
|
||||
>>> for value in [0, 'other', True, 123]:
|
||||
... assert convert_to_number(value) == value
|
||||
"""
|
||||
try:
|
||||
return float(value) if "." in value else int(value)
|
||||
except (TypeError, ValueError):
|
||||
return value
|
||||
|
||||
|
||||
def get_api_version():
|
||||
build = 'n/a'
|
||||
build_time = "n/a"
|
||||
|
||||
@@ -6,7 +6,7 @@ from flask.ext.script import Manager, Server
|
||||
from flask.ext.migrate import Migrate, MigrateCommand
|
||||
from app import (create_app, db, get_api_version, get_db_version)
|
||||
|
||||
application = create_app(os.getenv('NOTIFY_API_ENVIRONMENT') or 'development')
|
||||
application = create_app()
|
||||
manager = Manager(application)
|
||||
port = int(os.environ.get('PORT', 6011))
|
||||
manager.add_command("runserver", Server(host='0.0.0.0', port=port))
|
||||
|
||||
61
config.py
61
config.py
@@ -3,60 +3,27 @@ import os
|
||||
|
||||
class Config(object):
|
||||
DEBUG = False
|
||||
NOTIFY_LOG_LEVEL = 'DEBUG'
|
||||
ADMIN_CLIENT_USER_NAME = os.environ['ADMIN_CLIENT_USER_NAME']
|
||||
ADMIN_CLIENT_SECRET = os.environ['ADMIN_CLIENT_SECRET']
|
||||
AWS_REGION = os.environ['AWS_REGION']
|
||||
DANGEROUS_SALT = os.environ['DANGEROUS_SALT']
|
||||
DELIVERY_CLIENT_USER_NAME = os.environ['DELIVERY_CLIENT_USER_NAME']
|
||||
DELIVERY_CLIENT_SECRET = os.environ['DELIVERY_CLIENT_SECRET']
|
||||
NOTIFY_APP_NAME = 'api'
|
||||
NOTIFY_LOG_PATH = '/var/log/notify/application.log'
|
||||
SQLALCHEMY_COMMIT_ON_TEARDOWN = False
|
||||
SQLALCHEMY_RECORD_QUERIES = True
|
||||
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/notification_api'
|
||||
NOTIFY_DATA_API_URL = os.getenv('NOTIFY_API_URL', "http://localhost:6001")
|
||||
NOTIFY_DATA_API_AUTH_TOKEN = os.getenv('NOTIFY_API_TOKEN', "dev-token")
|
||||
ADMIN_CLIENT_USER_NAME = None
|
||||
ADMIN_CLIENT_SECRET = None
|
||||
DELIVERY_CLIENT_USER_NAME = None
|
||||
DELIVERY_CLIENT_SECRET = None
|
||||
|
||||
AWS_REGION = 'eu-west-1'
|
||||
NOTIFY_JOB_QUEUE = os.getenv('NOTIFY_JOB_QUEUE', 'notify-jobs-queue')
|
||||
NOTIFY_JOB_QUEUE = os.environ['NOTIFY_JOB_QUEUE']
|
||||
# Notification Queue names are a combination of a prefx plus a name
|
||||
NOTIFICATION_QUEUE_PREFIX = 'notification'
|
||||
VERIFY_CODE_FROM_EMAIL_ADDRESS = 'notify@digital.cabinet-office.gov.uk'
|
||||
NOTIFICATION_QUEUE_PREFIX = os.environ['NOTIFICATION_QUEUE_PREFIX']
|
||||
SECRET_KEY = os.environ['SECRET_KEY']
|
||||
SQLALCHEMY_COMMIT_ON_TEARDOWN = False
|
||||
SQLALCHEMY_DATABASE_URI = os.environ['SQLALCHEMY_DATABASE_URI']
|
||||
SQLALCHEMY_RECORD_QUERIES = True
|
||||
VERIFY_CODE_FROM_EMAIL_ADDRESS=os.environ['VERIFY_CODE_FROM_EMAIL_ADDRESS']
|
||||
|
||||
|
||||
class Development(Config):
|
||||
DEBUG = True
|
||||
SECRET_KEY = 'secret-key'
|
||||
DANGEROUS_SALT = 'dangerous-salt'
|
||||
ADMIN_CLIENT_USER_NAME = 'dev-notify-admin'
|
||||
ADMIN_CLIENT_SECRET = 'dev-notify-secret-key'
|
||||
DELIVERY_CLIENT_USER_NAME = 'dev-notify-delivery'
|
||||
DELIVERY_CLIENT_SECRET = 'dev-notify-secret-key'
|
||||
NOTIFICATION_QUEUE_PREFIX = 'notification_development'
|
||||
VERIFY_CODE_FROM_EMAIL_ADDRESS = 'notify-tests-preview@digital.cabinet-office.gov.uk'
|
||||
|
||||
|
||||
class Test(Development):
|
||||
SQLALCHEMY_DATABASE_URI = 'postgresql://localhost/test_notification_api'
|
||||
NOTIFICATION_QUEUE_PREFIX = 'notification_test'
|
||||
|
||||
|
||||
class Preview(Config):
|
||||
NOTIFICATION_QUEUE_PREFIX = 'notification_preview'
|
||||
VERIFY_CODE_FROM_EMAIL_ADDRESS = 'notify-tests-preview@digital.cabinet-office.gov.uk'
|
||||
|
||||
|
||||
class Staging(Config):
|
||||
NOTIFICATION_QUEUE_PREFIX = 'notification_staging'
|
||||
|
||||
|
||||
class Live(Config):
|
||||
NOTIFICATION_QUEUE_PREFIX = 'notification_live'
|
||||
|
||||
|
||||
configs = {
|
||||
'development': Development,
|
||||
'preview': Preview,
|
||||
'staging': Staging,
|
||||
'test': Test,
|
||||
'live': Live,
|
||||
}
|
||||
pass
|
||||
|
||||
2
db.py
2
db.py
@@ -5,7 +5,7 @@ from credstash import getAllSecrets
|
||||
|
||||
secrets = getAllSecrets(region="eu-west-1")
|
||||
|
||||
application = create_app('live', secrets)
|
||||
application = create_app()
|
||||
|
||||
manager = Manager(application)
|
||||
migrate = Migrate(application, db)
|
||||
|
||||
12
environment_test.sh
Normal file
12
environment_test.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
export NOTIFY_API_ENVIRONMENT='config.Test'
|
||||
export ADMIN_CLIENT_USER_NAME='dev-notify-admin'
|
||||
export ADMIN_CLIENT_SECRET='dev-notify-secret-key'
|
||||
export AWS_REGION='eu-west-1'
|
||||
export DANGEROUS_SALT='dangerous-salt'
|
||||
export DELIVERY_CLIENT_USER_NAME='dev-notify-delivery'
|
||||
export DELIVERY_CLIENT_SECRET='dev-notify-secret-key'
|
||||
export NOTIFY_JOB_QUEUE='notify-jobs-queue-test'
|
||||
export NOTIFICATION_QUEUE_PREFIX='notification_development-test'
|
||||
export SECRET_KEY='secret-key'
|
||||
export SQLALCHEMY_DATABASE_URI='postgresql://localhost/test_notification_api'
|
||||
|
||||
@@ -34,4 +34,5 @@ createdb notification_api
|
||||
createdb test_notification_api
|
||||
|
||||
# Upgrade databases
|
||||
source environment.sh
|
||||
python application.py db upgrade
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
python application.py runserver
|
||||
set -e
|
||||
|
||||
source environment.sh
|
||||
python3 application.py runserver
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
|
||||
set -o pipefail
|
||||
|
||||
source environment_test.sh
|
||||
|
||||
function display_result {
|
||||
RESULT=$1
|
||||
EXIT_STATUS=$2
|
||||
|
||||
@@ -114,7 +114,7 @@ def test_create_job(notify_api, notify_db, notify_db_session, sample_template):
|
||||
assert resp_json['data']['original_file_name'] == original_file_name
|
||||
|
||||
boto3.setup_default_session(region_name='eu-west-1')
|
||||
q = boto3.resource('sqs').get_queue_by_name(QueueName='notify-jobs-queue')
|
||||
q = boto3.resource('sqs').get_queue_by_name(QueueName=notify_api.config['NOTIFY_JOB_QUEUE'])
|
||||
messages = q.receive_messages()
|
||||
assert len(messages) == 1
|
||||
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
import pytest
|
||||
import mock
|
||||
import os
|
||||
|
||||
import boto3
|
||||
from config import configs
|
||||
import mock
|
||||
import pytest
|
||||
from alembic.command import upgrade
|
||||
from alembic.config import Config
|
||||
from flask.ext.migrate import Migrate, MigrateCommand
|
||||
from flask.ext.script import Manager
|
||||
from sqlalchemy.schema import MetaData
|
||||
|
||||
from app import create_app, db
|
||||
from app import models
|
||||
|
||||
|
||||
@pytest.fixture(scope='session')
|
||||
def notify_api(request):
|
||||
app = create_app('test')
|
||||
app = create_app()
|
||||
ctx = app.app_context()
|
||||
ctx.push()
|
||||
|
||||
@@ -59,12 +59,6 @@ def notify_db_session(request):
|
||||
request.addfinalizer(teardown)
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def notify_config(notify_api):
|
||||
notify_api.config['NOTIFY_API_ENVIRONMENT'] = 'test'
|
||||
notify_api.config.from_object(configs['test'])
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
def os_environ(request):
|
||||
env_patch = mock.patch('os.environ', {})
|
||||
|
||||
15
wsgi.py
15
wsgi.py
@@ -1,17 +1,14 @@
|
||||
from app import create_app
|
||||
from credstash import getAllSecrets
|
||||
import os
|
||||
|
||||
config = 'live'
|
||||
default_env_file = '/home/ubuntu/environment'
|
||||
|
||||
if os.path.isfile(default_env_file):
|
||||
environment = open(default_env_file, 'r')
|
||||
config = environment.readline().strip()
|
||||
from app import create_app
|
||||
from credstash import getAllSecrets
|
||||
|
||||
# on aws get secrets and export to env
|
||||
secrets = getAllSecrets(region="eu-west-1")
|
||||
for key, val in secrets.items():
|
||||
os.environ[key] = val
|
||||
|
||||
application = create_app(config, secrets)
|
||||
application = create_app()
|
||||
|
||||
if __name__ == "__main__":
|
||||
application.run()
|
||||
|
||||
Reference in New Issue
Block a user