1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2021 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
# pylint: disable=redefined-builtin |
20
|
|
|
# MAYBE we should change filter to filter_string (everywhere) |
21
|
|
|
|
22
|
|
|
from enum import Enum |
23
|
|
|
from typing import Any, Optional |
24
|
|
|
|
25
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
26
|
|
|
from gvm.protocols.gmpv208.entities.entities import EntityType |
27
|
|
|
from gvm.utils import add_filter, to_bool |
28
|
|
|
from gvm.xml import XmlCommand |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class PermissionSubjectType(Enum): |
32
|
|
|
"""Enum for permission subject type""" |
33
|
|
|
|
34
|
|
|
USER = 'user' |
35
|
|
|
GROUP = 'group' |
36
|
|
|
ROLE = 'role' |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
def get_permission_subject_type_from_string( |
40
|
|
|
subject_type: Optional[str], |
41
|
|
|
) -> Optional[PermissionSubjectType]: |
42
|
|
|
"""Convert a permission subject type string to an actual |
43
|
|
|
PermissionSubjectType instance |
44
|
|
|
|
45
|
|
|
Arguments: |
46
|
|
|
subject_type: Permission subject type string to convert to a |
47
|
|
|
PermissionSubjectType |
48
|
|
|
""" |
49
|
|
|
if not subject_type: |
50
|
|
|
return None |
51
|
|
|
|
52
|
|
|
try: |
53
|
|
|
return PermissionSubjectType[subject_type.upper()] |
54
|
|
|
except KeyError: |
55
|
|
|
raise InvalidArgument( |
56
|
|
|
argument='subject_type', |
57
|
|
|
function=get_permission_subject_type_from_string.__name__, |
58
|
|
|
) from None |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
class PermissionsMixin: |
62
|
|
|
def clone_permission(self, permission_id: str) -> Any: |
63
|
|
|
"""Clone an existing permission |
64
|
|
|
|
65
|
|
|
Arguments: |
66
|
|
|
permission_id: UUID of an existing permission to clone from |
67
|
|
|
|
68
|
|
|
Returns: |
69
|
|
|
The response. See :py:meth:`send_command` for details. |
70
|
|
|
""" |
71
|
|
|
if not permission_id: |
72
|
|
|
raise RequiredArgument( |
73
|
|
|
function=self.clone_permission.__name__, |
74
|
|
|
argument='permission_id', |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
cmd = XmlCommand("create_permission") |
78
|
|
|
cmd.add_element("copy", permission_id) |
79
|
|
|
return self._send_xml_command(cmd) |
80
|
|
|
|
81
|
|
|
def create_permission( |
82
|
|
|
self, |
83
|
|
|
name: str, |
84
|
|
|
subject_id: str, |
85
|
|
|
subject_type: PermissionSubjectType, |
86
|
|
|
*, |
87
|
|
|
resource_id: Optional[str] = None, |
88
|
|
|
resource_type: Optional[EntityType] = None, |
89
|
|
|
comment: Optional[str] = None, |
90
|
|
|
) -> Any: |
91
|
|
|
"""Create a new permission |
92
|
|
|
|
93
|
|
|
Arguments: |
94
|
|
|
name: Name of the new permission |
95
|
|
|
subject_id: UUID of subject to whom the permission is granted |
96
|
|
|
subject_type: Type of the subject user, group or role |
97
|
|
|
comment: Comment for the permission |
98
|
|
|
resource_id: UUID of entity to which the permission applies |
99
|
|
|
resource_type: Type of the resource. For Super permissions user, |
100
|
|
|
group or role |
101
|
|
|
|
102
|
|
|
Returns: |
103
|
|
|
The response. See :py:meth:`send_command` for details. |
104
|
|
|
""" |
105
|
|
|
if not name: |
106
|
|
|
raise RequiredArgument( |
107
|
|
|
function=self.create_permission.__name__, argument='name' |
108
|
|
|
) |
109
|
|
|
|
110
|
|
|
if not subject_id: |
111
|
|
|
raise RequiredArgument( |
112
|
|
|
function=self.create_permission.__name__, argument='subject_id' |
113
|
|
|
) |
114
|
|
|
|
115
|
|
|
if not isinstance(subject_type, PermissionSubjectType): |
116
|
|
|
raise InvalidArgumentType( |
117
|
|
|
function=self.create_permission.__name__, |
118
|
|
|
argument='subject_type', |
119
|
|
|
arg_type=PermissionSubjectType.__name__, |
120
|
|
|
) |
121
|
|
|
|
122
|
|
|
cmd = XmlCommand("create_permission") |
123
|
|
|
cmd.add_element("name", name) |
124
|
|
|
|
125
|
|
|
_xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
126
|
|
|
_xmlsubject.add_element("type", subject_type.value) |
127
|
|
|
|
128
|
|
|
if comment: |
129
|
|
|
cmd.add_element("comment", comment) |
130
|
|
|
|
131
|
|
View Code Duplication |
if resource_id or resource_type: |
|
|
|
|
132
|
|
|
if not resource_id: |
133
|
|
|
raise RequiredArgument( |
134
|
|
|
function=self.create_permission.__name__, |
135
|
|
|
argument='resource_id', |
136
|
|
|
) |
137
|
|
|
|
138
|
|
|
if not resource_type: |
139
|
|
|
raise RequiredArgument( |
140
|
|
|
function=self.create_permission.__name__, |
141
|
|
|
argument='resource_type', |
142
|
|
|
) |
143
|
|
|
|
144
|
|
|
if not isinstance(resource_type, EntityType): |
145
|
|
|
raise InvalidArgumentType( |
146
|
|
|
function=self.create_permission.__name__, |
147
|
|
|
argument='resource_type', |
148
|
|
|
arg_type=EntityType.__name__, |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
_xmlresource = cmd.add_element( |
152
|
|
|
"resource", attrs={"id": resource_id} |
153
|
|
|
) |
154
|
|
|
|
155
|
|
|
_actual_resource_type = resource_type |
156
|
|
|
if resource_type.value == EntityType.AUDIT.value: |
157
|
|
|
_actual_resource_type = EntityType.TASK |
158
|
|
|
elif resource_type.value == EntityType.POLICY.value: |
159
|
|
|
_actual_resource_type = EntityType.SCAN_CONFIG |
160
|
|
|
|
161
|
|
|
_xmlresource.add_element("type", _actual_resource_type.value) |
162
|
|
|
|
163
|
|
|
return self._send_xml_command(cmd) |
164
|
|
|
|
165
|
|
|
def delete_permission( |
166
|
|
|
self, permission_id: str, *, ultimate: Optional[bool] = False |
167
|
|
|
) -> Any: |
168
|
|
|
"""Deletes an existing permission |
169
|
|
|
|
170
|
|
|
Arguments: |
171
|
|
|
permission_id: UUID of the permission to be deleted. |
172
|
|
|
ultimate: Whether to remove entirely, or to the trashcan. |
173
|
|
|
""" |
174
|
|
|
if not permission_id: |
175
|
|
|
raise RequiredArgument( |
176
|
|
|
function=self.delete_permission.__name__, |
177
|
|
|
argument='permission_id', |
178
|
|
|
) |
179
|
|
|
|
180
|
|
|
cmd = XmlCommand("delete_permission") |
181
|
|
|
cmd.set_attribute("permission_id", permission_id) |
182
|
|
|
cmd.set_attribute("ultimate", to_bool(ultimate)) |
183
|
|
|
|
184
|
|
|
return self._send_xml_command(cmd) |
185
|
|
|
|
186
|
|
View Code Duplication |
def get_permissions( |
|
|
|
|
187
|
|
|
self, |
188
|
|
|
*, |
189
|
|
|
filter: Optional[str] = None, |
190
|
|
|
filter_id: Optional[str] = None, |
191
|
|
|
trash: Optional[bool] = None, |
192
|
|
|
) -> Any: |
193
|
|
|
"""Request a list of permissions |
194
|
|
|
|
195
|
|
|
Arguments: |
196
|
|
|
filter: Filter term to use for the query |
197
|
|
|
filter_id: UUID of an existing filter to use for the query |
198
|
|
|
trash: Whether to get permissions in the trashcan instead |
199
|
|
|
|
200
|
|
|
Returns: |
201
|
|
|
The response. See :py:meth:`send_command` for details. |
202
|
|
|
""" |
203
|
|
|
cmd = XmlCommand("get_permissions") |
204
|
|
|
|
205
|
|
|
add_filter(cmd, filter, filter_id) |
206
|
|
|
|
207
|
|
|
if trash is not None: |
208
|
|
|
cmd.set_attribute("trash", to_bool(trash)) |
209
|
|
|
|
210
|
|
|
return self._send_xml_command(cmd) |
211
|
|
|
|
212
|
|
|
def get_permission(self, permission_id: str) -> Any: |
213
|
|
|
"""Request a single permission |
214
|
|
|
|
215
|
|
|
Arguments: |
216
|
|
|
permission_id: UUID of an existing permission |
217
|
|
|
|
218
|
|
|
Returns: |
219
|
|
|
The response. See :py:meth:`send_command` for details. |
220
|
|
|
""" |
221
|
|
|
cmd = XmlCommand("get_permissions") |
222
|
|
|
|
223
|
|
|
if not permission_id: |
224
|
|
|
raise RequiredArgument( |
225
|
|
|
function=self.get_permission.__name__, argument='permission_id' |
226
|
|
|
) |
227
|
|
|
|
228
|
|
|
cmd.set_attribute("permission_id", permission_id) |
229
|
|
|
return self._send_xml_command(cmd) |
230
|
|
|
|
231
|
|
|
def modify_permission( |
232
|
|
|
self, |
233
|
|
|
permission_id: str, |
234
|
|
|
*, |
235
|
|
|
comment: Optional[str] = None, |
236
|
|
|
name: Optional[str] = None, |
237
|
|
|
resource_id: Optional[str] = None, |
238
|
|
|
resource_type: Optional[EntityType] = None, |
239
|
|
|
subject_id: Optional[str] = None, |
240
|
|
|
subject_type: Optional[PermissionSubjectType] = None, |
241
|
|
|
) -> Any: |
242
|
|
|
"""Modifies an existing permission. |
243
|
|
|
|
244
|
|
|
Arguments: |
245
|
|
|
permission_id: UUID of permission to be modified. |
246
|
|
|
comment: The comment on the permission. |
247
|
|
|
name: Permission name, currently the name of a command. |
248
|
|
|
subject_id: UUID of subject to whom the permission is granted |
249
|
|
|
subject_type: Type of the subject user, group or role |
250
|
|
|
resource_id: UUID of entity to which the permission applies |
251
|
|
|
resource_type: Type of the resource. For Super permissions user, |
252
|
|
|
group or role |
253
|
|
|
|
254
|
|
|
Returns: |
255
|
|
|
The response. See :py:meth:`send_command` for details. |
256
|
|
|
""" |
257
|
|
|
if not permission_id: |
258
|
|
|
raise RequiredArgument( |
259
|
|
|
function=self.modify_permission.__name__, |
260
|
|
|
argument='permission_id', |
261
|
|
|
) |
262
|
|
|
|
263
|
|
|
cmd = XmlCommand("modify_permission") |
264
|
|
|
cmd.set_attribute("permission_id", permission_id) |
265
|
|
|
|
266
|
|
|
if comment: |
267
|
|
|
cmd.add_element("comment", comment) |
268
|
|
|
|
269
|
|
|
if name: |
270
|
|
|
cmd.add_element("name", name) |
271
|
|
|
|
272
|
|
View Code Duplication |
if resource_id or resource_type: |
|
|
|
|
273
|
|
|
if not resource_id: |
274
|
|
|
raise RequiredArgument( |
275
|
|
|
function=self.modify_permission.__name__, |
276
|
|
|
argument='resource_id', |
277
|
|
|
) |
278
|
|
|
|
279
|
|
|
if not resource_type: |
280
|
|
|
raise RequiredArgument( |
281
|
|
|
function=self.modify_permission.__name__, |
282
|
|
|
argument='resource_type', |
283
|
|
|
) |
284
|
|
|
|
285
|
|
|
if not isinstance(resource_type, EntityType): |
286
|
|
|
raise InvalidArgumentType( |
287
|
|
|
function=self.modify_permission.__name__, |
288
|
|
|
argument='resource_type', |
289
|
|
|
arg_type=EntityType.__name__, |
290
|
|
|
) |
291
|
|
|
|
292
|
|
|
_xmlresource = cmd.add_element( |
293
|
|
|
"resource", attrs={"id": resource_id} |
294
|
|
|
) |
295
|
|
|
_actual_resource_type = resource_type |
296
|
|
|
if resource_type.value == EntityType.AUDIT.value: |
297
|
|
|
_actual_resource_type = EntityType.TASK |
298
|
|
|
elif resource_type.value == EntityType.POLICY.value: |
299
|
|
|
_actual_resource_type = EntityType.SCAN_CONFIG |
300
|
|
|
_xmlresource.add_element("type", _actual_resource_type.value) |
301
|
|
|
|
302
|
|
|
if subject_id or subject_type: |
303
|
|
|
if not subject_id: |
304
|
|
|
raise RequiredArgument( |
305
|
|
|
function=self.modify_permission.__name__, |
306
|
|
|
argument='subject_id', |
307
|
|
|
) |
308
|
|
|
|
309
|
|
|
if not isinstance(subject_type, PermissionSubjectType): |
310
|
|
|
raise InvalidArgumentType( |
311
|
|
|
function=self.modify_permission.__name__, |
312
|
|
|
argument='subject_type', |
313
|
|
|
arg_type=PermissionSubjectType.__name__, |
314
|
|
|
) |
315
|
|
|
|
316
|
|
|
_xmlsubject = cmd.add_element("subject", attrs={"id": subject_id}) |
317
|
|
|
_xmlsubject.add_element("type", subject_type.value) |
318
|
|
|
|
319
|
|
|
return self._send_xml_command(cmd) |
320
|
|
|
|