py-junos-eznc icon indicating copy to clipboard operation
py-junos-eznc copied to clipboard

Issues with error handling for HelloHandler when using tty_netconf transport

Open monnierj opened this issue 11 months ago • 3 comments

Hi,

I've found something off in the open function from lib/jnpr/junos/transport/tty_netconf.py.

The data read upon opening connection is fed into HelloHandler.parse can be of two types:

  • an XML element, if there are errors
  • a bytes sequence, if the data was XML-compliant

If the case of an unrecoverable error, _parse_buffer returns an XML element, on which we call decode(). It obviously raises an exception:

Traceback (most recent call last):
  File "/path/to/test/script/test_junos.py", line 15, in open
    jnpr_device.open()
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/console.py", line 216, in open
    raise ex
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/console.py", line 207, in open
    self._tty_login()
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/console.py", line 316, in _tty_login
    self._tty.login()
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/transport/tty.py", line 117, in login
    self.nc.open(at_shell=self.at_shell)
  File "/Users/julien.monnier/Library/Caches/pypoetry/virtualenvs/project-42eiPW28-py3.10/lib/python3.10/site-packages/jnpr/junos/transport/tty_netconf.py", line 72, in open
    self._session_id, _ = HelloHandler.parse(self.hello.decode("utf-8"))
AttributeError: 'lxml.etree._Element' object has no attribute 'decode'

The related PR fixes the issue by checking whether we have a bytes sequence or a XML element, and conditionally calling decode on it.

I can provide a reproduction script, even if it's not really useful (just a connection to an SRX300 through a terminal server).

monnierj avatar Aug 01 '23 10:08 monnierj