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

Fixed ssh-key auth #281

Merged
merged 3 commits into from
Jul 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/jnpr/junos/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def _sshconf_lkup(self):
self._hostname = found.get('hostname', self._hostname)
self._port = found.get('port', self._port)
self._auth_user = found.get('user')
self._ssh_private_key_file = found.get('identityfile')

def __init__(self, *vargs, **kvargs):
"""
Expand Down Expand Up @@ -307,7 +308,8 @@ def __init__(self, *vargs, **kvargs):
# but if user is explit from call, then use it.
self._auth_user = kvargs.get('user') or self._auth_user
self._auth_password = kvargs.get('password') or kvargs.get('passwd')
self._ssh_private_key_file = kvargs.get('ssh_private_key_file')
if not hasattr(self, '_ssh_private_key_file'):
self._ssh_private_key_file = kvargs.get('ssh_private_key_file')

# -----------------------------
# initialize instance variables
Expand Down Expand Up @@ -370,13 +372,13 @@ def open(self, *vargs, **kvargs):
try:
ts_start = datetime.datetime.now()

# we want to disable the ssh-agent if-and-only-if we are
# given a password and we are not given an ssh key file.
# in this condition it means that the password is
# the 'plain-text' password
# 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

allow_agent = not bool((self._auth_password is not None) and
(self._ssh_private_key_file is None))
allow_agent = bool((self._auth_password is None) and
(self._ssh_private_key_file is None))

# open connection using ncclient transport
self._conn = netconf_ssh.connect(
Expand All @@ -386,7 +388,6 @@ def open(self, *vargs, **kvargs):
password=self._auth_password,
hostkey_verify=False,
key_filename=self._ssh_private_key_file,
look_for_keys=False,
allow_agent=allow_agent,
device_params={'name': 'junos'})

Expand Down
25 changes: 25 additions & 0 deletions tests/functional/test_device_ssh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
@author: rsherman
'''
import unittest
from nose.plugins.attrib import attr

from jnpr.junos import Device


@attr('functional')
class TestDeviceSsh(unittest.TestCase):

def tearDown(self):
self.dev.close()

def test_device_open_default_key(self):
self.dev = Device('pabst.englab.juniper.net')
self.dev.open()
self.assertEqual(self.dev.connected, True)

def test_device_open_key_pass(self):
self.dev = Device(host='pabst.englab.juniper.net', ssh_private_key_file='/var/lib/jenkins/.ssh/passkey', passwd='password')
self.dev.open()
self.assertEqual(self.dev.connected, True)