2013-07-03 18:12:58 -05:00
|
|
|
# Copyright 2012-2013 OpenStack Foundation
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
2013-01-24 12:00:30 -06:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
2012-05-16 16:22:03 -05:00
|
|
|
#
|
|
|
|
|
2013-07-29 11:29:16 -05:00
|
|
|
"""Identity v2 Role action implementations"""
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2016-06-16 20:01:15 +08:00
|
|
|
import logging
|
|
|
|
|
2015-10-21 12:01:56 -05:00
|
|
|
from keystoneauth1 import exceptions as ks_exc
|
2016-05-13 17:27:12 -05:00
|
|
|
from osc_lib.command import command
|
2016-06-08 14:17:14 -05:00
|
|
|
from osc_lib import exceptions
|
2016-05-13 16:14:09 -05:00
|
|
|
from osc_lib import utils
|
2016-06-08 14:17:14 -05:00
|
|
|
import six
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2016-05-13 13:14:02 -07:00
|
|
|
from openstackclient.i18n import _
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2016-06-16 20:01:15 +08:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class AddRole(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Add role to project:user")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(AddRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role',
|
|
|
|
metavar='<role>',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Role to add to <project>:<user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
2013-07-29 11:29:16 -05:00
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2012-05-16 16:22:03 -05:00
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <project> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--user',
|
|
|
|
metavar='<user>',
|
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
role = utils.find_resource(identity_client.roles, parsed_args.role)
|
2013-07-29 11:29:16 -05:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.tenants,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2013-01-31 13:31:41 -06:00
|
|
|
user = utils.find_resource(identity_client.users, parsed_args.user)
|
2012-05-16 16:22:03 -05:00
|
|
|
role = identity_client.roles.add_user_role(
|
2013-08-27 16:57:30 -05:00
|
|
|
user.id,
|
|
|
|
role.id,
|
|
|
|
project.id,
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
info = {}
|
|
|
|
info.update(role._info)
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class CreateRole(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Create new role")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(CreateRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role_name',
|
2014-11-18 15:11:32 -06:00
|
|
|
metavar='<name>',
|
|
|
|
help=_('New role name'),
|
|
|
|
)
|
2014-11-12 10:48:47 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--or-show',
|
|
|
|
action='store_true',
|
|
|
|
help=_('Return existing role'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2014-11-12 10:48:47 -05:00
|
|
|
try:
|
|
|
|
role = identity_client.roles.create(parsed_args.role_name)
|
2016-07-07 07:42:01 -04:00
|
|
|
except ks_exc.Conflict:
|
2014-11-12 10:48:47 -05:00
|
|
|
if parsed_args.or_show:
|
|
|
|
role = utils.find_resource(
|
|
|
|
identity_client.roles,
|
|
|
|
parsed_args.role_name,
|
|
|
|
)
|
2016-06-16 20:01:15 +08:00
|
|
|
LOG.info(_('Returning existing role %s'), role.name)
|
2014-11-12 10:48:47 -05:00
|
|
|
else:
|
2016-07-07 07:42:01 -04:00
|
|
|
raise
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
info = {}
|
|
|
|
info.update(role._info)
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class DeleteRole(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Delete role(s)")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(DeleteRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
2014-12-10 14:09:01 +08:00
|
|
|
'roles',
|
2012-05-16 16:22:03 -05:00
|
|
|
metavar='<role>',
|
2014-12-10 14:09:01 +08:00
|
|
|
nargs="+",
|
|
|
|
help=_('Role(s) to delete (name or ID)'),
|
2014-11-18 15:11:32 -06:00
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-09-09 14:55:07 -05:00
|
|
|
|
2016-12-30 13:22:07 +08:00
|
|
|
errors = 0
|
2014-12-10 14:09:01 +08:00
|
|
|
for role in parsed_args.roles:
|
2016-12-30 13:22:07 +08:00
|
|
|
try:
|
|
|
|
role_obj = utils.find_resource(
|
|
|
|
identity_client.roles,
|
|
|
|
role,
|
|
|
|
)
|
|
|
|
identity_client.roles.delete(role_obj.id)
|
|
|
|
except Exception as e:
|
|
|
|
errors += 1
|
|
|
|
LOG.error(_("Failed to delete role with "
|
|
|
|
"name or ID '%(role)s': %(e)s"),
|
|
|
|
{'role': role, 'e': e})
|
|
|
|
|
|
|
|
if errors > 0:
|
|
|
|
total = len(parsed_args.roles)
|
|
|
|
msg = (_("%(errors)s of %(total)s roles failed "
|
|
|
|
"to delete.") % {'errors': errors, 'total': total})
|
|
|
|
raise exceptions.CommandError(msg)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ListRole(command.Lister):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("List roles")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2015-01-09 19:58:17 -05:00
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Filter roles by <project> (name or ID)'),
|
2015-01-09 19:58:17 -05:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--user',
|
|
|
|
metavar='<user>',
|
2016-06-05 10:58:48 +08:00
|
|
|
help=_('Filter roles by <user> (name or ID)'),
|
2015-01-09 19:58:17 -05:00
|
|
|
)
|
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2016-04-29 23:59:27 +01:00
|
|
|
|
|
|
|
def _deprecated():
|
|
|
|
# NOTE(henry-nash): Deprecated as of Newton, so we should remove
|
|
|
|
# this in the 'P' release.
|
|
|
|
self.log.warning(_('Listing assignments using role list is '
|
|
|
|
'deprecated as of the Newton release. Use role '
|
|
|
|
'assignment list --user <user-name> --project '
|
|
|
|
'<project-name> --names instead.'))
|
|
|
|
|
2015-01-09 19:58:17 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
|
|
|
auth_ref = self.app.client_manager.auth_ref
|
|
|
|
|
|
|
|
# No user or project specified, list all roles in the system
|
|
|
|
if not parsed_args.user and not parsed_args.project:
|
|
|
|
columns = ('ID', 'Name')
|
|
|
|
data = identity_client.roles.list()
|
|
|
|
elif parsed_args.user and parsed_args.project:
|
|
|
|
user = utils.find_resource(
|
|
|
|
identity_client.users,
|
|
|
|
parsed_args.user,
|
|
|
|
)
|
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.projects,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2016-04-29 23:59:27 +01:00
|
|
|
_deprecated()
|
2015-01-09 19:58:17 -05:00
|
|
|
data = identity_client.roles.roles_for_user(user.id, project.id)
|
|
|
|
|
|
|
|
elif parsed_args.user:
|
|
|
|
user = utils.find_resource(
|
|
|
|
identity_client.users,
|
|
|
|
parsed_args.user,
|
|
|
|
)
|
|
|
|
if self.app.client_manager.auth_ref:
|
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.projects,
|
|
|
|
auth_ref.project_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
msg = _("Project must be specified")
|
|
|
|
raise exceptions.CommandError(msg)
|
2016-04-29 23:59:27 +01:00
|
|
|
_deprecated()
|
2015-01-09 19:58:17 -05:00
|
|
|
data = identity_client.roles.roles_for_user(user.id, project.id)
|
|
|
|
elif parsed_args.project:
|
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.projects,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
|
|
|
if self.app.client_manager.auth_ref:
|
|
|
|
user = utils.find_resource(
|
|
|
|
identity_client.users,
|
|
|
|
auth_ref.user_id
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
msg = _("User must be specified")
|
|
|
|
raise exceptions.CommandError(msg)
|
2016-04-29 23:59:27 +01:00
|
|
|
_deprecated()
|
2015-01-09 19:58:17 -05:00
|
|
|
data = identity_client.roles.roles_for_user(user.id, project.id)
|
|
|
|
|
|
|
|
if parsed_args.user or parsed_args.project:
|
|
|
|
columns = ('ID', 'Name', 'Project', 'User')
|
|
|
|
for user_role in data:
|
|
|
|
user_role.user = user.name
|
|
|
|
user_role.project = project.name
|
|
|
|
|
2012-05-16 16:22:03 -05:00
|
|
|
return (columns,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={},
|
2013-01-31 13:31:41 -06:00
|
|
|
) for s in data))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ListUserRole(command.Lister):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("List user-role assignments")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ListUserRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'user',
|
|
|
|
metavar='<user>',
|
|
|
|
nargs='?',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('User to list (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
2013-07-29 11:29:16 -05:00
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Filter users by <project> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-08-27 16:57:30 -05:00
|
|
|
auth_ref = self.app.client_manager.auth_ref
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2013-08-27 16:57:30 -05:00
|
|
|
# Project and user are required, if not included in command args
|
|
|
|
# default to the values used for authentication. For token-flow
|
|
|
|
# authentication they must be included on the command line.
|
2016-02-04 16:45:38 +00:00
|
|
|
if (not parsed_args.project and
|
|
|
|
self.app.client_manager.auth_ref.project_id):
|
|
|
|
parsed_args.project = auth_ref.project_id
|
2013-07-29 11:29:16 -05:00
|
|
|
if not parsed_args.project:
|
2016-02-04 16:45:38 +00:00
|
|
|
msg = _("Project must be specified")
|
|
|
|
raise exceptions.CommandError(msg)
|
|
|
|
|
|
|
|
if (not parsed_args.user and
|
|
|
|
self.app.client_manager.auth_ref.user_id):
|
|
|
|
parsed_args.user = auth_ref.user_id
|
2012-05-16 16:22:03 -05:00
|
|
|
if not parsed_args.user:
|
2016-02-04 16:45:38 +00:00
|
|
|
msg = _("User must be specified")
|
|
|
|
raise exceptions.CommandError(msg)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2016-04-29 23:59:27 +01:00
|
|
|
self.log.warning(_('Listing assignments using user role list is '
|
|
|
|
'deprecated as of the Newton release. Use role '
|
|
|
|
'assignment list --user <user-name> --project '
|
|
|
|
'<project-name> --names instead.'))
|
2013-07-29 11:29:16 -05:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.tenants,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2013-01-31 13:31:41 -06:00
|
|
|
user = utils.find_resource(identity_client.users, parsed_args.user)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
2013-07-29 11:29:16 -05:00
|
|
|
data = identity_client.roles.roles_for_user(user.id, project.id)
|
|
|
|
|
|
|
|
columns = (
|
|
|
|
'ID',
|
|
|
|
'Name',
|
|
|
|
'Project',
|
|
|
|
'User',
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
# Add the names to the output even though they will be constant
|
|
|
|
for role in data:
|
2013-07-29 11:29:16 -05:00
|
|
|
role.user = user.name
|
|
|
|
role.project = project.name
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
return (columns,
|
|
|
|
(utils.get_item_properties(
|
|
|
|
s, columns,
|
|
|
|
formatters={},
|
2013-01-31 13:31:41 -06:00
|
|
|
) for s in data))
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
class RemoveRole(command.Command):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Remove role from project : user")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(RemoveRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role',
|
|
|
|
metavar='<role>',
|
2015-01-09 19:13:03 -05:00
|
|
|
help=_('Role to remove (name or ID)'),
|
2014-11-18 15:11:32 -06:00
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
2013-07-29 11:29:16 -05:00
|
|
|
'--project',
|
|
|
|
metavar='<project>',
|
2012-05-16 16:22:03 -05:00
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <project> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
parser.add_argument(
|
|
|
|
'--user',
|
|
|
|
metavar='<user>',
|
|
|
|
required=True,
|
2014-11-18 15:11:32 -06:00
|
|
|
help=_('Include <user> (name or ID)'),
|
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
role = utils.find_resource(identity_client.roles, parsed_args.role)
|
2013-07-29 11:29:16 -05:00
|
|
|
project = utils.find_resource(
|
|
|
|
identity_client.tenants,
|
|
|
|
parsed_args.project,
|
|
|
|
)
|
2013-01-31 13:31:41 -06:00
|
|
|
user = utils.find_resource(identity_client.users, parsed_args.user)
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client.roles.remove_user_role(
|
|
|
|
user.id,
|
|
|
|
role.id,
|
2013-07-29 11:29:16 -05:00
|
|
|
project.id)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
|
2016-01-10 21:54:53 +09:00
|
|
|
class ShowRole(command.ShowOne):
|
2016-11-13 09:42:09 -05:00
|
|
|
_description = _("Display role details")
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
def get_parser(self, prog_name):
|
|
|
|
parser = super(ShowRole, self).get_parser(prog_name)
|
|
|
|
parser.add_argument(
|
|
|
|
'role',
|
|
|
|
metavar='<role>',
|
2015-01-09 19:13:03 -05:00
|
|
|
help=_('Role to display (name or ID)'),
|
2014-11-18 15:11:32 -06:00
|
|
|
)
|
2012-05-16 16:22:03 -05:00
|
|
|
return parser
|
|
|
|
|
2012-06-01 11:17:30 -05:00
|
|
|
def take_action(self, parsed_args):
|
2012-05-16 16:22:03 -05:00
|
|
|
identity_client = self.app.client_manager.identity
|
2013-01-31 13:31:41 -06:00
|
|
|
role = utils.find_resource(identity_client.roles, parsed_args.role)
|
2012-05-16 16:22:03 -05:00
|
|
|
|
|
|
|
info = {}
|
|
|
|
info.update(role._info)
|
2013-07-03 18:12:58 -05:00
|
|
|
return zip(*sorted(six.iteritems(info)))
|