2015-05-14 20:47:38 +00:00
|
|
|
""" Superuser Config API. """
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2015-01-23 22:19:15 +00:00
|
|
|
import signal
|
2015-01-04 19:38:41 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import abort
|
2015-01-09 21:23:31 +00:00
|
|
|
from endpoints.api import (ApiResource, nickname, resource, internal_only, show_if,
|
2015-01-08 17:53:36 +00:00
|
|
|
require_fresh_login, request, validate_json_request, verify_not_prod)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
from endpoints.common import common_login
|
2015-01-20 17:43:11 +00:00
|
|
|
from app import app, CONFIG_PROVIDER, superusers
|
2015-01-04 19:38:41 +00:00
|
|
|
from data import model
|
2015-01-23 22:19:15 +00:00
|
|
|
from data.database import configure
|
2015-01-04 19:38:41 +00:00
|
|
|
from auth.permissions import SuperUserPermission
|
2015-07-20 17:18:07 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2015-01-07 21:20:51 +00:00
|
|
|
from data.database import User
|
2015-01-09 21:23:31 +00:00
|
|
|
from util.config.configutil import add_enterprise_config_defaults
|
2015-06-02 22:19:22 +00:00
|
|
|
from util.config.validator import validate_service_for_config, CONFIG_FILENAMES
|
2015-01-23 22:19:15 +00:00
|
|
|
from data.runmigration import run_alembic_migration
|
2015-07-20 17:18:07 +00:00
|
|
|
from data.users import get_federated_service_name
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
import features
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
def database_is_valid():
|
2015-01-08 17:53:36 +00:00
|
|
|
""" Returns whether the database, as configured, is valid. """
|
2015-01-23 22:19:15 +00:00
|
|
|
if app.config['TESTING']:
|
|
|
|
return False
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
try:
|
2015-01-23 22:19:15 +00:00
|
|
|
list(User.select().limit(1))
|
2015-01-04 19:38:41 +00:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
def database_has_users():
|
2015-01-08 17:53:36 +00:00
|
|
|
""" Returns whether the database has any users defined. """
|
2015-01-04 19:38:41 +00:00
|
|
|
return bool(list(User.select().limit(1)))
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/registrystatus')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserRegistryStatus(ApiResource):
|
|
|
|
""" Resource for determining the status of the registry, such as if config exists,
|
|
|
|
if a database is configured, and if it has any defined users.
|
|
|
|
"""
|
|
|
|
@nickname('scRegistryStatus')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
def get(self):
|
2015-01-23 22:19:15 +00:00
|
|
|
""" Returns the status of the registry. """
|
2015-02-17 16:31:50 +00:00
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
# If there is no conf/stack volume, then report that status.
|
|
|
|
if not CONFIG_PROVIDER.volume_exists():
|
|
|
|
return {
|
|
|
|
'status': 'missing-config-dir'
|
|
|
|
}
|
|
|
|
|
|
|
|
# If there is no config file, we need to setup the database.
|
|
|
|
if not CONFIG_PROVIDER.yaml_exists():
|
|
|
|
return {
|
|
|
|
'status': 'config-db'
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the database isn't yet valid, then we need to set it up.
|
|
|
|
if not database_is_valid():
|
|
|
|
return {
|
|
|
|
'status': 'setup-db'
|
|
|
|
}
|
|
|
|
|
|
|
|
# If we have SETUP_COMPLETE, then we're ready to go!
|
|
|
|
if app.config.get('SETUP_COMPLETE', False):
|
|
|
|
return {
|
|
|
|
'requires_restart': CONFIG_PROVIDER.requires_restart(app.config),
|
|
|
|
'status': 'ready'
|
|
|
|
}
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
return {
|
2015-01-23 22:19:15 +00:00
|
|
|
'status': 'create-superuser' if not database_has_users() else 'config'
|
2015-01-04 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-23 22:19:15 +00:00
|
|
|
class _AlembicLogHandler(logging.Handler):
|
|
|
|
def __init__(self):
|
|
|
|
super(_AlembicLogHandler, self).__init__()
|
|
|
|
self.records = []
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
self.records.append({
|
|
|
|
'level': record.levelname,
|
|
|
|
'message': record.getMessage()
|
|
|
|
})
|
|
|
|
|
|
|
|
@resource('/v1/superuser/setupdb')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserSetupDatabase(ApiResource):
|
|
|
|
""" Resource for invoking alembic to setup the database. """
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('scSetupDatabase')
|
|
|
|
def get(self):
|
|
|
|
""" Invokes the alembic upgrade process. """
|
|
|
|
# Note: This method is called after the database configured is saved, but before the
|
|
|
|
# database has any tables. Therefore, we only allow it to be run in that unique case.
|
|
|
|
if CONFIG_PROVIDER.yaml_exists() and not database_is_valid():
|
|
|
|
# Note: We need to reconfigure the database here as the config has changed.
|
|
|
|
combined = dict(**app.config)
|
|
|
|
combined.update(CONFIG_PROVIDER.get_yaml())
|
|
|
|
|
|
|
|
configure(combined)
|
|
|
|
app.config['DB_URI'] = combined['DB_URI']
|
|
|
|
|
|
|
|
log_handler = _AlembicLogHandler()
|
|
|
|
|
|
|
|
try:
|
|
|
|
run_alembic_migration(log_handler)
|
|
|
|
except Exception as ex:
|
|
|
|
return {
|
|
|
|
'error': str(ex)
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'logs': log_handler.records
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/shutdown')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserShutdown(ApiResource):
|
|
|
|
""" Resource for sending a shutdown signal to the container. """
|
|
|
|
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('scShutdownContainer')
|
|
|
|
def post(self):
|
|
|
|
""" Sends a signal to the phusion init system to shut down the container. """
|
|
|
|
# Note: This method is called to set the database configuration before super users exists,
|
|
|
|
# so we also allow it to be called if there is no valid registry configuration setup.
|
|
|
|
if app.config['TESTING'] or not database_has_users() or SuperUserPermission().can():
|
|
|
|
# Note: We skip if debugging locally.
|
|
|
|
if app.config.get('DEBUGGING') == True:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
os.kill(1, signal.SIGINT)
|
|
|
|
return {}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
@resource('/v1/superuser/config')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
2015-01-07 21:42:09 +00:00
|
|
|
class SuperUserConfig(ApiResource):
|
2015-01-04 19:38:41 +00:00
|
|
|
""" Resource for fetching and updating the current configuration, if any. """
|
|
|
|
schemas = {
|
|
|
|
'UpdateConfig': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Updates the YAML config file',
|
|
|
|
'required': [
|
2015-01-05 18:01:32 +00:00
|
|
|
'config',
|
|
|
|
'hostname'
|
2015-01-04 19:38:41 +00:00
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'config': {
|
|
|
|
'type': 'object'
|
2015-01-05 18:01:32 +00:00
|
|
|
},
|
|
|
|
'hostname': {
|
|
|
|
'type': 'string'
|
2015-01-04 19:38:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
@nickname('scGetConfig')
|
|
|
|
def get(self):
|
|
|
|
""" Returns the currently defined configuration, if any. """
|
|
|
|
if SuperUserPermission().can():
|
2015-01-09 21:23:31 +00:00
|
|
|
config_object = CONFIG_PROVIDER.get_yaml()
|
2015-01-04 19:38:41 +00:00
|
|
|
return {
|
|
|
|
'config': config_object
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
@nickname('scUpdateConfig')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
@validate_json_request('UpdateConfig')
|
|
|
|
def put(self):
|
|
|
|
""" Updates the config.yaml file. """
|
|
|
|
# Note: This method is called to set the database configuration before super users exists,
|
|
|
|
# so we also allow it to be called if there is no valid registry configuration setup.
|
2015-01-09 21:23:31 +00:00
|
|
|
if not CONFIG_PROVIDER.yaml_exists() or SuperUserPermission().can():
|
2015-01-04 19:38:41 +00:00
|
|
|
config_object = request.get_json()['config']
|
2015-01-05 18:01:32 +00:00
|
|
|
hostname = request.get_json()['hostname']
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
# Add any enterprise defaults missing from the config.
|
2015-01-05 18:01:32 +00:00
|
|
|
add_enterprise_config_defaults(config_object, app.config['SECRET_KEY'], hostname)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
# Write the configuration changes to the YAML file.
|
2015-01-09 21:23:31 +00:00
|
|
|
CONFIG_PROVIDER.save_yaml(config_object)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
2015-07-20 17:18:07 +00:00
|
|
|
# If the authentication system is not the database, link the superuser account to the
|
|
|
|
# the authentication system chosen.
|
|
|
|
if config_object.get('AUTHENTICATION_TYPE', 'Database') != 'Database':
|
|
|
|
service_name = get_federated_service_name(config_object['AUTHENTICATION_TYPE'])
|
|
|
|
current_user = get_authenticated_user()
|
|
|
|
model.user.confirm_attached_federated_login(current_user, service_name)
|
|
|
|
|
2015-01-04 19:38:41 +00:00
|
|
|
return {
|
|
|
|
'exists': True,
|
|
|
|
'config': config_object
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/config/file/<filename>')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserConfigFile(ApiResource):
|
|
|
|
""" Resource for fetching the status of config files and overriding them. """
|
|
|
|
@nickname('scConfigFileExists')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
def get(self, filename):
|
|
|
|
""" Returns whether the configuration file with the given name exists. """
|
2015-06-02 22:19:22 +00:00
|
|
|
if not filename in CONFIG_FILENAMES:
|
2015-01-04 19:38:41 +00:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
return {
|
2015-01-09 21:23:31 +00:00
|
|
|
'exists': CONFIG_PROVIDER.volume_file_exists(filename)
|
2015-01-04 19:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
@nickname('scUpdateConfigFile')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
def post(self, filename):
|
|
|
|
""" Updates the configuration file with the given name. """
|
2015-06-02 22:19:22 +00:00
|
|
|
if not filename in CONFIG_FILENAMES:
|
2015-01-04 19:38:41 +00:00
|
|
|
abort(404)
|
|
|
|
|
2015-06-29 05:08:10 +00:00
|
|
|
# Note: This method can be called before the configuration exists
|
|
|
|
# to upload the database SSL cert.
|
|
|
|
if not CONFIG_PROVIDER.yaml_exists() or SuperUserPermission().can():
|
2015-01-04 19:38:41 +00:00
|
|
|
uploaded_file = request.files['file']
|
|
|
|
if not uploaded_file:
|
2015-01-08 17:53:36 +00:00
|
|
|
abort(400)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
2015-01-09 21:23:31 +00:00
|
|
|
CONFIG_PROVIDER.save_volume_file(filename, uploaded_file)
|
2015-01-04 19:38:41 +00:00
|
|
|
return {
|
|
|
|
'status': True
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/config/createsuperuser')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserCreateInitialSuperUser(ApiResource):
|
|
|
|
""" Resource for creating the initial super user. """
|
|
|
|
schemas = {
|
|
|
|
'CreateSuperUser': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information for creating the initial super user',
|
|
|
|
'required': [
|
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'email'
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'username': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The username for the superuser'
|
|
|
|
},
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The password for the superuser'
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The e-mail address for the superuser'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('scCreateInitialSuperuser')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
@validate_json_request('CreateSuperUser')
|
|
|
|
def post(self):
|
|
|
|
""" Creates the initial super user, updates the underlying configuration and
|
|
|
|
sets the current session to have that super user. """
|
|
|
|
|
|
|
|
# Special security check: This method is only accessible when:
|
|
|
|
# - There is a valid config YAML file.
|
|
|
|
# - There are currently no users in the database (clean install)
|
|
|
|
#
|
|
|
|
# We do this special security check because at the point this method is called, the database
|
|
|
|
# is clean but does not (yet) have any super users for our permissions code to check against.
|
2015-01-09 21:23:31 +00:00
|
|
|
if CONFIG_PROVIDER.yaml_exists() and not database_has_users():
|
2015-01-04 19:38:41 +00:00
|
|
|
data = request.get_json()
|
|
|
|
username = data['username']
|
|
|
|
password = data['password']
|
|
|
|
email = data['email']
|
|
|
|
|
|
|
|
# Create the user in the database.
|
2015-07-15 21:25:41 +00:00
|
|
|
superuser = model.user.create_user(username, password, email, auto_verify=True)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
# Add the user to the config.
|
2015-01-09 21:23:31 +00:00
|
|
|
config_object = CONFIG_PROVIDER.get_yaml()
|
|
|
|
config_object['SUPER_USERS'] = [username]
|
|
|
|
CONFIG_PROVIDER.save_yaml(config_object)
|
|
|
|
|
|
|
|
# Update the in-memory config for the new superuser.
|
2015-01-20 17:43:11 +00:00
|
|
|
superusers.register_superuser(username)
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
# Conduct login with that user.
|
|
|
|
common_login(superuser)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': True
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/config/validate/<service>')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserConfigValidate(ApiResource):
|
|
|
|
""" Resource for validating a block of configuration against an external service. """
|
|
|
|
schemas = {
|
|
|
|
'ValidateConfig': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Validates configuration',
|
|
|
|
'required': [
|
|
|
|
'config'
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'config': {
|
|
|
|
'type': 'object'
|
2015-06-22 22:17:37 +00:00
|
|
|
},
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The users password, used for auth validation'
|
2015-01-04 19:38:41 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('scValidateConfig')
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-01-04 19:38:41 +00:00
|
|
|
@validate_json_request('ValidateConfig')
|
|
|
|
def post(self, service):
|
|
|
|
""" Validates the given config for the given service. """
|
|
|
|
# Note: This method is called to validate the database configuration before super users exists,
|
|
|
|
# so we also allow it to be called if there is no valid registry configuration setup. Note that
|
|
|
|
# this is also safe since this method does not access any information not given in the request.
|
2015-01-09 21:23:31 +00:00
|
|
|
if not CONFIG_PROVIDER.yaml_exists() or SuperUserPermission().can():
|
2015-01-04 19:38:41 +00:00
|
|
|
config = request.get_json()['config']
|
2015-06-22 22:17:37 +00:00
|
|
|
return validate_service_for_config(service, config, request.get_json().get('password', ''))
|
2015-01-04 19:38:41 +00:00
|
|
|
|
|
|
|
abort(403)
|