Skip to content

Commit 742fa3a

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 742fa3a

7 files changed

Lines changed: 104 additions & 3 deletions

File tree

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

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)