1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
|
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
from typing import Optional, Union
from gvm._enum import Enum
from gvm.errors import RequiredArgument
from gvm.protocols.core import Request
from gvm.utils import to_bool
from gvm.xml import XmlCommand
from .._entity_id import EntityID
class CredentialFormat(Enum):
"""Enum for credential format"""
KEY = "key"
RPM = "rpm"
DEB = "deb"
EXE = "exe"
PEM = "pem"
class CredentialType(Enum):
"""Enum for credential types"""
CLIENT_CERTIFICATE = "cc"
SNMP = "snmp"
USERNAME_PASSWORD = "up"
USERNAME_SSH_KEY = "usk"
SMIME_CERTIFICATE = "smime"
PGP_ENCRYPTION_KEY = "pgp"
PASSWORD_ONLY = "pw"
class SnmpAuthAlgorithm(Enum):
"""Enum for SNMP auth algorithm"""
SHA1 = "sha1"
MD5 = "md5"
class SnmpPrivacyAlgorithm(Enum):
"""Enum for SNMP privacy algorithm"""
AES = "aes"
DES = "des"
class Credentials:
@classmethod
def clone_credential(cls, credential_id: EntityID) -> Request:
"""Clone a credential
Args:
credential_id: The ID of the credential to clone
"""
if not credential_id:
raise RequiredArgument(
function=cls.clone_credential.__name__,
argument="credential_id",
)
cmd = XmlCommand("create_credential")
cmd.add_element("copy", str(credential_id))
return cmd
@classmethod
def create_credential(
cls,
name: str,
credential_type: Union[CredentialType, str],
*,
comment: Optional[str] = None,
allow_insecure: Optional[bool] = None,
certificate: Optional[str] = None,
key_phrase: Optional[str] = None,
private_key: Optional[str] = None,
login: Optional[str] = None,
password: Optional[str] = None,
auth_algorithm: Optional[Union[SnmpAuthAlgorithm, str]] = None,
community: Optional[str] = None,
privacy_algorithm: Optional[Union[SnmpPrivacyAlgorithm, str]] = None,
privacy_password: Optional[str] = None,
public_key: Optional[str] = None,
) -> Request:
"""Create a new credential
Create a new credential e.g. to be used in the method of an alert.
Currently the following credential types are supported:
- Username + Password
- Username + SSH-Key
- Client Certificates
- SNMPv1 or SNMPv2c protocol
- S/MIME Certificate
- OpenPGP Key
- Password only
Arguments:
name: Name of the new credential
credential_type: The credential type.
comment: Comment for the credential
allow_insecure: Whether to allow insecure use of the credential
certificate: Certificate for the credential.
Required for client-certificate and smime credential types.
key_phrase: Key passphrase for the private key.
Used for the username+ssh-key credential type.
private_key: Private key to use for login. Required
for usk credential type. Also used for the cc credential type.
The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM,
PKC#12, OpenSSL, ...) depend on your installed GnuTLS version.
login: Username for the credential. Required for username+password,
username+ssh-key and snmp credential type.
password: Password for the credential. Used for username+password
and snmp credential types.
community: The SNMP community
auth_algorithm: The SNMP authentication algorithm. Required for snmp
credential type.
privacy_algorithm: The SNMP privacy algorithm
privacy_password: The SNMP privacy password
public_key: PGP public key in *armor* plain text format. Required
for pgp credential type.
Examples:
Creating a Username + Password credential
.. code-block:: python
request = Credentials.create_credential(
name='UP Credential',
credential_type=CredentialType.USERNAME_PASSWORD,
login='foo',
password='bar',
)
Creating a Username + SSH Key credential
.. code-block:: python
with open('path/to/private-ssh-key') as f:
key = f.read()
request = Credentials.create_credential(
name='USK Credential',
credential_type=CredentialType.USERNAME_SSH_KEY,
login='foo',
key_phrase='foobar',
private_key=key,
)
Creating a PGP credential
.. note::
A compatible public pgp key file can be exported with GnuPG via
::
$ gpg --armor --export alice@cyb.org > alice.asc
.. code-block:: python
with open('path/to/pgp.key.asc') as f:
key = f.read()
request = Credentials.create_credential(
name='PGP Credential',
credential_type=CredentialType.PGP_ENCRYPTION_KEY,
public_key=key,
)
Creating a S/MIME credential
.. code-block:: python
with open('path/to/smime-cert') as f:
cert = f.read()
request = Credentials.create_credential(
name='SMIME Credential',
credential_type=CredentialType.SMIME_CERTIFICATE,
certificate=cert,
)
Creating a Password-Only credential
.. code-block:: python
request = Credentials.create_credential(
name='Password-Only Credential',
credential_type=CredentialType.PASSWORD_ONLY,
password='foo',
)
Creating an auto-generated password
.. code-block:: python
request = Credentials.create_credential(
name='UP Credential',
credential_type=CredentialType.USERNAME_PASSWORD,
login='foo',
)
Creating an auto-generated SSH-Key credential
.. code-block:: python
request = Credentials.create_credential(
name='USK Credential',
credential_type=CredentialType.USERNAME_SSH_KEY,
login='foo',
)
"""
if not name:
raise RequiredArgument(
function=cls.create_credential.__name__, argument="name"
)
if not credential_type:
raise RequiredArgument(
function=cls.create_credential.__name__,
argument="credential_type",
)
if not isinstance(credential_type, CredentialType):
credential_type = CredentialType(credential_type)
cmd = XmlCommand("create_credential")
cmd.add_element("name", name)
cmd.add_element("type", credential_type.value)
if comment:
cmd.add_element("comment", comment)
if allow_insecure is not None:
cmd.add_element("allow_insecure", to_bool(allow_insecure))
if (
credential_type == CredentialType.CLIENT_CERTIFICATE
or credential_type == CredentialType.SMIME_CERTIFICATE
):
if not certificate:
raise RequiredArgument(
function=cls.create_credential.__name__,
argument="certificate",
)
cmd.add_element("certificate", certificate)
if (
credential_type == CredentialType.USERNAME_PASSWORD
or credential_type == CredentialType.USERNAME_SSH_KEY
or credential_type == CredentialType.SNMP
):
if not login:
raise RequiredArgument(
function=cls.create_credential.__name__, argument="login"
)
cmd.add_element("login", login)
if credential_type == CredentialType.PASSWORD_ONLY and not password:
raise RequiredArgument(
function=cls.create_credential.__name__, argument="password"
)
if (
credential_type == CredentialType.USERNAME_PASSWORD
or credential_type == CredentialType.SNMP
or credential_type == CredentialType.PASSWORD_ONLY
) and password:
cmd.add_element("password", password)
if (
credential_type == CredentialType.USERNAME_SSH_KEY
and private_key is not None
):
if not private_key:
raise RequiredArgument(
function=cls.create_credential.__name__,
argument="private_key",
)
xml_key = cmd.add_element("key")
xml_key.add_element("private", private_key)
if key_phrase:
xml_key.add_element("phrase", key_phrase)
if credential_type == CredentialType.CLIENT_CERTIFICATE and private_key:
xml_key = cmd.add_element("key")
xml_key.add_element("private", private_key)
if credential_type == CredentialType.SNMP:
if not auth_algorithm:
raise RequiredArgument(
function=cls.create_credential.__name__,
argument="auth_algorithm",
)
if not isinstance(auth_algorithm, SnmpAuthAlgorithm):
auth_algorithm = SnmpAuthAlgorithm(auth_algorithm)
cmd.add_element("auth_algorithm", auth_algorithm.value)
if community:
cmd.add_element("community", community)
if privacy_algorithm is not None or privacy_password:
xml_privacy = cmd.add_element("privacy")
if privacy_algorithm is not None:
if not isinstance(privacy_algorithm, SnmpPrivacyAlgorithm):
privacy_algorithm = SnmpPrivacyAlgorithm(
privacy_algorithm
)
xml_privacy.add_element(
"algorithm", privacy_algorithm.value
)
if privacy_password:
xml_privacy.add_element("password", privacy_password)
if credential_type == CredentialType.PGP_ENCRYPTION_KEY:
if not public_key:
raise RequiredArgument(
function=cls.create_credential.__name__,
argument="public_key",
)
xml_key = cmd.add_element("key")
xml_key.add_element("public", public_key)
return cmd
@classmethod
def delete_credential(
cls, credential_id: EntityID, *, ultimate: Optional[bool] = False
) -> Request:
"""Delete a credential
Args:
credential_id: The ID of the credential to delete
ultimate: Whether to remove entirely, or to the trashcan.
"""
if not credential_id:
raise RequiredArgument(
function=cls.delete_credential.__name__,
argument="credential_id",
)
cmd = XmlCommand("delete_credential")
cmd.set_attribute("credential_id", str(credential_id))
cmd.set_attribute("ultimate", to_bool(ultimate))
return cmd
@staticmethod
def get_credentials(
*,
filter_string: Optional[str] = None,
filter_id: Optional[EntityID] = None,
scanners: Optional[bool] = None,
trash: Optional[bool] = None,
targets: Optional[bool] = None,
) -> Request:
"""Request a list of credentials
Arguments:
filter_string: Filter term to use for the query
filter_id: UUID of an existing filter to use for the query
scanners: Whether to include a list of scanners using the
credentials
trash: Whether to get the trashcan credentials instead
targets: Whether to include a list of targets using the credentials
"""
cmd = XmlCommand("get_credentials")
cmd.add_filter(filter_string, filter_id)
if scanners is not None:
cmd.set_attribute("scanners", to_bool(scanners))
if trash is not None:
cmd.set_attribute("trash", to_bool(trash))
if targets is not None:
cmd.set_attribute("targets", to_bool(targets))
return cmd
@classmethod
def get_credential(
cls,
credential_id: EntityID,
*,
scanners: Optional[bool] = None,
targets: Optional[bool] = None,
credential_format: Optional[Union[CredentialFormat, str]] = None,
) -> Request:
"""Request a single credential
Arguments:
credential_id: UUID of an existing credential
scanners: Whether to include a list of scanners using the
credentials
targets: Whether to include a list of targets using the credentials
credential_format: One of "key", "rpm", "deb", "exe" or "pem"
"""
if not credential_id:
raise RequiredArgument(
function=cls.get_credential.__name__, argument="credential_id"
)
cmd = XmlCommand("get_credentials")
cmd.set_attribute("credential_id", str(credential_id))
if credential_format:
if not isinstance(credential_format, CredentialFormat):
credential_format = CredentialFormat(credential_format)
cmd.set_attribute("format", credential_format.value)
if scanners is not None:
cmd.set_attribute("scanners", to_bool(scanners))
if targets is not None:
cmd.set_attribute("targets", to_bool(targets))
return cmd
@classmethod
def modify_credential(
cls,
credential_id: EntityID,
*,
name: Optional[str] = None,
comment: Optional[str] = None,
allow_insecure: Optional[bool] = None,
certificate: Optional[str] = None,
key_phrase: Optional[str] = None,
private_key: Optional[str] = None,
login: Optional[str] = None,
password: Optional[str] = None,
auth_algorithm: Optional[Union[SnmpAuthAlgorithm, str]] = None,
community: Optional[str] = None,
privacy_algorithm: Optional[Union[SnmpPrivacyAlgorithm, str]] = None,
privacy_password: Optional[str] = None,
public_key: Optional[str] = None,
) -> Request:
"""Modifies an existing credential.
Arguments:
credential_id: UUID of the credential
name: Name of the credential
comment: Comment for the credential
allow_insecure: Whether to allow insecure use of the credential
certificate: Certificate for the credential
key_phrase: Key passphrase for the private key
private_key: Private key to use for login
login: Username for the credential
password: Password for the credential
auth_algorithm: The authentication algorithm for SNMP
community: The SNMP community
privacy_algorithm: The privacy algorithm for SNMP
privacy_password: The SNMP privacy password
public_key: PGP public key in *armor* plain text format
"""
if not credential_id:
raise RequiredArgument(
function=cls.modify_credential.__name__,
argument="credential_id",
)
cmd = XmlCommand("modify_credential")
cmd.set_attribute("credential_id", str(credential_id))
if comment:
cmd.add_element("comment", comment)
if name:
cmd.add_element("name", name)
if allow_insecure is not None:
cmd.add_element("allow_insecure", to_bool(allow_insecure))
if certificate:
cmd.add_element("certificate", certificate)
if key_phrase and private_key:
xml_key = cmd.add_element("key")
xml_key.add_element("phrase", key_phrase)
xml_key.add_element("private", private_key)
elif (not key_phrase and private_key) or (
key_phrase and not private_key
):
raise RequiredArgument(
function=cls.modify_credential.__name__,
argument="key_phrase and private_key",
)
if login:
cmd.add_element("login", login)
if password:
cmd.add_element("password", password)
if auth_algorithm:
if not isinstance(auth_algorithm, SnmpAuthAlgorithm):
auth_algorithm = SnmpAuthAlgorithm(auth_algorithm)
cmd.add_element("auth_algorithm", auth_algorithm.value)
if community:
cmd.add_element("community", community)
if privacy_algorithm is not None or privacy_password is not None:
xml_privacy = cmd.add_element("privacy")
if privacy_algorithm is not None:
if not isinstance(privacy_algorithm, SnmpPrivacyAlgorithm):
privacy_algorithm = SnmpPrivacyAlgorithm(privacy_algorithm)
xml_privacy.add_element("algorithm", privacy_algorithm.value)
if privacy_password is not None:
xml_privacy.add_element("password", privacy_password)
if public_key:
xml_key = cmd.add_element("key")
xml_key.add_element("public", public_key)
return cmd
|