| @@ 56-304 (lines=249) @@ | ||
| 53 | # Is authenticated on gvmd |
|
| 54 | self._authenticated = False |
|
| 55 | ||
| 56 | def create_credential( |
|
| 57 | self, |
|
| 58 | name: str, |
|
| 59 | credential_type: CredentialType, |
|
| 60 | *, |
|
| 61 | comment: Optional[str] = None, |
|
| 62 | allow_insecure: Optional[bool] = None, |
|
| 63 | certificate: Optional[str] = None, |
|
| 64 | key_phrase: Optional[str] = None, |
|
| 65 | private_key: Optional[str] = None, |
|
| 66 | login: Optional[str] = None, |
|
| 67 | password: Optional[str] = None, |
|
| 68 | auth_algorithm: Optional[SnmpAuthAlgorithm] = None, |
|
| 69 | community: Optional[str] = None, |
|
| 70 | privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None, |
|
| 71 | privacy_password: Optional[str] = None, |
|
| 72 | public_key: Optional[str] = None, |
|
| 73 | ) -> Any: |
|
| 74 | """Create a new credential |
|
| 75 | ||
| 76 | Create a new credential e.g. to be used in the method of an alert. |
|
| 77 | ||
| 78 | Currently the following credential types are supported: |
|
| 79 | ||
| 80 | - Username + Password |
|
| 81 | - Username + SSH-Key |
|
| 82 | - Client Certificates |
|
| 83 | - SNMPv1 or SNMPv2c protocol |
|
| 84 | - S/MIME Certificate |
|
| 85 | - OpenPGP Key |
|
| 86 | - Password only |
|
| 87 | ||
| 88 | Arguments: |
|
| 89 | name: Name of the new credential |
|
| 90 | credential_type: The credential type. |
|
| 91 | comment: Comment for the credential |
|
| 92 | allow_insecure: Whether to allow insecure use of the credential |
|
| 93 | certificate: Certificate for the credential. |
|
| 94 | Required for client-certificate and smime credential types. |
|
| 95 | key_phrase: Key passphrase for the private key. |
|
| 96 | Used for the username+ssh-key credential type. |
|
| 97 | private_key: Private key to use for login. Required |
|
| 98 | for usk credential type. Also used for the cc credential type. |
|
| 99 | The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM, |
|
| 100 | PKC#12, OpenSSL, ...) depend on your installed GnuTLS version. |
|
| 101 | login: Username for the credential. Required for username+password, |
|
| 102 | username+ssh-key and snmp credential type. |
|
| 103 | password: Password for the credential. Used for username+password |
|
| 104 | and snmp credential types. |
|
| 105 | community: The SNMP community |
|
| 106 | auth_algorithm: The SNMP authentication algorithm. Required for snmp |
|
| 107 | credential type. |
|
| 108 | privacy_algorithm: The SNMP privacy algorithm |
|
| 109 | privacy_password: The SNMP privacy password |
|
| 110 | public_key: PGP public key in *armor* plain text format. Required |
|
| 111 | for pgp credential type. |
|
| 112 | ||
| 113 | Examples: |
|
| 114 | Creating a Username + Password credential |
|
| 115 | ||
| 116 | .. code-block:: python |
|
| 117 | ||
| 118 | gmp.create_credential( |
|
| 119 | name='UP Credential', |
|
| 120 | credential_type=CredentialType.USERNAME_PASSWORD, |
|
| 121 | login='foo', |
|
| 122 | password='bar', |
|
| 123 | ) |
|
| 124 | ||
| 125 | Creating a Username + SSH Key credential |
|
| 126 | ||
| 127 | .. code-block:: python |
|
| 128 | ||
| 129 | with open('path/to/private-ssh-key') as f: |
|
| 130 | key = f.read() |
|
| 131 | ||
| 132 | gmp.create_credential( |
|
| 133 | name='USK Credential', |
|
| 134 | credential_type=CredentialType.USERNAME_SSH_KEY, |
|
| 135 | login='foo', |
|
| 136 | key_phrase='foobar', |
|
| 137 | private_key=key, |
|
| 138 | ) |
|
| 139 | ||
| 140 | Creating a PGP credential |
|
| 141 | ||
| 142 | .. note:: |
|
| 143 | ||
| 144 | A compatible public pgp key file can be exported with GnuPG via |
|
| 145 | :: |
|
| 146 | ||
| 147 | $ gpg --armor --export [email protected] > alice.asc |
|
| 148 | ||
| 149 | .. code-block:: python |
|
| 150 | ||
| 151 | with open('path/to/pgp.key.asc') as f: |
|
| 152 | key = f.read() |
|
| 153 | ||
| 154 | gmp.create_credential( |
|
| 155 | name='PGP Credential', |
|
| 156 | credential_type=CredentialType.PGP_ENCRYPTION_KEY, |
|
| 157 | public_key=key, |
|
| 158 | ) |
|
| 159 | ||
| 160 | Creating a S/MIME credential |
|
| 161 | ||
| 162 | .. code-block:: python |
|
| 163 | ||
| 164 | with open('path/to/smime-cert') as f: |
|
| 165 | cert = f.read() |
|
| 166 | ||
| 167 | gmp.create_credential( |
|
| 168 | name='SMIME Credential', |
|
| 169 | credential_type=CredentialType.SMIME_CERTIFICATE, |
|
| 170 | certificate=cert, |
|
| 171 | ) |
|
| 172 | ||
| 173 | Creating a Password-Only credential |
|
| 174 | ||
| 175 | .. code-block:: python |
|
| 176 | ||
| 177 | gmp.create_credential( |
|
| 178 | name='Password-Only Credential', |
|
| 179 | credential_type=CredentialType.PASSWORD_ONLY, |
|
| 180 | password='foo', |
|
| 181 | ) |
|
| 182 | Returns: |
|
| 183 | The response. See :py:meth:`send_command` for details. |
|
| 184 | """ |
|
| 185 | if not name: |
|
| 186 | raise RequiredArgument( |
|
| 187 | function=self.create_credential.__name__, argument='name' |
|
| 188 | ) |
|
| 189 | ||
| 190 | if not isinstance(credential_type, self.types.CredentialType): |
|
| 191 | raise InvalidArgumentType( |
|
| 192 | function=self.create_credential.__name__, |
|
| 193 | argument='credential_type', |
|
| 194 | arg_type=CredentialType.__name__, |
|
| 195 | ) |
|
| 196 | ||
| 197 | cmd = XmlCommand("create_credential") |
|
| 198 | cmd.add_element("name", name) |
|
| 199 | ||
| 200 | cmd.add_element("type", credential_type.value) |
|
| 201 | ||
| 202 | if comment: |
|
| 203 | cmd.add_element("comment", comment) |
|
| 204 | ||
| 205 | if allow_insecure is not None: |
|
| 206 | cmd.add_element("allow_insecure", _to_bool(allow_insecure)) |
|
| 207 | ||
| 208 | if ( |
|
| 209 | credential_type == CredentialType.CLIENT_CERTIFICATE |
|
| 210 | or credential_type == CredentialType.SMIME_CERTIFICATE |
|
| 211 | ): |
|
| 212 | if not certificate: |
|
| 213 | raise RequiredArgument( |
|
| 214 | function=self.create_credential.__name__, |
|
| 215 | argument='certificate', |
|
| 216 | ) |
|
| 217 | ||
| 218 | cmd.add_element("certificate", certificate) |
|
| 219 | ||
| 220 | if ( |
|
| 221 | credential_type == CredentialType.USERNAME_PASSWORD |
|
| 222 | or credential_type == CredentialType.USERNAME_SSH_KEY |
|
| 223 | or credential_type == CredentialType.SNMP |
|
| 224 | ): |
|
| 225 | if not login: |
|
| 226 | raise RequiredArgument( |
|
| 227 | function=self.create_credential.__name__, argument='login' |
|
| 228 | ) |
|
| 229 | ||
| 230 | cmd.add_element("login", login) |
|
| 231 | ||
| 232 | if credential_type == CredentialType.PASSWORD_ONLY and not password: |
|
| 233 | raise RequiredArgument( |
|
| 234 | function=self.create_credential.__name__, argument='password' |
|
| 235 | ) |
|
| 236 | ||
| 237 | if ( |
|
| 238 | credential_type == CredentialType.USERNAME_PASSWORD |
|
| 239 | or credential_type == CredentialType.SNMP |
|
| 240 | or credential_type == CredentialType.PASSWORD_ONLY |
|
| 241 | ) and password: |
|
| 242 | cmd.add_element("password", password) |
|
| 243 | ||
| 244 | if credential_type == CredentialType.USERNAME_SSH_KEY: |
|
| 245 | if not private_key: |
|
| 246 | raise RequiredArgument( |
|
| 247 | function=self.create_credential.__name__, |
|
| 248 | argument='private_key', |
|
| 249 | ) |
|
| 250 | ||
| 251 | _xmlkey = cmd.add_element("key") |
|
| 252 | _xmlkey.add_element("private", private_key) |
|
| 253 | ||
| 254 | if key_phrase: |
|
| 255 | _xmlkey.add_element("phrase", key_phrase) |
|
| 256 | ||
| 257 | if credential_type == CredentialType.CLIENT_CERTIFICATE and private_key: |
|
| 258 | _xmlkey = cmd.add_element("key") |
|
| 259 | _xmlkey.add_element("private", private_key) |
|
| 260 | ||
| 261 | if credential_type == CredentialType.SNMP: |
|
| 262 | if not isinstance(auth_algorithm, self.types.SnmpAuthAlgorithm): |
|
| 263 | raise InvalidArgumentType( |
|
| 264 | function=self.create_credential.__name__, |
|
| 265 | argument='auth_algorithm', |
|
| 266 | arg_type=SnmpAuthAlgorithm.__name__, |
|
| 267 | ) |
|
| 268 | ||
| 269 | cmd.add_element("auth_algorithm", auth_algorithm.value) |
|
| 270 | ||
| 271 | if community: |
|
| 272 | cmd.add_element("community", community) |
|
| 273 | ||
| 274 | if privacy_algorithm is not None or privacy_password: |
|
| 275 | _xmlprivacy = cmd.add_element("privacy") |
|
| 276 | ||
| 277 | if privacy_algorithm is not None: |
|
| 278 | if not isinstance( |
|
| 279 | privacy_algorithm, self.types.SnmpPrivacyAlgorithm |
|
| 280 | ): |
|
| 281 | raise InvalidArgumentType( |
|
| 282 | function=self.create_credential.__name__, |
|
| 283 | argument='privacy_algorithm', |
|
| 284 | arg_type=SnmpPrivacyAlgorithm.__name__, |
|
| 285 | ) |
|
| 286 | ||
| 287 | _xmlprivacy.add_element( |
|
| 288 | "algorithm", privacy_algorithm.value |
|
| 289 | ) |
|
| 290 | ||
| 291 | if privacy_password: |
|
| 292 | _xmlprivacy.add_element("password", privacy_password) |
|
| 293 | ||
| 294 | if credential_type == CredentialType.PGP_ENCRYPTION_KEY: |
|
| 295 | if not public_key: |
|
| 296 | raise RequiredArgument( |
|
| 297 | function=self.create_credential.__name__, |
|
| 298 | argument='public_key', |
|
| 299 | ) |
|
| 300 | ||
| 301 | _xmlkey = cmd.add_element("key") |
|
| 302 | _xmlkey.add_element("public", public_key) |
|
| 303 | ||
| 304 | return self._send_xml_command(cmd) |
|
| 305 | ||
| 306 | def modify_credential( |
|
| 307 | self, |
|
| @@ 2508-2756 (lines=249) @@ | ||
| 2505 | ||
| 2506 | return self._send_xml_command(cmd) |
|
| 2507 | ||
| 2508 | def create_credential( |
|
| 2509 | self, |
|
| 2510 | name: str, |
|
| 2511 | credential_type: CredentialType, |
|
| 2512 | *, |
|
| 2513 | comment: Optional[str] = None, |
|
| 2514 | allow_insecure: Optional[bool] = None, |
|
| 2515 | certificate: Optional[str] = None, |
|
| 2516 | key_phrase: Optional[str] = None, |
|
| 2517 | private_key: Optional[str] = None, |
|
| 2518 | login: Optional[str] = None, |
|
| 2519 | password: Optional[str] = None, |
|
| 2520 | auth_algorithm: Optional[SnmpAuthAlgorithm] = None, |
|
| 2521 | community: Optional[str] = None, |
|
| 2522 | privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None, |
|
| 2523 | privacy_password: Optional[str] = None, |
|
| 2524 | public_key: Optional[str] = None, |
|
| 2525 | ) -> Any: |
|
| 2526 | """Create a new credential |
|
| 2527 | ||
| 2528 | Create a new credential e.g. to be used in the method of an alert. |
|
| 2529 | ||
| 2530 | Currently the following credential types are supported: |
|
| 2531 | ||
| 2532 | - Username + Password |
|
| 2533 | - Username + SSH-Key |
|
| 2534 | - Client Certificates |
|
| 2535 | - SNMPv1 or SNMPv2c protocol |
|
| 2536 | - S/MIME Certificate |
|
| 2537 | - OpenPGP Key |
|
| 2538 | - Password only |
|
| 2539 | ||
| 2540 | Arguments: |
|
| 2541 | name: Name of the new credential |
|
| 2542 | credential_type: The credential type. |
|
| 2543 | comment: Comment for the credential |
|
| 2544 | allow_insecure: Whether to allow insecure use of the credential |
|
| 2545 | certificate: Certificate for the credential. |
|
| 2546 | Required for client-certificate and smime credential types. |
|
| 2547 | key_phrase: Key passphrase for the private key. |
|
| 2548 | Used for the username+ssh-key credential type. |
|
| 2549 | private_key: Private key to use for login. Required |
|
| 2550 | for usk credential type. Also used for the cc credential type. |
|
| 2551 | The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM, |
|
| 2552 | PKC#12, OpenSSL, ...) depend on your installed GnuTLS version. |
|
| 2553 | login: Username for the credential. Required for username+password, |
|
| 2554 | username+ssh-key and snmp credential type. |
|
| 2555 | password: Password for the credential. Used for username+password |
|
| 2556 | and snmp credential types. |
|
| 2557 | community: The SNMP community |
|
| 2558 | auth_algorithm: The SNMP authentication algorithm. Required for snmp |
|
| 2559 | credential type. |
|
| 2560 | privacy_algorithm: The SNMP privacy algorithm |
|
| 2561 | privacy_password: The SNMP privacy password |
|
| 2562 | public_key: PGP public key in *armor* plain text format. Required |
|
| 2563 | for pgp credential type. |
|
| 2564 | ||
| 2565 | Examples: |
|
| 2566 | Creating a Username + Password credential |
|
| 2567 | ||
| 2568 | .. code-block:: python |
|
| 2569 | ||
| 2570 | gmp.create_credential( |
|
| 2571 | name='UP Credential', |
|
| 2572 | credential_type=CredentialType.USERNAME_PASSWORD, |
|
| 2573 | login='foo', |
|
| 2574 | password='bar', |
|
| 2575 | ) |
|
| 2576 | ||
| 2577 | Creating a Username + SSH Key credential |
|
| 2578 | ||
| 2579 | .. code-block:: python |
|
| 2580 | ||
| 2581 | with open('path/to/private-ssh-key') as f: |
|
| 2582 | key = f.read() |
|
| 2583 | ||
| 2584 | gmp.create_credential( |
|
| 2585 | name='USK Credential', |
|
| 2586 | credential_type=CredentialType.USERNAME_SSH_KEY, |
|
| 2587 | login='foo', |
|
| 2588 | key_phrase='foobar', |
|
| 2589 | private_key=key, |
|
| 2590 | ) |
|
| 2591 | ||
| 2592 | Creating a PGP credential |
|
| 2593 | ||
| 2594 | .. note:: |
|
| 2595 | ||
| 2596 | A compatible public pgp key file can be exported with GnuPG via |
|
| 2597 | :: |
|
| 2598 | ||
| 2599 | $ gpg --armor --export [email protected] > alice.asc |
|
| 2600 | ||
| 2601 | .. code-block:: python |
|
| 2602 | ||
| 2603 | with open('path/to/pgp.key.asc') as f: |
|
| 2604 | key = f.read() |
|
| 2605 | ||
| 2606 | gmp.create_credential( |
|
| 2607 | name='PGP Credential', |
|
| 2608 | credential_type=CredentialType.PGP_ENCRYPTION_KEY, |
|
| 2609 | public_key=key, |
|
| 2610 | ) |
|
| 2611 | ||
| 2612 | Creating a S/MIME credential |
|
| 2613 | ||
| 2614 | .. code-block:: python |
|
| 2615 | ||
| 2616 | with open('path/to/smime-cert') as f: |
|
| 2617 | cert = f.read() |
|
| 2618 | ||
| 2619 | gmp.create_credential( |
|
| 2620 | name='SMIME Credential', |
|
| 2621 | credential_type=CredentialType.SMIME_CERTIFICATE, |
|
| 2622 | certificate=cert, |
|
| 2623 | ) |
|
| 2624 | ||
| 2625 | Creating a Password-Only credential |
|
| 2626 | ||
| 2627 | .. code-block:: python |
|
| 2628 | ||
| 2629 | gmp.create_credential( |
|
| 2630 | name='Password-Only Credential', |
|
| 2631 | credential_type=CredentialType.PASSWORD_ONLY, |
|
| 2632 | password='foo', |
|
| 2633 | ) |
|
| 2634 | Returns: |
|
| 2635 | The response. See :py:meth:`send_command` for details. |
|
| 2636 | """ |
|
| 2637 | if not name: |
|
| 2638 | raise RequiredArgument( |
|
| 2639 | function=self.create_credential.__name__, argument='name' |
|
| 2640 | ) |
|
| 2641 | ||
| 2642 | if not isinstance(credential_type, self.types.CredentialType): |
|
| 2643 | raise InvalidArgumentType( |
|
| 2644 | function=self.create_credential.__name__, |
|
| 2645 | argument='credential_type', |
|
| 2646 | arg_type=CredentialType.__name__, |
|
| 2647 | ) |
|
| 2648 | ||
| 2649 | cmd = XmlCommand("create_credential") |
|
| 2650 | cmd.add_element("name", name) |
|
| 2651 | ||
| 2652 | cmd.add_element("type", credential_type.value) |
|
| 2653 | ||
| 2654 | if comment: |
|
| 2655 | cmd.add_element("comment", comment) |
|
| 2656 | ||
| 2657 | if allow_insecure is not None: |
|
| 2658 | cmd.add_element("allow_insecure", _to_bool(allow_insecure)) |
|
| 2659 | ||
| 2660 | if ( |
|
| 2661 | credential_type == CredentialType.CLIENT_CERTIFICATE |
|
| 2662 | or credential_type == CredentialType.SMIME_CERTIFICATE |
|
| 2663 | ): |
|
| 2664 | if not certificate: |
|
| 2665 | raise RequiredArgument( |
|
| 2666 | function=self.create_credential.__name__, |
|
| 2667 | argument='certificate', |
|
| 2668 | ) |
|
| 2669 | ||
| 2670 | cmd.add_element("certificate", certificate) |
|
| 2671 | ||
| 2672 | if ( |
|
| 2673 | credential_type == CredentialType.USERNAME_PASSWORD |
|
| 2674 | or credential_type == CredentialType.USERNAME_SSH_KEY |
|
| 2675 | or credential_type == CredentialType.SNMP |
|
| 2676 | ): |
|
| 2677 | if not login: |
|
| 2678 | raise RequiredArgument( |
|
| 2679 | function=self.create_credential.__name__, argument='login' |
|
| 2680 | ) |
|
| 2681 | ||
| 2682 | cmd.add_element("login", login) |
|
| 2683 | ||
| 2684 | if credential_type == CredentialType.PASSWORD_ONLY and not password: |
|
| 2685 | raise RequiredArgument( |
|
| 2686 | function=self.create_credential.__name__, argument='password' |
|
| 2687 | ) |
|
| 2688 | ||
| 2689 | if ( |
|
| 2690 | credential_type == CredentialType.USERNAME_PASSWORD |
|
| 2691 | or credential_type == CredentialType.SNMP |
|
| 2692 | or credential_type == CredentialType.PASSWORD_ONLY |
|
| 2693 | ) and password: |
|
| 2694 | cmd.add_element("password", password) |
|
| 2695 | ||
| 2696 | if credential_type == CredentialType.USERNAME_SSH_KEY: |
|
| 2697 | if not private_key: |
|
| 2698 | raise RequiredArgument( |
|
| 2699 | function=self.create_credential.__name__, |
|
| 2700 | argument='private_key', |
|
| 2701 | ) |
|
| 2702 | ||
| 2703 | _xmlkey = cmd.add_element("key") |
|
| 2704 | _xmlkey.add_element("private", private_key) |
|
| 2705 | ||
| 2706 | if key_phrase: |
|
| 2707 | _xmlkey.add_element("phrase", key_phrase) |
|
| 2708 | ||
| 2709 | if credential_type == CredentialType.CLIENT_CERTIFICATE and private_key: |
|
| 2710 | _xmlkey = cmd.add_element("key") |
|
| 2711 | _xmlkey.add_element("private", private_key) |
|
| 2712 | ||
| 2713 | if credential_type == CredentialType.SNMP: |
|
| 2714 | if not isinstance(auth_algorithm, self.types.SnmpAuthAlgorithm): |
|
| 2715 | raise InvalidArgumentType( |
|
| 2716 | function=self.create_credential.__name__, |
|
| 2717 | argument='auth_algorithm', |
|
| 2718 | arg_type=SnmpAuthAlgorithm.__name__, |
|
| 2719 | ) |
|
| 2720 | ||
| 2721 | cmd.add_element("auth_algorithm", auth_algorithm.value) |
|
| 2722 | ||
| 2723 | if community: |
|
| 2724 | cmd.add_element("community", community) |
|
| 2725 | ||
| 2726 | if privacy_algorithm is not None or privacy_password: |
|
| 2727 | _xmlprivacy = cmd.add_element("privacy") |
|
| 2728 | ||
| 2729 | if privacy_algorithm is not None: |
|
| 2730 | if not isinstance( |
|
| 2731 | privacy_algorithm, self.types.SnmpPrivacyAlgorithm |
|
| 2732 | ): |
|
| 2733 | raise InvalidArgumentType( |
|
| 2734 | function=self.create_credential.__name__, |
|
| 2735 | argument='privacy_algorithm', |
|
| 2736 | arg_type=SnmpPrivacyAlgorithm.__name__, |
|
| 2737 | ) |
|
| 2738 | ||
| 2739 | _xmlprivacy.add_element( |
|
| 2740 | "algorithm", privacy_algorithm.value |
|
| 2741 | ) |
|
| 2742 | ||
| 2743 | if privacy_password: |
|
| 2744 | _xmlprivacy.add_element("password", privacy_password) |
|
| 2745 | ||
| 2746 | if credential_type == CredentialType.PGP_ENCRYPTION_KEY: |
|
| 2747 | if not public_key: |
|
| 2748 | raise RequiredArgument( |
|
| 2749 | function=self.create_credential.__name__, |
|
| 2750 | argument='public_key', |
|
| 2751 | ) |
|
| 2752 | ||
| 2753 | _xmlkey = cmd.add_element("key") |
|
| 2754 | _xmlkey.add_element("public", public_key) |
|
| 2755 | ||
| 2756 | return self._send_xml_command(cmd) |
|
| 2757 | ||
| 2758 | def modify_credential( |
|
| 2759 | self, |
|