2015-05-14 20:47:38 +00:00
|
|
|
""" Superuser API. """
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
import logging
|
2014-12-23 16:40:51 +00:00
|
|
|
import os
|
2016-03-28 18:52:20 +00:00
|
|
|
import string
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
from datetime import datetime
|
2014-10-01 17:55:09 +00:00
|
|
|
from random import SystemRandom
|
2016-03-25 22:44:11 +00:00
|
|
|
|
|
|
|
from flask import request, make_response, jsonify
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
import features
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2016-10-18 22:47:51 +00:00
|
|
|
from app import app, avatar, superusers, authentication, config_provider, license_validator
|
2016-03-23 22:16:03 +00:00
|
|
|
from auth import scopes
|
|
|
|
from auth.auth_context import get_authenticated_user
|
|
|
|
from auth.permissions import SuperUserPermission
|
2015-07-15 21:25:41 +00:00
|
|
|
from endpoints.api import (ApiResource, nickname, resource, validate_json_request,
|
|
|
|
internal_only, require_scope, show_if, parse_args,
|
2015-12-22 14:05:17 +00:00
|
|
|
query_param, abort, require_fresh_login, path_param, verify_not_prod,
|
2016-10-11 19:16:28 +00:00
|
|
|
page_support, log_action, InvalidRequest)
|
2015-09-04 20:48:32 +00:00
|
|
|
from endpoints.api.logs import get_logs, get_aggregate_logs
|
2014-04-10 04:26:55 +00:00
|
|
|
from data import model
|
2016-03-28 18:52:20 +00:00
|
|
|
from data.database import ServiceKeyApprovalType
|
2014-10-01 17:55:09 +00:00
|
|
|
from util.useremails import send_confirmation_email, send_recovery_email
|
2016-10-18 22:47:51 +00:00
|
|
|
from util.license import decode_license, LicenseDecodeError
|
2014-04-10 04:26:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2014-12-23 16:40:51 +00:00
|
|
|
def get_immediate_subdirectories(directory):
|
|
|
|
return [name for name in os.listdir(directory) if os.path.isdir(os.path.join(directory, name))]
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2014-12-23 19:01:00 +00:00
|
|
|
def get_services():
|
|
|
|
services = set(get_immediate_subdirectories(app.config['SYSTEM_SERVICES_PATH']))
|
|
|
|
services = services - set(app.config['SYSTEM_SERVICE_BLACKLIST'])
|
|
|
|
return services
|
|
|
|
|
2014-12-23 16:40:51 +00:00
|
|
|
|
|
|
|
@resource('/v1/superuser/systemlogs/<service>')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserGetLogsForService(ApiResource):
|
|
|
|
""" Resource for fetching the kinds of system logs in the system. """
|
2014-12-23 19:25:04 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-12-23 16:40:51 +00:00
|
|
|
@nickname('getSystemLogs')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-12-23 16:40:51 +00:00
|
|
|
def get(self, service):
|
|
|
|
""" Returns the logs for the specific service. """
|
|
|
|
if SuperUserPermission().can():
|
2014-12-23 19:01:00 +00:00
|
|
|
if not service in get_services():
|
2014-12-23 16:40:51 +00:00
|
|
|
abort(404)
|
|
|
|
|
2015-07-13 09:45:08 +00:00
|
|
|
logs = []
|
2014-12-23 16:40:51 +00:00
|
|
|
try:
|
2015-06-16 00:55:23 +00:00
|
|
|
with open(app.config['SYSTEM_LOGS_FILE'], 'r') as f:
|
2015-07-13 09:45:08 +00:00
|
|
|
logs = [line for line in f if line.find(service + '[') >= 0]
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
except Exception:
|
2014-12-23 16:40:51 +00:00
|
|
|
logger.exception('Cannot read logs')
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
return {
|
2015-07-13 09:45:08 +00:00
|
|
|
'logs': '\n'.join(logs)
|
2014-12-23 16:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/systemlogs/')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserSystemLogServices(ApiResource):
|
|
|
|
""" Resource for fetching the kinds of system logs in the system. """
|
2014-12-23 19:25:04 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-12-23 16:40:51 +00:00
|
|
|
@nickname('listSystemLogServices')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-12-23 16:40:51 +00:00
|
|
|
def get(self):
|
|
|
|
""" List the system logs for the current system. """
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
return {
|
2014-12-23 19:01:00 +00:00
|
|
|
'services': list(get_services())
|
2014-12-23 16:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2015-09-04 20:48:32 +00:00
|
|
|
@resource('/v1/superuser/aggregatelogs')
|
|
|
|
@internal_only
|
|
|
|
class SuperUserAggregateLogs(ApiResource):
|
|
|
|
""" Resource for fetching aggregated logs for the current user. """
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('listAllAggregateLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-09-04 20:48:32 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs. (%m/%d/%Y %Z)', type=str)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, parsed_args):
|
2015-09-04 20:48:32 +00:00
|
|
|
""" Returns the aggregated logs for the current system. """
|
|
|
|
if SuperUserPermission().can():
|
2016-01-26 21:27:36 +00:00
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2015-09-04 20:48:32 +00:00
|
|
|
|
|
|
|
return get_aggregate_logs(start_time, end_time)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2014-12-23 16:40:51 +00:00
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
@resource('/v1/superuser/logs')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserLogs(ApiResource):
|
|
|
|
""" Resource for fetching all logs in the system. """
|
2014-12-23 19:25:04 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-04-10 04:26:55 +00:00
|
|
|
@nickname('listAllLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-09-04 20:48:32 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('page', 'The page number for the logs', type=int, default=1)
|
2016-01-26 21:27:36 +00:00
|
|
|
@page_support()
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, parsed_args, page_token):
|
2014-12-23 16:40:51 +00:00
|
|
|
""" List the usage logs for the current system. """
|
2014-04-10 04:26:55 +00:00
|
|
|
if SuperUserPermission().can():
|
2016-01-26 21:27:36 +00:00
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2015-12-22 14:05:17 +00:00
|
|
|
return get_logs(start_time, end_time, page_token=page_token)
|
2014-04-10 04:26:55 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2015-05-11 22:03:25 +00:00
|
|
|
def org_view(org):
|
|
|
|
return {
|
|
|
|
'name': org.username,
|
2015-10-16 19:17:51 +00:00
|
|
|
'email': org.email,
|
2015-05-11 22:03:25 +00:00
|
|
|
'avatar': avatar.get_data_for_org(org),
|
|
|
|
}
|
|
|
|
|
2015-06-30 16:42:19 +00:00
|
|
|
def user_view(user, password=None):
|
|
|
|
user_data = {
|
2016-04-01 17:55:29 +00:00
|
|
|
'kind': 'user',
|
|
|
|
'name': user.username,
|
2014-04-10 04:26:55 +00:00
|
|
|
'username': user.username,
|
|
|
|
'email': user.email,
|
|
|
|
'verified': user.verified,
|
2015-03-30 21:55:04 +00:00
|
|
|
'avatar': avatar.get_data_for_user(user),
|
2015-05-11 21:13:42 +00:00
|
|
|
'super_user': superusers.is_superuser(user.username),
|
2015-06-30 16:42:19 +00:00
|
|
|
'enabled': user.enabled,
|
2014-04-10 04:26:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-30 16:42:19 +00:00
|
|
|
if password is not None:
|
|
|
|
user_data['encrypted_password'] = authentication.encrypt_user_password(password)
|
|
|
|
|
|
|
|
return user_data
|
|
|
|
|
2015-06-28 05:27:39 +00:00
|
|
|
@resource('/v1/superuser/changelog/')
|
2014-10-28 20:33:13 +00:00
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
2015-06-28 05:27:39 +00:00
|
|
|
class ChangeLog(ApiResource):
|
|
|
|
""" Resource for returning the change log for enterprise customers. """
|
2014-10-28 20:33:13 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2015-06-28 05:27:39 +00:00
|
|
|
@nickname('getChangeLog')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-10-28 20:33:13 +00:00
|
|
|
def get(self):
|
2015-06-28 05:27:39 +00:00
|
|
|
""" Returns the change log for this installation. """
|
2014-10-28 20:33:13 +00:00
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
with open('CHANGELOG.md', 'r') as f:
|
2015-06-28 05:27:39 +00:00
|
|
|
return {
|
|
|
|
'log': f.read()
|
|
|
|
}
|
2014-10-28 20:33:13 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-05-11 22:03:25 +00:00
|
|
|
@resource('/v1/superuser/organizations/')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserOrganizationList(ApiResource):
|
|
|
|
""" Resource for listing organizations in the system. """
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('listAllOrganizations')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2015-05-11 22:03:25 +00:00
|
|
|
def get(self):
|
|
|
|
""" Returns a list of all organizations in the system. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
orgs = model.organization.get_organizations()
|
2015-05-11 22:03:25 +00:00
|
|
|
return {
|
|
|
|
'organizations': [org_view(org) for org in orgs]
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
@resource('/v1/superuser/users/')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserList(ApiResource):
|
|
|
|
""" Resource for listing users in the system. """
|
2014-10-01 17:55:09 +00:00
|
|
|
schemas = {
|
|
|
|
'CreateInstallUser': {
|
|
|
|
'id': 'CreateInstallUser',
|
|
|
|
'description': 'Data for creating a user',
|
|
|
|
'required': ['username', 'email'],
|
|
|
|
'properties': {
|
|
|
|
'username': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The username of the user being created'
|
|
|
|
},
|
|
|
|
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The email address of the user being created'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-04-10 04:26:55 +00:00
|
|
|
@nickname('listAllUsers')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-04-10 04:26:55 +00:00
|
|
|
def get(self):
|
|
|
|
""" Returns a list of all users in the system. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
users = model.user.get_active_users()
|
2014-04-10 04:26:55 +00:00
|
|
|
return {
|
|
|
|
'users': [user_view(user) for user in users]
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-10-01 17:55:09 +00:00
|
|
|
@nickname('createInstallUser')
|
|
|
|
@validate_json_request('CreateInstallUser')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-10-01 17:55:09 +00:00
|
|
|
def post(self):
|
|
|
|
""" Creates a new user. """
|
2015-10-16 19:14:49 +00:00
|
|
|
# Ensure that we are using database auth.
|
|
|
|
if app.config['AUTHENTICATION_TYPE'] != 'Database':
|
|
|
|
abort(400)
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
user_information = request.get_json()
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
username = user_information['username']
|
|
|
|
email = user_information['email']
|
|
|
|
|
|
|
|
# Generate a temporary password for the user.
|
|
|
|
random = SystemRandom()
|
2015-07-15 21:25:41 +00:00
|
|
|
password = ''.join([random.choice(string.ascii_uppercase + string.digits) for _ in range(32)])
|
2014-10-01 17:55:09 +00:00
|
|
|
|
|
|
|
# Create the user.
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.create_user(username, password, email, auto_verify=not features.MAILING)
|
2014-10-01 17:55:09 +00:00
|
|
|
|
|
|
|
# If mailing is turned on, send the user a verification email.
|
|
|
|
if features.MAILING:
|
2015-07-15 21:25:41 +00:00
|
|
|
confirmation = model.user.create_confirm_email_code(user)
|
2014-10-01 17:55:09 +00:00
|
|
|
send_confirmation_email(user.username, user.email, confirmation.code)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'username': username,
|
|
|
|
'email': email,
|
2015-06-30 16:42:19 +00:00
|
|
|
'password': password,
|
|
|
|
'encrypted_password': authentication.encrypt_user_password(password),
|
2014-10-01 17:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superusers/users/<username>/sendrecovery')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
@show_if(features.MAILING)
|
|
|
|
class SuperUserSendRecoveryEmail(ApiResource):
|
|
|
|
""" Resource for sending a recovery user on behalf of a user. """
|
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-10-01 17:55:09 +00:00
|
|
|
@nickname('sendInstallUserRecoveryEmail')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-10-01 17:55:09 +00:00
|
|
|
def post(self, username):
|
2015-10-16 19:14:49 +00:00
|
|
|
# Ensure that we are using database auth.
|
|
|
|
if app.config['AUTHENTICATION_TYPE'] != 'Database':
|
|
|
|
abort(400)
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_nonrobot_user(username)
|
2015-05-26 21:47:33 +00:00
|
|
|
if not user:
|
2014-10-01 17:55:09 +00:00
|
|
|
abort(404)
|
|
|
|
|
2015-01-20 17:43:11 +00:00
|
|
|
if superusers.is_superuser(username):
|
2015-07-15 21:25:41 +00:00
|
|
|
abort(403)
|
2014-10-01 17:55:09 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
code = model.user.create_reset_password_email_code(user.email)
|
2014-10-01 17:55:09 +00:00
|
|
|
send_recovery_email(user.email, code.code)
|
|
|
|
return {
|
|
|
|
'email': user.email
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
@resource('/v1/superuser/users/<username>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('username', 'The username of the user being managed')
|
2014-04-10 04:26:55 +00:00
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserManagement(ApiResource):
|
|
|
|
""" Resource for managing users in the system. """
|
|
|
|
schemas = {
|
|
|
|
'UpdateUser': {
|
|
|
|
'id': 'UpdateUser',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Description of updates for a user',
|
|
|
|
'properties': {
|
|
|
|
'password': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The new password for the user',
|
|
|
|
},
|
|
|
|
'email': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The new e-mail address for the user',
|
2015-05-11 22:03:25 +00:00
|
|
|
},
|
|
|
|
'enabled': {
|
|
|
|
'type': 'boolean',
|
|
|
|
'description': 'Whether the user is enabled'
|
2014-04-10 04:26:55 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-04-10 04:26:55 +00:00
|
|
|
@nickname('getInstallUser')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-04-10 04:26:55 +00:00
|
|
|
def get(self, username):
|
|
|
|
""" Returns information about the specified user. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_nonrobot_user(username)
|
2015-05-26 21:47:33 +00:00
|
|
|
if not user:
|
2014-10-01 17:55:09 +00:00
|
|
|
abort(404)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
return user_view(user)
|
2014-04-10 04:26:55 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-04-10 04:26:55 +00:00
|
|
|
@nickname('deleteInstallUser')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-04-10 04:26:55 +00:00
|
|
|
def delete(self, username):
|
|
|
|
""" Deletes the specified user. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_nonrobot_user(username)
|
2015-05-26 21:47:33 +00:00
|
|
|
if not user:
|
2014-04-10 04:26:55 +00:00
|
|
|
abort(404)
|
|
|
|
|
2015-01-20 17:43:11 +00:00
|
|
|
if superusers.is_superuser(username):
|
2015-07-15 21:25:41 +00:00
|
|
|
abort(403)
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.delete_user(user)
|
2014-04-10 04:26:55 +00:00
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
@require_fresh_login
|
2015-01-08 17:53:36 +00:00
|
|
|
@verify_not_prod
|
2014-04-10 04:26:55 +00:00
|
|
|
@nickname('changeInstallUser')
|
|
|
|
@validate_json_request('UpdateUser')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-04-10 04:26:55 +00:00
|
|
|
def put(self, username):
|
|
|
|
""" Updates information about the specified user. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
user = model.user.get_nonrobot_user(username)
|
|
|
|
if not user:
|
|
|
|
abort(404)
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
if superusers.is_superuser(username):
|
|
|
|
abort(403)
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
user_data = request.get_json()
|
|
|
|
if 'password' in user_data:
|
2015-10-16 19:14:49 +00:00
|
|
|
# Ensure that we are using database auth.
|
|
|
|
if app.config['AUTHENTICATION_TYPE'] != 'Database':
|
|
|
|
abort(400)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.change_password(user, user_data['password'])
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
if 'email' in user_data:
|
2015-10-16 19:14:49 +00:00
|
|
|
# Ensure that we are using database auth.
|
|
|
|
if app.config['AUTHENTICATION_TYPE'] != 'Database':
|
|
|
|
abort(400)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.update_email(user, user_data['email'], auto_verify=True)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
if 'enabled' in user_data:
|
|
|
|
# Disable/enable the user.
|
|
|
|
user.enabled = bool(user_data['enabled'])
|
|
|
|
user.save()
|
2015-05-11 21:13:42 +00:00
|
|
|
|
2015-10-16 19:38:06 +00:00
|
|
|
if 'superuser' in user_data:
|
|
|
|
config_object = config_provider.get_config()
|
|
|
|
superusers_set = set(config_object['SUPER_USERS'])
|
|
|
|
|
|
|
|
if user_data['superuser']:
|
|
|
|
superusers_set.add(username)
|
|
|
|
elif username in superusers_set:
|
|
|
|
superusers_set.remove(username)
|
|
|
|
|
|
|
|
config_object['SUPER_USERS'] = list(superusers_set)
|
|
|
|
config_provider.save_config(config_object)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return user_view(user, password=user_data.get('password'))
|
2014-04-10 04:26:55 +00:00
|
|
|
|
|
|
|
abort(403)
|
2015-05-11 22:03:25 +00:00
|
|
|
|
|
|
|
|
2016-06-07 22:12:11 +00:00
|
|
|
@resource('/v1/superuser/takeownership/<namespace>')
|
|
|
|
@path_param('namespace', 'The namespace of the user or organization being managed')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserTakeOwnership(ApiResource):
|
|
|
|
""" Resource for a superuser to take ownership of a namespace. """
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('takeOwnership')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
def post(self, namespace):
|
|
|
|
""" Takes ownership of the specified organization or user. """
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
# Disallow for superusers.
|
|
|
|
if superusers.is_superuser(namespace):
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
entity = model.user.get_user_or_org(namespace)
|
|
|
|
if entity is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
authed_user = get_authenticated_user()
|
|
|
|
was_user = not entity.organization
|
|
|
|
if entity.organization:
|
|
|
|
# Add the superuser as an admin to the owners team of the org.
|
|
|
|
model.organization.add_user_as_admin(authed_user, entity)
|
|
|
|
else:
|
|
|
|
# If the entity is a user, convert it to an organization and add the current superuser
|
|
|
|
# as the admin.
|
|
|
|
model.organization.convert_user_to_organization(entity, get_authenticated_user())
|
|
|
|
|
|
|
|
# Log the change.
|
|
|
|
log_metadata = {
|
|
|
|
'entity_id': entity.id,
|
|
|
|
'namespace': namespace,
|
|
|
|
'was_user': was_user,
|
|
|
|
'superuser': authed_user.username,
|
|
|
|
}
|
|
|
|
|
|
|
|
log_action('take_ownership', authed_user.username, log_metadata)
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'namespace': namespace
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2015-05-11 22:03:25 +00:00
|
|
|
@resource('/v1/superuser/organizations/<name>')
|
|
|
|
@path_param('name', 'The name of the organizaton being managed')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserOrganizationManagement(ApiResource):
|
|
|
|
""" Resource for managing organizations in the system. """
|
|
|
|
schemas = {
|
|
|
|
'UpdateOrg': {
|
|
|
|
'id': 'UpdateOrg',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Description of updates for an organization',
|
|
|
|
'properties': {
|
|
|
|
'name': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The new name for the organization',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('deleteOrganization')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2015-05-11 22:03:25 +00:00
|
|
|
def delete(self, name):
|
|
|
|
""" Deletes the specified organization. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
org = model.organization.get_organization(name)
|
2015-05-11 22:03:25 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.delete_user(org)
|
2015-05-11 22:03:25 +00:00
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('changeOrganization')
|
|
|
|
@validate_json_request('UpdateOrg')
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2015-05-11 22:03:25 +00:00
|
|
|
def put(self, name):
|
|
|
|
""" Updates information about the specified user. """
|
|
|
|
if SuperUserPermission().can():
|
2015-07-15 21:25:41 +00:00
|
|
|
org = model.organization.get_organization(name)
|
|
|
|
org_data = request.get_json()
|
2015-05-11 22:03:25 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
if 'name' in org_data:
|
|
|
|
org = model.user.change_username(org.id, org_data['name'])
|
2015-05-11 22:03:25 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
return org_view(org)
|
2015-05-11 22:03:25 +00:00
|
|
|
|
|
|
|
abort(403)
|
2016-03-23 22:16:03 +00:00
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
|
2016-03-30 20:48:06 +00:00
|
|
|
def key_view(key):
|
|
|
|
return {
|
|
|
|
'name': key.name,
|
|
|
|
'kid': key.kid,
|
|
|
|
'service': key.service,
|
|
|
|
'jwk': key.jwk,
|
|
|
|
'metadata': key.metadata,
|
|
|
|
'created_date': key.created_date,
|
|
|
|
'expiration_date': key.expiration_date,
|
2016-04-12 23:17:19 +00:00
|
|
|
'rotation_duration': key.rotation_duration,
|
2016-03-30 20:48:06 +00:00
|
|
|
'approval': approval_view(key.approval) if key.approval is not None else None,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def approval_view(approval):
|
|
|
|
return {
|
2016-04-29 14:14:50 +00:00
|
|
|
'approver': user_view(approval.approver) if approval.approver else None,
|
2016-03-30 20:48:06 +00:00
|
|
|
'approval_type': approval.approval_type,
|
|
|
|
'approved_date': approval.approved_date,
|
|
|
|
'notes': approval.notes,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
@resource('/v1/superuser/keys')
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserServiceKeyManagement(ApiResource):
|
|
|
|
""" Resource for managing service keys."""
|
|
|
|
schemas = {
|
|
|
|
'CreateServiceKey': {
|
2016-04-01 17:55:29 +00:00
|
|
|
'id': 'CreateServiceKey',
|
2016-03-25 22:44:11 +00:00
|
|
|
'type': 'object',
|
|
|
|
'description': 'Description of creation of a service key',
|
|
|
|
'required': ['service', 'expiration'],
|
|
|
|
'properties': {
|
|
|
|
'service': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The service authenticating with this key',
|
|
|
|
},
|
|
|
|
'name': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The friendly name of a service key',
|
|
|
|
},
|
|
|
|
'metadata': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'The key/value pairs of this key\'s metadata',
|
|
|
|
},
|
2016-04-01 17:55:29 +00:00
|
|
|
'notes': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'If specified, the extra notes for the key',
|
|
|
|
},
|
2016-03-25 22:44:11 +00:00
|
|
|
'expiration': {
|
|
|
|
'description': 'The expiration date as a unix timestamp',
|
2016-04-01 17:55:29 +00:00
|
|
|
'anyOf': [{'type': 'number'}, {'type': 'null'}],
|
2016-03-25 22:44:11 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@verify_not_prod
|
2016-04-08 21:48:44 +00:00
|
|
|
@nickname('listServiceKeys')
|
2016-03-25 22:44:11 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2016-03-28 18:52:20 +00:00
|
|
|
def get(self):
|
2016-03-25 22:44:11 +00:00
|
|
|
if SuperUserPermission().can():
|
2016-04-01 17:55:29 +00:00
|
|
|
keys = model.service_keys.list_all_keys()
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'keys': [key_view(key) for key in keys],
|
|
|
|
})
|
|
|
|
|
2016-03-25 22:44:11 +00:00
|
|
|
abort(403)
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
@require_fresh_login
|
2016-03-25 22:44:11 +00:00
|
|
|
@verify_not_prod
|
|
|
|
@nickname('createServiceKey')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
2016-04-01 17:55:29 +00:00
|
|
|
@validate_json_request('CreateServiceKey')
|
2016-03-28 18:52:20 +00:00
|
|
|
def post(self):
|
2016-03-25 22:44:11 +00:00
|
|
|
if SuperUserPermission().can():
|
|
|
|
body = request.get_json()
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
# Ensure we have a valid expiration date if specified.
|
2016-03-28 18:52:20 +00:00
|
|
|
expiration_date = body.get('expiration', None)
|
2016-03-30 17:20:35 +00:00
|
|
|
if expiration_date is not None:
|
2016-03-25 22:44:11 +00:00
|
|
|
try:
|
|
|
|
expiration_date = datetime.utcfromtimestamp(float(expiration_date))
|
|
|
|
except ValueError:
|
|
|
|
abort(400)
|
|
|
|
|
2016-04-12 23:17:19 +00:00
|
|
|
if expiration_date <= datetime.now():
|
|
|
|
abort(400)
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
# Create the metadata for the key.
|
2016-03-28 18:52:20 +00:00
|
|
|
user = get_authenticated_user()
|
2016-03-25 22:44:11 +00:00
|
|
|
metadata = body.get('metadata', {})
|
|
|
|
metadata.update({
|
2016-04-01 17:55:29 +00:00
|
|
|
'created_by': 'Quay Superuser Panel',
|
2016-03-25 22:44:11 +00:00
|
|
|
'creator': user.username,
|
2016-03-28 23:00:00 +00:00
|
|
|
'ip': request.remote_addr,
|
2016-03-25 22:44:11 +00:00
|
|
|
})
|
|
|
|
|
2016-04-07 00:03:04 +00:00
|
|
|
# Generate a key with a private key that we *never save*.
|
2016-04-12 23:17:19 +00:00
|
|
|
(private_key, key) = model.service_keys.generate_service_key(body['service'], expiration_date,
|
|
|
|
metadata=metadata,
|
2016-04-07 00:03:04 +00:00
|
|
|
name=body.get('name', ''))
|
2016-04-01 17:55:29 +00:00
|
|
|
# Auto-approve the service key.
|
2016-04-07 00:03:04 +00:00
|
|
|
model.service_keys.approve_service_key(key.kid, user, ServiceKeyApprovalType.SUPERUSER,
|
2016-04-01 17:55:29 +00:00
|
|
|
notes=body.get('notes', ''))
|
|
|
|
|
|
|
|
# Log the creation and auto-approval of the service key.
|
|
|
|
key_log_metadata = {
|
2016-04-07 00:03:04 +00:00
|
|
|
'kid': key.kid,
|
2016-04-01 17:55:29 +00:00
|
|
|
'preshared': True,
|
|
|
|
'service': body['service'],
|
|
|
|
'name': body.get('name', ''),
|
|
|
|
'expiration_date': expiration_date,
|
|
|
|
'auto_approved': True,
|
|
|
|
}
|
|
|
|
|
2016-04-05 19:27:45 +00:00
|
|
|
log_action('service_key_create', None, key_log_metadata)
|
|
|
|
log_action('service_key_approve', None, key_log_metadata)
|
2016-04-01 17:55:29 +00:00
|
|
|
|
|
|
|
return jsonify({
|
2016-04-07 00:03:04 +00:00
|
|
|
'kid': key.kid,
|
2016-04-01 17:55:29 +00:00
|
|
|
'name': body.get('name', ''),
|
2016-05-02 19:29:31 +00:00
|
|
|
'service': body['service'],
|
2016-04-01 17:55:29 +00:00
|
|
|
'public_key': private_key.publickey().exportKey('PEM'),
|
|
|
|
'private_key': private_key.exportKey('PEM'),
|
|
|
|
})
|
2016-03-25 22:44:11 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/keys/<kid>')
|
2016-03-23 22:16:03 +00:00
|
|
|
@path_param('kid', 'The unique identifier for a service key')
|
|
|
|
@show_if(features.SUPER_USERS)
|
2016-04-05 19:27:45 +00:00
|
|
|
class SuperUserServiceKey(ApiResource):
|
2016-03-23 22:16:03 +00:00
|
|
|
""" Resource for managing service keys. """
|
|
|
|
schemas = {
|
2016-03-25 22:44:11 +00:00
|
|
|
'PutServiceKey': {
|
|
|
|
'id': 'PutServiceKey',
|
2016-03-23 22:16:03 +00:00
|
|
|
'type': 'object',
|
2016-03-25 22:44:11 +00:00
|
|
|
'description': 'Description of updates for a service key',
|
2016-03-23 22:16:03 +00:00
|
|
|
'properties': {
|
2016-03-25 22:44:11 +00:00
|
|
|
'name': {
|
2016-03-23 22:16:03 +00:00
|
|
|
'type': 'string',
|
2016-03-25 22:44:11 +00:00
|
|
|
'description': 'The friendly name of a service key',
|
|
|
|
},
|
|
|
|
'metadata': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'The key/value pairs of this key\'s metadata',
|
|
|
|
},
|
|
|
|
'expiration': {
|
|
|
|
'description': 'The expiration date as a unix timestamp',
|
2016-04-01 17:55:29 +00:00
|
|
|
'anyOf': [{'type': 'number'}, {'type': 'null'}],
|
2016-03-23 22:16:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-05 19:27:45 +00:00
|
|
|
@verify_not_prod
|
|
|
|
@nickname('getServiceKey')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
def get(self, kid):
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
try:
|
2016-05-03 18:01:33 +00:00
|
|
|
key = model.service_keys.get_service_key(kid, approved_only=False, alive_only=False)
|
2016-04-05 19:27:45 +00:00
|
|
|
return jsonify(key_view(key))
|
|
|
|
except model.service_keys.ServiceKeyDoesNotExist:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
@require_fresh_login
|
2016-03-25 22:44:11 +00:00
|
|
|
@verify_not_prod
|
2016-04-01 17:55:29 +00:00
|
|
|
@nickname('updateServiceKey')
|
2016-03-25 22:44:11 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
@validate_json_request('PutServiceKey')
|
|
|
|
def put(self, kid):
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
body = request.get_json()
|
2016-04-01 17:55:29 +00:00
|
|
|
try:
|
2016-05-03 18:01:33 +00:00
|
|
|
key = model.service_keys.get_service_key(kid, approved_only=False, alive_only=False)
|
2016-04-01 17:55:29 +00:00
|
|
|
except model.service_keys.ServiceKeyDoesNotExist:
|
|
|
|
abort(404)
|
2016-03-25 22:44:11 +00:00
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
key_log_metadata = {
|
|
|
|
'kid': key.kid,
|
|
|
|
'service': key.service,
|
|
|
|
'name': body.get('name', key.name),
|
|
|
|
'expiration_date': key.expiration_date,
|
|
|
|
}
|
|
|
|
|
|
|
|
if 'expiration' in body:
|
|
|
|
expiration_date = body['expiration']
|
|
|
|
if expiration_date is not None and expiration_date != '':
|
|
|
|
try:
|
|
|
|
expiration_date = datetime.utcfromtimestamp(float(expiration_date))
|
|
|
|
except ValueError:
|
|
|
|
abort(400)
|
2016-03-25 22:44:11 +00:00
|
|
|
|
2016-04-12 23:17:19 +00:00
|
|
|
if expiration_date <= datetime.now():
|
|
|
|
abort(400)
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
key_log_metadata.update({
|
|
|
|
'old_expiration_date': key.expiration_date,
|
|
|
|
'expiration_date': expiration_date,
|
|
|
|
})
|
|
|
|
|
2016-04-05 19:27:45 +00:00
|
|
|
log_action('service_key_extend', None, key_log_metadata)
|
2016-04-01 17:55:29 +00:00
|
|
|
model.service_keys.set_key_expiration(kid, expiration_date)
|
|
|
|
|
|
|
|
|
|
|
|
if 'name' in body or 'metadata' in body:
|
|
|
|
model.service_keys.update_service_key(kid, body.get('name'), body.get('metadata'))
|
2016-04-05 19:27:45 +00:00
|
|
|
log_action('service_key_modify', None, key_log_metadata)
|
2016-04-01 17:55:29 +00:00
|
|
|
|
2016-05-03 18:01:33 +00:00
|
|
|
updated_key = model.service_keys.get_service_key(kid, approved_only=False, alive_only=False)
|
|
|
|
return jsonify(key_view(updated_key))
|
2016-04-01 17:55:29 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
@require_fresh_login
|
|
|
|
@verify_not_prod
|
|
|
|
@nickname('deleteServiceKey')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
def delete(self, kid):
|
|
|
|
if SuperUserPermission().can():
|
2016-04-05 19:27:45 +00:00
|
|
|
try:
|
|
|
|
key = model.service_keys.delete_service_key(kid)
|
|
|
|
except model.service_keys.ServiceKeyDoesNotExist:
|
|
|
|
abort(404)
|
2016-04-01 17:55:29 +00:00
|
|
|
|
|
|
|
key_log_metadata = {
|
|
|
|
'kid': kid,
|
|
|
|
'service': key.service,
|
|
|
|
'name': key.name,
|
|
|
|
'created_date': key.created_date,
|
|
|
|
'expiration_date': key.expiration_date,
|
|
|
|
}
|
|
|
|
|
2016-04-05 19:27:45 +00:00
|
|
|
log_action('service_key_delete', None, key_log_metadata)
|
|
|
|
return make_response('', 204)
|
2016-03-25 22:44:11 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/approvedkeys/<kid>')
|
|
|
|
@path_param('kid', 'The unique identifier for a service key')
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserServiceKeyApproval(ApiResource):
|
|
|
|
""" Resource for approving service keys. """
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
schemas = {
|
|
|
|
'ApproveServiceKey': {
|
|
|
|
'id': 'ApproveServiceKey',
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Information for approving service keys',
|
|
|
|
'properties': {
|
|
|
|
'notes': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Optional approval notes',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_fresh_login
|
2016-03-23 22:16:03 +00:00
|
|
|
@verify_not_prod
|
|
|
|
@nickname('approveServiceKey')
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
2016-04-05 19:27:45 +00:00
|
|
|
@validate_json_request('ApproveServiceKey')
|
|
|
|
def post(self, kid):
|
2016-03-23 22:16:03 +00:00
|
|
|
if SuperUserPermission().can():
|
2016-04-01 17:55:29 +00:00
|
|
|
notes = request.get_json().get('notes', '')
|
2016-03-23 22:16:03 +00:00
|
|
|
approver = get_authenticated_user()
|
|
|
|
try:
|
2016-04-05 19:27:45 +00:00
|
|
|
key = model.service_keys.approve_service_key(kid, approver, ServiceKeyApprovalType.SUPERUSER,
|
|
|
|
notes=notes)
|
|
|
|
|
|
|
|
# Log the approval of the service key.
|
|
|
|
key_log_metadata = {
|
|
|
|
'kid': kid,
|
|
|
|
'service': key.service,
|
|
|
|
'name': key.name,
|
|
|
|
'expiration_date': key.expiration_date,
|
|
|
|
}
|
|
|
|
|
|
|
|
log_action('service_key_approve', None, key_log_metadata)
|
2016-03-23 22:16:03 +00:00
|
|
|
except model.ServiceKeyDoesNotExist:
|
|
|
|
abort(404)
|
|
|
|
except model.ServiceKeyAlreadyApproved:
|
|
|
|
pass
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
return make_response('', 201)
|
2016-03-25 22:44:11 +00:00
|
|
|
|
|
|
|
abort(403)
|
2016-10-11 19:16:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/superuser/license')
|
|
|
|
@internal_only
|
|
|
|
@show_if(features.SUPER_USERS)
|
|
|
|
class SuperUserLicense(ApiResource):
|
|
|
|
""" Resource for getting and setting a license. """
|
|
|
|
schemas = {
|
|
|
|
'UpdateLicense': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Updates a license',
|
|
|
|
'required': [
|
|
|
|
'license',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'license': {
|
|
|
|
'type': 'string'
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@nickname('getLicense')
|
|
|
|
@require_fresh_login
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
@verify_not_prod
|
|
|
|
def get(self):
|
|
|
|
""" Returns the current decoded license. """
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
try:
|
|
|
|
decoded_license = config_provider.get_license()
|
2016-10-18 22:47:51 +00:00
|
|
|
except LicenseDecodeError as le:
|
2016-10-11 19:16:28 +00:00
|
|
|
raise InvalidRequest(le.message)
|
|
|
|
|
2016-10-18 22:47:51 +00:00
|
|
|
statuses = decoded_license.validate(app.config)
|
|
|
|
all_met = all(status.is_met() for status in statuses)
|
2016-10-11 19:16:28 +00:00
|
|
|
|
|
|
|
return {
|
2016-10-19 19:35:34 +00:00
|
|
|
'status': [status.as_dict(for_private=True) for status in statuses],
|
|
|
|
'success': all_met,
|
2016-10-11 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
@nickname('updateLicense')
|
|
|
|
@require_fresh_login
|
|
|
|
@require_scope(scopes.SUPERUSER)
|
|
|
|
@verify_not_prod
|
|
|
|
@validate_json_request('UpdateLicense')
|
|
|
|
def put(self):
|
|
|
|
""" Validates the given license contents and then saves it to the config volume. """
|
|
|
|
if SuperUserPermission().can():
|
|
|
|
license_contents = request.get_json()['license']
|
|
|
|
try:
|
|
|
|
decoded_license = decode_license(license_contents)
|
2016-10-18 22:47:51 +00:00
|
|
|
except LicenseDecodeError as le:
|
2016-10-11 19:16:28 +00:00
|
|
|
raise InvalidRequest(le.message)
|
|
|
|
|
2016-10-18 22:47:51 +00:00
|
|
|
statuses = decoded_license.validate(app.config)
|
|
|
|
all_met = all(status.is_met() for status in statuses)
|
2016-10-19 19:35:34 +00:00
|
|
|
if all_met:
|
|
|
|
# Save the license and update the license check thread.
|
|
|
|
config_provider.save_license(license_contents)
|
|
|
|
license_validator.compute_license_sufficiency()
|
2016-10-18 22:47:51 +00:00
|
|
|
|
2016-10-11 19:16:28 +00:00
|
|
|
return {
|
2016-10-19 19:35:34 +00:00
|
|
|
'status': [status.as_dict(for_private=True) for status in statuses],
|
|
|
|
'success': all_met,
|
2016-10-11 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|