Skip to content

Instantly share code, notes, and snippets.

@ixs
Created December 8, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ixs/8fe55975789cac84263196384ec8ed92 to your computer and use it in GitHub Desktop.
Save ixs/8fe55975789cac84263196384ec8ed92 to your computer and use it in GitHub Desktop.
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: t -*-
# vi: set ft=python sts=4 ts=4 sw=4 noet :
# (c) Andreas Thienemann 2017
#
# Fail2Ban is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Fail2Ban is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Fail2Ban; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Send messages about fail2ban actions onto the salt event bus
#
import salt.client
from fail2ban.server.actions import ActionBase, CallingMap
class SaltEventAction(ActionBase):
'''
Fail2ban action class that is able to send salt events
'''
def __init__(self, jail, name):
super(SaltEventAction, self).__init__(jail, name)
self.caller = salt.client.Caller()
self.event_id = "{}/{}".format("fail2ban", jail.name)
self.vals = CallingMap(
jailname = self._jail.name,
bantime = lambda: self._jail.actions.getBanTime(),
)
def _sendMessage(self, action, ip = None, fcount = None, bantime = None):
event_msg = {
'action': action,
}
if ip:
event_msg.update({'ip': ip})
if fcount:
event_msg.update({'failures': fcount})
if bantime:
event_msg.update({'bantime': bantime})
self.caller.sminion.functions['event.send'](
'{}/{}'.format(self.event_id, action), event_msg)
def start(self):
self._sendMessage('startup')
def stop(self):
self._sendMessage('shutdown')
def ban(self, aInfo):
self._sendMessage('ban', aInfo['ip'], aInfo['failures'], self.vals['bantime'])
def unban(self, aInfo):
self._sendMessage('unban', aInfo['ip'])
Action = SaltEventAction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment