Moving validation to server start. (#563)
This commit is contained in:
parent
e622a49b72
commit
81272a2f7a
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue