Skip to content

Commit 1bb8067

Browse files
fix: add test for publickey auth using wrong key
1 parent 06264c4 commit 1bb8067

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

tests/integration/test_ncclient.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,30 @@ def test_when_connecting_using_username_and_rsa_key_then_authentication_passes(
266266
) as m:
267267
# THEN expect to be connected
268268
assert m.connected
269+
270+
271+
def test_when_connecting_using_username_and_wrong_key_then_authentication_fails(
272+
netconf_server, tmp_path
273+
):
274+
# GIVEN generated key
275+
key_filepath = (tmp_path / "key").as_posix()
276+
key = paramiko.RSAKey.generate(bits=2048)
277+
key.write_private_key_file(key_filepath)
278+
279+
# GIVEN SSH username and a different key have been defined
280+
netconf_server.username = "admin"
281+
netconf_server.authorized_key = f"foobar"
282+
283+
# WHEN connecting using wrong key
284+
with pytest.raises(AuthenticationError) as error:
285+
with manager.connect(
286+
host="localhost",
287+
port=8830,
288+
username="foo",
289+
key_filename=key_filepath,
290+
hostkey_verify=False,
291+
):
292+
...
293+
294+
# THEN expect error
295+
assert error

tests/integration/test_netconf_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,29 @@ def test_when_connecting_using_username_and_rsa_key_then_authentication_passes(
264264
) as session:
265265
# THEN expect to be connected
266266
assert session.session_id
267+
268+
def test_when_connecting_using_username_and_wrong_key_then_authentication_fails(
269+
netconf_server, tmp_path
270+
):
271+
# GIVEN generated key
272+
key_filepath = (tmp_path / "key").as_posix()
273+
key = paramiko.RSAKey.generate(bits=2048)
274+
key.write_private_key_file(key_filepath)
275+
276+
# GIVEN SSH username and a different key have been defined
277+
netconf_server.username = "admin"
278+
netconf_server.authorized_key = f"foobar"
279+
280+
# WHEN connecting using wrong key
281+
with pytest.raises(paramiko.ssh_exception.AuthenticationException) as error:
282+
with connect_ssh(
283+
host="localhost",
284+
port=8830,
285+
username="foo",
286+
password=None,
287+
key_filename=key_filepath,
288+
) as session:
289+
Manager(session=session)
290+
291+
# THEN expect error
292+
assert "Authentication failed." in str(error)

tests/integration/test_scrapli_netconf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,29 @@ def test_when_connecting_using_username_and_rsa_key_then_authentication_passes(
280280
) as conn:
281281
# THEN expect to be connected
282282
assert conn.isalive()
283+
284+
def test_when_connecting_using_username_and_wrong_key_then_authentication_fails(
285+
netconf_server, tmp_path
286+
):
287+
# GIVEN generated key
288+
key_filepath = (tmp_path / "key").as_posix()
289+
key = paramiko.RSAKey.generate(bits=2048)
290+
key.write_private_key_file(key_filepath)
291+
292+
# GIVEN SSH username and a different key have been defined
293+
netconf_server.username = "admin"
294+
netconf_server.authorized_key = f"foobar"
295+
296+
# WHEN connecting using wrong key
297+
with pytest.raises(ScrapliConnectionError) as error:
298+
with NetconfDriver(
299+
host="localhost",
300+
port=8830,
301+
auth_username="foo",
302+
auth_private_key=key_filepath,
303+
auth_strict_key=False,
304+
):
305+
pass
306+
307+
# THEN expect error
308+
assert "permission denied, please try again." in str(error)

0 commit comments

Comments
 (0)