Skip to content

Commit f704923

Browse files
larsewiclaude
andcommitted
Install and configure fail2ban on all build hosts
Install fail2ban on Debian/Ubuntu and RHEL/CentOS platforms to ban IPs with repeated failed SSH auth attempts. Configures sshd jail with 5 max retries, 1 hour ban time, and 10 minute find window. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 158039d commit f704923

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

ci/cfengine-build-host-setup.cf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ bundle agent cfengine_build_host_setup
2424
"ntp";
2525

2626
debian|ubuntu::
27+
"fail2ban"
28+
comment => "Ban IPs with repeated failed SSH auth attempts";
2729
"libltdl7" package_policy => "delete";
2830
"libltdl-dev" package_policy => "delete";
2931
"binutils";
@@ -126,6 +128,8 @@ bundle agent cfengine_build_host_setup
126128
# note that shellcheck, fakeroot and ccache require epel-release to be installed
127129
(redhat|centos).(yum_dnf_conf_ok)::
128130
"epel-release";
131+
"fail2ban"
132+
comment => "Ban IPs with repeated failed SSH auth attempts";
129133
(redhat_7|centos_7).(yum_dnf_conf_ok)::
130134
"ccache";
131135
"fakeroot";
@@ -263,6 +267,17 @@ root - core unlimited
263267
* - core unlimited
264268
");
265269

270+
"/etc/fail2ban/jail.local"
271+
create => "true",
272+
content => "[sshd]
273+
enabled = true
274+
port = ssh
275+
maxretry = 5
276+
bantime = 3600
277+
findtime = 600",
278+
classes => if_repaired("fail2ban_config_changed"),
279+
comment => "Configure fail2ban to ban IPs after 5 failed SSH attempts within 10 minutes";
280+
266281
"/etc/ssh/sshd_config"
267282
edit_line => comment_lines_matching("^PermitRootLogin\s+(?!no\s*$).*", "#"),
268283
classes => if_repaired("sshd_hardened"),
@@ -373,6 +388,14 @@ jenkins_builds ALL=NOPASSWD: /usr/bin/podman
373388
"sshd"
374389
service_policy => "restart",
375390
comment => "Restart sshd to apply hardened configuration";
391+
any::
392+
"fail2ban"
393+
service_policy => "start",
394+
comment => "Ensure fail2ban is running";
395+
fail2ban_config_changed::
396+
"fail2ban"
397+
service_policy => "restart",
398+
comment => "Restart fail2ban to apply jail configuration";
376399

377400
# skip /etc/hosts change for now, seems kind of wrong and corrupts ip6 entries like `::1 ip6-ip6-loopback`
378401
# maybe the following is needed to silence such errors as: ubuntu-16-mingw-j1: sudo: unable to resolve host localhost.localdomain

0 commit comments

Comments
 (0)