Skip to content

Commit 6e9dd1f

Browse files
committed
Install fail2ban using system manager
Introducing a system manager fail2ban module. This module introduce two extra filters and fails on top of the builtin ones adapted from the current ansible deployment for postgresql and pgbouncer.
1 parent ee5fe34 commit 6e9dd1f

File tree

10 files changed

+108
-83
lines changed

10 files changed

+108
-83
lines changed

ansible/files/fail2ban_config/fail2ban.service.conf

Lines changed: 0 additions & 6 deletions
This file was deleted.

ansible/files/fail2ban_config/jail.local

Lines changed: 0 additions & 4 deletions
This file was deleted.

ansible/tasks/setup-fail2ban.yml

Lines changed: 0 additions & 70 deletions
This file was deleted.

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nix/systemConfigs.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
let
33
mkModules = system: [
44
self.systemModules.ssh-config
5+
self.systemModules.fail2ban
56
({
67
nixpkgs.hostPlatform = system;
8+
supabase.services.fail2ban.enable = true;
79
})
810
];
911

nix/systemModules/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
mode = "0644";
1919
};
2020
};
21+
fail2ban = ./fail2ban.nix;
2122
};
2223
};
2324
}

nix/systemModules/fail2ban.nix

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
lib,
3+
nixosModulesPath,
4+
config,
5+
pkgs,
6+
...
7+
}:
8+
let
9+
cfg = config.supabase.services.fail2ban;
10+
in
11+
{
12+
imports = [
13+
"${nixosModulesPath}/services/security/fail2ban.nix"
14+
];
15+
16+
options = {
17+
18+
services.openssh.settings.logLevel = lib.mkOption {
19+
type = lib.types.str;
20+
};
21+
# Create a dummy openssh option to unbreak the
22+
# > The option `services.openssh.settings' does not exist.
23+
# we face when importing the NixOS fail2ban.nix module.
24+
#
25+
# Note: the fail2ban module is trying to increase the log
26+
# verbosity of the openssh daemon to simplify debug. We don't
27+
# really need this feature: system-manager is not controlling the
28+
# ssh daemon here.
29+
#
30+
# TOREMOVE if we end up provisionning openssh through
31+
# systemmanager.
32+
services.openssh.settings = lib.mkOption {
33+
type = lib.types.attrs;
34+
};
35+
# Some goes for nftables
36+
networking.nftables.enable = lib.mkEnableOption "dummy nftable module";
37+
38+
# TODO move to iptables
39+
supabase.services.fail2ban = {
40+
enable = lib.mkEnableOption "Fail2Ban";
41+
};
42+
};
43+
44+
config = lib.mkIf cfg.enable {
45+
# Dummy
46+
networking.nftables.enable = true;
47+
services.fail2ban = {
48+
enable = true;
49+
bantime = "3600";
50+
packageFirewall = pkgs.nftables;
51+
jails = {
52+
postgresql = {
53+
settings = {
54+
enabled = true;
55+
port = "5432";
56+
protocol = "tcp";
57+
filter = "postgresql";
58+
logpath = "/var/log/postgresql/auth-failures.csv";
59+
maxretry = 3;
60+
ignoreip = "192.168.0.0/16 172.17.1.0/20";
61+
};
62+
};
63+
pgbouncer = {
64+
settings = {
65+
enabled = true;
66+
port = "6543";
67+
protocol = "tcp";
68+
filter = "pgbouncer";
69+
backend = "systemd[journalflags=1]";
70+
maxretry = 3;
71+
};
72+
};
73+
};
74+
};
75+
76+
environment.etc = {
77+
"fail2ban/filter.d/postgresql.conf".source = ./postgresql-filter.conf;
78+
"fail2ban/filter.d/pgbouncer.conf".source = ./pgbouncer.conf;
79+
};
80+
81+
systemd.services.fail2ban = {
82+
wantedBy = lib.mkForce [
83+
"system-manager.target"
84+
];
85+
};
86+
};
87+
}

nix/systemModules/pgbouncer.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[Init]
2+
maxlines = 3
3+
4+
[Definition]
5+
failregex = ^.+@<HOST>:.+password authentication failed$
6+
^.+@<HOST>:.+pooler error: no such user$
7+
^.+@<HOST>:.+registered new auto-database.*\n.*\n.*server login failed: FATAL database ".+" does not exist.*$
8+
journalmatch = _SYSTEMD_UNIT=pgbouncer.service
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[Definition]
2+
failregex = ^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user.*$
3+
^.*no pg_hba\.conf entry for host "<HOST>",.*$
4+
ignoreregex = ^.*,.*,.*,.*,"127\.0\.0\.1.*password authentication failed for user.*$

nix/systemModules/tests/default.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
assert machine.file("/etc/ssh/sshd_config.d/local.conf").user == "root", "/etc/ssh/sshd_config.d/local.conf should be owned by root"
3131
assert machine.file("/etc/ssh/sshd_config.d/local.conf").group == "root", "/etc/ssh/sshd_config.d/local.conf should be owned by root"
3232
assert machine.file("/etc/ssh/sshd_config.d/local.conf").contains("Match Address"), "/etc/ssh/sshd_config.d/local.conf should contain 'Match Address'"
33+
with subtest("Verify system manager config"):
34+
machine.wait_for_unit("fail2ban.service")
35+
3336
'';
3437
};
3538
};

0 commit comments

Comments
 (0)