| Conditions | 30 |
| Total Lines | 221 |
| Code Lines | 80 |
| Lines | 47 |
| Ratio | 21.27 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like gvm.protocols.gmpv8.Gmp.create_credential() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
| 1 | # -*- coding: utf-8 -*- |
||
| 51 | def create_credential( |
||
|
|
|||
| 52 | self, |
||
| 53 | name, |
||
| 54 | credential_type, |
||
| 55 | *, |
||
| 56 | comment=None, |
||
| 57 | allow_insecure=None, |
||
| 58 | certificate=None, |
||
| 59 | key_phrase=None, |
||
| 60 | private_key=None, |
||
| 61 | login=None, |
||
| 62 | password=None, |
||
| 63 | auth_algorithm=None, |
||
| 64 | community=None, |
||
| 65 | privacy_algorithm=None, |
||
| 66 | privacy_password=None, |
||
| 67 | public_key=None |
||
| 68 | ): |
||
| 69 | """Create a new credential |
||
| 70 | |||
| 71 | Create a new credential e.g. to be used in the method of an alert. |
||
| 72 | |||
| 73 | Currently the following credential types are supported: |
||
| 74 | |||
| 75 | - 'up' - Username + Password |
||
| 76 | - 'usk' - Username + private SSH-Key |
||
| 77 | - 'cc' - Client Certificates |
||
| 78 | - 'snmp' - SNMPv1 or SNMPv2c protocol |
||
| 79 | - 'smime' - S/MIME Certificate |
||
| 80 | - 'pgp' - OpenPGP Key |
||
| 81 | |||
| 82 | Arguments: |
||
| 83 | name (str): Name of the new credential |
||
| 84 | credential_type (str): The credential type. One of 'cc', 'snmp', |
||
| 85 | 'up', 'usk', 'smime', 'pgp' |
||
| 86 | comment (str, optional): Comment for the credential |
||
| 87 | allow_insecure (boolean, optional): Whether to allow insecure use of |
||
| 88 | the credential |
||
| 89 | certificate (str, optional): Certificate for the credential. |
||
| 90 | Required for cc and smime credential types. |
||
| 91 | key_phrase (str, optional): Key passphrase for the private key. |
||
| 92 | Used for the usk credential type. |
||
| 93 | private_key (str, optional): Private key to use for login. Required |
||
| 94 | for usk credential type. Also used for the cc credential type. |
||
| 95 | The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM, |
||
| 96 | PKC#12, OpenSSL, ...) depend on your installed GnuTLS version. |
||
| 97 | login (str, optional): Username for the credential. Required for |
||
| 98 | up, usk and snmp credential type. |
||
| 99 | password (str, optional): Password for the credential. Used for |
||
| 100 | up and snmp credential types. |
||
| 101 | community (str, optional): The SNMP community |
||
| 102 | auth_algorithm (str, optional): The SNMP authentication algorithm. |
||
| 103 | Either 'md5' or 'sha1'. Required for snmp credential type. |
||
| 104 | privacy_algorithm (str, optional): The SNMP privacy algorithm, |
||
| 105 | either aes or des. |
||
| 106 | privacy_password (str, optional): The SNMP privacy password |
||
| 107 | public_key: (str, optional): PGP public key in *armor* plain text |
||
| 108 | format. Required for pgp credential type. |
||
| 109 | |||
| 110 | Examples: |
||
| 111 | Creating a Username + Password credential |
||
| 112 | |||
| 113 | .. code-block:: python |
||
| 114 | |||
| 115 | gmp.create_credential( |
||
| 116 | name='UP Credential', |
||
| 117 | credential_type='up', |
||
| 118 | login='foo', |
||
| 119 | password='bar', |
||
| 120 | ); |
||
| 121 | |||
| 122 | Creating a Username + SSH Key credential |
||
| 123 | |||
| 124 | .. code-block:: python |
||
| 125 | |||
| 126 | with open('path/to/private-ssh-key') as f: |
||
| 127 | key = f.read() |
||
| 128 | |||
| 129 | gmp.create_credential( |
||
| 130 | name='USK Credential', |
||
| 131 | credential_type='usk', |
||
| 132 | login='foo', |
||
| 133 | key_phrase='foobar', |
||
| 134 | private_key=key, |
||
| 135 | ) |
||
| 136 | |||
| 137 | Creating a PGP credential |
||
| 138 | |||
| 139 | .. note:: |
||
| 140 | |||
| 141 | A compatible public pgp key file can be exported with GnuPG via |
||
| 142 | :: |
||
| 143 | |||
| 144 | $ gpg --armor --export [email protected] > alice.asc |
||
| 145 | |||
| 146 | .. code-block:: python |
||
| 147 | |||
| 148 | with open('path/to/pgp.key.asc') as f: |
||
| 149 | key = f.read() |
||
| 150 | |||
| 151 | gmp.create_credential( |
||
| 152 | name='PGP Credential', |
||
| 153 | credential_type='pgp', |
||
| 154 | public_key=key, |
||
| 155 | ) |
||
| 156 | |||
| 157 | Creating a S/MIME credential |
||
| 158 | |||
| 159 | .. code-block:: python |
||
| 160 | |||
| 161 | with open('path/to/smime-cert') as f: |
||
| 162 | cert = f.read() |
||
| 163 | |||
| 164 | gmp.create_credential( |
||
| 165 | name='SMIME Credential', |
||
| 166 | credential_type='smime', |
||
| 167 | certificate=cert, |
||
| 168 | ) |
||
| 169 | |||
| 170 | Returns: |
||
| 171 | The response. See :py:meth:`send_command` for details. |
||
| 172 | """ |
||
| 173 | if not name: |
||
| 174 | raise RequiredArgument("create_credential requires name argument") |
||
| 175 | |||
| 176 | if credential_type not in CREDENTIAL_TYPES: |
||
| 177 | raise InvalidArgument( |
||
| 178 | "create_credential requires type to be either cc, snmp, up," |
||
| 179 | "smime, pgp or usk" |
||
| 180 | ) |
||
| 181 | |||
| 182 | cmd = XmlCommand("create_credential") |
||
| 183 | cmd.add_element("name", name) |
||
| 184 | |||
| 185 | cmd.add_element("type", credential_type) |
||
| 186 | |||
| 187 | if comment: |
||
| 188 | cmd.add_element("comment", comment) |
||
| 189 | |||
| 190 | if allow_insecure is not None: |
||
| 191 | cmd.add_element("allow_insecure", _to_bool(allow_insecure)) |
||
| 192 | |||
| 193 | if credential_type == "cc" or credential_type == "smime": |
||
| 194 | if not certificate: |
||
| 195 | raise RequiredArgument( |
||
| 196 | "create_credential requires certificate argument for " |
||
| 197 | "credential_type {0}".format(credential_type) |
||
| 198 | ) |
||
| 199 | |||
| 200 | cmd.add_element("certificate", certificate) |
||
| 201 | |||
| 202 | View Code Duplication | if ( |
|
| 203 | credential_type == "up" |
||
| 204 | or credential_type == "usk" |
||
| 205 | or credential_type == "snmp" |
||
| 206 | ): |
||
| 207 | if not login: |
||
| 208 | raise RequiredArgument( |
||
| 209 | "create_credential requires login argument for " |
||
| 210 | "credential_type {0}".format(credential_type) |
||
| 211 | ) |
||
| 212 | |||
| 213 | cmd.add_element("login", login) |
||
| 214 | |||
| 215 | if (credential_type == "up" or credential_type == "snmp") and password: |
||
| 216 | cmd.add_element("password", password) |
||
| 217 | |||
| 218 | View Code Duplication | if credential_type == "usk": |
|
| 219 | if not private_key: |
||
| 220 | raise RequiredArgument( |
||
| 221 | "create_credential requires certificate argument for " |
||
| 222 | "credential_type usk" |
||
| 223 | ) |
||
| 224 | |||
| 225 | _xmlkey = cmd.add_element("key") |
||
| 226 | _xmlkey.add_element("private", private_key) |
||
| 227 | |||
| 228 | if key_phrase: |
||
| 229 | _xmlkey.add_element("phrase", key_phrase) |
||
| 230 | |||
| 231 | if credential_type == "cc" and private_key: |
||
| 232 | _xmlkey = cmd.add_element("key") |
||
| 233 | _xmlkey.add_element("private", private_key) |
||
| 234 | |||
| 235 | View Code Duplication | if credential_type == "snmp": |
|
| 236 | if auth_algorithm not in ("md5", "sha1"): |
||
| 237 | raise InvalidArgument( |
||
| 238 | "create_credential requires auth_algorithm to be either " |
||
| 239 | "md5 or sha1" |
||
| 240 | ) |
||
| 241 | |||
| 242 | cmd.add_element("auth_algorithm", auth_algorithm) |
||
| 243 | |||
| 244 | if community: |
||
| 245 | cmd.add_element("community", community) |
||
| 246 | |||
| 247 | if privacy_algorithm is not None or privacy_password: |
||
| 248 | _xmlprivacy = cmd.add_element("privacy") |
||
| 249 | |||
| 250 | if privacy_algorithm is not None: |
||
| 251 | if privacy_algorithm not in ("aes", "des"): |
||
| 252 | raise InvalidArgument( |
||
| 253 | "create_credential requires algorithm to be either " |
||
| 254 | "aes or des" |
||
| 255 | ) |
||
| 256 | |||
| 257 | _xmlprivacy.add_element("algorithm", privacy_algorithm) |
||
| 258 | |||
| 259 | if privacy_password: |
||
| 260 | _xmlprivacy.add_element("password", privacy_password) |
||
| 261 | |||
| 262 | if credential_type == "pgp": |
||
| 263 | if not public_key: |
||
| 264 | raise RequiredArgument( |
||
| 265 | "Creating a pgp credential requires a public_key argument" |
||
| 266 | ) |
||
| 267 | |||
| 268 | _xmlkey = cmd.add_element("key") |
||
| 269 | _xmlkey.add_element("public", public_key) |
||
| 270 | |||
| 271 | return self._send_xml_command(cmd) |
||
| 272 | |||
| 551 |