2015-05-14 20:47:38 +00:00
|
|
|
""" Manage user and organization robot accounts. """
|
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
from endpoints.api import (resource, nickname, ApiResource, log_action, related_user_resource,
|
2015-07-15 21:25:41 +00:00
|
|
|
Unauthorized, require_user_admin, require_scope, path_param, parse_args,
|
|
|
|
truthy_bool, query_param)
|
2014-03-14 20:02:13 +00:00
|
|
|
from auth.permissions import AdministerOrganizationPermission, OrganizationMemberPermission
|
|
|
|
from auth.auth_context import get_authenticated_user
|
2014-08-06 00:53:00 +00:00
|
|
|
from auth import scopes
|
2014-03-14 20:02:13 +00:00
|
|
|
from data import model
|
2015-04-01 17:56:30 +00:00
|
|
|
from data.database import User, Team, Repository, FederatedLogin
|
2014-03-14 20:02:13 +00:00
|
|
|
from util.names import format_robot_username
|
2015-03-31 22:50:43 +00:00
|
|
|
from flask import abort
|
2015-04-01 17:56:30 +00:00
|
|
|
from app import avatar
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2015-04-01 17:56:30 +00:00
|
|
|
def robot_view(name, token):
|
2014-03-14 20:02:13 +00:00
|
|
|
return {
|
|
|
|
'name': name,
|
2015-04-01 17:56:30 +00:00
|
|
|
'token': token
|
2015-03-31 22:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def permission_view(permission):
|
|
|
|
return {
|
|
|
|
'repository': {
|
|
|
|
'name': permission.repository.name,
|
|
|
|
'is_public': permission.repository.visibility.name == 'public'
|
|
|
|
},
|
|
|
|
'role': permission.role.name
|
2014-03-14 20:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
def robots_list(prefix, include_permissions=False):
|
2015-07-15 21:25:41 +00:00
|
|
|
tuples = model.user.list_entity_robot_permission_teams(prefix,
|
|
|
|
include_permissions=include_permissions)
|
2015-04-01 17:56:30 +00:00
|
|
|
|
|
|
|
robots = {}
|
|
|
|
robot_teams = set()
|
|
|
|
|
|
|
|
for robot_tuple in tuples:
|
|
|
|
robot_name = robot_tuple.get(User.username)
|
|
|
|
if not robot_name in robots:
|
|
|
|
robots[robot_name] = {
|
|
|
|
'name': robot_name,
|
2015-05-08 20:43:07 +00:00
|
|
|
'token': robot_tuple.get(FederatedLogin.service_ident)
|
2015-04-01 17:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
if include_permissions:
|
|
|
|
robots[robot_name].update({
|
|
|
|
'teams': [],
|
|
|
|
'repositories': []
|
|
|
|
})
|
2015-04-01 17:56:30 +00:00
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
if include_permissions:
|
|
|
|
team_name = robot_tuple.get(Team.name)
|
|
|
|
repository_name = robot_tuple.get(Repository.name)
|
2015-04-01 17:56:30 +00:00
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
if team_name is not None:
|
|
|
|
check_key = robot_name + ':' + team_name
|
|
|
|
if not check_key in robot_teams:
|
|
|
|
robot_teams.add(check_key)
|
|
|
|
|
|
|
|
robots[robot_name]['teams'].append({
|
|
|
|
'name': team_name,
|
|
|
|
'avatar': avatar.get_data(team_name, team_name, 'team')
|
|
|
|
})
|
2015-04-01 17:56:30 +00:00
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
if repository_name is not None:
|
|
|
|
if not repository_name in robots[robot_name]['repositories']:
|
|
|
|
robots[robot_name]['repositories'].append(repository_name)
|
2015-04-01 17:56:30 +00:00
|
|
|
|
|
|
|
return {'robots': robots.values()}
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/user/robots')
|
2014-03-14 20:02:13 +00:00
|
|
|
class UserRobotList(ApiResource):
|
|
|
|
""" Resource for listing user robots. """
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('getUserRobots')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-05-08 20:43:07 +00:00
|
|
|
@query_param('permissions',
|
|
|
|
'Whether to include repostories and teams in which the robots have permission.',
|
|
|
|
type=truthy_bool, default=False)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, parsed_args):
|
2014-03-14 20:02:13 +00:00
|
|
|
""" List the available robots for the user. """
|
|
|
|
user = get_authenticated_user()
|
2016-01-26 21:27:36 +00:00
|
|
|
return robots_list(user.username, include_permissions=parsed_args.get('permissions', False))
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/user/robots/<robot_shortname>')
|
2015-07-15 21:25:41 +00:00
|
|
|
@path_param('robot_shortname',
|
|
|
|
'The short name for the robot, without any user or organization prefix')
|
2014-03-14 20:02:13 +00:00
|
|
|
class UserRobot(ApiResource):
|
|
|
|
""" Resource for managing a user's robots. """
|
2014-08-25 21:19:23 +00:00
|
|
|
@require_user_admin
|
|
|
|
@nickname('getUserRobot')
|
|
|
|
def get(self, robot_shortname):
|
|
|
|
""" Returns the user's robot with the specified name. """
|
|
|
|
parent = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
robot, password = model.user.get_robot(robot_shortname, parent)
|
2014-08-25 21:19:23 +00:00
|
|
|
return robot_view(robot.username, password)
|
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('createUserRobot')
|
|
|
|
def put(self, robot_shortname):
|
|
|
|
""" Create a new user robot with the specified name. """
|
|
|
|
parent = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
robot, password = model.user.create_robot(robot_shortname, parent)
|
2014-03-14 20:02:13 +00:00
|
|
|
log_action('create_robot', parent.username, {'robot': robot_shortname})
|
2014-03-17 18:52:52 +00:00
|
|
|
return robot_view(robot.username, password), 201
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('deleteUserRobot')
|
|
|
|
def delete(self, robot_shortname):
|
|
|
|
""" Delete an existing robot. """
|
|
|
|
parent = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.delete_robot(format_robot_username(parent.username, robot_shortname))
|
2014-03-14 20:02:13 +00:00
|
|
|
log_action('delete_robot', parent.username, {'robot': robot_shortname})
|
|
|
|
return 'Deleted', 204
|
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/organization/<orgname>/robots')
|
2014-08-06 21:47:32 +00:00
|
|
|
@path_param('orgname', 'The name of the organization')
|
2014-03-14 21:35:52 +00:00
|
|
|
@related_user_resource(UserRobotList)
|
2014-03-14 20:02:13 +00:00
|
|
|
class OrgRobotList(ApiResource):
|
|
|
|
""" Resource for listing an organization's robots. """
|
2014-08-06 00:53:00 +00:00
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('getOrgRobots')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-05-08 20:43:07 +00:00
|
|
|
@query_param('permissions',
|
|
|
|
'Whether to include repostories and teams in which the robots have permission.',
|
|
|
|
type=truthy_bool, default=False)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, orgname, parsed_args):
|
2014-03-14 20:02:13 +00:00
|
|
|
""" List the organization's robots. """
|
|
|
|
permission = OrganizationMemberPermission(orgname)
|
|
|
|
if permission.can():
|
2016-01-26 21:27:36 +00:00
|
|
|
return robots_list(orgname, include_permissions=parsed_args.get('permissions', False))
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2014-03-14 20:09:16 +00:00
|
|
|
@resource('/v1/organization/<orgname>/robots/<robot_shortname>')
|
2014-08-06 21:47:32 +00:00
|
|
|
@path_param('orgname', 'The name of the organization')
|
2015-07-15 21:25:41 +00:00
|
|
|
@path_param('robot_shortname',
|
|
|
|
'The short name for the robot, without any user or organization prefix')
|
2014-03-14 21:35:52 +00:00
|
|
|
@related_user_resource(UserRobot)
|
2014-03-14 20:02:13 +00:00
|
|
|
class OrgRobot(ApiResource):
|
|
|
|
""" Resource for managing an organization's robots. """
|
2014-08-25 21:19:23 +00:00
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
|
|
|
@nickname('getOrgRobot')
|
|
|
|
def get(self, orgname, robot_shortname):
|
|
|
|
""" Returns the organization's robot with the specified name. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2015-07-15 21:25:41 +00:00
|
|
|
parent = model.organization.get_organization(orgname)
|
|
|
|
robot, password = model.user.get_robot(robot_shortname, parent)
|
2014-08-25 21:19:23 +00:00
|
|
|
return robot_view(robot.username, password)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2014-08-25 21:19:23 +00:00
|
|
|
raise Unauthorized()
|
|
|
|
|
2014-08-06 00:53:00 +00:00
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('createOrgRobot')
|
|
|
|
def put(self, orgname, robot_shortname):
|
|
|
|
""" Create a new robot in the organization. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2015-07-15 21:25:41 +00:00
|
|
|
parent = model.organization.get_organization(orgname)
|
|
|
|
robot, password = model.user.create_robot(robot_shortname, parent)
|
|
|
|
log_action('create_robot', orgname, {'robot': robot_shortname})
|
2014-03-17 19:26:16 +00:00
|
|
|
return robot_view(robot.username, password), 201
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-08-06 00:53:00 +00:00
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('deleteOrgRobot')
|
|
|
|
def delete(self, orgname, robot_shortname):
|
|
|
|
""" Delete an existing organization robot. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2015-07-15 21:25:41 +00:00
|
|
|
model.user.delete_robot(format_robot_username(orgname, robot_shortname))
|
2014-03-14 20:02:13 +00:00
|
|
|
log_action('delete_robot', orgname, {'robot': robot_shortname})
|
|
|
|
return 'Deleted', 204
|
|
|
|
|
2014-03-17 20:57:35 +00:00
|
|
|
raise Unauthorized()
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
|
2015-03-31 22:50:43 +00:00
|
|
|
@resource('/v1/user/robots/<robot_shortname>/permissions')
|
2015-07-15 21:25:41 +00:00
|
|
|
@path_param('robot_shortname',
|
|
|
|
'The short name for the robot, without any user or organization prefix')
|
2015-03-31 22:50:43 +00:00
|
|
|
class UserRobotPermissions(ApiResource):
|
|
|
|
""" Resource for listing the permissions a user's robot has in the system. """
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('getUserRobotPermissions')
|
|
|
|
def get(self, robot_shortname):
|
|
|
|
""" Returns the list of repository permissions for the user's robot. """
|
|
|
|
parent = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
robot, _ = model.user.get_robot(robot_shortname, parent)
|
|
|
|
permissions = model.permission.list_robot_permissions(robot.username)
|
2015-03-31 22:50:43 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'permissions': [permission_view(permission) for permission in permissions]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/organization/<orgname>/robots/<robot_shortname>/permissions')
|
|
|
|
@path_param('orgname', 'The name of the organization')
|
2015-07-15 21:25:41 +00:00
|
|
|
@path_param('robot_shortname',
|
|
|
|
'The short name for the robot, without any user or organization prefix')
|
2015-03-31 22:50:43 +00:00
|
|
|
@related_user_resource(UserRobotPermissions)
|
|
|
|
class OrgRobotPermissions(ApiResource):
|
|
|
|
""" Resource for listing the permissions an org's robot has in the system. """
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('getOrgRobotPermissions')
|
|
|
|
def get(self, orgname, robot_shortname):
|
|
|
|
""" Returns the list of repository permissions for the org's robot. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2015-07-15 21:25:41 +00:00
|
|
|
parent = model.organization.get_organization(orgname)
|
|
|
|
robot, _ = model.user.get_robot(robot_shortname, parent)
|
|
|
|
permissions = model.permission.list_robot_permissions(robot.username)
|
2015-03-31 22:50:43 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'permissions': [permission_view(permission) for permission in permissions]
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-08-25 21:19:23 +00:00
|
|
|
@resource('/v1/user/robots/<robot_shortname>/regenerate')
|
2015-07-15 21:25:41 +00:00
|
|
|
@path_param('robot_shortname',
|
|
|
|
'The short name for the robot, without any user or organization prefix')
|
2014-08-25 21:19:23 +00:00
|
|
|
class RegenerateUserRobot(ApiResource):
|
|
|
|
""" Resource for regenerate an organization's robot's token. """
|
|
|
|
@require_user_admin
|
|
|
|
@nickname('regenerateUserRobotToken')
|
|
|
|
def post(self, robot_shortname):
|
|
|
|
""" Regenerates the token for a user's robot. """
|
|
|
|
parent = get_authenticated_user()
|
2015-07-15 21:25:41 +00:00
|
|
|
robot, password = model.user.regenerate_robot_token(robot_shortname, parent)
|
|
|
|
log_action('regenerate_robot_token', parent.username, {'robot': robot_shortname})
|
2014-08-25 21:19:23 +00:00
|
|
|
return robot_view(robot.username, password)
|
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/organization/<orgname>/robots/<robot_shortname>/regenerate')
|
|
|
|
@path_param('orgname', 'The name of the organization')
|
2015-07-15 21:25:41 +00:00
|
|
|
@path_param('robot_shortname',
|
|
|
|
'The short name for the robot, without any user or organization prefix')
|
2014-08-25 21:19:23 +00:00
|
|
|
@related_user_resource(RegenerateUserRobot)
|
|
|
|
class RegenerateOrgRobot(ApiResource):
|
|
|
|
""" Resource for regenerate an organization's robot's token. """
|
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
|
|
|
@nickname('regenerateOrgRobotToken')
|
|
|
|
def post(self, orgname, robot_shortname):
|
|
|
|
""" Regenerates the token for an organization robot. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2015-07-15 21:25:41 +00:00
|
|
|
parent = model.organization.get_organization(orgname)
|
|
|
|
robot, password = model.user.regenerate_robot_token(robot_shortname, parent)
|
|
|
|
log_action('regenerate_robot_token', orgname, {'robot': robot_shortname})
|
2014-08-25 21:19:23 +00:00
|
|
|
return robot_view(robot.username, password)
|
|
|
|
|
|
|
|
raise Unauthorized()
|