2014-03-29 22:07:33 +00:00
|
|
|
.. __ _ _ ___ _
|
2013-09-26 02:12:36 +00:00
|
|
|
/ _|__ _(_) |_ ) |__ __ _ _ _
|
|
|
|
| _/ _` | | |/ /| '_ \/ _` | ' \
|
2012-06-26 16:25:52 +00:00
|
|
|
|_| \__,_|_|_/___|_.__/\__,_|_||_|
|
|
|
|
|
|
|
|
================================================================================
|
|
|
|
How to develop for Fail2Ban
|
|
|
|
================================================================================
|
|
|
|
|
2012-10-03 13:12:37 +00:00
|
|
|
Fail2Ban uses GIT (http://git-scm.com/) distributed source control. This gives
|
|
|
|
each developer their own complete copy of the entire repository. Developers can
|
|
|
|
add and switch branches and commit changes when ever they want and then ask a
|
2012-06-26 16:25:52 +00:00
|
|
|
maintainer to merge their changes.
|
|
|
|
|
2012-10-03 13:12:37 +00:00
|
|
|
Fail2Ban uses GitHub (https://github.com/fail2ban/fail2ban) to manage access to
|
|
|
|
the Git repository. GitHub provides free hosting for open-source projects as
|
2012-06-26 16:25:52 +00:00
|
|
|
well as a web-based Git repository browser and an issue tracker.
|
|
|
|
|
2012-10-03 13:12:37 +00:00
|
|
|
If you are familiar with Python and you have a bug fix or a feature that you
|
|
|
|
would like to add to Fail2Ban, the best way to do so it to use the GitHub Pull
|
|
|
|
Request feature. You can find more details on the Fail2Ban wiki
|
2012-06-26 16:25:52 +00:00
|
|
|
(http://www.fail2ban.org/wiki/index.php/Get_Involved)
|
|
|
|
|
2013-04-17 18:48:51 +00:00
|
|
|
Pull Requests
|
|
|
|
=============
|
|
|
|
|
|
|
|
When submitting pull requests on GitHub we ask you to:
|
2014-03-29 22:07:33 +00:00
|
|
|
|
2013-04-17 18:48:51 +00:00
|
|
|
* Clearly describe the problem you're solving;
|
2013-09-26 02:12:36 +00:00
|
|
|
* Don't introduce regressions that will make it hard for systems administrators
|
2013-04-17 18:48:51 +00:00
|
|
|
to update;
|
2013-04-17 21:09:07 +00:00
|
|
|
* If adding a major feature rebase your changes on master and get to a single commit;
|
2013-04-17 18:48:51 +00:00
|
|
|
* Include test cases (see below);
|
|
|
|
* Include sample logs (if relevant);
|
|
|
|
* Include a change to the relevant section of the ChangeLog; and
|
|
|
|
* Include yourself in THANKS if not already there.
|
|
|
|
|
2013-12-29 07:26:41 +00:00
|
|
|
If you are developing filters see the FILTERS file for documentation.
|
2013-05-30 00:25:03 +00:00
|
|
|
|
|
|
|
Code Testing
|
|
|
|
============
|
|
|
|
|
2013-06-13 01:30:47 +00:00
|
|
|
Existing tests can be run by executing `bin/fail2ban-testcases`. It has
|
|
|
|
options like --log-level that will probably be useful. Run
|
|
|
|
`bin/fail2ban-testcases --help` for the full list of options.
|
2013-03-10 04:18:42 +00:00
|
|
|
|
|
|
|
Test cases should cover all usual cases, all exception cases and all inside
|
|
|
|
/ outside boundary conditions.
|
|
|
|
|
|
|
|
Test cases should cover all branches. The coverage tool will help identify
|
|
|
|
missing branches. Also see http://nedbatchelder.com/code/coverage/branch.html
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
Install the package python-coverage to visualise your test coverage. Run the
|
2013-03-11 01:21:27 +00:00
|
|
|
following (note: on Debian-based systems, the script is called
|
2014-03-29 22:07:33 +00:00
|
|
|
`python-coverage`)::
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2014-03-29 22:07:33 +00:00
|
|
|
coverage run bin/fail2ban-testcases
|
2015-07-08 00:03:34 +00:00
|
|
|
coverage report
|
|
|
|
|
|
|
|
Optionally:
|
2014-03-29 22:07:33 +00:00
|
|
|
coverage html
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2015-07-08 00:03:34 +00:00
|
|
|
And then browse htmlcov/index.html and see how much coverage your test cases
|
2013-09-19 09:18:04 +00:00
|
|
|
exert over the code base. Full coverage is a good thing however it may not be
|
2013-03-22 13:26:33 +00:00
|
|
|
complete. Try to ensure tests cover as many independent paths through the
|
2013-03-10 04:18:42 +00:00
|
|
|
code.
|
|
|
|
|
2014-03-29 22:07:33 +00:00
|
|
|
Manual Execution. To run in a development environment do::
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2014-03-29 22:07:33 +00:00
|
|
|
./fail2ban-client -c config/ -s /tmp/f2b.sock -i start
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2014-03-29 22:07:33 +00:00
|
|
|
some quick commands::
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2014-03-29 22:07:33 +00:00
|
|
|
status
|
|
|
|
add test pyinotify
|
|
|
|
status test
|
|
|
|
set test addaction iptables
|
|
|
|
set test actionban iptables echo <ip> <cidr> >> /tmp/ban
|
|
|
|
set test actionunban iptables echo <ip> <cidr> >> /tmp/unban
|
|
|
|
get test actionban iptables
|
|
|
|
get test actionunban iptables
|
|
|
|
set test banip 192.168.2.2
|
|
|
|
status test
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2012-06-27 03:16:16 +00:00
|
|
|
|
2014-10-25 14:38:18 +00:00
|
|
|
Testing with vagrant
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
Testing can now be done inside a vagrant VM. Vagrantfile provided in
|
|
|
|
source code repository established two VMs:
|
|
|
|
|
|
|
|
- VM "secure" which can be used for testing fail2ban code.
|
2015-07-07 15:44:27 +00:00
|
|
|
- VM "attacker" which can be used to perform attack against our "secure" VM.
|
2014-10-25 14:38:18 +00:00
|
|
|
|
|
|
|
Both VMs are sharing the 192.168.200/24 network. If you are using this network
|
|
|
|
take a look into the Vagrantfile and change the IP.
|
|
|
|
|
2012-06-26 16:25:52 +00:00
|
|
|
|
2012-06-29 16:59:26 +00:00
|
|
|
Coding Standards
|
2013-03-22 13:26:33 +00:00
|
|
|
================
|
2013-03-10 04:18:42 +00:00
|
|
|
|
|
|
|
Style
|
|
|
|
-----
|
|
|
|
|
|
|
|
Please use tabs for now. Keep to 80 columns, at least for readable text.
|
|
|
|
|
|
|
|
Tests
|
|
|
|
-----
|
|
|
|
|
|
|
|
Add tests. They should test all the code you add in a meaning way.
|
|
|
|
|
|
|
|
Coverage
|
|
|
|
--------
|
|
|
|
|
|
|
|
Test coverage should always increase as you add code.
|
|
|
|
|
|
|
|
You may use "# pragma: no cover" in the code for branches of code that support
|
|
|
|
older versions on python. For all other uses of "pragma: no cover" or
|
|
|
|
"pragma: no branch" document the reason why its not covered. "I haven't written
|
|
|
|
a test case" isn't a sufficient reason.
|
|
|
|
|
2014-03-16 22:26:10 +00:00
|
|
|
pyflakes
|
|
|
|
--------
|
|
|
|
|
|
|
|
pyflakes can be used to find unused imports, and unused, undefined and
|
|
|
|
redefined variables. pyflakes should be run in any python code, including
|
|
|
|
python based actions::
|
|
|
|
|
|
|
|
pyflakes bin/ config/ fail2ban/
|
|
|
|
|
2013-03-10 04:18:42 +00:00
|
|
|
Documentation
|
|
|
|
-------------
|
|
|
|
|
|
|
|
Ensure this documentation is up to date after changes. Also ensure that the man
|
2013-03-10 22:05:33 +00:00
|
|
|
pages still are accurate. Ensure that there is sufficient documentation for
|
2013-03-10 04:18:42 +00:00
|
|
|
your new features to be used.
|
|
|
|
|
|
|
|
Bugs
|
|
|
|
----
|
|
|
|
|
|
|
|
Remove them and don't add any more.
|
|
|
|
|
|
|
|
Git
|
|
|
|
---
|
|
|
|
|
|
|
|
Use the following tags in your commit messages:
|
|
|
|
|
2014-03-29 22:07:33 +00:00
|
|
|
* 'BF:' for bug fixes
|
|
|
|
* 'DOC:' for documentation fixes
|
|
|
|
* 'ENH:' for enhancements
|
|
|
|
* 'TST:' for commits concerning tests only (thus not touching the main code-base)
|
2013-03-22 13:26:33 +00:00
|
|
|
|
|
|
|
Multiple tags could be joined with +, e.g. "BF+TST:".
|
2013-03-10 04:18:42 +00:00
|
|
|
|
2013-07-08 22:51:11 +00:00
|
|
|
Use the text "closes #333"/"resolves #333 "/"fixes #333" where 333 represents
|
|
|
|
an issue that is closed. Other text and details in link below.
|
|
|
|
See: https://help.github.com/articles/closing-issues-via-commit-messages
|
|
|
|
|
2013-07-18 18:43:41 +00:00
|
|
|
If merge resulted in conflicts, clarify what changes were done to
|
|
|
|
corresponding files in the 'Conflicts:' section of the merge commit
|
|
|
|
message. See e.g. https://github.com/fail2ban/fail2ban/commit/f5a8a8ac
|
|
|
|
|
2013-03-10 04:18:42 +00:00
|
|
|
Adding Actions
|
|
|
|
--------------
|
|
|
|
|
|
|
|
If you add an action.d/*.conf file also add a example in config/jail.conf
|
|
|
|
with enabled=false and maxretry=5 for ssh.
|
2012-06-29 16:59:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
Design
|
|
|
|
======
|
|
|
|
|
|
|
|
Fail2Ban was initially developed with Python 2.3 (IIRC). It should
|
|
|
|
still be compatible with Python 2.4 and such compatibility assurance
|
|
|
|
makes code ... old-fashioned in many places (RF-Note). In 0.7 the
|
2013-09-19 09:18:04 +00:00
|
|
|
design went through major re-factoring into client/server,
|
2012-06-29 16:59:26 +00:00
|
|
|
a-thread-per-jail design which made it a bit difficult to follow.
|
|
|
|
Below you can find a sketchy description of the main components of the
|
|
|
|
system to orient yourself better.
|
|
|
|
|
|
|
|
server/
|
|
|
|
------
|
|
|
|
|
|
|
|
Core classes hierarchy (feel welcome to draw a better/more complete
|
|
|
|
one)::
|
|
|
|
|
|
|
|
-> inheritance
|
|
|
|
+ delegation
|
|
|
|
* storage of multiple instances
|
|
|
|
|
|
|
|
RF-Note just a note which might be useful to address while doing RF
|
|
|
|
|
|
|
|
JailThread -> Filter -> FileFilter -> {FilterPoll, FilterPyinotify, ...}
|
2012-11-06 02:12:03 +00:00
|
|
|
| * FileContainer
|
|
|
|
+ FailManager
|
|
|
|
+ DateDetector
|
|
|
|
+ Jail (provided in __init__) which contains this Filter
|
|
|
|
(used for passing tickets from FailManager to Jail's __queue)
|
2012-06-29 16:59:26 +00:00
|
|
|
Server
|
|
|
|
+ Jails
|
|
|
|
* Jail
|
2012-11-06 02:12:03 +00:00
|
|
|
+ Filter (in __filter)
|
2012-06-29 16:59:26 +00:00
|
|
|
* tickets (in __queue)
|
2012-11-06 02:12:03 +00:00
|
|
|
+ Actions (in __action)
|
|
|
|
* Action
|
|
|
|
+ BanManager
|
|
|
|
|
2012-06-29 16:59:26 +00:00
|
|
|
|
|
|
|
failmanager.py
|
|
|
|
~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
FailManager
|
|
|
|
|
|
|
|
Keeps track of failures, recorded as 'tickets'. All operations are
|
|
|
|
done via acquiring a lock
|
|
|
|
|
|
|
|
FailManagerEmpty(Exception)
|
|
|
|
|
|
|
|
raised by FailManager.toBan after reaching the list of tickets
|
|
|
|
(RF-Note: asks to become a generator ;) )
|
|
|
|
|
|
|
|
|
|
|
|
filter.py
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
|
|
|
Filter(JailThread)
|
|
|
|
|
|
|
|
Wraps (non-threaded) FailManager (and proxies to it quite a bit),
|
|
|
|
and provides all primary logic for processing new lines, what IPs to
|
|
|
|
ignore, etc
|
|
|
|
|
|
|
|
.failManager [FailManager]
|
|
|
|
.dateDetector [DateDetector]
|
|
|
|
.__failRegex [list]
|
|
|
|
.__ignoreRegex [list]
|
|
|
|
Contains regular expressions for failures and ignores
|
|
|
|
.__findTime [numeric]
|
|
|
|
Used in `processLineAndAdd` to skip old lines
|
|
|
|
|
|
|
|
FileFilter(Filter):
|
|
|
|
|
|
|
|
Files-aware Filter
|
|
|
|
|
|
|
|
.__logPath [list]
|
|
|
|
keeps the tracked files (added 1-by-1 using addLogPath)
|
|
|
|
stored as FileContainer's
|
|
|
|
.getFailures
|
|
|
|
actually just returns
|
|
|
|
True
|
|
|
|
if managed to open and get lines (until empty)
|
|
|
|
False
|
|
|
|
if failed to open or absent container matching the filename
|
|
|
|
|
|
|
|
FileContainer
|
|
|
|
|
|
|
|
Adapter for a file to deal with log rotation.
|
|
|
|
|
|
|
|
.open,.close,.readline
|
|
|
|
RF-Note: readline returns "" with handler absent... shouldn't it be None?
|
|
|
|
.__pos
|
|
|
|
Keeps the position pointer
|
|
|
|
|
2013-03-10 04:18:42 +00:00
|
|
|
|
|
|
|
dnsutils.py
|
|
|
|
~~~~~~~~~~~
|
|
|
|
|
2012-06-29 16:59:26 +00:00
|
|
|
DNSUtils
|
|
|
|
|
|
|
|
Utility class for DNS and IP handling
|
|
|
|
|
|
|
|
|
|
|
|
filter*.py
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
|
|
|
Implementations of FileFilter's for specific backends. Derived
|
|
|
|
classes should provide an implementation of `run` and usually
|
2012-07-19 05:04:05 +00:00
|
|
|
override `addLogPath`, `delLogPath` methods. In run() method they all
|
|
|
|
one way or another provide
|
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
ticket = self.failManager.toBan()
|
|
|
|
self.jail.putFailTicket(ticket)
|
|
|
|
except FailManagerEmpty:
|
|
|
|
self.failManager.cleanup(MyTime.time())
|
|
|
|
|
2013-09-19 09:18:04 +00:00
|
|
|
thus channelling "ban tickets" from their failManager to the
|
2012-07-19 05:04:05 +00:00
|
|
|
corresponding jail.
|
2012-06-29 16:59:26 +00:00
|
|
|
|
|
|
|
action.py
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
|
|
Takes care about executing start/check/ban/unban/stop commands
|
2013-03-10 04:18:42 +00:00
|
|
|
|