1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
from st2client.formatters import table |
17
|
|
|
from st2client.commands import resource |
18
|
|
|
from st2client.models.rbac import Role |
19
|
|
|
from st2client.models.rbac import UserRoleAssignment |
20
|
|
|
|
21
|
|
|
__all__ = [ |
22
|
|
|
'RoleBranch', |
23
|
|
|
'RoleAssignmentBranch' |
24
|
|
|
] |
25
|
|
|
|
26
|
|
|
ROLE_ATTRIBUTE_DISPLAY_ORDER = ['id', 'name', 'system', 'permission_grants'] |
27
|
|
|
ROLE_ASSIGNMENT_ATTRIBUTE_DISPLAY_ORDER = ['id', 'role', 'user', 'is_remote', 'description'] |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
class RoleBranch(resource.ResourceBranch): |
31
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
32
|
|
|
super(RoleBranch, self).__init__( |
33
|
|
|
Role, description, app, subparsers, |
34
|
|
|
parent_parser=parent_parser, |
35
|
|
|
read_only=True, |
36
|
|
|
commands={ |
37
|
|
|
'list': RoleListCommand, |
38
|
|
|
'get': RoleGetCommand |
39
|
|
|
}) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
class RoleListCommand(resource.ResourceCommand): |
43
|
|
|
display_attributes = ['id', 'name', 'system', 'description'] |
44
|
|
|
attribute_display_order = ROLE_ATTRIBUTE_DISPLAY_ORDER |
45
|
|
|
|
46
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
47
|
|
|
super(RoleListCommand, self).__init__( |
48
|
|
|
resource, 'list', 'Get the list of the %s.' % |
49
|
|
|
resource.get_plural_display_name().lower(), |
50
|
|
|
*args, **kwargs) |
51
|
|
|
|
52
|
|
|
self.group = self.parser.add_mutually_exclusive_group() |
53
|
|
|
|
54
|
|
|
# Filter options |
55
|
|
|
self.group.add_argument('-s', '--system', action='store_true', |
56
|
|
|
help='Only display system roles.') |
57
|
|
|
|
58
|
|
|
# Display options |
59
|
|
|
self.parser.add_argument('-a', '--attr', nargs='+', |
60
|
|
|
default=self.display_attributes, |
61
|
|
|
help=('List of attributes to include in the ' |
62
|
|
|
'output. "all" will return all ' |
63
|
|
|
'attributes.')) |
64
|
|
|
self.parser.add_argument('-w', '--width', nargs='+', type=int, |
65
|
|
|
default=None, |
66
|
|
|
help=('Set the width of columns in output.')) |
67
|
|
|
|
68
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
69
|
|
|
def run(self, args, **kwargs): |
70
|
|
|
# Filtering options |
71
|
|
|
if args.system: |
72
|
|
|
kwargs['system'] = args.system |
73
|
|
|
|
74
|
|
|
if args.system: |
75
|
|
|
result = self.manager.query(**kwargs) |
76
|
|
|
else: |
77
|
|
|
result = self.manager.get_all(**kwargs) |
78
|
|
|
|
79
|
|
|
return result |
80
|
|
|
|
81
|
|
|
def run_and_print(self, args, **kwargs): |
82
|
|
|
instances = self.run(args, **kwargs) |
83
|
|
|
self.print_output(instances, table.MultiColumnTable, |
84
|
|
|
attributes=args.attr, widths=args.width, |
85
|
|
|
json=args.json, yaml=args.yaml) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
class RoleGetCommand(resource.ResourceGetCommand): |
89
|
|
|
display_attributes = ['all'] |
90
|
|
|
attribute_display_order = ROLE_ATTRIBUTE_DISPLAY_ORDER |
91
|
|
|
pk_argument_name = 'id' |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
class RoleAssignmentBranch(resource.ResourceBranch): |
95
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
96
|
|
|
super(RoleAssignmentBranch, self).__init__( |
97
|
|
|
UserRoleAssignment, description, app, subparsers, |
98
|
|
|
parent_parser=parent_parser, |
99
|
|
|
read_only=True, |
100
|
|
|
commands={ |
101
|
|
|
'list': RoleAssignmentListCommand, |
102
|
|
|
'get': RoleAssignmentGetCommand |
103
|
|
|
}) |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
class RoleAssignmentListCommand(resource.ResourceCommand): |
107
|
|
|
display_attributes = ['id', 'role', 'user', 'is_remote', 'source', 'description'] |
108
|
|
|
attribute_display_order = ROLE_ASSIGNMENT_ATTRIBUTE_DISPLAY_ORDER |
109
|
|
|
|
110
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
111
|
|
|
super(RoleAssignmentListCommand, self).__init__( |
112
|
|
|
resource, 'list', 'Get the list of the %s.' % |
113
|
|
|
resource.get_plural_display_name().lower(), |
114
|
|
|
*args, **kwargs) |
115
|
|
|
|
116
|
|
|
# Filter options |
117
|
|
|
self.parser.add_argument('-r', '--role', help='Role to filter on.') |
118
|
|
|
self.parser.add_argument('-u', '--user', help='User to filter on.') |
119
|
|
|
self.parser.add_argument('-s', '--source', help='Source to filter on.') |
120
|
|
|
self.parser.add_argument('--remote', action='store_true', |
121
|
|
|
help='Only display remote role assignments.') |
122
|
|
|
|
123
|
|
|
# Display options |
124
|
|
|
self.parser.add_argument('-a', '--attr', nargs='+', |
125
|
|
|
default=self.display_attributes, |
126
|
|
|
help=('List of attributes to include in the ' |
127
|
|
|
'output. "all" will return all ' |
128
|
|
|
'attributes.')) |
129
|
|
|
self.parser.add_argument('-w', '--width', nargs='+', type=int, |
130
|
|
|
default=None, |
131
|
|
|
help=('Set the width of columns in output.')) |
132
|
|
|
|
133
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
134
|
|
|
def run(self, args, **kwargs): |
135
|
|
|
# Filtering options |
136
|
|
|
if args.role: |
137
|
|
|
kwargs['role'] = args.role |
138
|
|
|
if args.user: |
139
|
|
|
kwargs['user'] = args.user |
140
|
|
|
if args.source: |
141
|
|
|
kwargs['source'] = args.source |
142
|
|
|
if args.remote: |
143
|
|
|
kwargs['remote'] = args.remote |
144
|
|
|
|
145
|
|
|
if args.role or args.user or args.remote or args.source: |
146
|
|
|
result = self.manager.query(**kwargs) |
147
|
|
|
else: |
148
|
|
|
result = self.manager.get_all(**kwargs) |
149
|
|
|
|
150
|
|
|
return result |
151
|
|
|
|
152
|
|
|
def run_and_print(self, args, **kwargs): |
153
|
|
|
instances = self.run(args, **kwargs) |
154
|
|
|
self.print_output(instances, table.MultiColumnTable, |
155
|
|
|
attributes=args.attr, widths=args.width, |
156
|
|
|
json=args.json, yaml=args.yaml) |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
class RoleAssignmentGetCommand(resource.ResourceGetCommand): |
160
|
|
|
display_attributes = ['all'] |
161
|
|
|
attribute_display_order = ROLE_ASSIGNMENT_ATTRIBUTE_DISPLAY_ORDER |
162
|
|
|
pk_argument_name = 'id' |
163
|
|
|
|
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: