Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding IPv6 to fail2ban sensor #19457

Merged
merged 5 commits into from Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/sensor/fail2ban.py
Expand Up @@ -65,7 +65,7 @@ def __init__(self, name, jail, log_parser):
self.last_ban = None
self.log_parser = log_parser
self.log_parser.ip_regex[self.jail] = re.compile(
r"\[{}\].(Ban|Unban) ([\w+\.]{{3,}})".format(re.escape(self.jail))
r"\[{}\]\s*(Ban|Unban) (.*)".format(re.escape(self.jail))
)
_LOGGER.debug("Setting up jail %s", self.jail)

Expand Down
21 changes: 21 additions & 0 deletions tests/components/sensor/test_fail2ban.py
Expand Up @@ -20,6 +20,10 @@ def fake_log(log_key):
'2017-01-01 12:23:35 fail2ban.actions [111]: '
'NOTICE [jail_one] Ban 111.111.111.111'
),
'ipv6_ban': (
'2017-01-01 12:23:35 fail2ban.actions [111]: '
'NOTICE [jail_one] Ban 2607:f0d0:1002:51::4'
),
'multi_ban': (
'2017-01-01 12:23:35 fail2ban.actions [111]: '
'NOTICE [jail_one] Ban 111.111.111.111\n'
Expand Down Expand Up @@ -113,6 +117,23 @@ def test_single_ban(self):
assert \
sensor.state_attributes[STATE_ALL_BANS] == ['111.111.111.111']

def test_ipv6_ban(self):
"""Test that log is parsed correctly for IPV6 bans."""
log_parser = BanLogParser(timedelta(seconds=-1), '/tmp')
sensor = BanSensor('fail2ban', 'jail_one', log_parser)
assert sensor.name == 'fail2ban jail_one'
mock_fh = MockOpen(read_data=fake_log('ipv6_ban'))
with patch('homeassistant.components.sensor.fail2ban.open', mock_fh,
create=True):
sensor.update()

assert sensor.state == '2607:f0d0:1002:51::4'
assert \
sensor.state_attributes[STATE_CURRENT_BANS] == \
['2607:f0d0:1002:51::4']
grea09 marked this conversation as resolved.
Show resolved Hide resolved
assert \
sensor.state_attributes[STATE_ALL_BANS] == ['2607:f0d0:1002:51::4']

def test_multiple_ban(self):
"""Test that log is parsed correctly for multiple ban."""
log_parser = BanLogParser(timedelta(seconds=-1), '/tmp')
Expand Down