Conditions | 33 |
Total Lines | 247 |
Code Lines | 98 |
Lines | 17 |
Ratio | 6.88 % |
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.gmpv208.entities.credentials.CredentialsMixin.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 -*- |
||
149 | def create_credential( |
||
150 | self, |
||
151 | name: str, |
||
152 | credential_type: CredentialType, |
||
153 | *, |
||
154 | comment: Optional[str] = None, |
||
155 | allow_insecure: Optional[bool] = None, |
||
156 | certificate: Optional[str] = None, |
||
157 | key_phrase: Optional[str] = None, |
||
158 | private_key: Optional[str] = None, |
||
159 | login: Optional[str] = None, |
||
160 | password: Optional[str] = None, |
||
161 | auth_algorithm: Optional[SnmpAuthAlgorithm] = None, |
||
162 | community: Optional[str] = None, |
||
163 | privacy_algorithm: Optional[SnmpPrivacyAlgorithm] = None, |
||
164 | privacy_password: Optional[str] = None, |
||
165 | public_key: Optional[str] = None, |
||
166 | ) -> Any: |
||
167 | """Create a new credential |
||
168 | |||
169 | Create a new credential e.g. to be used in the method of an alert. |
||
170 | |||
171 | Currently the following credential types are supported: |
||
172 | |||
173 | - Username + Password |
||
174 | - Username + SSH-Key |
||
175 | - Client Certificates |
||
176 | - SNMPv1 or SNMPv2c protocol |
||
177 | - S/MIME Certificate |
||
178 | - OpenPGP Key |
||
179 | - Password only |
||
180 | |||
181 | Arguments: |
||
182 | name: Name of the new credential |
||
183 | credential_type: The credential type. |
||
184 | comment: Comment for the credential |
||
185 | allow_insecure: Whether to allow insecure use of the credential |
||
186 | certificate: Certificate for the credential. |
||
187 | Required for client-certificate and smime credential types. |
||
188 | key_phrase: Key passphrase for the private key. |
||
189 | Used for the username+ssh-key credential type. |
||
190 | private_key: Private key to use for login. Required |
||
191 | for usk credential type. Also used for the cc credential type. |
||
192 | The supported key types (dsa, rsa, ecdsa, ...) and formats (PEM, |
||
193 | PKC#12, OpenSSL, ...) depend on your installed GnuTLS version. |
||
194 | login: Username for the credential. Required for username+password, |
||
195 | username+ssh-key and snmp credential type. |
||
196 | password: Password for the credential. Used for username+password |
||
197 | and snmp credential types. |
||
198 | community: The SNMP community |
||
199 | auth_algorithm: The SNMP authentication algorithm. Required for snmp |
||
200 | credential type. |
||
201 | privacy_algorithm: The SNMP privacy algorithm |
||
202 | privacy_password: The SNMP privacy password |
||
203 | public_key: PGP public key in *armor* plain text format. Required |
||
204 | for pgp credential type. |
||
205 | |||
206 | Examples: |
||
207 | Creating a Username + Password credential |
||
208 | |||
209 | .. code-block:: python |
||
210 | |||
211 | gmp.create_credential( |
||
212 | name='UP Credential', |
||
213 | credential_type=CredentialType.USERNAME_PASSWORD, |
||
214 | login='foo', |
||
215 | password='bar', |
||
216 | ) |
||
217 | |||
218 | Creating a Username + SSH Key credential |
||
219 | |||
220 | .. code-block:: python |
||
221 | |||
222 | with open('path/to/private-ssh-key') as f: |
||
223 | key = f.read() |
||
224 | |||
225 | gmp.create_credential( |
||
226 | name='USK Credential', |
||
227 | credential_type=CredentialType.USERNAME_SSH_KEY, |
||
228 | login='foo', |
||
229 | key_phrase='foobar', |
||
230 | private_key=key, |
||
231 | ) |
||
232 | |||
233 | Creating a PGP credential |
||
234 | |||
235 | .. note:: |
||
236 | |||
237 | A compatible public pgp key file can be exported with GnuPG via |
||
238 | :: |
||
239 | |||
240 | $ gpg --armor --export [email protected] > alice.asc |
||
241 | |||
242 | .. code-block:: python |
||
243 | |||
244 | with open('path/to/pgp.key.asc') as f: |
||
245 | key = f.read() |
||
246 | |||
247 | gmp.create_credential( |
||
248 | name='PGP Credential', |
||
249 | credential_type=CredentialType.PGP_ENCRYPTION_KEY, |
||
250 | public_key=key, |
||
251 | ) |
||
252 | |||
253 | Creating a S/MIME credential |
||
254 | |||
255 | .. code-block:: python |
||
256 | |||
257 | with open('path/to/smime-cert') as f: |
||
258 | cert = f.read() |
||
259 | |||
260 | gmp.create_credential( |
||
261 | name='SMIME Credential', |
||
262 | credential_type=CredentialType.SMIME_CERTIFICATE, |
||
263 | certificate=cert, |
||
264 | ) |
||
265 | |||
266 | Creating a Password-Only credential |
||
267 | |||
268 | .. code-block:: python |
||
269 | |||
270 | gmp.create_credential( |
||
271 | name='Password-Only Credential', |
||
272 | credential_type=CredentialType.PASSWORD_ONLY, |
||
273 | password='foo', |
||
274 | ) |
||
275 | Returns: |
||
276 | The response. See :py:meth:`send_command` for details. |
||
277 | """ |
||
278 | if not name: |
||
279 | raise RequiredArgument( |
||
280 | function=self.create_credential.__name__, argument='name' |
||
281 | ) |
||
282 | |||
283 | if not isinstance(credential_type, CredentialType): |
||
284 | raise InvalidArgumentType( |
||
285 | function=self.create_credential.__name__, |
||
286 | argument='credential_type', |
||
287 | arg_type=CredentialType.__name__, |
||
288 | ) |
||
289 | |||
290 | cmd = XmlCommand("create_credential") |
||
291 | cmd.add_element("name", name) |
||
292 | |||
293 | cmd.add_element("type", credential_type.value) |
||
294 | |||
295 | if comment: |
||
296 | cmd.add_element("comment", comment) |
||
297 | |||
298 | if allow_insecure is not None: |
||
299 | cmd.add_element("allow_insecure", to_bool(allow_insecure)) |
||
300 | |||
301 | if ( |
||
302 | credential_type == CredentialType.CLIENT_CERTIFICATE |
||
303 | or credential_type == CredentialType.SMIME_CERTIFICATE |
||
304 | ): |
||
305 | if not certificate: |
||
306 | raise RequiredArgument( |
||
307 | function=self.create_credential.__name__, |
||
308 | argument='certificate', |
||
309 | ) |
||
310 | |||
311 | cmd.add_element("certificate", certificate) |
||
312 | |||
313 | if ( |
||
314 | credential_type == CredentialType.USERNAME_PASSWORD |
||
315 | or credential_type == CredentialType.USERNAME_SSH_KEY |
||
316 | or credential_type == CredentialType.SNMP |
||
317 | ): |
||
318 | if not login: |
||
319 | raise RequiredArgument( |
||
320 | function=self.create_credential.__name__, argument='login' |
||
321 | ) |
||
322 | |||
323 | cmd.add_element("login", login) |
||
324 | |||
325 | if credential_type == CredentialType.PASSWORD_ONLY and not password: |
||
326 | raise RequiredArgument( |
||
327 | function=self.create_credential.__name__, argument='password' |
||
328 | ) |
||
329 | |||
330 | if ( |
||
331 | credential_type == CredentialType.USERNAME_PASSWORD |
||
332 | or credential_type == CredentialType.SNMP |
||
333 | or credential_type == CredentialType.PASSWORD_ONLY |
||
334 | ) and password: |
||
335 | cmd.add_element("password", password) |
||
336 | |||
337 | if credential_type == CredentialType.USERNAME_SSH_KEY: |
||
338 | if not private_key: |
||
339 | raise RequiredArgument( |
||
340 | function=self.create_credential.__name__, |
||
341 | argument='private_key', |
||
342 | ) |
||
343 | |||
344 | _xmlkey = cmd.add_element("key") |
||
345 | _xmlkey.add_element("private", private_key) |
||
346 | |||
347 | if key_phrase: |
||
348 | _xmlkey.add_element("phrase", key_phrase) |
||
349 | |||
350 | if credential_type == CredentialType.CLIENT_CERTIFICATE and private_key: |
||
351 | _xmlkey = cmd.add_element("key") |
||
352 | _xmlkey.add_element("private", private_key) |
||
353 | |||
354 | if credential_type == CredentialType.SNMP: |
||
355 | if not isinstance(auth_algorithm, SnmpAuthAlgorithm): |
||
356 | raise InvalidArgumentType( |
||
357 | function=self.create_credential.__name__, |
||
358 | argument='auth_algorithm', |
||
359 | arg_type=SnmpAuthAlgorithm.__name__, |
||
360 | ) |
||
361 | |||
362 | cmd.add_element("auth_algorithm", auth_algorithm.value) |
||
363 | |||
364 | if community: |
||
365 | cmd.add_element("community", community) |
||
366 | |||
367 | View Code Duplication | if privacy_algorithm is not None or privacy_password: |
|
|
|||
368 | _xmlprivacy = cmd.add_element("privacy") |
||
369 | |||
370 | if privacy_algorithm is not None: |
||
371 | if not isinstance(privacy_algorithm, SnmpPrivacyAlgorithm): |
||
372 | raise InvalidArgumentType( |
||
373 | function=self.create_credential.__name__, |
||
374 | argument='privacy_algorithm', |
||
375 | arg_type=SnmpPrivacyAlgorithm.__name__, |
||
376 | ) |
||
377 | |||
378 | _xmlprivacy.add_element( |
||
379 | "algorithm", privacy_algorithm.value |
||
380 | ) |
||
381 | |||
382 | if privacy_password: |
||
383 | _xmlprivacy.add_element("password", privacy_password) |
||
384 | |||
385 | if credential_type == CredentialType.PGP_ENCRYPTION_KEY: |
||
386 | if not public_key: |
||
387 | raise RequiredArgument( |
||
388 | function=self.create_credential.__name__, |
||
389 | argument='public_key', |
||
390 | ) |
||
391 | |||
392 | _xmlkey = cmd.add_element("key") |
||
393 | _xmlkey.add_element("public", public_key) |
||
394 | |||
395 | return self._send_xml_command(cmd) |
||
396 | |||
612 |