Skip to content

Commit bdc0a05

Browse files
committed
fix: move audit to end of image build
1 parent c46ee41 commit bdc0a05

File tree

3 files changed

+79
-76
lines changed

3 files changed

+79
-76
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
# CIS Baseline Validation Check
3+
#
4+
# This script validates that the machine matches the committed baseline
5+
# specification. It's run during the image build to catch configuration
6+
# drift before releasing an image.
7+
#
8+
# Usage: cis_baseline_check.sh <baseline-file> [flake-path]
9+
10+
set -euo pipefail
11+
12+
BASELINE_FILE="${1:-/tmp/ansible-playbook/audit-specs/baselines/baseline.yml}"
13+
FLAKE_PATH="${2:-/tmp/ansible-playbook}"
14+
15+
echo "============================================================"
16+
echo "CIS Baseline Validation"
17+
echo "============================================================"
18+
echo ""
19+
echo "Baseline file: $BASELINE_FILE"
20+
echo "Flake path: $FLAKE_PATH"
21+
echo ""
22+
23+
# Check baseline file exists
24+
if [[ ! -f "$BASELINE_FILE" ]]; then
25+
echo "ERROR: Baseline file not found: $BASELINE_FILE"
26+
echo ""
27+
echo "Make sure the baseline.yml is copied to the build machine."
28+
exit 1
29+
fi
30+
31+
# Source nix environment for the ubuntu user
32+
# The build runs as root but nix is installed for ubuntu
33+
if [[ -f /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh ]]; then
34+
. /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh
35+
fi
36+
37+
# Install cis-audit from the flake if not already installed
38+
echo "Installing cis-audit from flake..."
39+
if ! command -v cis-audit &> /dev/null; then
40+
# Install cis-audit package which includes goss
41+
nix profile install "${FLAKE_PATH}#cis-audit" --accept-flake-config
42+
echo "✓ cis-audit installed"
43+
else
44+
echo "✓ cis-audit already available"
45+
fi
46+
47+
echo ""
48+
echo "Running baseline validation..."
49+
echo "------------------------------------------------------------"
50+
51+
# Run cis-audit with the baseline spec
52+
# The cis-audit wrapper handles running goss with sudo
53+
if cis-audit --spec "$BASELINE_FILE" --format documentation; then
54+
echo "------------------------------------------------------------"
55+
echo ""
56+
echo "✓ CIS baseline validation PASSED"
57+
echo " Machine configuration matches the committed baseline."
58+
echo ""
59+
exit 0
60+
else
61+
EXIT_CODE=$?
62+
echo "------------------------------------------------------------"
63+
echo ""
64+
echo "✗ CIS baseline validation FAILED"
65+
echo ""
66+
echo " The machine configuration has drifted from the baseline."
67+
echo " Review the failures above and either:"
68+
echo " 1. Fix the configuration to match the baseline, OR"
69+
echo " 2. Update the baseline if the change is intentional:"
70+
echo " nix run .#cis-generate-spec -- audit-specs/baselines/baseline.yml"
71+
echo ""
72+
exit $EXIT_CODE
73+
fi

ansible/playbook.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@
220220
systemctl stop fail2ban.service
221221
when: stage2_nix
222222

223+
- name: Run CIS baseline validation
224+
become: yes
225+
shell: |
226+
/bin/bash /tmp/ansible-playbook/ansible/files/cis_baseline_check.sh /tmp/ansible-playbook/audit-specs/baselines/baseline.yml
227+
when: stage2_nix
228+
223229
- name: nix collect garbage
224230
become: yes
225231
shell: |

testinfra/test_ami_nix.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -824,79 +824,3 @@ def test_postgrest_read_only_session_attrs(host):
824824
print("Warning: Failed to restart PostgreSQL after restoring config")
825825
else:
826826
print("Warning: Failed to restore PostgreSQL configuration")
827-
828-
829-
def test_cis_baseline_audit(host):
830-
"""Run CIS baseline audit against the machine and report results.
831-
832-
This test uploads the current baseline.yml from the repo and uses
833-
cis-audit to validate the machine against it. The test reports findings
834-
but does not fail the build - it's for visibility into configuration drift.
835-
"""
836-
git_sha = os.environ.get("GITHUB_SHA", "HEAD")
837-
838-
# Find the baseline file relative to the test file location
839-
test_dir = Path(__file__).parent.parent
840-
baseline_path = test_dir / "audit-specs" / "baselines" / "baseline.yml"
841-
842-
if not baseline_path.exists():
843-
print(f"\n⚠️ Baseline file not found at {baseline_path}")
844-
print("Skipping CIS baseline audit - no baseline file available")
845-
pytest.skip("Baseline file not found")
846-
return
847-
848-
print(f"\n{'='*60}")
849-
print("CIS BASELINE AUDIT")
850-
print(f"{'='*60}")
851-
print(f"Baseline file: {baseline_path}")
852-
853-
# Upload baseline file to the instance
854-
remote_baseline_path = "/tmp/baseline.yml"
855-
try:
856-
upload_file_via_sftp(host["ssh"], str(baseline_path), remote_baseline_path)
857-
print(f"✓ Uploaded baseline to {remote_baseline_path}")
858-
except Exception as e:
859-
print(f"✗ Failed to upload baseline file: {e}")
860-
pytest.skip(f"Failed to upload baseline: {e}")
861-
return
862-
863-
# Install cis-audit via nix
864-
print("\nInstalling cis-audit tool...")
865-
install_cmd = f"nix profile install github:supabase/postgres/{git_sha}#cis-audit --refresh 2>&1"
866-
result = run_ssh_command(host["ssh"], install_cmd, timeout=300)
867-
if not result["succeeded"]:
868-
print(f"Warning: {result['stderr'][:500]}")
869-
870-
# Run cis-audit with documentation format for readable output
871-
print("\nRunning CIS baseline validation...")
872-
print(f"{'-'*60}")
873-
874-
# Use the uploaded baseline file (local path, not bundled)
875-
validate_cmd = f"~/.nix-profile/bin/cis-audit --spec {remote_baseline_path} --format documentation 2>&1"
876-
result = run_ssh_command(host["ssh"], validate_cmd, timeout=600)
877-
878-
# Print full output for visibility in GitHub Actions logs
879-
print(result["stdout"])
880-
if result["stderr"]:
881-
print(f"\nStderr:\n{result['stderr']}")
882-
883-
print(f"{'-'*60}")
884-
885-
# Also run with tap format to get summary counts
886-
validate_tap_cmd = f"~/.nix-profile/bin/cis-audit --spec {remote_baseline_path} --format tap 2>&1 | tail -10"
887-
result_tap = run_ssh_command(host["ssh"], validate_tap_cmd, timeout=600)
888-
889-
print(f"\nSummary:")
890-
print(result_tap["stdout"])
891-
892-
# Clean up
893-
run_ssh_command(host["ssh"], f"rm -f {remote_baseline_path}")
894-
895-
print(f"{'='*60}")
896-
print("CIS BASELINE AUDIT COMPLETE")
897-
print(f"{'='*60}\n")
898-
899-
# Note: This test intentionally does not assert/fail on validation results
900-
# It's meant to provide visibility into configuration state
901-
# To make this test fail on drift, uncomment the following:
902-
# assert result["succeeded"], "CIS baseline validation found differences"

0 commit comments

Comments
 (0)