@@ -351,6 +351,30 @@ def gzip_then_base64_encode(s: str) -> str:
351351 instance .terminate ()
352352 raise TimeoutError ("init.sh failed to complete within the timeout period" )
353353
354+ # Create auth-failures.csv file if it doesn't exist (required for fail2ban to start)
355+ # This matches what setup_fail2ban() does in the init script
356+ logger .info ("Ensuring PostgreSQL auth-failures.csv exists..." )
357+ result = run_ssh_command (
358+ ssh ,
359+ "sudo mkdir -p /var/log/postgresql && sudo chown -R postgres:postgres /var/log/postgresql && sudo chmod 1775 /var/log/postgresql && sudo -u postgres touch /var/log/postgresql/auth-failures.csv && sudo chmod 0664 /var/log/postgresql/auth-failures.csv" ,
360+ )
361+ if not result ["succeeded" ]:
362+ logger .warning (f"Failed to create auth-failures.csv: { result ['stderr' ]} " )
363+
364+ # Start fail2ban service before health checks
365+ logger .info ("Starting fail2ban service..." )
366+ result = run_ssh_command (ssh , "sudo systemctl start fail2ban.service" )
367+ if not result ["succeeded" ]:
368+ logger .warning (f"Failed to start fail2ban: { result ['stderr' ]} " )
369+ # Check fail2ban logs for more details
370+ log_result = run_ssh_command (
371+ ssh , "sudo journalctl -u fail2ban -n 20 --no-pager"
372+ )
373+ if log_result ["succeeded" ]:
374+ logger .warning (f"fail2ban logs:\n { log_result ['stdout' ]} " )
375+ else :
376+ logger .info ("fail2ban service started successfully" )
377+
354378 def is_healthy (ssh ) -> bool :
355379 health_checks = [
356380 ("postgres" , "sudo -u postgres /usr/bin/pg_isready -U postgres" ),
0 commit comments