Skip to content

Commit a3085ad

Browse files
authored
Remove: Deprecate ifaces and icafes allow_parameters in user commands
The ifaces and ifaces_allow parameters for the create_user and modify_user commands have been deprecated because the interface access feature is no longer supported.
2 parents 44e4363 + 63b99da commit a3085ad

6 files changed

Lines changed: 346 additions & 30 deletions

File tree

gvm/protocols/gmpv2110/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@
127127
AliveTest,
128128
TargetsMixin,
129129
)
130-
from gvm.protocols.gmpv214.entities.users import UsersMixin
131130

132131
# NEW IN 2110
132+
from gvm.protocols.gmpv2110.entities.users import UsersMixin
133+
133134
from gvm.protocols.gmpv2110.system.version import VersionMixin
134135

135136
from gvm.connections import GvmConnection
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (C) 2022 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=arguments-differ, arguments-renamed
20+
21+
from typing import Any, List, Optional
22+
23+
from gvm.errors import RequiredArgument
24+
from gvm.protocols.gmpv214.entities.users import (
25+
UsersMixin as Gmp214UsersMixin,
26+
UserAuthType,
27+
)
28+
from gvm.utils import deprecation, to_comma_list, to_bool
29+
from gvm.xml import XmlCommand
30+
31+
32+
class UsersMixin(Gmp214UsersMixin):
33+
def create_user(
34+
self,
35+
name: str,
36+
*,
37+
password: Optional[str] = None,
38+
hosts: Optional[List[str]] = None,
39+
hosts_allow: Optional[bool] = False,
40+
ifaces: Any = None,
41+
ifaces_allow: Any = None,
42+
role_ids: Optional[List[str]] = None,
43+
) -> Any:
44+
"""Create a new user
45+
46+
Arguments:
47+
name: Name of the user
48+
password: Password of the user
49+
hosts: A list of host addresses (IPs, DNS names)
50+
hosts_allow: If True allow only access to passed hosts otherwise
51+
deny access. Default is False for deny hosts.
52+
ifaces: deprecated
53+
ifaces_allow: deprecated
54+
role_ids: A list of role UUIDs for the user
55+
56+
Returns:
57+
The response. See :py:meth:`send_command` for details.
58+
"""
59+
if not name:
60+
raise RequiredArgument(
61+
function=self.create_user.__name__, argument='name'
62+
)
63+
64+
cmd = XmlCommand("create_user")
65+
cmd.add_element("name", name)
66+
67+
if password:
68+
cmd.add_element("password", password)
69+
70+
if hosts:
71+
cmd.add_element(
72+
"hosts",
73+
to_comma_list(hosts),
74+
attrs={"allow": to_bool(hosts_allow)},
75+
)
76+
77+
if ifaces is not None:
78+
major, minor = self.get_protocol_version()
79+
deprecation(
80+
"The ifaces parameter has been removed in GMP"
81+
f" version {major}{minor}"
82+
)
83+
84+
if ifaces_allow is not None:
85+
major, minor = self.get_protocol_version()
86+
deprecation(
87+
"The ifaces_allow parameter has been removed in GMP"
88+
f" version {major}{minor}"
89+
)
90+
91+
if role_ids:
92+
for role in role_ids:
93+
cmd.add_element("role", attrs={"id": role})
94+
95+
return self._send_xml_command(cmd)
96+
97+
def modify_user(
98+
self,
99+
user_id: str = None,
100+
*,
101+
name: Optional[str] = None,
102+
comment: Optional[str] = None,
103+
password: Optional[str] = None,
104+
auth_source: Optional[UserAuthType] = None,
105+
role_ids: Optional[List[str]] = None,
106+
hosts: Optional[List[str]] = None,
107+
hosts_allow: Optional[bool] = False,
108+
ifaces: Any = None,
109+
ifaces_allow: Any = None,
110+
group_ids: Optional[List[str]] = None,
111+
) -> Any:
112+
"""Modifies an existing user.
113+
114+
Most of the fields need to be supplied
115+
for changing a single field even if no change is wanted for those.
116+
Else empty values are inserted for the missing fields instead.
117+
118+
Arguments:
119+
user_id: UUID of the user to be modified.
120+
name: The new name for the user.
121+
comment: Comment on the user.
122+
password: The password for the user.
123+
auth_source: Source allowed for authentication for this user.
124+
roles_id: List of roles UUIDs for the user.
125+
hosts: User access rules: List of hosts.
126+
hosts_allow: Defines how the hosts list is to be interpreted.
127+
If False (default) the list is treated as a deny list.
128+
All hosts are allowed by default except those provided by
129+
the hosts parameter. If True the list is treated as a
130+
allow list. All hosts are denied by default except those
131+
provided by the hosts parameter.
132+
ifaces: deprecated
133+
ifaces_allow: deprecated
134+
group_ids: List of group UUIDs for the user.
135+
136+
Returns:
137+
The response. See :py:meth:`send_command` for details.
138+
"""
139+
if not user_id:
140+
raise RequiredArgument(
141+
function=self.modify_user.__name__, argument='user_id'
142+
)
143+
144+
cmd = XmlCommand("modify_user")
145+
146+
cmd.set_attribute("user_id", user_id)
147+
148+
if name:
149+
cmd.add_element("new_name", name)
150+
151+
if role_ids:
152+
for role in role_ids:
153+
cmd.add_element("role", attrs={"id": role})
154+
155+
if hosts:
156+
cmd.add_element(
157+
"hosts",
158+
to_comma_list(hosts),
159+
attrs={"allow": to_bool(hosts_allow)},
160+
)
161+
162+
if ifaces is not None:
163+
major, minor = self.get_protocol_version()
164+
deprecation(
165+
"The ifaces parameter has been removed in GMP"
166+
f" version {major}{minor}"
167+
)
168+
169+
if ifaces_allow is not None:
170+
major, minor = self.get_protocol_version()
171+
deprecation(
172+
"The ifaces_allow parameter has been removed in GMP"
173+
f" version {major}{minor}"
174+
)
175+
176+
if comment:
177+
cmd.add_element("comment", comment)
178+
179+
if password:
180+
cmd.add_element("password", password)
181+
182+
if auth_source:
183+
_xmlauthsrc = cmd.add_element("sources")
184+
_xmlauthsrc.add_element("source", auth_source.value)
185+
186+
if group_ids:
187+
_xmlgroups = cmd.add_element("groups")
188+
for group_id in group_ids:
189+
_xmlgroups.add_element("group", attrs={"id": group_id})
190+
191+
return self._send_xml_command(cmd)

tests/protocols/gmpv2110/entities/test_users.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from ...gmpv2110 import Gmpv2110TestCase
20-
from .users import GmpModifyUserTestMixin
20+
from .users import GmpCreateUserTestMixin, GmpModifyUserTestMixin
2121
from ...gmpv208.entities.users import (
22-
GmpCreateUserTestMixin,
2322
GmpCloneUserTestMixin,
2423
GmpDeleteUserTestMixin,
2524
GmpGetUsersTestMixin,

tests/protocols/gmpv2110/entities/users/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
# You should have received a copy of the GNU General Public License
1717
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from .test_create_user import GmpCreateUserTestMixin
1920
from .test_modify_user import GmpModifyUserTestMixin
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (C) 2018-2022 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 unittest.mock import patch, call
20+
from gvm.errors import RequiredArgument
21+
22+
23+
class GmpCreateUserTestMixin:
24+
def test_create_user_missing_name(self):
25+
with self.assertRaises(RequiredArgument):
26+
self.gmp.create_user(name=None)
27+
28+
with self.assertRaises(RequiredArgument):
29+
self.gmp.create_user(name='')
30+
31+
def test_create_user(self):
32+
self.gmp.create_user(name='foo')
33+
34+
self.connection.send.has_been_called_with(
35+
'<create_user>' '<name>foo</name>' '</create_user>'
36+
)
37+
38+
def test_create_user_with_password(self):
39+
self.gmp.create_user(name='foo', password='bar')
40+
41+
self.connection.send.has_been_called_with(
42+
'<create_user>'
43+
'<name>foo</name>'
44+
'<password>bar</password>'
45+
'</create_user>'
46+
)
47+
48+
def test_create_user_with_hosts(self):
49+
self.gmp.create_user(name='foo', hosts=['h1', 'h2'], hosts_allow=True)
50+
51+
self.connection.send.has_been_called_with(
52+
'<create_user>'
53+
'<name>foo</name>'
54+
'<hosts allow="1">h1,h2</hosts>'
55+
'</create_user>'
56+
)
57+
58+
self.gmp.create_user(name='foo', hosts=['h1', 'h2'])
59+
60+
self.connection.send.has_been_called_with(
61+
'<create_user>'
62+
'<name>foo</name>'
63+
'<hosts allow="0">h1,h2</hosts>'
64+
'</create_user>'
65+
)
66+
67+
self.gmp.create_user(name='foo', hosts=['h1', 'h2'], hosts_allow=False)
68+
69+
self.connection.send.has_been_called_with(
70+
'<create_user>'
71+
'<name>foo</name>'
72+
'<hosts allow="0">h1,h2</hosts>'
73+
'</create_user>'
74+
)
75+
76+
@patch('gvm.protocols.gmpv2110.entities.users.deprecation')
77+
def test_create_user_with_ifaces(self, deprecation_mock):
78+
self.gmp.create_user(name='foo', ifaces=['h1', 'h2'], ifaces_allow=True)
79+
80+
self.connection.send.has_been_called_with(
81+
'<create_user>' '<name>foo</name>' '</create_user>'
82+
)
83+
84+
self.gmp.create_user(name='foo', ifaces=['h1', 'h2'])
85+
86+
self.connection.send.has_been_called_with(
87+
'<create_user>' '<name>foo</name>' '</create_user>'
88+
)
89+
90+
self.gmp.create_user(
91+
name='foo', ifaces=['h1', 'h2'], ifaces_allow=False
92+
)
93+
94+
self.connection.send.has_been_called_with(
95+
'<create_user>' '<name>foo</name>' '</create_user>'
96+
)
97+
98+
# pylint: disable=line-too-long
99+
deprecation_calls = [
100+
call('The ifaces parameter has been removed in GMP version 2110'),
101+
call(
102+
'The ifaces_allow parameter has been removed in GMP version 2110'
103+
),
104+
call('The ifaces parameter has been removed in GMP version 2110'),
105+
call('The ifaces parameter has been removed in GMP version 2110'),
106+
call(
107+
'The ifaces_allow parameter has been removed in GMP version 2110'
108+
),
109+
]
110+
# pylint: enable=line-too-long
111+
deprecation_mock.assert_has_calls(deprecation_calls)
112+
113+
def test_create_user_with_role_ids(self):
114+
self.gmp.create_user(name='foo', role_ids=['r1', 'r2'])
115+
116+
self.connection.send.has_been_called_with(
117+
'<create_user>'
118+
'<name>foo</name>'
119+
'<role id="r1"/>'
120+
'<role id="r2"/>'
121+
'</create_user>'
122+
)

0 commit comments

Comments
 (0)