nornir-automation / nornir

Pluggable multi-threaded framework with inventory management to help operate collections of devices
https://nornir.readthedocs.io/
Apache License 2.0
1.36k stars 228 forks source link

Netbox add support to requests lib to accept self-signed ssl certificates #431

Closed m1009d closed 4 years ago

m1009d commented 4 years ago

Hello,
can we add support to Nornir Netbox plugin to accept self-signed ssl certificates?
Requests library supports this but Nornir doesn't implement the verify=False option in the requests.get() method.

m1009d commented 4 years ago

My proposed solution could be something like this:

class NBInventory(Inventory):
    def __init__(
        self,
        nb_url: Optional[str] = None,
        nb_token: Optional[str] = None,
        use_slugs: bool = True,
        flatten_custom_fields: bool = True,
        filter_parameters: Optional[Dict[str, Any]] = None,
        verify: Optional[bool] = True,
        **kwargs: Any,
    ) -> None:
        """
        Netbox plugin

        Arguments:
            nb_url: Netbox url, defaults to http://localhost:8080.
                You can also use env variable NB_URL
            nb_token: Netbokx token. You can also use env variable NB_TOKEN
            use_slugs: Whether to use slugs or not
            flatten_custom_fields: Whether to assign custom fields directly to the host or not
            filter_parameters: Key-value pairs to filter down hosts
        """
        filter_parameters = filter_parameters or {}

        nb_url = nb_url or os.environ.get("NB_URL", "http://localhost:8080")
        nb_token = nb_token or os.environ.get(
            "NB_TOKEN", "0123456789abcdef0123456789abcdef01234567"
        )
        headers = {"Authorization": "Token {}".format(nb_token)}

        # Create dict of hosts using 'devices' from NetBox
        r = requests.get(
            "{}/api/dcim/devices/?limit=0".format(nb_url),
            headers=headers,
            params=filter_parameters,
        )
...
wvandeun commented 4 years ago

Not sure which version of Nornir you are using, but 2.2.0 (which is published on pypi) has the ssl_verify parameter for this purpose. https://github.com/nornir-automation/nornir/blob/5b17fa627e64cd5af2be02ed2964636679d21731/nornir/plugins/inventory/netbox.py#L16 The ssl_verify parameter is used to configure the requests.Session object https://github.com/nornir-automation/nornir/blob/5b17fa627e64cd5af2be02ed2964636679d21731/nornir/plugins/inventory/netbox.py#L41

dbarrosop commented 4 years ago

This is already supported although it wasn't properly documented. Thanks to @wvandeun for fixing this in #434