The problem
A server is built, reviewed once, and declared compliant. Months later nobody can say whether it still matches the baseline. Typical symptoms of this situation:
- Kernel network settings such as ICMP redirect handling or IP forwarding differ between hosts that were supposed to be identical.
- A compliance review is a spreadsheet filled in by hand. It describes one day, not the current state.
- A hardening change, applied by hand or blindly by script, breaks something else, for example cluster heartbeats or container networking.
- Kubernetes objects such as an Ingress without a TLS section are accepted by the API server because nothing checks them.
The goal is to turn the baseline into code that is applied, verified and enforced automatically, and to fail a deployment when the verification fails.
Why it happens
Configuration drift has several ordinary causes:
- Manual changes. A hotfix over SSH is never recorded in the repository, so the documented state and the running state diverge.
- Software that changes the baseline. Container runtimes, Kubernetes node components and hypervisors commonly enable IP forwarding, which a network hardening baseline wants disabled on ordinary hosts.
- Implicit defaults. Several kernel parameters have defaults that depend on other settings. For example, whether ICMP redirects are accepted depends on whether the host forwards packets, and
secure_redirectsdefaults to enabled. A baseline that does not set them explicitly depends on the distribution and kernel. - Point-in-time review. A quarterly check cannot detect a change made the day after it.
- No admission control. Without a policy engine, the Kubernetes API accepts any syntactically valid object.
The legal side adds pressure but not a specific technical recipe. The GDPR requires technical and organisational measures appropriate to the risk, including a process for regularly testing their effectiveness. Physical data location is a separate control. It is decided when a region or provider is chosen, and no sysctl or policy engine can change it afterwards. Transfers outside the EU/EEA need their own assessment since the Court of Justice judgment in Schrems II (C-311/18, July 2020).
Source of truth
The following references define the behaviour that the fix relies on:
- Linux kernel IP sysctl documentation. It documents the parameters used here (
ip_forward,accept_redirects,secure_redirects,send_redirects,accept_source_route,icmp_echo_ignore_broadcasts,icmp_ignore_bogus_error_responses). It also explains theconf/allandconf/defaultscopes. - ansible.posix.sysctl module. It confirms the module parameters
name,value,state,sysctl_file,sysctl_setandreload, which write a persistent entry and optionally apply it to the running kernel. - OpenSCAP user manual. It describes evaluating an XCCDF benchmark from a SCAP data stream, producing machine-readable results and an HTML report, and the exit status of
oscap. - ComplianceAsCode/content. This is the project that builds the SCAP Security Guide data streams, including CIS-based profiles, and generates Ansible and shell remediation from the same rules.
- OPA Kubernetes admission control tutorial. It shows policies in
package kubernetes.admissionthat read the AdmissionReview data frominput.request. - GDPR text (Regulation 2016/679). Article 32 covers security of processing, including encryption and regular testing of measures. Article 83 sets the fine ceilings.
The CIS benchmark documents themselves are published by the Center for Internet Security. The OpenSCAP profiles encode their rules, so the scan rather than a PDF becomes the working reference.
Fix options
| Layer | Option | Strength | Weakness |
|---|---|---|---|
| OS baseline | Hand-written Ansible tasks | Small, readable, easy to review and exempt single settings | Coverage is whatever was written |
| OS baseline | Generated remediation from the SCAP Security Guide (oscap xccdf generate fix, or the shipped Ansible playbooks) | Covers the whole profile | Large changes that can break services; needs review and testing first |
| OS baseline | Manual hardening | None worth the cost | Not repeatable, drifts |
| Verification | OpenSCAP scan in the pipeline | Open source, standard content, XML and HTML evidence | Checks configuration, not application logic |
| Verification | Commercial scanner | Broader features | Cost; not needed for a configuration gate |
| Kubernetes policy | OPA admission policy (kube-mgmt style, or Gatekeeper constraint templates) | Rejects bad objects before they are stored | Another component to operate; policy format differs between the two |
| Provisioning | Terraform plus a static checker such as tfsec, or Sentinel in HashiCorp's commercial products | Catches open firewall rules before terraform apply | Only sees what the code declares |
Remediation by rebuild (immutable images) and remediation in place (configuration management) are both valid. Rebuilding gives the cleanest result. In-place enforcement is simpler for long-lived database hosts.
Why this fix
The chosen combination is hand-written Ansible for the settings that are understood, an OpenSCAP scan as the gate, and an OPA policy for Kubernetes. The reasons:
- Explicit settings. A short list of sysctl values can be read in review and tested on a staging host, which matters because network hardening can interfere with clustering and container networking.
- Independent verification. The scan checks the result against an external rule set instead of trusting the playbook that produced it. Applying and checking with different tools catches mistakes in either.
- Standard evidence. The scan produces an XML results file and an HTML report that can be archived with the build.
- Early rejection. An admission policy stops the problem at the API instead of reporting it later.
Two corrections to a commonly published version of this setup are built in. First, an OpenSCAP XCCDF evaluation is a configuration compliance scan, not a vulnerability scan. Vulnerability checks use OVAL content for known CVEs, which is a different data source. Second, a naive not input.request.object.spec.tls test accepts tls: [], because an empty list is still a defined value. The policy below checks the count instead.
Step by step
1. Find the data stream and the profile
The data stream file name and the profile IDs depend on the distribution and the SCAP Security Guide version. List them instead of assuming them.
sudo dnf install -y openscap-scanner scap-security-guide
ls /usr/share/xml/scap/ssg/content/
oscap info /usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml
Use the file name that matches the installed distribution, and pick one of the CIS profile IDs printed by oscap info.
2. Take a baseline scan
sudo oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis \
--results scan-results.xml \
--report report.html \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml
Exit status 0 means all rules passed. 1 means the evaluation itself failed. 2 means at least one rule failed or returned an unknown result. Open report.html to see which rules failed.
3. Apply the network settings with Ansible
Save as cis-network.yml. The playbook uses the fully qualified module name and writes to a drop-in file so that the settings persist across reboots. It also sets both all and default scopes, so existing and newly created interfaces behave the same. Remove the ip_forward entry on routers, container hosts and Kubernetes nodes, where forwarding is required.
- name: Apply CIS network hardening
hosts: all
become: true
tasks:
- name: Set network sysctls
ansible.posix.sysctl:
name: '{{ item.key }}'
value: '{{ item.value }}'
state: present
sysctl_file: /etc/sysctl.d/60-cis-network.conf
sysctl_set: true
reload: true
loop:
- { key: 'net.ipv4.ip_forward', value: '0' }
- { key: 'net.ipv4.conf.all.send_redirects', value: '0' }
- { key: 'net.ipv4.conf.default.send_redirects', value: '0' }
- { key: 'net.ipv4.conf.all.accept_source_route', value: '0' }
- { key: 'net.ipv4.conf.default.accept_source_route', value: '0' }
- { key: 'net.ipv4.conf.all.accept_redirects', value: '0' }
- { key: 'net.ipv4.conf.default.accept_redirects', value: '0' }
- { key: 'net.ipv4.conf.all.secure_redirects', value: '0' }
- { key: 'net.ipv4.conf.default.secure_redirects', value: '0' }
- { key: 'net.ipv4.conf.all.log_martians', value: '1' }
- { key: 'net.ipv4.conf.default.log_martians', value: '1' }
- { key: 'net.ipv4.icmp_echo_ignore_broadcasts', value: '1' }
- { key: 'net.ipv4.icmp_ignore_bogus_error_responses', value: '1' }
- { key: 'net.ipv6.conf.all.accept_redirects', value: '0' }
- { key: 'net.ipv6.conf.default.accept_redirects', value: '0' }
- { key: 'net.ipv6.conf.all.accept_source_route', value: '0' }
- { key: 'net.ipv6.conf.default.accept_source_route', value: '0' }
Run a dry run against a staging host first, then apply:
ansible-playbook -i inventory.ini cis-network.yml --check --diff
ansible-playbook -i inventory.ini cis-network.yml
4. Verify the running kernel
sudo sysctl --system
sysctl net.ipv4.conf.all.accept_redirects net.ipv4.conf.default.secure_redirects net.ipv4.ip_forward
sysctl net.ipv4.icmp_echo_ignore_broadcasts net.ipv4.icmp_ignore_bogus_error_responses
5. Turn the scan into a gate
Run this script before a host joins the load balancer pool. It fails on both evaluation errors and non-compliance, since a scan that could not run proves nothing.
#!/bin/bash
oscap xccdf eval \
--profile xccdf_org.ssgproject.content_profile_cis \
--results scan-results.xml \
--report report.html \
/usr/share/xml/scap/ssg/content/ssg-rhel9-ds.xml
rc=$?
if [ "$rc" -ne 0 ]; then
echo "compliance gate failed with exit code $rc" >&2
exit "$rc"
fi
Archive scan-results.xml and report.html as build artifacts. Exceptions for individual rules belong in a tailoring file kept in the repository, not in a skipped pipeline step.
6. Reject Ingress objects without TLS
Save as ingress.rego. This uses the classic deny rule form from the OPA admission tutorial. Gatekeeper uses a different template format with violation rules.
package kubernetes.admission
deny[msg] {
input.request.kind.kind == "Ingress"
tls := object.get(input.request.object.spec, "tls", [])
count(tls) == 0
msg := "Ingress must define at least one TLS entry"
}
Test it locally with a sample AdmissionReview fragment saved as input-no-tls.json:
{"request": {"kind": {"kind": "Ingress"}, "object": {"spec": {"rules": []}}}}
opa eval --format pretty --data ingress.rego --input input-no-tls.json "data.kubernetes.admission.deny"
The result should contain the message. Repeat with an input that has a non-empty tls list and confirm the result is an empty set.
7. Check Terraform before apply
Whatever provider is used, a firewall rule that exposes SSH or a database port to every address is the typical finding. For example, an OpenStack-style rule limited to an internal range:
resource "openstack_networking_secgroup_rule_v2" "db_internal" {
direction = "ingress"
ethertype = "IPv4"
protocol = "tcp"
port_range_min = 5432
port_range_max = 5432
remote_ip_prefix = "10.0.0.0/8"
security_group_id = openstack_networking_secgroup_v2.db.id
}
terraform validate
tfsec .
terraform plan -out=tfplan
A source range restricts exposure to the listed networks. It does not make a service safe by itself, and authentication and patching still apply.
Short review
What works
- The sysctl baseline is explicit, persistent and reviewable, and the scan checks it independently.
- The exit-code gate needs no human step and leaves XML and HTML evidence.
- The admission policy stops non-TLS Ingress objects at the API.
Limits
- A passing CIS scan is not GDPR compliance. The regulation concerns risk-appropriate measures, lawful processing and transfers, and documentation. Technical baselines support only part of that, and a fine of up to 4% of worldwide turnover (or the fixed ceiling in Article 83) depends on the whole picture.
- The TLS policy checks that a TLS section exists. It does not check certificate validity or protocol versions. Where TLS ends at an upstream load balancer, the rule may need adjusting.
- Disabling IP forwarding and tightening redirect handling can break routers, container hosts, Kubernetes nodes and some cluster setups. Test on staging first.
- Data location, provider jurisdiction and transfer assessments are decisions made outside the code, even if tags and region variables document them.
When to choose another method
- For a full profile on a new fleet, generate remediation from the SCAP Security Guide, review it, and test it before rollout instead of writing every rule by hand.
- For many clusters and shared policy libraries, Gatekeeper constraint templates fit better than a standalone admission webhook.
- For short-lived workloads, rebuilding images from a hardened template is cleaner than enforcing in place.