|
| 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) |
0 commit comments