Skip to content

Commit e23fe2d

Browse files
Merge pull request #1388 from chidanandpujar/agentfix1
allow_agent param support
2 parents 8e3f5f2 + ac043c1 commit e23fe2d

2 files changed

Lines changed: 119 additions & 9 deletions

File tree

lib/jnpr/junos/device.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,14 @@ def __init__(self, *vargs, **kvargs):
12171217
*OPTIONAL* To disable public key authentication.
12181218
default is ``None``.
12191219
1220+
:param bool allow_agent:
1221+
*OPTIONAL* Specifies whether to use keys provided by an SSH agent for authentication.
1222+
If set to ``True``, the SSH connection will use any keys loaded in the agent.
1223+
If set to ``False``, keys from the SSH agent will not be used.
1224+
If set to ``None``, the default behavior is applied: agent keys are used only if
1225+
both password and private key file are not provided.
1226+
Default is ``None``.
1227+
12201228
:param str bind_addr:
12211229
*OPTIONAL* To use (local) source IP address.
12221230
default is ``None``.
@@ -1243,6 +1251,7 @@ def __init__(self, *vargs, **kvargs):
12431251
self._huge_tree = kvargs.get("huge_tree", False)
12441252
self._conn_open_timeout = kvargs.get("conn_open_timeout", 30)
12451253
self._look_for_keys = kvargs.get("look_for_keys", None)
1254+
self._allow_agent = kvargs.get("allow_agent", None)
12461255
self._bind_addr = kvargs.get("bind_addr", None)
12471256
self._hostkey_verify = kvargs.get("hostkey_verify", False)
12481257
if self._fact_style != "new":
@@ -1367,9 +1376,14 @@ def open(self, *vargs, **kvargs):
13671376
# in this condition it means we want to query the agent
13681377
# for available ssh keys
13691378

1370-
allow_agent = bool(
1371-
(self._auth_password is None) and (self._ssh_private_key_file is None)
1372-
)
1379+
if self._allow_agent is not None:
1380+
allow_agent = self._allow_agent
1381+
else:
1382+
# Default behaviour if allow_agent is None
1383+
allow_agent = bool(
1384+
(self._auth_password is None)
1385+
and (self._ssh_private_key_file is None)
1386+
)
13731387

13741388
# option to disable ncclient transport ssh authentication
13751389
# using public keys look_for_keys=False
Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,116 @@
11
__author__ = "rsherman, vnitinv"
22

3-
import unittest
4-
53
from jnpr.junos import Device
64

5+
try:
6+
import unittest2 as unittest
7+
except ImportError:
8+
import unittest
79

810
class TestDeviceSsh(unittest.TestCase):
911
def tearDown(self):
1012
self.dev.close()
1113

1214
def test_device_open_key_pass(self):
1315
self.dev = Device(
14-
host="xxxx",
15-
user="jenkins",
16-
ssh_private_key_file="/var/lib/jenkins/.ssh/passkey",
17-
passwd="password",
16+
host="x.x.x.x",
17+
user="netops",
18+
ssh_private_key_file="~/.ssh/id_rsa",
19+
passwd="net123",
20+
)
21+
self.dev.open()
22+
self.assertEqual(self.dev.connected, True)
23+
24+
def test_device_open_password(self):
25+
self.dev = Device(
26+
host="x.x.x.x",
27+
user="netops",
28+
passwd="net123",
29+
)
30+
self.dev.open()
31+
self.assertEqual(self.dev.connected, True)
32+
33+
def test_device_open_ssh_agent_true(self):
34+
self.dev = Device(
35+
host="x.x.x.x",
36+
user="netops",
37+
allow_agent=True
38+
)
39+
self.dev.open()
40+
self.assertEqual(self.dev.connected, True)
41+
42+
def test_device_open_ssh_agent_false(self):
43+
self.dev = Device(
44+
host="x.x.x.x",
45+
user="netops",
46+
allow_agent=False,
47+
)
48+
self.dev.open()
49+
self.assertEqual(self.dev.connected, True)
50+
51+
def test_device_open_key_file(self):
52+
self.dev = Device(
53+
host="x.x.x.x",
54+
user="netops",
55+
ssh_private_key_file="~/.ssh/id_rsa",
56+
)
57+
self.dev.open()
58+
self.assertEqual(self.dev.connected, True)
59+
60+
def test_device_open_key_file(self):
61+
self.dev = Device(
62+
host="x.x.x.x",
63+
user="netops",
64+
ssh_private_key_file="~/.ssh/id_rsa",
65+
)
66+
self.dev.open()
67+
self.assertEqual(self.dev.connected, True)
68+
69+
def test_device_open_proxy(self):
70+
self.dev = Device(
71+
host="x.x.x.x",
72+
user="netops",
73+
proxy_command="ssh -J netops@y.y.y.y"
74+
)
75+
self.dev.open()
76+
self.assertEqual(self.dev.connected, True)
77+
78+
def test_device_open_ssh_agent_proxy(self):
79+
self.dev = Device(
80+
host="x.x.x.x",
81+
user="netops",
82+
proxy_command="ssh -J netops@y.y.y.y",
83+
allow_agent=True,
84+
)
85+
self.dev.open()
86+
self.assertEqual(self.dev.connected, True)
87+
88+
def test_device_open_key_file_proxy(self):
89+
self.dev = Device(
90+
host="x.x.x.x",
91+
user="netops",
92+
proxy_command="ssh -J netops@y.y.y.y",
93+
ssh_private_key_file="~/.ssh/id_rsa",
94+
)
95+
self.dev.open()
96+
self.assertEqual(self.dev.connected, True)
97+
98+
def test_device_open_ssh_agent_proxy(self):
99+
self.dev = Device(
100+
host="x.x.x.x",
101+
user="netops",
102+
proxy_command="ssh -J netops@y.y.y.y",
103+
allow_agent=True,
104+
)
105+
self.dev.open()
106+
self.assertEqual(self.dev.connected, True)
107+
108+
def test_device_open_key_file_proxy(self):
109+
self.dev = Device(
110+
host="x.x.x.x",
111+
user="netops",
112+
proxy_command="ssh -J netops@y.y.y.y",
113+
ssh_private_key_file="~/.ssh/id_rsa",
18114
)
19115
self.dev.open()
20116
self.assertEqual(self.dev.connected, True)

0 commit comments

Comments
 (0)