Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Adds allow_agent option to Device() object #920

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
41 changes: 31 additions & 10 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,13 @@ def __init__(self, *vargs, **kvargs):
:param bool normalize:
*OPTIONAL* default is ``False``. If ``True`` then the
XML returned by :meth:`execute` will have whitespace normalized

:param bool allow_agent:
*OPTIONAL* If ``True`` then the SSH config file is not parsed by PyEZ
and passed down to ncclient. If ``False`` then the SSH config file will
be parsed by PyEZ. If option is not provided will fallback to default
behavior. This option is passed down to the ncclient as is, if it is
present in the kwargs.
"""

# ----------------------------------------
Expand All @@ -1123,6 +1130,7 @@ def __init__(self, *vargs, **kvargs):
self._normalize = kvargs.get('normalize', False)
self._auto_probe = kvargs.get('auto_probe', self.__class__.auto_probe)
self._fact_style = kvargs.get('fact_style', 'new')
self._allow_agent = kvargs.get('allow_agent')
if self._fact_style != 'new':
warnings.warn('fact-style %s will be removed in a future '
'release.' %
Expand Down Expand Up @@ -1154,11 +1162,19 @@ def __init__(self, *vargs, **kvargs):
# user can get updated by ssh_config
self._ssh_config = kvargs.get('ssh_config')
self._sshconf_lkup()
# but if user or private key is explicit from call, then use it.
self._auth_user = kvargs.get('user') or self._conf_auth_user or \
self._auth_user
self._ssh_private_key_file = kvargs.get('ssh_private_key_file') \
or self._conf_ssh_private_key_file

# if allow_agent is provided and is True, then PyEZ shouldn't load
# the values from config file
if self._allow_agent is not None and self._allow_agent is True:
self._auth_user = kvargs.get('user')
self._ssh_private_key_file = kvargs.get('ssh_private_key_file')
# if allow_agent is not provided or provided but set to False, and
# if user or private key is explicit from call, then use it.
else:
self._auth_user = kvargs.get('user') or self._conf_auth_user or \
self._auth_user
self._ssh_private_key_file = kvargs.get('ssh_private_key_file') \
or self._conf_ssh_private_key_file
self._auth_password = kvargs.get(
'password') or kvargs.get('passwd')

Expand Down Expand Up @@ -1238,13 +1254,18 @@ def open(self, *vargs, **kvargs):
try:
ts_start = datetime.datetime.now()

# we want to enable the ssh-agent if-and-only-if we are
# not given a password or an ssh key file.
# in this condition it means we want to query the agent
# for available ssh keys
# if allow_agent is provided in the call, then the same
# value is passed to the ncclient.
# if allow_agent isn't provided in the call, then it is
# set to True if we are not able to find password or
# ssh_keyfile. user provided allow_agent value should be
# preferred over the runtime value

allow_agent = bool((self._auth_password is None) and
if self._allow_agent is None:
allow_agent = bool((self._auth_password is None) and
(self._ssh_private_key_file is None))
else:
allow_agent = self.allow_agent

# open connection using ncclient transport
self._conn = netconf_ssh.connect(
Expand Down