Skip to content

Commit cf6b842

Browse files
authored
Ssh passwd auth (#277)
* Fixed password authentication for ssh client. Added tests. * Simplify exceptions * Added pre-commit to dev requirements. Updated single clients * Updated changelog
1 parent 1bdd4ef commit cf6b842

File tree

8 files changed

+59
-29
lines changed

8 files changed

+59
-29
lines changed

.circleci/config.yml

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ jobs:
2020
name: Deps
2121
command: |
2222
sudo apt-get install openssh-server --fix-missing
23+
- run:
24+
command: |
25+
pip install -U -r requirements_dev.txt
26+
name: Build
2327
- python/save-cache:
2428
dependency-file: requirements_dev.txt
2529
key: depsv3-{{ .Branch }}.{{ arch }}-PY<< parameters.python_ver >>
2630
- run:
2731
command: |
2832
python setup.py check --restructuredtext
2933
name: Check readme
30-
- run:
31-
command: |
32-
pip install -U -r requirements_dev.txt
33-
name: Build
3434
- run:
3535
command: |
3636
flake8 pssh
3737
name: flake
3838
- run:
3939
command: |
40+
set -x
4041
eval "$(ssh-agent -s)"
4142
pytest --cov-append --cov=pssh tests/test_output.py tests/test_utils.py tests/test_host_config.py
4243
pytest --reruns 10 --cov-append --cov=pssh tests/native/test_tunnel.py tests/native/test_agent.py

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
repos:
2+
- repo: https://gitlab.com/pycqa/flake8
3+
rev: 3.7.9
4+
hooks:
5+
- id: flake8
6+
exclude: ^[tests,doc,versioneer]

Changelog.rst

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Change Log
22
============
33

4+
5+
2.5.4
6+
+++++
7+
8+
Fixes
9+
------
10+
11+
* Password authentication via ``pssh.clients.ssh`` would not work - #276
12+
13+
414
2.5.3
515
+++++
616

pssh/clients/native/single.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from ..base.single import BaseSSHClient
3535
from ...output import HostOutput
3636
from ...exceptions import SessionError, SFTPError, \
37-
SFTPIOError, Timeout, SCPError, ProxyError, AuthenticationError
37+
SFTPIOError, Timeout, SCPError, ProxyError
3838
from ...constants import DEFAULT_RETRIES, RETRY_DELAY
3939

4040

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

228228
def _password_auth(self):
229-
try:
230-
self.session.userauth_password(self.user, self.password)
231-
except Exception as ex:
232-
raise AuthenticationError("Password authentication failed - %s", ex)
229+
self.session.userauth_password(self.user, self.password)
233230

234231
def _open_session(self):
235232
chan = self._eagain(self.session.open_session)

pssh/clients/ssh/single.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from ..base.single import BaseSSHClient
2828
from ..common import _validate_pkey_path
2929
from ...output import HostOutput
30-
from ...exceptions import AuthenticationError, SessionError, Timeout
30+
from ...exceptions import SessionError, Timeout
3131
from ...constants import DEFAULT_RETRIES, RETRY_DELAY
3232

3333

@@ -168,10 +168,7 @@ def auth(self):
168168
return super(SSHClient, self).auth()
169169

170170
def _password_auth(self):
171-
try:
172-
self.session.userauth_password(self.password)
173-
except Exception as ex:
174-
raise AuthenticationError("Password authentication failed - %s", ex)
171+
self.session.userauth_password(self.user, self.password)
175172

176173
def _pkey_auth(self, pkey_file, password=None):
177174
pkey = import_privkey_file(pkey_file, passphrase=password if password is not None else '')

requirements_dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ pytest<6.1.0
77
pytest-cov
88
pytest-rerunfailures
99
jinja2
10+
pre-commit
1011
-r requirements.txt
1112
-e .

tests/native/test_single_client.py

+22-11
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626

2727
from pssh.clients.native import SSHClient
2828
from ssh2.session import Session
29-
from ssh2.exceptions import SocketDisconnectError, BannerRecvError, SocketRecvError, \
30-
AgentConnectionError, AgentListIdentitiesError, \
31-
AgentAuthenticationError, AgentGetIdentityError, SFTPProtocolError
32-
from pssh.exceptions import AuthenticationException, ConnectionErrorException, \
33-
SessionError, SFTPIOError, SFTPError, SCPError, PKeyFileError, Timeout, \
34-
AuthenticationError
29+
from ssh2.exceptions import (SocketDisconnectError, BannerRecvError, SocketRecvError,
30+
AgentConnectionError, AgentListIdentitiesError,
31+
AgentAuthenticationError, AgentGetIdentityError, SFTPProtocolError,
32+
AuthenticationError as SSH2AuthenticationError,
33+
)
34+
from pssh.exceptions import (AuthenticationException, ConnectionErrorException,
35+
SessionError, SFTPIOError, SFTPError, SCPError, PKeyFileError, Timeout,
36+
AuthenticationError,
37+
)
3538

3639
from .base_ssh2_case import SSH2TestCase
3740

@@ -292,10 +295,16 @@ def test_identity_auth_failure(self):
292295
allow_agent=False)
293296

294297
def test_password_auth_failure(self):
295-
self.assertRaises(AuthenticationException,
296-
SSHClient, self.host, port=self.port, num_retries=1,
297-
allow_agent=False,
298-
password='blah blah blah')
298+
try:
299+
client = SSHClient(self.host, port=self.port, num_retries=1,
300+
allow_agent=False,
301+
identity_auth=False,
302+
password='blah blah blah',
303+
)
304+
except AuthenticationException as ex:
305+
self.assertIsInstance(ex.args[3], SSH2AuthenticationError)
306+
else:
307+
raise AssertionError
299308

300309
def test_retry_failure(self):
301310
self.assertRaises(ConnectionErrorException,
@@ -311,7 +320,9 @@ def test_auth_retry_failure(self):
311320
password='fake',
312321
num_retries=3,
313322
retry_delay=.1,
314-
allow_agent=False)
323+
allow_agent=False,
324+
identity_auth=False,
325+
)
315326

316327
def test_connection_timeout(self):
317328
cmd = spawn(SSHClient, 'fakehost.com', port=12345,

tests/ssh/test_single_client.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,16 @@ def test_identity_auth_failure(self):
200200
allow_agent=False)
201201

202202
def test_password_auth_failure(self):
203-
self.assertRaises(AuthenticationError,
204-
SSHClient, self.host, port=self.port, num_retries=1,
205-
allow_agent=False,
206-
password='blah blah blah')
203+
try:
204+
client = SSHClient(self.host, port=self.port, num_retries=1,
205+
allow_agent=False,
206+
identity_auth=False,
207+
password='blah blah blah',
208+
)
209+
except AuthenticationException as ex:
210+
self.assertIsInstance(ex.args[3], AuthenticationDenied)
211+
else:
212+
raise AssertionError
207213

208214
def test_retry_failure(self):
209215
self.assertRaises(ConnectionErrorException,
@@ -221,6 +227,7 @@ def test_auth_retry_failure(self):
221227
retry_delay=.1,
222228
num_retries=2,
223229
allow_agent=False,
230+
identity_auth=False,
224231
)
225232

226233
def test_connection_timeout(self):

0 commit comments

Comments
 (0)