|
| 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 | +} |
0 commit comments