netutils
netutils copied to clipboard
Add a `get_certificate` filter for Cisco and other configurations that include special characters
Environment
- netutils version:
A get_certificates filter needs to be added for configurations that add special characters in their certificates, for example Cisco ios has a hidden tab character: https://napalm.readthedocs.io/en/latest/support/ios.html
Proposed Functionality
A filter get_certificate that takes as input a running configuration and extracts its certificates, similar to the following:
def get_certificate(
configuration: str,
platform: str,
) -> Optional[str]:
"""Extracts certificate from downloaded configuration with all the included special characters and intricacies of a cisco certificate.
Args:
configuration (str): Running configuration copied from device.
platform (str): Device platform. Ex: ios, nxos etc.
{{ config | get_certificate("ios")}}
{{ "ios" | get_certificate }}
Returns:
str: Certificates.
"""
# this function is Cisco specific, it can take a list of config commands or the name of a file
# with cisco config, parse it and return a specific part based on a regex
parse = CiscoConfParse(config=configuration, syntax=platform)
crypto_trustpoint = parse.find_all_children("^crypto pki trustpoint")
crypto_cert_chain = parse.find_all_children("^crypto pki certificate chain")
if crypto_trustpoint or crypto_cert_chain:
return "\n".join(["!"] + crypto_trustpoint + ["!"] + crypto_cert_chain + ["!"])
return None
Use Case
This feature will be useful for config replacement and a simple diff of running and candidate configs that does not include special chars.
We try to avoid dependencies, any change we can do this without CiscoConfParse? Any dependency would be optional.
Docstring would also need an example section so the docs are auto generated properly.
We try to avoid dependencies, any change we can do this without CiscoConfParse? Any dependency would be optional.
I can implement something similar to CiscoConfParse from scratch. It would be specific to certificates. Would that be acceptable?
That would be great, that was kinda the point of #124