Skip to content

Automation and IaC

Heisenbug edited this page Mar 5, 2026 · 1 revision

Home · CLI Guide

Automation and IaC

At a glance

  • Idempotent configuration management for CSF.
  • Patterns for Ansible and Terraform, plus safe shell-based rollout checks.
  • Safe automated deployment and verification flows.

1. Idempotency Strategies

When using configuration management (Ansible, Chef, etc.), avoid "clobbering" manual emergency edits made by operators.

Using .include files

CSF supports including external files. This is the cleanest way to separate "managed" config from "local" config.

  • Pattern: Keep csf.conf for core settings, but use csf.allow, csf.deny, and csf.ignore for dynamic data.
  • Ansible example: Use the blockinfile or template module to manage specific sections of csf.allow.

Prefer explicit file management over ad-hoc regex edits

For automation, treat /etc/csf/csf.conf as managed configuration and keep dynamic trust data in list files (csf.allow, csf.ignore, csf.deny).

  • Prefer templating (template, copy) for stable defaults.
  • Prefer lineinfile/blockinfile for small, targeted changes.
  • Always follow with csf -ra and a connectivity validation step.

2. Safe Deployment Flow (The "Canary" Pattern)

Automated firewall changes can lock out an entire fleet if not handled carefully.

  1. Stage 1: Testing Mode. Deploy with TESTING = "1".
  2. Stage 2: Validation. Run a script to verify SSH and web connectivity from a known watcher node.
  3. Stage 3: Commitment. Only if Stage 2 passes, flip to TESTING = "0" and reload.

⚠️ Lockout warning: Never auto-promote to TESTING="0" without a successful external connectivity check and console fallback access.

Sample Bash Verification Script

#!/bin/bash
# Apply changes
csf -ra

# Wait for LFD to settle
sleep 5

# Check connectivity
curl --silent --connect-timeout 5 https://my-check-service.com/ok || {
  echo "Connectivity lost! Rolling back..."
  csf -x
  exit 1
}

echo "Verification successful."

3. IaC Examples

Ansible

Prefer using the copy module for fixed configuration and lineinfile for dynamic allow-lists.

- name: Ensure Admin IP is allowed
  lineinfile:
    path: /etc/csf/csf.allow
    line: "{{ admin_ip }} # Managed by Ansible"
    state: present
  notify: restart csf

Terraform (User-Data)

Inject bootstrap setup in user_data, but keep TESTING="1" until external checks pass.

user_data = <<-EOF
  #!/bin/bash
  bash <(curl -sL https://csf.black.host)
  echo "${var.admin_ip} # Initial Admin Access" >> /etc/csf/csf.allow
  # Keep TESTING=1 during first boot; promote to TESTING=0 only after validation.
  csf -ra
EOF

4. Avoiding Configuration Drift

  • Auditing: Periodically run csf -l and compare against your "Source of Truth".
  • LFD Alerts: Monitor /var/log/lfd.log for "Modified File" alerts if you use Directory Watching.

Last reviewed: 2026-03-04


← Cloud and Container Hardening · Integrations Guide →

Clone this wiki locally