Moving validation to server start. (#563)

This commit is contained in:
kevgliss 2016-12-05 16:43:38 -08:00 committed by GitHub
parent e622a49b72
commit 81272a2f7a
2 changed files with 21 additions and 18 deletions

View File

@ -19,7 +19,6 @@ from logging.handlers import RotatingFileHandler
from flask import Flask from flask import Flask
from lemur.common.health import mod as health 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 from lemur.extensions import db, migrate, principal, smtp_mail, metrics
@ -29,16 +28,6 @@ DEFAULT_BLUEPRINTS = (
API_VERSION = 1 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): def create_app(app_name=None, blueprints=None, config=None):
""" """
@ -104,7 +93,6 @@ def configure_app(app, config=None):
# respect the config first # respect the config first
if config and config != 'None': if config and config != 'None':
app.config.from_object(from_file(config)) app.config.from_object(from_file(config))
else: else:
try: try:
app.config.from_envvar("LEMUR_CONF") app.config.from_envvar("LEMUR_CONF")
@ -115,8 +103,6 @@ def configure_app(app, config=None):
else: else:
app.config.from_object(from_file(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'default.conf.py'))) 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): def configure_extensions(app):
""" """

View File

@ -32,6 +32,8 @@ from lemur.notifications import service as notification_service
from lemur.certificates.verify import verify_string from lemur.certificates.verify import verify_string
from lemur.sources import service as source_service from lemur.sources import service as source_service
from lemur.common.utils import validate_conf
from lemur import create_app from lemur import create_app
# Needed to be imported so that SQLAlchemy create_all can find our models # 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) 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 KEY_LENGTH = 40
DEFAULT_CONFIG_PATH = '~/.lemur/lemur.conf.py' DEFAULT_CONFIG_PATH = '~/.lemur/lemur.conf.py'
DEFAULT_SETTINGS = 'lemur.conf.server' DEFAULT_SETTINGS = 'lemur.conf.server'
SETTINGS_ENVVAR = 'LEMUR_CONF' SETTINGS_ENVVAR = 'LEMUR_CONF'
CONFIG_TEMPLATE = """ CONFIG_TEMPLATE = """
# This is just Python which means you can inherit and tweak settings # This is just Python which means you can inherit and tweak settings
@ -182,9 +193,9 @@ def generate_settings():
output = CONFIG_TEMPLATE.format( output = CONFIG_TEMPLATE.format(
# we use Fernet.generate_key to make sure that the key length is # we use Fernet.generate_key to make sure that the key length is
# compatible with Fernet # compatible with Fernet
encryption_key=Fernet.generate_key(), encryption_key=Fernet.generate_key().decode('utf-8'),
secret_token=base64.b64encode(os.urandom(KEY_LENGTH)), secret_token=base64.b64encode(os.urandom(KEY_LENGTH)).decode('utf-8'),
flask_secret_key=base64.b64encode(os.urandom(KEY_LENGTH)), flask_secret_key=base64.b64encode(os.urandom(KEY_LENGTH)).decode('utf-8'),
) )
return output return output
@ -402,6 +413,11 @@ class LemurServer(Command):
from gunicorn.app.wsgiapp import WSGIApplication from gunicorn.app.wsgiapp import WSGIApplication
app = 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')) app.app_uri = 'lemur:create_app(config="{0}")'.format(kwargs.get('config'))
return app.run() return app.run()
@ -417,6 +433,7 @@ def create_config(config_path=None):
config_path = os.path.expanduser(config_path) config_path = os.path.expanduser(config_path)
dir = os.path.dirname(config_path) dir = os.path.dirname(config_path)
if not os.path.exists(dir): if not os.path.exists(dir):
os.makedirs(dir) os.makedirs(dir)