Skip to content

Commit c1ef52a

Browse files
committed
(WIP) Install fail2ban using system manager
1 parent 8fac60d commit c1ef52a

4 files changed

Lines changed: 108 additions & 1 deletion

File tree

nix/systemConfigs.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{ self, inputs, ... }:
22
let
33
mkModules = system: [
4+
self.systemModules.fail2ban
45
({
56
services.nginx.enable = true;
67
nixpkgs.hostPlatform = system;
8+
supabase.services.fail2ban.enable = true;
79
})
810
];
911

nix/systemModules/default.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{
55
imports = [ ./tests ];
66
flake = {
7-
systemModules = { };
7+
systemModules = {
8+
fail2ban = ./fail2ban.nix;
9+
};
810
};
911
}

nix/systemModules/fail2ban.nix

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
lib,
3+
nixosModulesPath,
4+
config,
5+
...
6+
}:
7+
let
8+
cfg = config.supabase.services.fail2ban;
9+
in
10+
{
11+
imports = map (path: nixosModulesPath + path) [
12+
# FIXME:
13+
# error: The option `users' in module `/nix/store/...-source/nix/modules'
14+
# would be a parent of the following options, but its type `attribute set' does not support nested options.
15+
# - option(s) with prefix `users.users' in module `/nix/store/...-source/nixos/modules/services/networking/ssh/sshd.nix'
16+
# "/services/networking/ssh/sshd.nix"
17+
"/services/security/fail2ban.nix"
18+
];
19+
20+
options = {
21+
supabase.services.fail2ban = {
22+
enable = lib.mkEnableOption "Fail2Ban";
23+
};
24+
};
25+
26+
config = lib.mkIf cfg.enable {
27+
# TODO: (last bit form Ansible task)
28+
# - name: Configure journald
29+
# copy:
30+
# src: files/fail2ban_config/jail-ssh.conf
31+
# dest: /etc/fail2ban/jail.d/sshd.local
32+
# when: debpkg_mode or nixpkg_mode
33+
supabase.services.fail2ban = {
34+
enable = true; # FIXME: fail2ban was disabled in ansible/tasks/setup-fail2ban.yml
35+
bantime = "3600";
36+
jails = {
37+
postgresql = {
38+
settings = {
39+
enabled = true;
40+
port = "5432";
41+
protocol = "tcp";
42+
filter = "postgresql";
43+
logpath = "/var/log/postgresql/auth-failures.csv";
44+
maxretry = 3;
45+
ignoreip = "192.168.0.0/16 172.17.1.0/20";
46+
};
47+
};
48+
pgbouncer = {
49+
settings = {
50+
enabled = true;
51+
port = "6543";
52+
protocol = "tcp";
53+
filter = "pgbouncer";
54+
backend = "systemd[journalflags=1]";
55+
maxretry = 3;
56+
};
57+
};
58+
};
59+
# TODO: extraPackages = [ pkgs.nftables ];
60+
};
61+
62+
environment.etc = {
63+
"fail2ban/jail.local".text = ''
64+
[DEFAULT]
65+
banaction = nftables-multiport
66+
banaction_allports = nftables-allports
67+
'';
68+
69+
"fail2ban/filter.d/postgresql.conf".text = ''
70+
[Definition]
71+
failregex = ^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user.*$
72+
ignoreregex = ^.*,.*,.*,.*,"127\.0\.0\.1.*password authentication failed for user.*$
73+
^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user ""supabase_admin".*$
74+
^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user ""supabase_auth_admin".*$
75+
^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user ""supabase_storage_admin".*$
76+
^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user ""authenticator".*$
77+
^.*,.*,.*,.*,"<HOST>:.*password authentication failed for user ""pgbouncer".*$
78+
'';
79+
80+
"fail2ban/filter.d/pgbouncer.conf".text = ''
81+
[Definition]
82+
failregex = ^.+@<HOST>:.+password authentication failed$
83+
journalmatch = _SYSTEMD_UNIT=pgbouncer.service
84+
'';
85+
};
86+
87+
systemd.services.fail2ban = {
88+
wantedBy = lib.mkForce [
89+
"system-manager.target"
90+
];
91+
# TODO:
92+
# after = [ "nftables.service" ];
93+
# wants = [ "nftables.service" ];
94+
};
95+
};
96+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# from time import sleep
2+
3+
4+
def test_fail2ban_service(host):
5+
# sleep(5000) # Handy for interactive debugging (with docker exec -it $CONTAINER_ID /bin/bash)
6+
assert host.service("fail2ban.service").is_valid
7+
assert host.service("fail2ban.service").is_running, "Fail2Ban service should be running but failed: {}".format(host.run("systemctl status fail2ban.service").stdout)

0 commit comments

Comments
 (0)