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
|
|
|
from typing import Any, List, Optional |
20
|
|
|
|
21
|
|
|
from gvm.errors import RequiredArgument |
22
|
|
|
from gvm.utils import add_filter, to_bool, to_comma_list |
23
|
|
|
from gvm.xml import XmlCommand |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
class RolesMixin: |
27
|
|
|
def clone_role(self, role_id: str) -> Any: |
28
|
|
|
"""Clone an existing role |
29
|
|
|
|
30
|
|
|
Arguments: |
31
|
|
|
role_id: UUID of an existing role to clone from |
32
|
|
|
|
33
|
|
|
Returns: |
34
|
|
|
The response. See :py:meth:`send_command` for details. |
35
|
|
|
""" |
36
|
|
|
if not role_id: |
37
|
|
|
raise RequiredArgument( |
38
|
|
|
function=self.clone_role.__name__, argument='role_id' |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
cmd = XmlCommand("create_role") |
42
|
|
|
cmd.add_element("copy", role_id) |
43
|
|
|
return self._send_xml_command(cmd) |
44
|
|
|
|
45
|
|
|
def create_role( |
46
|
|
|
self, |
47
|
|
|
name: str, |
48
|
|
|
*, |
49
|
|
|
comment: Optional[str] = None, |
50
|
|
|
users: Optional[List[str]] = None, |
51
|
|
|
) -> Any: |
52
|
|
|
"""Create a new role |
53
|
|
|
|
54
|
|
|
Arguments: |
55
|
|
|
name: Name of the role |
56
|
|
|
comment: Comment for the role |
57
|
|
|
users: List of user names to add to the role |
58
|
|
|
|
59
|
|
|
Returns: |
60
|
|
|
The response. See :py:meth:`send_command` for details. |
61
|
|
|
""" |
62
|
|
|
|
63
|
|
|
if not name: |
64
|
|
|
raise RequiredArgument( |
65
|
|
|
function=self.create_role.__name__, argument='name' |
66
|
|
|
) |
67
|
|
|
|
68
|
|
|
cmd = XmlCommand("create_role") |
69
|
|
|
cmd.add_element("name", name) |
70
|
|
|
|
71
|
|
|
if comment: |
72
|
|
|
cmd.add_element("comment", comment) |
73
|
|
|
|
74
|
|
|
if users: |
75
|
|
|
cmd.add_element("users", to_comma_list(users)) |
76
|
|
|
|
77
|
|
|
return self._send_xml_command(cmd) |
78
|
|
|
|
79
|
|
|
def delete_role( |
80
|
|
|
self, role_id: str, *, ultimate: Optional[bool] = False |
81
|
|
|
) -> Any: |
82
|
|
|
"""Deletes an existing role |
83
|
|
|
|
84
|
|
|
Arguments: |
85
|
|
|
role_id: UUID of the role to be deleted. |
86
|
|
|
ultimate: Whether to remove entirely, or to the trashcan. |
87
|
|
|
""" |
88
|
|
|
if not role_id: |
89
|
|
|
raise RequiredArgument( |
90
|
|
|
function=self.delete_role.__name__, argument='role_id' |
91
|
|
|
) |
92
|
|
|
|
93
|
|
|
cmd = XmlCommand("delete_role") |
94
|
|
|
cmd.set_attribute("role_id", role_id) |
95
|
|
|
cmd.set_attribute("ultimate", to_bool(ultimate)) |
96
|
|
|
|
97
|
|
|
return self._send_xml_command(cmd) |
98
|
|
|
|
99
|
|
|
def get_roles( |
100
|
|
|
self, |
101
|
|
|
*, |
102
|
|
|
filter_string: Optional[str] = None, |
103
|
|
|
filter_id: Optional[str] = None, |
104
|
|
|
trash: Optional[bool] = None, |
105
|
|
|
) -> Any: |
106
|
|
|
"""Request a list of roles |
107
|
|
|
|
108
|
|
|
Arguments: |
109
|
|
|
filter_string: Filter term to use for the query |
110
|
|
|
filter_id: UUID of an existing filter to use for the query |
111
|
|
|
trash: Whether to get the trashcan roles instead |
112
|
|
|
|
113
|
|
|
Returns: |
114
|
|
|
The response. See :py:meth:`send_command` for details. |
115
|
|
|
""" |
116
|
|
|
cmd = XmlCommand("get_roles") |
117
|
|
|
|
118
|
|
|
add_filter(cmd, filter_string, filter_id) |
119
|
|
|
|
120
|
|
|
if trash is not None: |
121
|
|
|
cmd.set_attribute("trash", to_bool(trash)) |
122
|
|
|
|
123
|
|
|
return self._send_xml_command(cmd) |
124
|
|
|
|
125
|
|
|
def get_role(self, role_id: str) -> Any: |
126
|
|
|
"""Request a single role |
127
|
|
|
|
128
|
|
|
Arguments: |
129
|
|
|
role_id: UUID of an existing role |
130
|
|
|
|
131
|
|
|
Returns: |
132
|
|
|
The response. See :py:meth:`send_command` for details. |
133
|
|
|
""" |
134
|
|
|
if not role_id: |
135
|
|
|
raise RequiredArgument( |
136
|
|
|
function=self.get_role.__name__, argument='role_id' |
137
|
|
|
) |
138
|
|
|
|
139
|
|
|
cmd = XmlCommand("get_roles") |
140
|
|
|
cmd.set_attribute("role_id", role_id) |
141
|
|
|
return self._send_xml_command(cmd) |
142
|
|
|
|
143
|
|
View Code Duplication |
def modify_role( |
|
|
|
|
144
|
|
|
self, |
145
|
|
|
role_id: str, |
146
|
|
|
*, |
147
|
|
|
comment: Optional[str] = None, |
148
|
|
|
name: Optional[str] = None, |
149
|
|
|
users: Optional[List[str]] = None, |
150
|
|
|
) -> Any: |
151
|
|
|
"""Modifies an existing role. |
152
|
|
|
|
153
|
|
|
Arguments: |
154
|
|
|
role_id: UUID of role to modify. |
155
|
|
|
comment: Name of role. |
156
|
|
|
name: Comment on role. |
157
|
|
|
users: List of user names. |
158
|
|
|
|
159
|
|
|
Returns: |
160
|
|
|
The response. See :py:meth:`send_command` for details. |
161
|
|
|
""" |
162
|
|
|
if not role_id: |
163
|
|
|
raise RequiredArgument( |
164
|
|
|
function=self.modify_role.__name__, argument='role_id argument' |
165
|
|
|
) |
166
|
|
|
|
167
|
|
|
cmd = XmlCommand("modify_role") |
168
|
|
|
cmd.set_attribute("role_id", role_id) |
169
|
|
|
|
170
|
|
|
if comment: |
171
|
|
|
cmd.add_element("comment", comment) |
172
|
|
|
|
173
|
|
|
if name: |
174
|
|
|
cmd.add_element("name", name) |
175
|
|
|
|
176
|
|
|
if users: |
177
|
|
|
cmd.add_element("users", to_comma_list(users)) |
178
|
|
|
|
179
|
|
|
return self._send_xml_command(cmd) |
180
|
|
|
|