Skip to content

Commit 0de802b

Browse files
FrenkenFloreshthiery
authored andcommitted
fix: add long password ipmi error
The given IPMI password can be longer than IPMI expect. In that case the _parse_output method in Ipmitool's will output the following error "invalid literal for int() with base 16: 'lanplus:'". While if we look at the line that was taken from IPMI we will see that its "lanplus: password is longer than 20 bytes.". So the issue is that there is no check for this error and the function skips it and tries to convert that string from a hex into an integer. I have added an execption, regex check, the match check and a test function. Signed-off-by: Evloev Sayfuddin <[email protected]>
1 parent f0df31e commit 0de802b

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

pyipmi/errors.py

+9
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ def __init__(self, msg=None):
9090

9191
def __str__(self):
9292
return "{}".format(self.msg)
93+
94+
95+
class IpmiLongPasswordError(Exception):
96+
"""Password longer than 20 bytes."""
97+
def __init__(self, msg=None):
98+
self.msg = msg
99+
100+
def __str__(self):
101+
return "{}".format(self.msg)

pyipmi/interfaces/ipmitool.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from array import array
2222

2323
from ..session import Session
24-
from ..errors import IpmiTimeoutError, IpmiConnectionError
24+
from ..errors import IpmiTimeoutError, IpmiConnectionError, IpmiLongPasswordError
2525
from ..logger import log
2626
from ..msgs import encode_message, decode_message, create_message
2727
from ..msgs.constants import CC_OK
@@ -62,7 +62,8 @@ def __init__(self, interface_type='lan', cipher=None):
6262
r".*Unable to establish.*")
6363
self.re_could_not_open = re.compile(
6464
r".*Could not open device.*")
65-
65+
self.re_long_password = re.compile(
66+
r".*password is longer than.*")
6667
self._session = None
6768

6869
def open(self):
@@ -135,6 +136,9 @@ def _parse_output(self, output):
135136
if self.re_could_not_open.match(line):
136137
raise RuntimeError('ipmitool failed: {}'.format(output))
137138

139+
if self.re_long_password.match(line):
140+
raise IpmiLongPasswordError(line)
141+
138142
hexstr += line.replace('\r', '').strip() + ' '
139143

140144
hexstr = hexstr.strip()

tests/interfaces/test_ipmitool.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from pyipmi.errors import IpmiTimeoutError, IpmiConnectionError
8+
from pyipmi.errors import IpmiTimeoutError, IpmiConnectionError, IpmiLongPasswordError
99
from pyipmi.interfaces import Ipmitool
1010
from pyipmi import Session, Target
1111
from pyipmi.utils import py3_array_tobytes
@@ -223,3 +223,9 @@ def test_parse_output_connection_error_rmcp(self):
223223
with pytest.raises(IpmiConnectionError):
224224
cc, rsp = self._interface._parse_output(test_str)
225225
assert rsp is None
226+
227+
def test_parse_long_password_error(self):
228+
test_str = b'lanplus: password is longer than 20 bytes.'
229+
with pytest.raises(IpmiLongPasswordError):
230+
cc, rsp = self._interface._parse_output(test_str)
231+
assert rsp is None

0 commit comments

Comments
 (0)