Skip to content

Commit ed29e0a

Browse files
committed
specify authentication error
Signed-off-by: evloev.sayfuddin <[email protected]>
1 parent e0fe45a commit ed29e0a

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

pyipmi/errors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,12 @@ def __init__(self, msg=None):
9999

100100
def __str__(self):
101101
return "{}".format(self.msg)
102+
103+
104+
class AuthenticationError(Exception):
105+
"""Authentication error."""
106+
def __init__(self, msg=None):
107+
self.msg = msg
108+
109+
def __str__(self):
110+
return "{}".format(self.msg)

pyipmi/interfaces/ipmitool.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
from array import array
2222

2323
from ..session import Session
24-
from ..errors import IpmiTimeoutError, IpmiConnectionError, IpmiLongPasswordError
24+
from ..errors import (
25+
IpmiTimeoutError,
26+
IpmiConnectionError,
27+
IpmiLongPasswordError,
28+
AuthenticationError,
29+
)
2530
from ..logger import log
2631
from ..msgs import encode_message, decode_message, create_message
2732
from ..msgs.constants import CC_OK
@@ -64,6 +69,8 @@ def __init__(self, interface_type='lan', cipher=None):
6469
r".*Could not open device.*")
6570
self.re_long_password = re.compile(
6671
r".*password is longer than.*")
72+
self.re_authentication_error = re.compile(
73+
r".*RAKP [0-9]+ HMAC.*")
6774
self._session = None
6875

6976
def open(self):
@@ -89,6 +96,7 @@ def rmcp_ping(self):
8996
cmd += (' -I %s' % self._interface_type)
9097
cmd += (' -H %s' % self._session.rmcp_host)
9198
cmd += (' -p %s' % self._session.rmcp_port)
99+
cmd += (' -v')
92100
if self._session.auth_type == Session.AUTH_TYPE_NONE:
93101
cmd += (' -A NONE')
94102
elif self._session.auth_type == Session.AUTH_TYPE_PASSWORD:
@@ -142,13 +150,19 @@ def _parse_output(self, output):
142150
if self.re_long_password.match(line):
143151
raise IpmiLongPasswordError(line)
144152

153+
if self.re_authentication_error.match(line):
154+
raise AuthenticationError('Authentication error')
155+
145156
hexstr += line.replace('\r', '').strip() + ' '
146157

147158
hexstr = hexstr.strip()
148159
if len(hexstr):
149-
rsp = array('B', [
150-
int(value, 16) for value in hexstr.split(' ')
151-
])
160+
try:
161+
rsp = array('B', [
162+
int(value, 16) for value in hexstr.split(' ')
163+
])
164+
except ValueError:
165+
pass
152166

153167
return cc, rsp
154168

@@ -250,6 +264,7 @@ def _build_ipmitool_cmd(self, target, lun, netfn, raw_bytes):
250264
cmd += (' -I %s' % self._interface_type)
251265
cmd += (' -H %s' % self._session.rmcp_host)
252266
cmd += (' -p %s' % self._session.rmcp_port)
267+
cmd += (' -v')
253268

254269
cmd += self._build_ipmitool_priv_level(self._session.priv_level)
255270

tests/interfaces/test_ipmitool.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from pyipmi.errors import IpmiTimeoutError, IpmiConnectionError, IpmiLongPasswordError
8+
from pyipmi.errors import IpmiTimeoutError, IpmiConnectionError, IpmiLongPasswordError, AuthenticationError
99
from pyipmi.interfaces import Ipmitool
1010
from pyipmi import Session, Target
1111
from pyipmi.utils import py3_array_tobytes
@@ -53,7 +53,7 @@ def test_rmcp_ping(self):
5353

5454
self._interface.rmcp_ping()
5555
mock.assert_called_once_with('ipmitool -I lan -H 10.0.1.1 -p 623 '
56-
'-U "admin" -P "secret" '
56+
'-v -U "admin" -P "secret" '
5757
'session info all')
5858

5959
def test_send_and_receive_raw_valid(self):
@@ -65,7 +65,7 @@ def test_send_and_receive_raw_valid(self):
6565
self._interface.send_and_receive_raw(target, 0, 0x6, b'\x01')
6666

6767
mock.assert_called_once_with('ipmitool -I lan -H 10.0.1.1 -p 623 '
68-
'-L ADMINISTRATOR -U "admin" -P "secret" '
68+
'-v -L ADMINISTRATOR -U "admin" -P "secret" '
6969
'-t 0x20 -l 0 raw 0x06 0x01 2>&1')
7070

7171
def test_send_and_receive_raw_lanplus(self):
@@ -80,7 +80,7 @@ def test_send_and_receive_raw_lanplus(self):
8080
interface.send_and_receive_raw(target, 0, 0x6, b'\x01')
8181

8282
mock.assert_called_once_with('ipmitool -I lanplus -H 10.0.1.1 -p 623 '
83-
'-L ADMINISTRATOR -U "admin" -P "secret" '
83+
'-v -L ADMINISTRATOR -U "admin" -P "secret" '
8484
'-t 0x20 -l 0 raw 0x06 0x01 2>&1')
8585

8686
def test_send_and_receive_raw_cipher(self):
@@ -95,7 +95,7 @@ def test_send_and_receive_raw_cipher(self):
9595
interface.send_and_receive_raw(target, 0, 0x6, b'\x01')
9696

9797
mock.assert_called_once_with('ipmitool -I lan -H 10.0.1.1 -p 623 '
98-
'-L ADMINISTRATOR -C 7 '
98+
'-v -L ADMINISTRATOR -C 7 '
9999
'-U "admin" -P "secret" '
100100
'-t 0x20 -l 0 raw 0x06 0x01 2>&1')
101101

@@ -110,7 +110,7 @@ def test_send_and_receive_raw_no_auth(self):
110110
self._interface.send_and_receive_raw(target, 0, 0x6, b'\x01')
111111

112112
mock.assert_called_once_with('ipmitool -I lan -H 10.0.1.1 -p 623 '
113-
'-L ADMINISTRATOR -P "" '
113+
'-v -L ADMINISTRATOR -P "" '
114114
'-t 0x20 -l 0 raw 0x06 0x01 2>&1')
115115

116116
def test_send_and_receive_raw_return_value(self):
@@ -236,3 +236,9 @@ def test_parse_long_password_error(self):
236236
with pytest.raises(IpmiLongPasswordError):
237237
cc, rsp = self._interface._parse_output(test_str)
238238
assert rsp is None
239+
240+
def test_parse_output_authentication_error(self):
241+
test_str = b'Loading ...\nUsing ...\n> RAKP 2 HMAC is invalid\nError:'
242+
with pytest.raises(AuthenticationError):
243+
cc, rsp = self._interface._parse_output(test_str)
244+
assert rsp is None

0 commit comments

Comments
 (0)