Skip to content

Commit

Permalink
Ssh passwd auth (#277)
Browse files Browse the repository at this point in the history
* Fixed password authentication for ssh client. Added tests.
* Simplify exceptions
* Added pre-commit to dev requirements. Updated single clients
* Updated changelog
  • Loading branch information
pkittenis authored Jan 28, 2021
1 parent 1bdd4ef commit cf6b842
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 29 deletions.
9 changes: 5 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ jobs:
name: Deps
command: |
sudo apt-get install openssh-server --fix-missing
- run:
command: |
pip install -U -r requirements_dev.txt
name: Build
- python/save-cache:
dependency-file: requirements_dev.txt
key: depsv3-{{ .Branch }}.{{ arch }}-PY<< parameters.python_ver >>
- run:
command: |
python setup.py check --restructuredtext
name: Check readme
- run:
command: |
pip install -U -r requirements_dev.txt
name: Build
- run:
command: |
flake8 pssh
name: flake
- run:
command: |
set -x
eval "$(ssh-agent -s)"
pytest --cov-append --cov=pssh tests/test_output.py tests/test_utils.py tests/test_host_config.py
pytest --reruns 10 --cov-append --cov=pssh tests/native/test_tunnel.py tests/native/test_agent.py
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
repos:
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
exclude: ^[tests,doc,versioneer]
10 changes: 10 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Change Log
============


2.5.4
+++++

Fixes
------

* Password authentication via ``pssh.clients.ssh`` would not work - #276


2.5.3
+++++

Expand Down
7 changes: 2 additions & 5 deletions pssh/clients/native/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from ..base.single import BaseSSHClient
from ...output import HostOutput
from ...exceptions import SessionError, SFTPError, \
SFTPIOError, Timeout, SCPError, ProxyError, AuthenticationError
SFTPIOError, Timeout, SCPError, ProxyError
from ...constants import DEFAULT_RETRIES, RETRY_DELAY


Expand Down Expand Up @@ -226,10 +226,7 @@ def _pkey_auth(self, pkey_file, password=None):
passphrase=password if password is not None else b'')

def _password_auth(self):
try:
self.session.userauth_password(self.user, self.password)
except Exception as ex:
raise AuthenticationError("Password authentication failed - %s", ex)
self.session.userauth_password(self.user, self.password)

def _open_session(self):
chan = self._eagain(self.session.open_session)
Expand Down
7 changes: 2 additions & 5 deletions pssh/clients/ssh/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from ..base.single import BaseSSHClient
from ..common import _validate_pkey_path
from ...output import HostOutput
from ...exceptions import AuthenticationError, SessionError, Timeout
from ...exceptions import SessionError, Timeout
from ...constants import DEFAULT_RETRIES, RETRY_DELAY


Expand Down Expand Up @@ -168,10 +168,7 @@ def auth(self):
return super(SSHClient, self).auth()

def _password_auth(self):
try:
self.session.userauth_password(self.password)
except Exception as ex:
raise AuthenticationError("Password authentication failed - %s", ex)
self.session.userauth_password(self.user, self.password)

def _pkey_auth(self, pkey_file, password=None):
pkey = import_privkey_file(pkey_file, passphrase=password if password is not None else '')
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pytest<6.1.0
pytest-cov
pytest-rerunfailures
jinja2
pre-commit
-r requirements.txt
-e .
33 changes: 22 additions & 11 deletions tests/native/test_single_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@

from pssh.clients.native import SSHClient
from ssh2.session import Session
from ssh2.exceptions import SocketDisconnectError, BannerRecvError, SocketRecvError, \
AgentConnectionError, AgentListIdentitiesError, \
AgentAuthenticationError, AgentGetIdentityError, SFTPProtocolError
from pssh.exceptions import AuthenticationException, ConnectionErrorException, \
SessionError, SFTPIOError, SFTPError, SCPError, PKeyFileError, Timeout, \
AuthenticationError
from ssh2.exceptions import (SocketDisconnectError, BannerRecvError, SocketRecvError,
AgentConnectionError, AgentListIdentitiesError,
AgentAuthenticationError, AgentGetIdentityError, SFTPProtocolError,
AuthenticationError as SSH2AuthenticationError,
)
from pssh.exceptions import (AuthenticationException, ConnectionErrorException,
SessionError, SFTPIOError, SFTPError, SCPError, PKeyFileError, Timeout,
AuthenticationError,
)

from .base_ssh2_case import SSH2TestCase

Expand Down Expand Up @@ -292,10 +295,16 @@ def test_identity_auth_failure(self):
allow_agent=False)

def test_password_auth_failure(self):
self.assertRaises(AuthenticationException,
SSHClient, self.host, port=self.port, num_retries=1,
allow_agent=False,
password='blah blah blah')
try:
client = SSHClient(self.host, port=self.port, num_retries=1,
allow_agent=False,
identity_auth=False,
password='blah blah blah',
)
except AuthenticationException as ex:
self.assertIsInstance(ex.args[3], SSH2AuthenticationError)
else:
raise AssertionError

def test_retry_failure(self):
self.assertRaises(ConnectionErrorException,
Expand All @@ -311,7 +320,9 @@ def test_auth_retry_failure(self):
password='fake',
num_retries=3,
retry_delay=.1,
allow_agent=False)
allow_agent=False,
identity_auth=False,
)

def test_connection_timeout(self):
cmd = spawn(SSHClient, 'fakehost.com', port=12345,
Expand Down
15 changes: 11 additions & 4 deletions tests/ssh/test_single_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,16 @@ def test_identity_auth_failure(self):
allow_agent=False)

def test_password_auth_failure(self):
self.assertRaises(AuthenticationError,
SSHClient, self.host, port=self.port, num_retries=1,
allow_agent=False,
password='blah blah blah')
try:
client = SSHClient(self.host, port=self.port, num_retries=1,
allow_agent=False,
identity_auth=False,
password='blah blah blah',
)
except AuthenticationException as ex:
self.assertIsInstance(ex.args[3], AuthenticationDenied)
else:
raise AssertionError

def test_retry_failure(self):
self.assertRaises(ConnectionErrorException,
Expand All @@ -221,6 +227,7 @@ def test_auth_retry_failure(self):
retry_delay=.1,
num_retries=2,
allow_agent=False,
identity_auth=False,
)

def test_connection_timeout(self):
Expand Down

0 comments on commit cf6b842

Please sign in to comment.