From 81272a2f7aa24a1a458951f4ba8f58e3c86908ad Mon Sep 17 00:00:00 2001 From: kevgliss Date: Mon, 5 Dec 2016 16:43:38 -0800 Subject: [PATCH] Moving validation to server start. (#563) --- lemur/factory.py | 14 -------------- lemur/manage.py | 25 +++++++++++++++++++++---- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lemur/factory.py b/lemur/factory.py index e4a99fb8..ca25b339 100644 --- a/lemur/factory.py +++ b/lemur/factory.py @@ -19,7 +19,6 @@ from logging.handlers import RotatingFileHandler from flask import Flask from lemur.common.health import mod as health -from lemur.common.utils import validate_conf from lemur.extensions import db, migrate, principal, smtp_mail, metrics @@ -29,16 +28,6 @@ DEFAULT_BLUEPRINTS = ( API_VERSION = 1 -REQUIRED_VARIABLES = [ - 'LEMUR_SECURITY_TEAM_EMAIL', - 'LEMUR_DEFAULT_ORGANIZATIONAL_UNIT', - 'LEMUR_DEFAULT_ORGANIZATION', - 'LEMUR_DEFAULT_LOCATION', - 'LEMUR_DEFAULT_COUNTRY', - 'LEMUR_DEFAULT_STATE', - 'SQLALCHEMY_DATABASE_URI' -] - def create_app(app_name=None, blueprints=None, config=None): """ @@ -104,7 +93,6 @@ def configure_app(app, config=None): # respect the config first if config and config != 'None': app.config.from_object(from_file(config)) - else: try: app.config.from_envvar("LEMUR_CONF") @@ -115,8 +103,6 @@ def configure_app(app, config=None): else: app.config.from_object(from_file(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'default.conf.py'))) - validate_conf(app, REQUIRED_VARIABLES) - def configure_extensions(app): """ diff --git a/lemur/manage.py b/lemur/manage.py index 9ee60eb5..ab7cb592 100755 --- a/lemur/manage.py +++ b/lemur/manage.py @@ -32,6 +32,8 @@ from lemur.notifications import service as notification_service from lemur.certificates.verify import verify_string from lemur.sources import service as source_service +from lemur.common.utils import validate_conf + from lemur import create_app # Needed to be imported so that SQLAlchemy create_all can find our models @@ -51,12 +53,21 @@ manager.add_option('-c', '--config', dest='config') migrate = Migrate(create_app) +REQUIRED_VARIABLES = [ + 'LEMUR_SECURITY_TEAM_EMAIL', + 'LEMUR_DEFAULT_ORGANIZATIONAL_UNIT', + 'LEMUR_DEFAULT_ORGANIZATION', + 'LEMUR_DEFAULT_LOCATION', + 'LEMUR_DEFAULT_COUNTRY', + 'LEMUR_DEFAULT_STATE', + 'SQLALCHEMY_DATABASE_URI' +] + KEY_LENGTH = 40 DEFAULT_CONFIG_PATH = '~/.lemur/lemur.conf.py' DEFAULT_SETTINGS = 'lemur.conf.server' SETTINGS_ENVVAR = 'LEMUR_CONF' - CONFIG_TEMPLATE = """ # This is just Python which means you can inherit and tweak settings @@ -182,9 +193,9 @@ def generate_settings(): output = CONFIG_TEMPLATE.format( # we use Fernet.generate_key to make sure that the key length is # compatible with Fernet - encryption_key=Fernet.generate_key(), - secret_token=base64.b64encode(os.urandom(KEY_LENGTH)), - flask_secret_key=base64.b64encode(os.urandom(KEY_LENGTH)), + encryption_key=Fernet.generate_key().decode('utf-8'), + secret_token=base64.b64encode(os.urandom(KEY_LENGTH)).decode('utf-8'), + flask_secret_key=base64.b64encode(os.urandom(KEY_LENGTH)).decode('utf-8'), ) return output @@ -402,6 +413,11 @@ class LemurServer(Command): from gunicorn.app.wsgiapp import WSGIApplication app = WSGIApplication() + + # run startup tasks on a app like object + pre_app = create_app(kwargs.get('config')) + validate_conf(pre_app, REQUIRED_VARIABLES) + app.app_uri = 'lemur:create_app(config="{0}")'.format(kwargs.get('config')) return app.run() @@ -417,6 +433,7 @@ def create_config(config_path=None): config_path = os.path.expanduser(config_path) dir = os.path.dirname(config_path) + if not os.path.exists(dir): os.makedirs(dir)