2015-05-14 20:47:38 +00:00
|
|
|
""" Superuser API. """
|
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
import string
|
2014-04-10 04:26:55 +00:00
|
|
|
import logging
|
|
|
|
import json
|
2014-12-23 16:40:51 +00:00
|
|
|
import os
|
2014-04-10 04:26:55 +00:00
|
|
|
|
2014-10-01 17:55:09 +00:00
|
|
|
from random import SystemRandom
|
2015-01-20 17:43:11 +00:00
|
|
|
from app import app, avatar, superusers
|
2014-04-10 04:26:55 +00:00
|
|
|
from flask import request
|
|
|
|
|
|
|
|
from endpoints.api import (ApiResource, nickname, resource, validate_json_request, request_error,
|
|
|
|
log_action, internal_only, NotFound, require_user_admin, format_date,
|
|
|
|
InvalidToken, require_scope, format_date, hide_if, show_if, parse_args,
|
2015-01-08 17:53:36 +00:00
|
|
|
query_param, abort, require_fresh_login, path_param, verify_not_prod)
|
2014-04-10 04:26:55 +00:00
|
|
|
|
|
|
|
from endpoints.api.logs import get_logs
|
|
|
|
|
|
|
|
from data import model
|
|
|
|
from auth.permissions import SuperUserPermission
|
|
|
|
from auth.auth_context import get_authenticated_user
|
2015-06-28 08:22:34 +00:00
|
|
|
from auth import scopes
|
2014-10-01 17:55:09 +00:00
|
|
|
from util.useremails import send_confirmation_email, send_recovery_email
|
2014-04-10 04:26:55 +00:00
|
|
|
|
|
|
|
import features
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
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))]
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
try:
|
2014-12-23 19:01:00 +00:00
|
|
|
with open(app.config['SYSTEM_SERVICE_LOGS_PATH'] % service, 'r') as f:
|
2014-12-23 16:40:51 +00:00
|
|
|
logs = f.read()
|
|
|
|
except Exception as ex:
|
|
|
|
logger.exception('Cannot read logs')
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'logs': logs
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
|
|
|
@parse_args
|
|
|
|
@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('performer', 'Username for which to filter logs.', type=str)
|
2015-06-28 08:22:34 +00:00
|
|
|
@require_scope(scopes.SUPERUSER)
|
2014-04-10 04:26:55 +00:00
|
|
|
def get(self, args):
|
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():
|
|
|
|
performer_name = args['performer']
|
|
|
|
start_time = args['starttime']
|
|
|
|
end_time = args['endtime']
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
return get_logs(start_time, end_time)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2015-05-11 22:03:25 +00:00
|
|
|
def org_view(org):
|
|
|
|
return {
|
|
|
|
'name': org.username,
|
|
|
|
'avatar': avatar.get_data_for_org(org),
|
|
|
|
}
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
def user_view(user):
|
|
|
|
return {
|
|
|
|
'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),
|
|
|
|
'enabled': user.enabled
|
2014-04-10 04:26:55 +00:00
|
|
|
}
|
|
|
|
|
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-06-28 05:27:39 +00:00
|
|
|
with open ('CHANGELOG.md', 'r') as f:
|
|
|
|
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():
|
|
|
|
orgs = model.get_organizations()
|
|
|
|
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():
|
|
|
|
users = model.get_active_users()
|
|
|
|
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. """
|
|
|
|
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()
|
|
|
|
password = ''.join([random.choice(string.ascii_uppercase + string.digits) for _ in range(32)])
|
|
|
|
|
|
|
|
# Create the user.
|
|
|
|
user = model.create_user(username, password, email, auto_verify=not features.MAILING)
|
|
|
|
|
|
|
|
# If mailing is turned on, send the user a verification email.
|
|
|
|
if features.MAILING:
|
2015-03-03 19:26:48 +00:00
|
|
|
confirmation = model.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,
|
|
|
|
'password': password
|
|
|
|
}
|
|
|
|
|
|
|
|
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):
|
|
|
|
if SuperUserPermission().can():
|
2015-05-26 21:47:33 +00:00
|
|
|
user = model.get_nonrobot_user(username)
|
|
|
|
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):
|
2014-10-01 17:55:09 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
code = model.create_reset_password_email_code(user.email)
|
|
|
|
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-05-26 21:47:33 +00:00
|
|
|
user = model.get_nonrobot_user(username)
|
|
|
|
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-05-26 21:47:33 +00:00
|
|
|
user = model.get_nonrobot_user(username)
|
|
|
|
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):
|
2014-04-10 04:26:55 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
model.delete_user(user)
|
|
|
|
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-05-26 21:47:33 +00:00
|
|
|
user = model.get_nonrobot_user(username)
|
|
|
|
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):
|
2014-04-10 04:26:55 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
user_data = request.get_json()
|
|
|
|
if 'password' in user_data:
|
|
|
|
model.change_password(user, user_data['password'])
|
|
|
|
|
|
|
|
if 'email' in user_data:
|
2015-05-11 18:38:10 +00:00
|
|
|
model.update_email(user, user_data['email'], auto_verify=True)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2015-05-11 21:13:42 +00:00
|
|
|
if 'enabled' in user_data:
|
|
|
|
# Disable/enable the user.
|
|
|
|
user.enabled = bool(user_data['enabled'])
|
|
|
|
user.save()
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
return user_view(user)
|
|
|
|
|
|
|
|
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():
|
|
|
|
org = model.get_organization(name)
|
|
|
|
|
|
|
|
model.delete_user(org)
|
|
|
|
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():
|
|
|
|
org = model.get_organization(name)
|
|
|
|
org_data = request.get_json()
|
|
|
|
|
|
|
|
if 'name' in org_data:
|
|
|
|
org = model.change_username(org.id, org_data['name'])
|
|
|
|
|
|
|
|
return org_view(org)
|
|
|
|
|
|
|
|
abort(403)
|