Cryptography 3.1 breaks certain Netmiko drivers with bit_length errors

Hello All,

I recently upgrade to nornir3 from version 2.
However, It seems that if i define more then one host on the inventory.yaml file it brakes. Even if I use the serial runner.
The same code under nornir2 works (with the due tweaks):

  • config.yaml:

runners:
plugin: serial

inventory:
plugin: SimpleInventory
options:
host_file: “./hosts.yaml”
group_file: “./groups.yaml”
defaults_file: “./defaults.yaml”

  • hosts.yaml:

host_1:
hostname: 189.196.41.75
groups:
- GRP

data:
  interface:
    - ten0/0/0/0
    - ten0/0/0/1  

host_2:
hostname: 189.196.41.35
groups:
- GRP

data:
  interface:
    - ten0/0/0/0
    - ten0/0/0/1  

When I run this, it only collects for one of the hosts. in this case it was host_2. However, if I run only the script against host_2 it run fine.
As can be seen on the traceback it complains on the first command it runs against host_2. If I change that command to anything else it will also fail. So I believe this issue is when connecting to the second issue (not related to the command that is being run).

(nornir) htts@htts-vpnc:~/Documents/Nornir/Nornir3/Cases/MEGA/TEST_MEGA_N3$ ./Collect.py -collect
########################################################################

Usage:

Collect.py [(-collect)(-nocollect)]

-nocollect: renders jinja template only (no collection)

########################################################################
**** Playbook to collect information *******************************************
2020-10-15 08:03:26,419 - nornir.core - INFO - run() - Running task ‘renderandcollect’ with args {‘nocollection’: ‘-collect’} on 2 hosts
2020-10-15 08:03:26,420 - nornir.core.task - DEBUG - start() - Host ‘host_1’: running task ‘renderandcollect’
2020-10-15 08:03:26,422 - nornir.core.task - DEBUG - start() - Host ‘host_1’: running task ‘render’
2020-10-15 08:03:26,425 - nornir.core.task - DEBUG - start() - Host ‘host_1’: running task ‘show run | i hostname’
2020-10-15 08:03:26,426 - nornir.core.task - DEBUG - start() - Host ‘host_2’: running task ‘renderandcollect’
2020-10-15 08:03:26,430 - nornir.core.task - DEBUG - start() - Host ‘host_2’: running task ‘render’
2020-10-15 08:03:26,431 - nornir.core.task - DEBUG - start() - Host ‘host_2’: running task ‘show run | i hostname’
Unknown exception: module ‘cryptography.utils’ has no attribute ‘bit_length’
Traceback (most recent call last):
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/transport.py”, line 2075, in run
self.kex_engine.parse_next(ptype, m)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/kex_ecdh_nist.py”, line 53, in parse_next
return self._parse_kexecdh_reply(m)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/kex_ecdh_nist.py”, line 136, in _parse_kexecdh_reply
self.transport._verify_key(K_S, sig)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/transport.py”, line 1886, in _verify_key
if not key.verify_ssh_sig(self.H, Message(sig)):
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/dsskey.py”, line 153, in verify_ssh_sig
).public_key(backend=default_backend())
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/cryptography/hazmat/primitives/asymmetric/dsa.py”, line 212, in public_key
return backend.load_dsa_public_numbers(self)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/cryptography/hazmat/backends/openssl/backend.py”, line 873, in load_dsa_public_numbers
dsa._check_dsa_parameters(numbers.parameter_numbers)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/netmiko/tplink/tplink_jetstream.py”, line 145, in _override_check_dsa_parameters
if crypto_utils.bit_length(parameters.q) not in [160, 256]:
AttributeError: module ‘cryptography.utils’ has no attribute ‘bit_length’

2020-10-15 08:03:28,487 - nornir.core.task - ERROR - start() - Host ‘host_2’: task ‘show run | i hostname’ failed with traceback:
Traceback (most recent call last):
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir/core/task.py”, line 98, in start
r = self.task(self, **self.params)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir_netmiko/tasks/netmiko_send_command.py”, line 26, in netmiko_send_command
net_connect = task.host.get_connection(CONNECTION_NAME, task.nornir.config)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir/core/inventory.py”, line 448, in get_connection
extras=conn.extras,
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir/core/inventory.py”, line 499, in open_connection
configuration=configuration,
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir_netmiko/connections/netmiko.py”, line 59, in open
connection = ConnectHandler(**parameters)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/netmiko/ssh_dispatcher.py”, line 312, in ConnectHandler
return ConnectionClass(*args, **kwargs)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/netmiko/cisco/cisco_ios.py”, line 17, in init
return super().init(*args, **kwargs)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/netmiko/base_connection.py”, line 346, in init
self._open()
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/netmiko/base_connection.py”, line 351, in _open
self.establish_connection()
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/netmiko/base_connection.py”, line 920, in establish_connection
self.remote_conn_pre.connect(**ssh_connect_params)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/client.py”, line 406, in connect
t.start_client(timeout=timeout)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/transport.py”, line 660, in start_client
raise e
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/transport.py”, line 2075, in run
self.kex_engine.parse_next(ptype, m)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/kex_ecdh_nist.py”, line 53, in parse_next
return self._parse_kexecdh_reply(m)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/paramiko/kex_ecdh_nist.py”, line 136, in _parse_kexecdh_reply
self.transport._verify_key(K_S, sig)
AttributeError: module ‘cryptography.utils’ has no attribute ‘bit_length’

2020-10-15 08:03:28,489 - nornir.core.task - ERROR - start() - Host ‘host_2’: task ‘renderandcollect’ failed with traceback:
Traceback (most recent call last):
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir/core/task.py”, line 98, in start
r = self.task(self, **self.params)
File “./Collect.py”, line 38, in renderandcollect
command_string=command)
File “/home/htts/.virtualenvs/nornir/lib/python3.6/site-packages/nornir/core/task.py”, line 168, in run
raise NornirSubTaskError(task=run_task, result=r)
nornir.core.exceptions.NornirSubTaskError: Subtask: show run | i hostname (failed)

Solved by downgrading the cryptography module from version 3.1.1 to version 2.9.2 (the one I was using with nornir2)

(nornir) htts@htts-vpnc:~/Documents/Nornir/Nornir3/Cases/MEGA/TESTE_JUMP_N3$ pip3 show cryptography
Name: cryptography
Version: 2.9.2
Summary: cryptography is a package which provides cryptographic recipes and primitives to Python developers.
Home-page: https://github.com/pyca/cryptography
Author: The cryptography developers
Author-email: cryptography-dev@python.org
License: BSD or Apache License, Version 2.0
Location: /home/htts/.virtualenvs/nornir/lib/python3.6/site-packages
Requires: six, cffi
Required-by: paramiko

That is this issue here:

Which Netmiko device_type are you using?