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,
|
2016-04-11 20:20:11 +00:00
|
|
|
require_user_admin, require_scope, path_param, parse_args,
|
2015-07-15 21:25:41 +00:00
|
|
|
truthy_bool, query_param)
|
2017-07-25 17:41:20 +00:00
|
|
|
from endpoints.api.robot_models_pre_oci import pre_oci_model as model
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.exception import Unauthorized
|
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 util.names import format_robot_username
|
2015-03-31 22:50:43 +00:00
|
|
|
from flask import abort
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2015-05-08 20:43:07 +00:00
|
|
|
def robots_list(prefix, include_permissions=False):
|
2017-07-25 17:41:20 +00:00
|
|
|
robots = model.list_entity_robot_permission_teams(prefix, include_permissions=include_permissions)
|
|
|
|
return {'robots': [robot.to_dict() for robot in robots]}
|
2015-04-01 17:56:30 +00:00
|
|
|
|
|
|
|
|
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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
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',
|
2017-05-22 20:59:12 +00:00
|
|
|
'Whether to include repositories and teams in which the robots have permission.',
|
2015-05-08 20:43:07 +00:00
|
|
|
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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
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()
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.get_user_robot(robot_shortname, parent)
|
|
|
|
return robot.to_dict()
|
2014-08-25 21:19:23 +00:00
|
|
|
|
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()
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.create_user_robot(robot_shortname, parent)
|
2014-03-14 20:02:13 +00:00
|
|
|
log_action('create_robot', parent.username, {'robot': robot_shortname})
|
2017-07-25 17:41:20 +00:00
|
|
|
return robot.to_dict(), 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()
|
2017-07-25 17:41:20 +00:00
|
|
|
model.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})
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
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():
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.get_org_robot(robot_shortname, orgname)
|
|
|
|
return robot.to_dict()
|
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():
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.create_org_robot(robot_shortname, orgname)
|
2015-07-15 21:25:41 +00:00
|
|
|
log_action('create_robot', orgname, {'robot': robot_shortname})
|
2017-07-25 17:41:20 +00:00
|
|
|
return robot.to_dict(), 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():
|
2017-07-25 17:41:20 +00:00
|
|
|
model.delete_robot(format_robot_username(orgname, robot_shortname))
|
2014-03-14 20:02:13 +00:00
|
|
|
log_action('delete_robot', orgname, {'robot': robot_shortname})
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2014-03-14 20:02:13 +00:00
|
|
|
|
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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
2015-03-31 22:50:43 +00:00
|
|
|
@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()
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.get_user_robot(robot_shortname, parent)
|
2017-08-01 17:47:48 +00:00
|
|
|
permissions = model.list_robot_permissions(robot.name)
|
2015-03-31 22:50:43 +00:00
|
|
|
|
|
|
|
return {
|
2017-07-25 17:41:20 +00:00
|
|
|
'permissions': [permission.to_dict() for permission in permissions]
|
2015-03-31 22:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
2015-03-31 22:50:43 +00:00
|
|
|
@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():
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.get_org_robot(robot_shortname, orgname)
|
2017-08-01 17:47:48 +00:00
|
|
|
permissions = model.list_robot_permissions(robot.name)
|
2015-03-31 22:50:43 +00:00
|
|
|
|
|
|
|
return {
|
2017-07-25 17:41:20 +00:00
|
|
|
'permissions': [permission.to_dict() for permission in permissions]
|
2015-03-31 22:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
2014-08-25 21:19:23 +00:00
|
|
|
@require_user_admin
|
|
|
|
@nickname('regenerateUserRobotToken')
|
|
|
|
def post(self, robot_shortname):
|
|
|
|
""" Regenerates the token for a user's robot. """
|
|
|
|
parent = get_authenticated_user()
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.regenerate_user_robot_token(robot_shortname, parent)
|
2015-07-15 21:25:41 +00:00
|
|
|
log_action('regenerate_robot_token', parent.username, {'robot': robot_shortname})
|
2017-07-25 17:41:20 +00:00
|
|
|
return robot.to_dict()
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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. """
|
2017-07-25 17:41:20 +00:00
|
|
|
|
2014-08-25 21:19:23 +00:00
|
|
|
@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():
|
2017-07-25 17:41:20 +00:00
|
|
|
robot = model.regenerate_org_robot_token(robot_shortname, orgname)
|
2015-07-15 21:25:41 +00:00
|
|
|
log_action('regenerate_robot_token', orgname, {'robot': robot_shortname})
|
2017-07-25 17:41:20 +00:00
|
|
|
return robot.to_dict()
|
2014-08-25 21:19:23 +00:00
|
|
|
|
|
|
|
raise Unauthorized()
|