From dc99c66f94b538bfdb4c2e3be30064eaf7783fdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:29:35 -0300 Subject: [PATCH 01/17] test(fips): run unchanged callers in isolated filesystems --- .github/workflows/xcat_test.yml | 4 ++ .../native/cumulus_discovery_key_failure.t | 37 ++++++++++++++ xCAT-test/native/statelite_optional_hostkey.t | 49 +++++++++++++++++++ .../unit/cumulus_discovery_key_failure.t | 43 ---------------- xCAT-test/unit/fips_ssh_keys.t | 47 ------------------ 5 files changed, 90 insertions(+), 90 deletions(-) create mode 100644 xCAT-test/native/cumulus_discovery_key_failure.t create mode 100644 xCAT-test/native/statelite_optional_hostkey.t delete mode 100644 xCAT-test/unit/cumulus_discovery_key_failure.t diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index da6e641b74..b757dfb068 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -16,6 +16,10 @@ jobs: run: sudo env DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends --no-install-suggests ucf debconf-utils systemd libxml-simple-perl apache2 curl - name: Test Debian package lifecycle run: prove -v xCAT-test/native/xcatd_debian_lifecycle.t xCAT-test/native/xcatd_restart_callers.t xCAT-test/native/apache_package_lifecycle.t xCAT-test/native/package_upgrade_logging.t + - name: Test optional statelite host keys + run: prove -v xCAT-test/native/statelite_optional_hostkey.t + - name: Test Cumulus discovery key failure + run: prove -v xCAT-test/native/cumulus_discovery_key_failure.t - name: Install image test dependencies if: ${{ !cancelled() }} diff --git a/xCAT-test/native/cumulus_discovery_key_failure.t b/xCAT-test/native/cumulus_discovery_key_failure.t new file mode 100644 index 0000000000..722cfaabef --- /dev/null +++ b/xCAT-test/native/cumulus_discovery_key_failure.t @@ -0,0 +1,37 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Path qw(make_path); +use File::Slurper qw(write_text); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Sandbox qw(sandbox_root sandbox_run); + +plan skip_all => 'Cumulus discovery requires Linux' unless $^O eq 'linux'; + +my $directory = sandbox_root(); +make_path(map { "$directory/$_" } qw(bin tmp etc/xcat etc/pki/tls var/lib/dhcp)); +write_text("$directory/var/lib/dhcp/dhclient.eth0.leases", ''); +write_text("$directory/bin/openssl", <<'SH'); +#!/bin/sh +echo attempted > /fixture/key-attempt +exit 1 +SH +write_text("$directory/bin/logger", "#!/bin/sh\nexit 0\n"); +write_text("$directory/bin/socat", "#!/bin/sh\ntouch /fixture/listener-started\nexit 0\n"); +chmod(0755, map { "$directory/bin/$_" } qw(openssl logger socat)); + +my ($rc, $output) = sandbox_run($directory, { + read_only => {repo_path('xCAT/postscripts') => '/postscripts'}, + writable => {"$directory/var/lib/dhcp" => '/var/lib/dhcp'}}, + '/bin/bash', '/postscripts/documulusdiscovery'); +is($rc, 1, 'discovery reports failed private-key generation') or diag($output); +ok(-f "$directory/key-attempt", 'the failure comes from the key generator'); +ok(!-e "$directory/tmp/helper.socat.sh", + 'failed key generation leaves no listener helper behind'); +ok(!-e "$directory/listener-started", 'failed key generation does not start a listener'); + +done_testing(); diff --git a/xCAT-test/native/statelite_optional_hostkey.t b/xCAT-test/native/statelite_optional_hostkey.t new file mode 100644 index 0000000000..b52287740c --- /dev/null +++ b/xCAT-test/native/statelite_optional_hostkey.t @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Sandbox qw(sandbox_root sandbox_run); + +plan skip_all => 'statelite requires Linux' unless $^O eq 'linux'; +my $script = repo_path('xCAT-server/share/xcat/netboot/add-on/statelite/add_ssh'); + +for my $with_dsa (0, 1) { + my $root = sandbox_root(); + make_path(map { "$root/$_" } + qw(etc/xcat/hostkeys etc/ssh root/.ssh target/etc/ssh)); + write_text("$root/target/etc/ssh/sshd_config", "X11Forwarding no\n"); + write_text("$root/etc/xcat/hostkeys/ssh_host_rsa_key", "rsa-host-key\n"); + write_text("$root/etc/xcat/hostkeys/ssh_host_dsa_key", "dsa-host-key\n") + if $with_dsa; + write_text("$root/root/.ssh/$_", "client-$_\n") for qw(id_rsa id_rsa.pub config); + + my ($rc, $output) = sandbox_run($root, { + read_only => {$script => '/add_ssh'}, + writable => {"$root/root" => '/root'}, + env => {XCATROOT => '/opt/xcat'}}, + '/bin/bash', '/add_ssh', '/target'); + is($rc, 0, "statelite completes with DSA present=$with_dsa") or diag($output); + is($output, '', "statelite reports no missing-key errors with DSA present=$with_dsa"); + like(read_text("$root/etc/ssh/ssh_config"), qr/StrictHostKeyChecking no/, + 'statelite writes client configuration inside the private filesystem'); + is(read_text("$root/target/etc/ssh/ssh_host_rsa_key"), "rsa-host-key\n", + "statelite preserves the RSA host key with DSA present=$with_dsa"); + is(-f "$root/target/etc/ssh/ssh_host_dsa_key" ? 1 : 0, $with_dsa, + 'statelite copies the optional DSA key only when supplied'); + is((stat("$root/target/etc/ssh/ssh_host_rsa_key"))[2] & oct('0777'), oct('0600'), + 'statelite limits RSA host-key permissions'); + if ($with_dsa) { + is(read_text("$root/target/etc/ssh/ssh_host_dsa_key"), "dsa-host-key\n", + 'statelite preserves the optional DSA key'); + is((stat("$root/target/etc/ssh/ssh_host_dsa_key"))[2] & oct('0777'), oct('0600'), + 'statelite limits DSA host-key permissions'); + } +} + +done_testing(); diff --git a/xCAT-test/unit/cumulus_discovery_key_failure.t b/xCAT-test/unit/cumulus_discovery_key_failure.t deleted file mode 100644 index fb6381d0a8..0000000000 --- a/xCAT-test/unit/cumulus_discovery_key_failure.t +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/../lib"; -use File::Path qw(make_path); -use File::Slurper qw(read_text write_text); -use File::Temp qw(tempdir); -use Test::More; -use XCAT::Test::File qw(repo_path); - -my $bash = qx(command -v bash 2>/dev/null); -chomp($bash); -plan skip_all => 'bash is required' unless $bash && -x $bash; - -my $directory = tempdir(CLEANUP => 1); -make_path(map { "$directory/$_" } qw(bin tmp etc/xcat etc/pki/tls var/lib/dhcp)); -write_text("$directory/var/lib/dhcp/dhclient.eth0.leases", ''); -symlink(repo_path('xCAT/postscripts/xcatlib.sh'), "$directory/xcatlib.sh") - or die "link shell library: $!"; - -my $script = read_text(repo_path('xCAT/postscripts/documulusdiscovery')); -$script =~ s{(/etc/xcat|/etc/pki/tls|/var/lib/dhcp|/tmp)(?=/|\b)}{$directory$1}g; -write_text("$directory/documulusdiscovery", $script); -write_text("$directory/bin/openssl", <<'SH'); -#!/bin/sh -echo attempted > "$KEY_ATTEMPT" -exit 1 -SH -write_text("$directory/bin/logger", "#!/bin/sh\nexit 0\n"); -write_text("$directory/bin/socat", "#!/bin/sh\nexit 0\n"); -chmod(0755, map { "$directory/bin/$_" } qw(openssl logger socat)); - -local $ENV{PATH} = "$directory/bin:$ENV{PATH}"; -local $ENV{KEY_ATTEMPT} = "$directory/key-attempt"; -system($bash, "$directory/documulusdiscovery"); -is($? >> 8, 1, 'discovery reports failed private-key generation'); -ok(-f "$directory/key-attempt", 'the failure comes from the key generator'); -ok(!-e "$directory/tmp/helper.socat.sh", - 'failed key generation leaves no listener helper behind'); - -done_testing(); diff --git a/xCAT-test/unit/fips_ssh_keys.t b/xCAT-test/unit/fips_ssh_keys.t index 66d37edb6a..8c81c5213f 100644 --- a/xCAT-test/unit/fips_ssh_keys.t +++ b/xCAT-test/unit/fips_ssh_keys.t @@ -3,7 +3,6 @@ use strict; use warnings; use FindBin; -use File::Path qw(make_path); use File::Spec; use File::Slurper qw(read_text write_text); use File::Temp qw(tempdir); @@ -11,7 +10,6 @@ use Test::More; use lib "$FindBin::Bin/../lib"; use lib "$FindBin::Bin/../../perl-xCAT"; -use XCAT::Test::File qw(repo_path); use xCAT::Utils; my $tmpdir = tempdir(CLEANUP => 1); @@ -41,49 +39,4 @@ foreach my $case ( "$platform rejects DSA in FIPS mode"); } -SKIP: { - skip 'statelite requires Linux shell utilities', 12 unless $^O eq 'linux'; - - foreach my $with_dsa (0, 1) { - my $fixture = tempdir(CLEANUP => 1); - my $image = "$fixture/image"; - make_path("$fixture/hostkeys", "$fixture/client", - "$image/etc/ssh", "$image/root/.ssh"); - write_text("$image/etc/ssh/sshd_config", "X11Forwarding no\n"); - write_text("$fixture/hostkeys/ssh_host_rsa_key", "rsa-host-key\n"); - write_text("$fixture/hostkeys/ssh_host_dsa_key", "dsa-host-key\n") - if $with_dsa; - write_text("$fixture/client/$_", "client-$_\n") for qw(id_rsa id_rsa.pub config); - - my $source = read_text(repo_path('xCAT-server/share/xcat/netboot/add-on/statelite/add_ssh')); - $source =~ s{/etc/xcat/hostkeys}{$fixture/hostkeys}g; - $source =~ s{/install/postscripts/_ssh}{$fixture/postscript-keys}g; - $source =~ s{(?', "$fixture/stdout") or die $!; - open(STDERR, '>', "$fixture/stderr") or die $!; - $ENV{XCATROOT} = $fixture; - exec('bash', "$fixture/add_ssh", $image); - die "exec bash: $!"; - } - waitpid($pid, 0); - is($?, 0, "statelite completes with DSA present=$with_dsa"); - is(read_text("$fixture/stderr"), '', - "statelite reports no missing-key errors with DSA present=$with_dsa"); - like(read_text("$fixture/client/ssh_config"), qr/StrictHostKeyChecking no/, - 'statelite writes client configuration only inside the fixture'); - is(read_text("$image/etc/ssh/ssh_host_rsa_key"), "rsa-host-key\n", - "statelite preserves the RSA host key with DSA present=$with_dsa"); - is(-f "$image/etc/ssh/ssh_host_dsa_key" ? 1 : 0, $with_dsa, - 'statelite copies the optional DSA key only when supplied'); - is((stat("$image/etc/ssh/ssh_host_rsa_key"))[2] & oct('0777'), oct('0600'), - 'statelite limits host-key permissions'); - } -} - done_testing(); From 4835de80fe8e88eae1d029962a12cc9b9c0053a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:29:43 -0300 Subject: [PATCH 02/17] test(fips): reuse the checked process runner --- xCAT-test/unit/fips_shell_runtime.t | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/xCAT-test/unit/fips_shell_runtime.t b/xCAT-test/unit/fips_shell_runtime.t index f9382bd88b..db987cdf1a 100644 --- a/xCAT-test/unit/fips_shell_runtime.t +++ b/xCAT-test/unit/fips_shell_runtime.t @@ -10,6 +10,7 @@ use File::Temp qw(tempdir); use Test::More; use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); my $bash = qx(command -v bash 2>/dev/null); chomp($bash); @@ -145,11 +146,11 @@ sub shell_output { sub shell_status { my ($shell, $library, $command, @arguments) = @_; - system( + my ($status, $output) = run_command( $shell, '-c', '. "$1"; ' . $command, 'xcat-fips-test', $library, @arguments, ); - return $? >> 8; + return $status; } sub run_key_cycle { @@ -161,11 +162,11 @@ printf '%s\n' "$3" > "$2" xcat_generate_discovery_private_key "$fips_state" "$4" 2>/dev/null || exit xcat_discovery_public_key "$fips_state" "$4" > "$5" 2>/dev/null EOF - system( + my ($exit_status, $output) = run_command( $shell, '-c', $command, 'xcat-fips-test', $library, $status, $next_state, $key, $public, ); - return $? >> 8; + return $exit_status; } sub command_output { From 5ea05d0e6021be1fa6d8c0aa5dea43ad58ee3069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:39:36 -0300 Subject: [PATCH 03/17] refactor(fips): share shell policy across runtime artifacts Generate the Genesis and postscript policy blocks from one POSIX source. Keep the runtime libraries self-contained and reject stale generated blocks before RPM or Debian builds. Preserve the existing state predicate, key algorithms, key sizes and public-key export fallbacks. Keep caller-specific helpers separate. --- build-utils/fips-policy.sh | 33 ++++++++++++++++ build-utils/sync-fips-policy | 46 +++++++++++++++++++++++ builddebs.pl | 5 +++ buildrpms.pl | 5 +++ xCAT-genesis-scripts/usr/lib/xcat/fips.sh | 13 ++++--- xCAT/postscripts/xcatlib.sh | 2 + 6 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 build-utils/fips-policy.sh create mode 100755 build-utils/sync-fips-policy diff --git a/build-utils/fips-policy.sh b/build-utils/fips-policy.sh new file mode 100644 index 0000000000..9839987374 --- /dev/null +++ b/build-utils/fips-policy.sh @@ -0,0 +1,33 @@ +xcat_fips_enabled() +{ + grep -q '^1$' "${1:-/proc/sys/crypto/fips_enabled}" 2>/dev/null +} + +xcat_fips_state() +{ + if xcat_fips_enabled "${1:-}"; then + printf '1' + else + printf '0' + fi +} + +xcat_generate_discovery_private_key() +{ + case "$1" in + 1) openssl ecparam -name prime256v1 -genkey -noout -out "$2" ;; + 0) openssl genrsa -out "$2" 1024 ;; + *) return 1 ;; + esac +} + +xcat_discovery_public_key() +{ + case "$1" in + 0|1) ;; + *) return 1 ;; + esac + openssl pkey -in "$2" -pubout 2>/dev/null || + openssl rsa -in "$2" -pubout 2>/dev/null || + openssl ec -in "$2" -pubout +} diff --git a/build-utils/sync-fips-policy b/build-utils/sync-fips-policy new file mode 100755 index 0000000000..9009ac090e --- /dev/null +++ b/build-utils/sync-fips-policy @@ -0,0 +1,46 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use FindBin; +use File::Basename qw(dirname); +use File::Slurper qw(read_binary write_binary); +use File::Temp qw(tempfile); + +my $mode = shift // ''; +die "Usage: $0 --check|--write\n" + unless !@ARGV && ($mode eq '--check' || $mode eq '--write'); +my $root = "$FindBin::Bin/.."; +my $policy = read_binary("$FindBin::Bin/fips-policy.sh"); +die "FIPS policy is empty\n" unless length $policy; +die "FIPS policy must end with a newline\n" unless $policy =~ /\n\z/; +my $begin = "# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write\n"; +my $end = "# END generated FIPS policy\n"; +my $block = $begin . $policy . $end; +my $failed = 0; + +for my $relative ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', + 'xCAT/postscripts/xcatlib.sh') { + my $path = "$root/$relative"; + die "$relative must be a regular file\n" unless -f $path && !-l $path; + my $content = read_binary($path); + die "$relative must contain one FIPS policy block\n" + unless (() = $content =~ /\Q$begin\E/g) == 1 + && (() = $content =~ /\Q$end\E/g) == 1; + my $rendered = $content; + my $count = $rendered =~ s/\Q$begin\E.*?\Q$end\E/$block/sg; + die "$relative must contain one FIPS policy block\n" unless $count == 1; + next if $rendered eq $content; + if ($mode eq '--check') { + warn "$relative is out of date; run build-utils/sync-fips-policy --write\n"; + $failed = 1; + next; + } + my ($fh, $temporary) = tempfile('.fips-policy-XXXXXX', + DIR => dirname($path), UNLINK => 1); + close $fh or die "close $temporary: $!\n"; + write_binary($temporary, $rendered); + chmod((stat($path))[2] & oct('0777'), $temporary) or die "chmod $temporary: $!\n"; + rename($temporary, $path) or die "replace $path: $!\n"; +} +exit $failed; diff --git a/builddebs.pl b/builddebs.pl index 3c7778d692..0a659d26a8 100755 --- a/builddebs.pl +++ b/builddebs.pl @@ -112,6 +112,11 @@ for my $pkg ($opts{packages}->@*) { unless grep { $_ eq $pkg } @PACKAGES; } +if (grep { $_ eq 'xCAT' || $_ eq 'xCAT-genesis-scripts' } $opts{packages}->@*) { + system($^X, "$FindBin::Bin/build-utils/sync-fips-policy", '--check') == 0 + or die "FATAL: generated FIPS policy is not current\n"; +} + my $ROOT = abs_path($FindBin::Bin); my $VERSION = read_line("$ROOT/Version") // die "Cannot read $ROOT/Version\n"; my $EPOCH = source_date_epoch(); diff --git a/buildrpms.pl b/buildrpms.pl index b0ac601be2..2b19585c46 100755 --- a/buildrpms.pl +++ b/buildrpms.pl @@ -989,6 +989,11 @@ sub main { usage(message => "openEuler binary repository builds require --gpg-sign") if defined($native_subdir) && !$opts{source_only} && !$opts{gpg_sign}; + if (grep { $_ eq 'xCAT' || $_ eq 'xCAT-genesis-scripts' } $opts{packages}->@*) { + system($^X, "$Bin/build-utils/sync-fips-policy", '--check') == 0 + or die "FATAL: generated FIPS policy is not current\n"; + } + # ---- concurrency guard (mirrors cluster-test.pl's per-cluster lock) -------------------------- # Every per-package mock chroot/config for this run shares the "-" namespace: # /etc/mock/.cfg, /var/lib/mock// and its buildroot.lock. Two builds of the SAME diff --git a/xCAT-genesis-scripts/usr/lib/xcat/fips.sh b/xCAT-genesis-scripts/usr/lib/xcat/fips.sh index 2bff97cdba..ef4aec1cd3 100644 --- a/xCAT-genesis-scripts/usr/lib/xcat/fips.sh +++ b/xCAT-genesis-scripts/usr/lib/xcat/fips.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Genesis ships separately from xCAT/postscripts/xcatlib.sh but must use the same FIPS policy. +# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write xcat_fips_enabled() { grep -q '^1$' "${1:-/proc/sys/crypto/fips_enabled}" 2>/dev/null @@ -15,11 +15,6 @@ xcat_fips_state() fi } -xcat_dsa_allowed() -{ - [ "$1" = 0 ] -} - xcat_generate_discovery_private_key() { case "$1" in @@ -39,3 +34,9 @@ xcat_discovery_public_key() openssl rsa -in "$2" -pubout 2>/dev/null || openssl ec -in "$2" -pubout } +# END generated FIPS policy + +xcat_dsa_allowed() +{ + [ "$1" = 0 ] +} diff --git a/xCAT/postscripts/xcatlib.sh b/xCAT/postscripts/xcatlib.sh index 3e68c2f522..5f34381936 100755 --- a/xCAT/postscripts/xcatlib.sh +++ b/xCAT/postscripts/xcatlib.sh @@ -1,5 +1,6 @@ #!/bin/bash # Postscripts ship separately from xCAT-genesis-scripts/usr/lib/xcat/fips.sh but must use the same FIPS policy. +# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write xcat_fips_enabled() { grep -q '^1$' "${1:-/proc/sys/crypto/fips_enabled}" 2>/dev/null @@ -33,6 +34,7 @@ xcat_discovery_public_key() openssl rsa -in "$2" -pubout 2>/dev/null || openssl ec -in "$2" -pubout } +# END generated FIPS policy xcat_prepare_discovery_key() { From 6a8755ac1341b6718c707b93c018c9b199686c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:39:51 -0300 Subject: [PATCH 04/17] test(fips): verify generated policy and package payloads --- .github/workflows/xcat_test.yml | 6 +- xCAT-test/lib/XCAT/Test/Lifecycle.pm | 6 ++ xCAT-test/lib/XCAT/Test/RPM.pm | 4 ++ xCAT-test/native/fips_build_guard.t | 43 ++++++++++++ xCAT-test/native/fips_package_payload.t | 46 +++++++++++++ xCAT-test/unit/fips_policy_sources.t | 79 +++++++++++++++++++++++ xCAT-test/unit/fips_public_key_fallback.t | 49 ++++++++++++++ xCAT-test/unit/fips_shell_runtime.t | 12 ++-- 8 files changed, 239 insertions(+), 6 deletions(-) create mode 100644 xCAT-test/native/fips_build_guard.t create mode 100644 xCAT-test/native/fips_package_payload.t create mode 100644 xCAT-test/unit/fips_policy_sources.t create mode 100644 xCAT-test/unit/fips_public_key_fallback.t diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index b757dfb068..0d63be47fd 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -20,6 +20,8 @@ jobs: run: prove -v xCAT-test/native/statelite_optional_hostkey.t - name: Test Cumulus discovery key failure run: prove -v xCAT-test/native/cumulus_discovery_key_failure.t + - name: Test FIPS package artifacts + run: prove -v xCAT-test/native/fips_build_guard.t xCAT-test/native/fips_package_payload.t - name: Install image test dependencies if: ${{ !cancelled() }} @@ -115,8 +117,10 @@ jobs: - name: Install lifecycle test dependencies run: | dnf install -y epel-release - dnf --enablerepo=${{ matrix.repository }} install -y perl perl-Capture-Tiny perl-File-Slurper perl-Test-Simple rpm-build bubblewrap systemd chkconfig initscripts tar gzip which diffutils httpd curl procps-ng + dnf --enablerepo=${{ matrix.repository }} install -y perl perl-Capture-Tiny perl-File-Slurper perl-Test-Simple rpm-build bubblewrap systemd chkconfig initscripts tar gzip bzip2 which diffutils httpd curl procps-ng openssl - name: Enable lifecycle test namespaces run: sysctl -w kernel.apparmor_restrict_unprivileged_userns=0 - name: Test RPM package lifecycle run: su -s /bin/sh nobody -c 'prove -v xCAT-test/native/xcatd_rpm_lifecycle.t xCAT-test/native/apache_rpm_configuration.t xCAT-test/native/apache_package_lifecycle.t xCAT-test/native/package_upgrade_logging.t' + - name: Test FIPS package artifacts + run: su -s /bin/sh nobody -c 'prove -v xCAT-test/native/fips_package_payload.t' diff --git a/xCAT-test/lib/XCAT/Test/Lifecycle.pm b/xCAT-test/lib/XCAT/Test/Lifecycle.pm index 8aaa597f18..696530a65f 100644 --- a/xCAT-test/lib/XCAT/Test/Lifecycle.pm +++ b/xCAT-test/lib/XCAT/Test/Lifecycle.pm @@ -31,6 +31,12 @@ sub build_deb { my $work = tempdir(CLEANUP => 1); checked('tar', '-cf', "$work/source.tar", '-C', repo_path('.'), $package); checked('tar', '-xf', "$work/source.tar", '-C', $work); + if ($package eq 'xCAT-genesis-scripts') { + my $arch = checked('dpkg-architecture', '-qDEB_HOST_ARCH'); + chomp $arch; + copy("$work/$package/debian/control-$arch", "$work/$package/debian/control") + or die "copy Genesis control-$arch: $!"; + } my $previous = getcwd(); chdir "$work/$package" or die "chdir: $!"; local $ENV{PWD} = getcwd(); diff --git a/xCAT-test/lib/XCAT/Test/RPM.pm b/xCAT-test/lib/XCAT/Test/RPM.pm index 03ce6c8fe8..226bae68fd 100644 --- a/xCAT-test/lib/XCAT/Test/RPM.pm +++ b/xCAT-test/lib/XCAT/Test/RPM.pm @@ -20,6 +20,10 @@ sub build { chomp $version; XCAT::Test::Lifecycle::checked('tar', '-czf', "$top/SOURCES/$package-$version.tar.gz", '-C', repo_path('.'), $package); + if ($package eq 'xCAT-genesis-scripts') { + XCAT::Test::Lifecycle::checked('tar', '-cjf', "$top/SOURCES/$package.tar.bz2", + '-C', repo_path('.'), $package); + } if ($package eq 'xCAT' || $package eq 'xCATsn') { for my $source (glob(repo_path('xCAT/*.lua')), glob(repo_path('xCAT/*.sh'))) { copy($source, "$top/SOURCES/" . basename($source)) or croak "copy $source: $!"; diff --git a/xCAT-test/native/fips_build_guard.t b/xCAT-test/native/fips_build_guard.t new file mode 100644 index 0000000000..30b2c49426 --- /dev/null +++ b/xCAT-test/native/fips_build_guard.t @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Copy qw(copy); +use File::Basename qw(dirname); +use File::Path qw(make_path); +use File::Slurper qw(read_binary write_binary); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); +use XCAT::Test::Sandbox qw(sandbox_root sandbox_run); + +plan skip_all => 'build guards require Linux namespaces' unless $^O eq 'linux'; +my $root = sandbox_root(); +make_path("$root/source", "$root/home"); +my ($rc, $output) = run_command('cp', '-a', repo_path('build-utils'), "$root/source/"); +die "copy build tools: $output" if $rc; +for my $relative (qw(builddebs.pl buildrpms.pl Version Release), + 'xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh') { + make_path(dirname("$root/source/$relative")); + copy(repo_path($relative), "$root/source/$relative") or die "copy $relative: $!"; +} +copy('/etc/os-release', "$root/etc/os-release") or die "copy OS identity: $!"; +write_binary("$root/source/Gitepoch", "1600000000\n"); +my $source = "$root/source/build-utils/fips-policy.sh"; +write_binary($source, read_binary($source) . "\nxcat_policy_probe() { :; }\n"); +for my $builder (qw(buildrpms.pl builddebs.pl)) { + for my $package (qw(xCAT xCAT-genesis-scripts)) { + ($rc, $output) = sandbox_run($root, { + writable => {"$root/source" => '/source'}, + env => {HOME => '/fixture/home'}}, + '/bin/sh', '-c', 'cd /source && exec /usr/bin/perl "$@"', + 'fips-build-test', "./$builder", '--package', $package); + isnt($rc, 0, "$builder rejects stale policy for $package"); + like($output, qr/generated FIPS policy is not current/, + 'the builder reports policy drift before package work'); + } +} + +done_testing(); diff --git a/xCAT-test/native/fips_package_payload.t b/xCAT-test/native/fips_package_payload.t new file mode 100644 index 0000000000..d1dec8f7b7 --- /dev/null +++ b/xCAT-test/native/fips_package_payload.t @@ -0,0 +1,46 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Path qw(make_path); +use File::Temp qw(tempdir); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); +use XCAT::Test::Lifecycle; +use XCAT::Test::RPM; + +plan skip_all => 'requires Linux package tools' unless $^O eq 'linux'; +die 'run package builds as an ordinary user' unless $>; +my $root = tempdir(CLEANUP => 1); +make_path("$root/home"); +local %ENV = (%ENV, HOME => "$root/home", LC_ALL => 'C'); +delete @ENV{qw(PERL5OPT PERL5LIB PERLLIB XCATROOT)}; +my $format = -f '/etc/debian_version' ? 'deb' : 'rpm'; +for my $package (qw(xCAT xCAT-genesis-scripts)) { + my $artifact = $format eq 'deb' ? XCAT::Test::Lifecycle::build_deb($package) + : XCAT::Test::RPM->build($package, 1, '--define', 'rhel 9'); + make_path("$root/$package"); + if ($format eq 'deb') { + XCAT::Test::Lifecycle::checked('dpkg-deb', '-x', $artifact, "$root/$package"); + } else { + XCAT::Test::Lifecycle::checked('bash', '-o', 'pipefail', '-c', + 'cd "$1" && rpm2cpio "$2" | cpio -idm --quiet', + 'extract-package', "$root/$package", $artifact); + } +} +my @genesis = glob "$root/xCAT-genesis-scripts/opt/xcat/share/xcat/netboot/genesis/*/fs/usr/lib/xcat/fips.sh"; +is(scalar @genesis, 1, 'the Genesis package contains one standalone FIPS library') + or die 'missing Genesis library'; +my $cumulus = "$root/xCAT/install/postscripts/xcatlib.sh"; +ok(-f $cumulus, 'the xCAT package contains the Cumulus library') or die 'missing postscript library'; +local $ENV{XCAT_TEST_FIPS_GENESIS_LIBRARY} = $genesis[0]; +local $ENV{XCAT_TEST_FIPS_CUMULUS_LIBRARY} = $cumulus; +my ($rc, $output) = run_command('prove', '-v', repo_path('xCAT-test/unit/fips_shell_runtime.t')); +is($rc, 0, 'packaged libraries pass the same real-crypto characterization') or diag($output); +unlike($output, qr/#\s*skip.*required/i, + 'runtime prerequisites are available for the packaged libraries'); + +done_testing(); diff --git a/xCAT-test/unit/fips_policy_sources.t b/xCAT-test/unit/fips_policy_sources.t new file mode 100644 index 0000000000..03aa0a1abd --- /dev/null +++ b/xCAT-test/unit/fips_policy_sources.t @@ -0,0 +1,79 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Copy qw(copy); +use File::Basename qw(dirname); +use File::Path qw(make_path); +use File::Slurper qw(read_binary write_binary); +use File::Temp qw(tempdir); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); + +my $sync = 'build-utils/sync-fips-policy'; +my $source = 'build-utils/fips-policy.sh'; +my @libraries = ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh'); +my ($rc, $output) = run_command($^X, repo_path($sync), '--check'); +is($rc, 0, 'committed runtime artifacts match the canonical policy') or diag($output); +is($output, '', 'a current artifact check is quiet'); + +my $root = tempdir(CLEANUP => 1); +for my $relative ($sync, $source, @libraries) { + make_path(dirname("$root/$relative")); + copy(repo_path($relative), "$root/$relative") or die "copy $relative: $!"; +} +my @original = map { scalar read_binary("$root/$_") } @libraries; +my $policy = read_binary("$root/$source"); +write_binary("$root/$source", $policy . "\nxcat_policy_probe() { printf canonical; }\n"); +($rc, $output) = run_command($^X, "$root/$sync", '--check'); +is($rc, 1, 'a changed canonical policy invalidates both artifacts'); +for my $relative (@libraries) { + like($output, qr/\Q$relative\E is out of date/, 'check identifies the stale artifact'); +} +is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@original, + 'check does not modify stale artifacts'); +($rc, $output) = run_command($^X, "$root/$sync", '--write'); +is($rc, 0, 'write refreshes both artifacts') or diag($output); +for my $relative (@libraries) { + my $shell = $relative eq $libraries[0] ? 'sh' : 'bash'; + ($rc, $output) = run_command($shell, '-c', '. "$1"; xcat_policy_probe', + 'fips-artifact-test', "$root/$relative"); + is($rc, 0, 'the generated artifact provides the updated policy'); + is($output, 'canonical', 'the generated artifact executes canonical content'); +} +my @rendered = map { scalar read_binary("$root/$_") } @libraries; +($rc, $output) = run_command($^X, "$root/$sync", '--write'); +is($rc, 0, 'repeated generation succeeds'); +is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, + 'generation is idempotent'); +($rc, $output) = run_command($^X, "$root/$sync", '--check'); +is($rc, 0, 'refreshed artifacts pass the check'); + +my $unterminated = $policy; +$unterminated =~ s/\n+\z//; +write_binary("$root/$source", $unterminated); +for my $mode (qw(--check --write)) { + ($rc, $output) = run_command($^X, "$root/$sync", $mode); + isnt($rc, 0, "$mode rejects a policy without a final newline"); + like($output, qr/FIPS policy must end with a newline/, + 'the error identifies the unterminated policy'); + is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, + 'an unterminated policy leaves both artifacts untouched'); +} + +unlink "$root/$source" or die "unlink policy: $!"; +($rc, $output) = run_command($^X, "$root/$sync", '--write'); +isnt($rc, 0, 'a missing source fails generation'); +is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, + 'a missing source leaves both artifacts untouched'); +write_binary("$root/$source", $policy); +write_binary("$root/$libraries[0]", "#!/bin/sh\n"); +($rc, $output) = run_command($^X, "$root/$sync", '--write'); +isnt($rc, 0, 'a missing insertion block fails generation'); +is(read_binary("$root/$libraries[0]"), "#!/bin/sh\n", + 'generation does not replace a malformed artifact'); + +done_testing(); diff --git a/xCAT-test/unit/fips_public_key_fallback.t b/xCAT-test/unit/fips_public_key_fallback.t new file mode 100644 index 0000000000..790bc6d567 --- /dev/null +++ b/xCAT-test/unit/fips_public_key_fallback.t @@ -0,0 +1,49 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +use FindBin; +use lib "$FindBin::Bin/../lib"; +use File::Path qw(make_path); +use File::Slurper qw(read_text write_text); +use File::Temp qw(tempdir); +use Test::More; +use XCAT::Test::File qw(repo_path); +use XCAT::Test::Process qw(run_command); + +my $root = tempdir(CLEANUP => 1); +make_path("$root/bin"); +write_text("$root/bin/openssl", <<'SH'); +#!/bin/sh +printf '%s\n' "$1" >> "$CALLS" +if [ "$1" = "$ACCEPT" ]; then + printf 'public key\n' + exit 0 +fi +printf '%s failed\n' "$1" >&2 +exit 73 +SH +chmod 0755, "$root/bin/openssl" or die "chmod openssl: $!"; +local $ENV{PATH} = "$root/bin:$ENV{PATH}"; +local $ENV{CALLS} = "$root/calls"; + +for my $library ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh') { + my $shell = $library =~ /genesis/ ? 'sh' : 'bash'; + for my $mode (0, 1) { + for my $case (['pkey', "pkey\n"], ['rsa', "pkey\nrsa\n"], + ['ec', "pkey\nrsa\nec\n"], ['none', "pkey\nrsa\nec\n"]) { + my ($accept, $calls) = @$case; + local $ENV{ACCEPT} = $accept; + write_text($ENV{CALLS}, ''); + my ($rc, $output) = run_command($shell, '-c', + '. "$1"; xcat_discovery_public_key "$2" unused-key', + 'fips-fallback-test', repo_path($library), $mode); + is($rc, $accept eq 'none' ? 73 : 0, "$library mode $mode exports through $accept"); + is($output, $accept eq 'none' ? "ec failed\n" : "public key\n", + 'only the final error or successful key reaches the caller'); + is(read_text($ENV{CALLS}), $calls, 'fallbacks run in order and stop on success'); + } + } +} + +done_testing(); diff --git a/xCAT-test/unit/fips_shell_runtime.t b/xCAT-test/unit/fips_shell_runtime.t index db987cdf1a..4db6ec896f 100644 --- a/xCAT-test/unit/fips_shell_runtime.t +++ b/xCAT-test/unit/fips_shell_runtime.t @@ -19,6 +19,10 @@ chomp($sh); my $openssl = qx(command -v openssl 2>/dev/null); chomp($openssl); my $host_fips = 0; +my $genesis_library = $ENV{XCAT_TEST_FIPS_GENESIS_LIBRARY} + // repo_path('xCAT-genesis-scripts/usr/lib/xcat/fips.sh'); +my $cumulus_library = $ENV{XCAT_TEST_FIPS_CUMULUS_LIBRARY} + // repo_path('xCAT/postscripts/xcatlib.sh'); if (-r '/proc/sys/crypto/fips_enabled') { $host_fips = read_text('/proc/sys/crypto/fips_enabled') =~ /^1\s*$/; } @@ -28,11 +32,11 @@ SKIP: { unless $sh && -x $sh && $bash && -x $bash && $openssl && -x $openssl; foreach my $runtime ( - ['xCAT-genesis-scripts/usr/lib/xcat/fips.sh', $sh], - ['xCAT/postscripts/xcatlib.sh', $bash], + [$genesis_library, $sh], + [$cumulus_library, $bash], ) { my ($relative, $shell) = @{$runtime}; - my $library = repo_path($relative); + my $library = $relative; my $tmpdir = tempdir(CLEANUP => 1); my $status = File::Spec->catfile($tmpdir, 'fips_enabled'); my $missing = File::Spec->catfile($tmpdir, 'missing'); @@ -111,13 +115,11 @@ SKIP: { } } - my $genesis_library = repo_path('xCAT-genesis-scripts/usr/lib/xcat/fips.sh'); is(shell_status($sh, $genesis_library, 'xcat_dsa_allowed 0'), 0, 'Genesis permits DSA outside FIPS mode'); isnt(shell_status($sh, $genesis_library, 'xcat_dsa_allowed 1'), 0, 'Genesis rejects DSA in FIPS mode'); - my $cumulus_library = repo_path('xCAT/postscripts/xcatlib.sh'); my $key_dir = tempdir(CLEANUP => 1); my $existing_key = "$key_dir/existing.pem"; system($openssl, 'genrsa', '-out', $existing_key, 2048) == 0 From e754e5657cd5104c86d14f578e9668b891ca5c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 17 Oct 2026 02:39:52 -0300 Subject: [PATCH 05/17] docs(fips): describe policy generation and native test scope --- build-utils/README.fips.md | 16 ++++++++++++++++ xCAT-test/native/README.md | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 build-utils/README.fips.md diff --git a/build-utils/README.fips.md b/build-utils/README.fips.md new file mode 100644 index 0000000000..688f5db493 --- /dev/null +++ b/build-utils/README.fips.md @@ -0,0 +1,16 @@ +# Shared FIPS shell policy + +Edit `fips-policy.sh`, then run: + +``` +perl build-utils/sync-fips-policy --write +perl build-utils/sync-fips-policy --check +``` + +Commit the source and both updated runtime libraries together. The generated +blocks in Genesis `usr/lib/xcat/fips.sh` and postscripts `xcatlib.sh` stay tracked +so source-checkout callers and standalone packages need no generation step. +The RPM and Debian builders reject stale blocks before building either package. + +Keep caller-specific helpers outside the generated block. The policy source +must remain POSIX shell. It must not depend on the other package at runtime. diff --git a/xCAT-test/native/README.md b/xCAT-test/native/README.md index 3fe5ac7738..231b85fa9e 100644 --- a/xCAT-test/native/README.md +++ b/xCAT-test/native/README.md @@ -186,6 +186,38 @@ an installation, capture a kernel crash or modify host accounts. Check the TAP output for skipped prerequisites. A successful `prove` exit with skipped cases does not qualify those cases. +## FIPS policy and discovery keys + +Run the unchanged statelite and Cumulus callers in private filesystems: + +``` +prove xCAT-test/native/statelite_optional_hostkey.t \ + xCAT-test/native/cumulus_discovery_key_failure.t \ + xCAT-test/native/fips_build_guard.t +``` + +These cases require Bubblewrap, GNU timeout, Bash, File::Slurper, +Capture::Tiny and Parallel::ForkManager. Enable Linux user namespaces. +The Cumulus case replaces OpenSSL with a failing command to test error handling. +It does not exercise a switch or its discovery network. + +Run the package case as an ordinary user: + +``` +prove xCAT-test/native/fips_package_payload.t +``` + +It builds xCAT and Genesis scripts with the host's package tools, extracts the +packages, and runs `unit/fips_shell_runtime.t` against their libraries. Use +Debian build tools on Debian or Ubuntu, and RPM build tools with cpio and bzip2 +on EL. Both routes need OpenSSL, File::Slurper and Capture::Tiny. +The check bypasses build dependencies and does not install either package. +It does not build or boot a Genesis image. + +Run the crypto cases on both FIPS-enabled and non-FIPS hosts. A status-file +fixture selects the policy but does not enable FIPS in OpenSSL. Legacy RSA-1024 +signing is skipped on FIPS hosts and must pass on the non-FIPS host. + ## Package lifecycle Run these cases as an ordinary user with unprivileged Linux namespaces enabled. From 5e532b20e4ffe4c6d037ecd7556bb7cf1783be78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sat, 17 Oct 2026 14:23:55 -0300 Subject: [PATCH 06/17] test(build): include FIPS policy in the release repository fixture Stage the policy checker, its canonical source and both runtime artifacts in the isolated repository used by the default package scheduling case. Keep the real build guard active so the fixture follows the same policy checks as a source checkout. --- xCAT-test/native/release_repository.t | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/xCAT-test/native/release_repository.t b/xCAT-test/native/release_repository.t index fca0c8ae2d..dc131a98e5 100644 --- a/xCAT-test/native/release_repository.t +++ b/xCAT-test/native/release_repository.t @@ -1,6 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; +use File::Basename qw(dirname); use File::Copy qw(copy); use File::Path qw(make_path); use File::Slurper qw(read_binary write_text); @@ -40,9 +41,12 @@ my $files = command($root, 'rpm', '-qp', '--qf', '[%{FILENAMES}\t%{FILEFLAGS}\n] for my $repo (qw(xcat-core xcat-dep xcat-dep-common)) { like($files, qr{^/etc/yum\.repos\.d/\Q$repo\E\.repo\t17$}m, "$repo remains config(noreplace)"); } -make_path("$root/work/build-utils/lib/XCAT"); -copy(repo_path('buildrpms.pl'), "$root/work/buildrpms.pl") or die $!; -copy(repo_path('build-utils/lib/XCAT/BuildUtils.pm'), "$root/work/build-utils/lib/XCAT/BuildUtils.pm") or die $!; +for my $relative (qw(buildrpms.pl build-utils/lib/XCAT/BuildUtils.pm + build-utils/sync-fips-policy build-utils/fips-policy.sh + xCAT-genesis-scripts/usr/lib/xcat/fips.sh xCAT/postscripts/xcatlib.sh)) { + make_path(dirname("$root/work/$relative")); + copy(repo_path($relative), "$root/work/$relative") or die "copy $relative: $!"; +} write_text("$root/work/Version", "9.9.9\n"); write_text("$root/work/Gitepoch", "1600000000\n"); write_text("$root/work/Gitinfo", ('a' x 40) . "\n"); From 53dfdd29023e42b32239fea56c3c4938d82e8d55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:44:30 -0300 Subject: [PATCH 07/17] test(ci): run FIPS checks after earlier failures --- .github/workflows/xcat_test.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index 0d63be47fd..ceebb71b19 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -17,10 +17,13 @@ jobs: - name: Test Debian package lifecycle run: prove -v xCAT-test/native/xcatd_debian_lifecycle.t xCAT-test/native/xcatd_restart_callers.t xCAT-test/native/apache_package_lifecycle.t xCAT-test/native/package_upgrade_logging.t - name: Test optional statelite host keys + if: ${{ !cancelled() }} run: prove -v xCAT-test/native/statelite_optional_hostkey.t - name: Test Cumulus discovery key failure + if: ${{ !cancelled() }} run: prove -v xCAT-test/native/cumulus_discovery_key_failure.t - name: Test FIPS package artifacts + if: ${{ !cancelled() }} run: prove -v xCAT-test/native/fips_build_guard.t xCAT-test/native/fips_package_payload.t - name: Install image test dependencies @@ -123,4 +126,5 @@ jobs: - name: Test RPM package lifecycle run: su -s /bin/sh nobody -c 'prove -v xCAT-test/native/xcatd_rpm_lifecycle.t xCAT-test/native/apache_rpm_configuration.t xCAT-test/native/apache_package_lifecycle.t xCAT-test/native/package_upgrade_logging.t' - name: Test FIPS package artifacts + if: ${{ !cancelled() }} run: su -s /bin/sh nobody -c 'prove -v xCAT-test/native/fips_package_payload.t' From 8537fb97fb1b739634ed7dd151e2d351419f389f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:45:31 -0300 Subject: [PATCH 08/17] test(fips): require stale artifact diagnostics from build guards --- xCAT-test/native/fips_build_guard.t | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xCAT-test/native/fips_build_guard.t b/xCAT-test/native/fips_build_guard.t index 30b2c49426..bf51309e19 100644 --- a/xCAT-test/native/fips_build_guard.t +++ b/xCAT-test/native/fips_build_guard.t @@ -18,8 +18,8 @@ my $root = sandbox_root(); make_path("$root/source", "$root/home"); my ($rc, $output) = run_command('cp', '-a', repo_path('build-utils'), "$root/source/"); die "copy build tools: $output" if $rc; -for my $relative (qw(builddebs.pl buildrpms.pl Version Release), - 'xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh') { +my @libraries = ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh'); +for my $relative (qw(builddebs.pl buildrpms.pl Version Release), @libraries) { make_path(dirname("$root/source/$relative")); copy(repo_path($relative), "$root/source/$relative") or die "copy $relative: $!"; } @@ -37,6 +37,10 @@ for my $builder (qw(buildrpms.pl builddebs.pl)) { isnt($rc, 0, "$builder rejects stale policy for $package"); like($output, qr/generated FIPS policy is not current/, 'the builder reports policy drift before package work'); + for my $relative (@libraries) { + like($output, qr/\Q$relative\E is out of date/, + "$builder identifies stale $relative for $package"); + } } } From e07f488643b1f0c544346ae7e8bc03306892b2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Sun, 18 Oct 2026 13:46:52 -0300 Subject: [PATCH 09/17] test(fips): verify generated artifact modes are preserved --- xCAT-test/unit/fips_policy_sources.t | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xCAT-test/unit/fips_policy_sources.t b/xCAT-test/unit/fips_policy_sources.t index 03aa0a1abd..9ffae71770 100644 --- a/xCAT-test/unit/fips_policy_sources.t +++ b/xCAT-test/unit/fips_policy_sources.t @@ -25,6 +25,10 @@ for my $relative ($sync, $source, @libraries) { make_path(dirname("$root/$relative")); copy(repo_path($relative), "$root/$relative") or die "copy $relative: $!"; } +my %modes = ($libraries[0] => oct('0644'), $libraries[1] => oct('0755')); +for my $relative (@libraries) { + chmod($modes{$relative}, "$root/$relative") or die "chmod $relative: $!"; +} my @original = map { scalar read_binary("$root/$_") } @libraries; my $policy = read_binary("$root/$source"); write_binary("$root/$source", $policy . "\nxcat_policy_probe() { printf canonical; }\n"); @@ -38,6 +42,8 @@ is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@original, ($rc, $output) = run_command($^X, "$root/$sync", '--write'); is($rc, 0, 'write refreshes both artifacts') or diag($output); for my $relative (@libraries) { + is((stat("$root/$relative"))[2] & oct('0777'), $modes{$relative}, + "generation preserves the mode of $relative"); my $shell = $relative eq $libraries[0] ? 'sh' : 'bash'; ($rc, $output) = run_command($shell, '-c', '. "$1"; xcat_policy_probe', 'fips-artifact-test', "$root/$relative"); @@ -49,6 +55,10 @@ my @rendered = map { scalar read_binary("$root/$_") } @libraries; is($rc, 0, 'repeated generation succeeds'); is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, 'generation is idempotent'); +for my $relative (@libraries) { + is((stat("$root/$relative"))[2] & oct('0777'), $modes{$relative}, + "repeated generation preserves the mode of $relative"); +} ($rc, $output) = run_command($^X, "$root/$sync", '--check'); is($rc, 0, 'refreshed artifacts pass the check'); From 9eec586e7b6275fc25c9bcd657e5753898ee475e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:21:12 -0300 Subject: [PATCH 10/17] test(fips): move public key fallback cases to Bats --- xCAT-test/bats/fips_public_key_fallback.bats | 80 ++++++++++++++++++++ xCAT-test/unit/fips_public_key_fallback.t | 49 ------------ 2 files changed, 80 insertions(+), 49 deletions(-) create mode 100644 xCAT-test/bats/fips_public_key_fallback.bats delete mode 100644 xCAT-test/unit/fips_public_key_fallback.t diff --git a/xCAT-test/bats/fips_public_key_fallback.bats b/xCAT-test/bats/fips_public_key_fallback.bats new file mode 100644 index 0000000000..8b471cde9d --- /dev/null +++ b/xCAT-test/bats/fips_public_key_fallback.bats @@ -0,0 +1,80 @@ +#!/usr/bin/env bats + +load 'helpers/shell_source' + +setup() +{ + mkdir -p "$BATS_TEST_TMPDIR/bin" + cat >"$BATS_TEST_TMPDIR/bin/openssl" <<'SH' +#!/bin/sh +printf '%s\n' "$1" >> "$CALLS" +if [ "$1" = "$ACCEPT" ]; then + printf 'public key\n' + exit 0 +fi +printf '%s failed\n' "$1" >&2 +exit 73 +SH + chmod 0755 "$BATS_TEST_TMPDIR/bin/openssl" + export PATH="$BATS_TEST_TMPDIR/bin:$PATH" + export CALLS="$BATS_TEST_TMPDIR/calls" + export RESULT="$BATS_TEST_TMPDIR/result" +} + +check_fallback() +{ + local shell="$1" library="$2" accept="$3" calls="$4" mode + local expected_status=0 + export ACCEPT="$accept" + printf '%s' "$calls" >"$BATS_TEST_TMPDIR/expected-calls" + if [ "$accept" = none ]; then + expected_status=73 + printf 'ec failed\n' >"$BATS_TEST_TMPDIR/expected-result" + else + printf 'public key\n' >"$BATS_TEST_TMPDIR/expected-result" + fi + for mode in 0 1; do + printf '# %s policy mode %s\n' "$library" "$mode" >&3 + : >"$CALLS" + run "$shell" -c ' + exec >"$RESULT" 2>&1 + . "$1" + xcat_discovery_public_key "$2" unused-key + ' fips-fallback-test "$(repo_path "$library")" "$mode" + [ "$status" -eq "$expected_status" ] + cmp "$BATS_TEST_TMPDIR/expected-result" "$RESULT" + cmp "$BATS_TEST_TMPDIR/expected-calls" "$CALLS" + done +} + +@test "Genesis exports through pkey without trying fallbacks" { + check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh pkey $'pkey\n' +} + +@test "Genesis falls back to rsa and stops on success" { + check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh rsa $'pkey\nrsa\n' +} + +@test "Genesis falls back to ec and hides earlier errors" { + check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh ec $'pkey\nrsa\nec\n' +} + +@test "Genesis returns the final error when all exporters fail" { + check_fallback sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh none $'pkey\nrsa\nec\n' +} + +@test "Cumulus exports through pkey without trying fallbacks" { + check_fallback bash xCAT/postscripts/xcatlib.sh pkey $'pkey\n' +} + +@test "Cumulus falls back to rsa and stops on success" { + check_fallback bash xCAT/postscripts/xcatlib.sh rsa $'pkey\nrsa\n' +} + +@test "Cumulus falls back to ec and hides earlier errors" { + check_fallback bash xCAT/postscripts/xcatlib.sh ec $'pkey\nrsa\nec\n' +} + +@test "Cumulus returns the final error when all exporters fail" { + check_fallback bash xCAT/postscripts/xcatlib.sh none $'pkey\nrsa\nec\n' +} diff --git a/xCAT-test/unit/fips_public_key_fallback.t b/xCAT-test/unit/fips_public_key_fallback.t deleted file mode 100644 index 790bc6d567..0000000000 --- a/xCAT-test/unit/fips_public_key_fallback.t +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; - -use FindBin; -use lib "$FindBin::Bin/../lib"; -use File::Path qw(make_path); -use File::Slurper qw(read_text write_text); -use File::Temp qw(tempdir); -use Test::More; -use XCAT::Test::File qw(repo_path); -use XCAT::Test::Process qw(run_command); - -my $root = tempdir(CLEANUP => 1); -make_path("$root/bin"); -write_text("$root/bin/openssl", <<'SH'); -#!/bin/sh -printf '%s\n' "$1" >> "$CALLS" -if [ "$1" = "$ACCEPT" ]; then - printf 'public key\n' - exit 0 -fi -printf '%s failed\n' "$1" >&2 -exit 73 -SH -chmod 0755, "$root/bin/openssl" or die "chmod openssl: $!"; -local $ENV{PATH} = "$root/bin:$ENV{PATH}"; -local $ENV{CALLS} = "$root/calls"; - -for my $library ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh') { - my $shell = $library =~ /genesis/ ? 'sh' : 'bash'; - for my $mode (0, 1) { - for my $case (['pkey', "pkey\n"], ['rsa', "pkey\nrsa\n"], - ['ec', "pkey\nrsa\nec\n"], ['none', "pkey\nrsa\nec\n"]) { - my ($accept, $calls) = @$case; - local $ENV{ACCEPT} = $accept; - write_text($ENV{CALLS}, ''); - my ($rc, $output) = run_command($shell, '-c', - '. "$1"; xcat_discovery_public_key "$2" unused-key', - 'fips-fallback-test', repo_path($library), $mode); - is($rc, $accept eq 'none' ? 73 : 0, "$library mode $mode exports through $accept"); - is($output, $accept eq 'none' ? "ec failed\n" : "public key\n", - 'only the final error or successful key reaches the caller'); - is(read_text($ENV{CALLS}), $calls, 'fallbacks run in order and stop on success'); - } - } -} - -done_testing(); From aebdbb804b1dfc3e105b1ad3ebecad0eeb789d16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:23:02 -0300 Subject: [PATCH 11/17] test(fips): separate generated policy shell execution --- xCAT-test/bats/fips_policy_artifacts.bats | 39 +++++++++++++++++++++++ xCAT-test/unit/fips_policy_sources.t | 5 --- 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 xCAT-test/bats/fips_policy_artifacts.bats diff --git a/xCAT-test/bats/fips_policy_artifacts.bats b/xCAT-test/bats/fips_policy_artifacts.bats new file mode 100644 index 0000000000..2c205a1eef --- /dev/null +++ b/xCAT-test/bats/fips_policy_artifacts.bats @@ -0,0 +1,39 @@ +#!/usr/bin/env bats + +load 'helpers/shell_source' + +setup() +{ + ROOT="$BATS_TEST_TMPDIR/source" + local relative + for relative in build-utils/sync-fips-policy build-utils/fips-policy.sh \ + xCAT-genesis-scripts/usr/lib/xcat/fips.sh xCAT/postscripts/xcatlib.sh; do + mkdir -p "$ROOT/$(dirname "$relative")" + cp "$(repo_path "$relative")" "$ROOT/$relative" + done + printf '\nxcat_policy_probe() { printf canonical; }\n' >>"$ROOT/build-utils/fips-policy.sh" + export RESULT="$BATS_TEST_TMPDIR/result" +} + +check_generated_policy() +{ + local shell="$1" library="$2" + run perl "$ROOT/build-utils/sync-fips-policy" --write + [ "$status" -eq 0 ] + run "$shell" -c ' + exec >"$RESULT" 2>&1 + . "$1" + xcat_policy_probe + ' fips-artifact-test "$ROOT/$library" + [ "$status" -eq 0 ] + printf canonical >"$BATS_TEST_TMPDIR/expected-result" + cmp "$BATS_TEST_TMPDIR/expected-result" "$RESULT" +} + +@test "generated Genesis policy runs in sh" { + check_generated_policy sh xCAT-genesis-scripts/usr/lib/xcat/fips.sh +} + +@test "generated Cumulus policy runs in bash" { + check_generated_policy bash xCAT/postscripts/xcatlib.sh +} diff --git a/xCAT-test/unit/fips_policy_sources.t b/xCAT-test/unit/fips_policy_sources.t index 9ffae71770..70fc141edb 100644 --- a/xCAT-test/unit/fips_policy_sources.t +++ b/xCAT-test/unit/fips_policy_sources.t @@ -44,11 +44,6 @@ is($rc, 0, 'write refreshes both artifacts') or diag($output); for my $relative (@libraries) { is((stat("$root/$relative"))[2] & oct('0777'), $modes{$relative}, "generation preserves the mode of $relative"); - my $shell = $relative eq $libraries[0] ? 'sh' : 'bash'; - ($rc, $output) = run_command($shell, '-c', '. "$1"; xcat_policy_probe', - 'fips-artifact-test', "$root/$relative"); - is($rc, 0, 'the generated artifact provides the updated policy'); - is($output, 'canonical', 'the generated artifact executes canonical content'); } my @rendered = map { scalar read_binary("$root/$_") } @libraries; ($rc, $output) = run_command($^X, "$root/$sync", '--write'); From 3d7212c3d15aabc9e28b24430306b4f2666f1b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:35:12 -0300 Subject: [PATCH 12/17] test(fips): guard Bats fixtures and expose failure output --- xCAT-test/bats/fips_policy_artifacts.bats | 6 ++++++ xCAT-test/bats/fips_public_key_fallback.bats | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/xCAT-test/bats/fips_policy_artifacts.bats b/xCAT-test/bats/fips_policy_artifacts.bats index 2c205a1eef..dab4f3ba2c 100644 --- a/xCAT-test/bats/fips_policy_artifacts.bats +++ b/xCAT-test/bats/fips_policy_artifacts.bats @@ -4,6 +4,7 @@ load 'helpers/shell_source' setup() { + : "${BATS_TEST_TMPDIR:?Bats 1.4 or newer is required}" ROOT="$BATS_TEST_TMPDIR/source" local relative for relative in build-utils/sync-fips-policy build-utils/fips-policy.sh \ @@ -19,12 +20,17 @@ check_generated_policy() { local shell="$1" library="$2" run perl "$ROOT/build-utils/sync-fips-policy" --write + printf '%s\n' "$output" [ "$status" -eq 0 ] run "$shell" -c ' exec >"$RESULT" 2>&1 . "$1" xcat_policy_probe ' fips-artifact-test "$ROOT/$library" + printf '%s\n' "$output" + if [ -f "$RESULT" ]; then + cat "$RESULT" + fi [ "$status" -eq 0 ] printf canonical >"$BATS_TEST_TMPDIR/expected-result" cmp "$BATS_TEST_TMPDIR/expected-result" "$RESULT" diff --git a/xCAT-test/bats/fips_public_key_fallback.bats b/xCAT-test/bats/fips_public_key_fallback.bats index 8b471cde9d..6f050e7a29 100644 --- a/xCAT-test/bats/fips_public_key_fallback.bats +++ b/xCAT-test/bats/fips_public_key_fallback.bats @@ -4,6 +4,7 @@ load 'helpers/shell_source' setup() { + : "${BATS_TEST_TMPDIR:?Bats 1.4 or newer is required}" mkdir -p "$BATS_TEST_TMPDIR/bin" cat >"$BATS_TEST_TMPDIR/bin/openssl" <<'SH' #!/bin/sh @@ -34,13 +35,17 @@ check_fallback() printf 'public key\n' >"$BATS_TEST_TMPDIR/expected-result" fi for mode in 0 1; do - printf '# %s policy mode %s\n' "$library" "$mode" >&3 + printf '%s policy mode %s\n' "$library" "$mode" : >"$CALLS" run "$shell" -c ' exec >"$RESULT" 2>&1 . "$1" xcat_discovery_public_key "$2" unused-key ' fips-fallback-test "$(repo_path "$library")" "$mode" + printf '%s\n' "$output" + if [ -f "$RESULT" ]; then + cat "$RESULT" + fi [ "$status" -eq "$expected_status" ] cmp "$BATS_TEST_TMPDIR/expected-result" "$RESULT" cmp "$BATS_TEST_TMPDIR/expected-calls" "$CALLS" From 748493b9ea9348e5d20b0093917c762545d940d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Mon, 19 Oct 2026 17:35:54 -0300 Subject: [PATCH 13/17] test(fips): validate public key exporter arguments --- xCAT-test/bats/fips_public_key_fallback.bats | 34 +++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/xCAT-test/bats/fips_public_key_fallback.bats b/xCAT-test/bats/fips_public_key_fallback.bats index 6f050e7a29..8bec343910 100644 --- a/xCAT-test/bats/fips_public_key_fallback.bats +++ b/xCAT-test/bats/fips_public_key_fallback.bats @@ -8,18 +8,38 @@ setup() mkdir -p "$BATS_TEST_TMPDIR/bin" cat >"$BATS_TEST_TMPDIR/bin/openssl" <<'SH' #!/bin/sh -printf '%s\n' "$1" >> "$CALLS" -if [ "$1" = "$ACCEPT" ]; then +exporter=$1 +shift +printf '%s\n' "$exporter" >> "$CALLS" +printf 'openssl' >> "$INVOCATIONS" +printf ' <%s>' "$exporter" "$@" >> "$INVOCATIONS" +printf '\n' >> "$INVOCATIONS" +input= +public=0 +while [ "$#" -gt 0 ]; do + case "$1" in + -in) [ "$#" -ge 2 ] || exit 64; input=$2; shift 2 ;; + -pubout) public=1; shift ;; + *) exit 64 ;; + esac +done +if [ "$input" != "$KEY" ] || [ "$public" -ne 1 ]; then + printf 'rejected: expected -in with the requested key and -pubout\n' >> "$INVOCATIONS" + exit 64 +fi +if [ "$exporter" = "$ACCEPT" ]; then printf 'public key\n' exit 0 fi -printf '%s failed\n' "$1" >&2 +printf '%s failed\n' "$exporter" >&2 exit 73 SH chmod 0755 "$BATS_TEST_TMPDIR/bin/openssl" export PATH="$BATS_TEST_TMPDIR/bin:$PATH" export CALLS="$BATS_TEST_TMPDIR/calls" + export INVOCATIONS="$BATS_TEST_TMPDIR/invocations" export RESULT="$BATS_TEST_TMPDIR/result" + export KEY="$BATS_TEST_TMPDIR/unused key.pem" } check_fallback() @@ -37,15 +57,19 @@ check_fallback() for mode in 0 1; do printf '%s policy mode %s\n' "$library" "$mode" : >"$CALLS" + : >"$INVOCATIONS" run "$shell" -c ' exec >"$RESULT" 2>&1 . "$1" - xcat_discovery_public_key "$2" unused-key - ' fips-fallback-test "$(repo_path "$library")" "$mode" + xcat_discovery_public_key "$2" "$3" + ' fips-fallback-test "$(repo_path "$library")" "$mode" "$KEY" printf '%s\n' "$output" if [ -f "$RESULT" ]; then cat "$RESULT" fi + cat "$INVOCATIONS" + printf 'exporters:\n' + cat "$CALLS" [ "$status" -eq "$expected_status" ] cmp "$BATS_TEST_TMPDIR/expected-result" "$RESULT" cmp "$BATS_TEST_TMPDIR/expected-calls" "$CALLS" From cfa3073da76dd65304cb5e5717e6e6da8587ebc3 Mon Sep 17 00:00:00 2001 From: Daniel Hilst <392820+dhilst@users.noreply.github.com> Date: Wed, 21 Oct 2026 10:42:37 -0300 Subject: [PATCH 14/17] refactor(fips): generate the shell policy through a module sub xCAT-test/unit/fips_policy_sources.t ran build-utils/sync-fips-policy as a subprocess, so a file in the unit directory measured a program rather than a routine, and it asserted on the diagnostics the script printed instead of on an answer. buildrpms.pl and builddebs.pl started the same script with system() to check the generated libraries before packaging xCAT or xCAT-genesis-scripts. XCAT::BuildUtils gains fips_policy_artifacts, fips_policy_plan and sync_fips_policy. fips_policy_plan reads the canonical policy and both runtime libraries and reports what generation would write, without writing. sync_fips_policy applies that plan in write mode and returns the stale paths, the paths it rewrote and the diagnostics for its caller to print. The two builders now call sync_fips_policy in their own process and print the diagnostics before they stop, which keeps the output xCAT-test/native/ fips_build_guard.t asserts on. build-utils/sync-fips-policy keeps its --check and --write contract and its exit status, and is now a thin caller. The unit test calls the sub in-process and asserts on the returned hash. It also covers a library holding two policy blocks and a library replaced by a symbolic link, which the subprocess form did not reach. Ten mutants of the module, one per guard, each fail the assertion that names that guard. xCAT-test/bats/fips_policy_artifacts.bats copies XCAT::BuildUtils into its fixture, because the script it runs now loads the module. Signed-off-by: Daniel Hilst <392820+dhilst@users.noreply.github.com> --- build-utils/lib/XCAT/BuildUtils.pm | 141 +++++++++++++++++- build-utils/sync-fips-policy | 42 +----- builddebs.pl | 7 +- buildrpms.pl | 7 +- xCAT-test/bats/fips_policy_artifacts.bats | 1 + xCAT-test/unit/fips_policy_sources.t | 173 +++++++++++++++------- 6 files changed, 272 insertions(+), 99 deletions(-) diff --git a/build-utils/lib/XCAT/BuildUtils.pm b/build-utils/lib/XCAT/BuildUtils.pm index e24d6a760f..389d4220c9 100644 --- a/build-utils/lib/XCAT/BuildUtils.pm +++ b/build-utils/lib/XCAT/BuildUtils.pm @@ -17,10 +17,10 @@ use Exporter 'import'; use Cwd (); use File::Copy qw(copy move); use File::Find qw(find); -use File::Basename qw(basename); +use File::Basename qw(basename dirname); use File::Path qw(make_path remove_tree); use File::Temp qw(tempdir tempfile); -use File::Slurper qw(read_text write_text); +use File::Slurper qw(read_binary read_text write_binary write_text); use POSIX qw(strftime); use Sys::Hostname (); use Pod::Usage qw(pod2usage); @@ -48,6 +48,7 @@ our @EXPORT_OK = qw( genesis_chroot_name genesis_target_arch genesis_build_plan genesis_log_errors genesis_log_deny_rules deb_belongs_to_dist genesis_dists genesis_dist_reason + fips_policy_artifacts fips_policy_plan sync_fips_policy ); # Both builders echo the commands they run under --verbose. Set once, after @@ -1275,3 +1276,139 @@ sub prepare_mock_resultdirs { } } + +#------------------------------------------------------------------------------- + +=head3 fips_policy_artifacts + + Descriptions: Report the runtime libraries that carry a generated copy of + the FIPS shell policy, relative to the repository root. + + Arguments: none. + + Returns: the list of relative paths, in generation order. + +=cut + +#------------------------------------------------------------------------------- +sub fips_policy_artifacts { + return ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', + 'xCAT/postscripts/xcatlib.sh'); +} + +#------------------------------------------------------------------------------- + +=head3 fips_policy_plan + + Descriptions: Read the canonical FIPS shell policy and each runtime + library, and report what generation would write. Reads + only; it writes nothing. + + Arguments: + root - the repository root + + Returns: a hash reference. + block - the delimited policy block generation inserts + artifacts - one hash reference per runtime library, in + generation order, with: relative, path, mode, + current, rendered and stale + + Dies when the policy is empty, does not end with a newline, or + when a library is not a regular file or does not hold exactly + one policy block. + +=cut + +#------------------------------------------------------------------------------- +sub fips_policy_plan { + my (%args) = @_; + my $root = $args{root} // die "fips_policy_plan needs a root\n"; + my $policy = read_binary("$root/build-utils/fips-policy.sh"); + die "FIPS policy is empty\n" unless length $policy; + die "FIPS policy must end with a newline\n" unless $policy =~ /\n\z/; + my $begin = "# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write\n"; + my $end = "# END generated FIPS policy\n"; + my $block = $begin . $policy . $end; + + my @artifacts; + for my $relative (fips_policy_artifacts()) { + my $path = "$root/$relative"; + die "$relative must be a regular file\n" unless -f $path && !-l $path; + my $current = read_binary($path); + my $rendered = $current; + # A second block, or none, makes the insertion point ambiguous. Count + # before substituting: s///g on no match leaves the file unchanged and + # reads as current. + my $count = $rendered =~ s/\Q$begin\E.*?\Q$end\E/$block/sg; + die "$relative must contain one FIPS policy block\n" + unless $count == 1 + && (() = $current =~ /\Q$begin\E/g) == 1 + && (() = $current =~ /\Q$end\E/g) == 1; + push @artifacts, { + relative => $relative, + path => $path, + mode => (stat($path))[2] & oct('0777'), + current => $current, + rendered => $rendered, + stale => ($rendered eq $current ? 0 : 1), + }; + } + return {block => $block, artifacts => \@artifacts}; +} + +#------------------------------------------------------------------------------- + +=head3 sync_fips_policy + + Descriptions: Compare each runtime library with the canonical FIPS shell + policy, and in write mode replace a stale block. Both + builders call this before they package xCAT or + xCAT-genesis-scripts, so a stale library never reaches a + package. + + Arguments: + root - the repository root + mode - 'check' to report only, 'write' to regenerate + + Returns: a hash reference. + mode - the mode that ran + stale - the relative paths that did not match the policy + written - the relative paths this call rewrote; empty in check + mode + messages - one diagnostic per stale path in check mode; the + caller prints them + + Dies on every condition fips_policy_plan dies on. A stale path is + a result, not a failure, so the caller decides what it means. + +=cut + +#------------------------------------------------------------------------------- +sub sync_fips_policy { + my (%args) = @_; + my $mode = $args{mode} // 'check'; + die "FIPS policy mode must be check or write, not '$mode'\n" + unless $mode eq 'check' || $mode eq 'write'; + my $plan = fips_policy_plan(root => $args{root}); + my (@stale, @written, @messages); + for my $artifact (grep { $_->{stale} } @{$plan->{artifacts}}) { + push @stale, $artifact->{relative}; + if ($mode eq 'check') { + push @messages, "$artifact->{relative} is out of date;" + . ' run build-utils/sync-fips-policy --write'; + next; + } + # Rename over the library so a reader never sees a partial block, and + # carry the mode across: the Genesis copy is 0644 and xcatlib.sh 0755. + my ($fh, $temporary) = tempfile('.fips-policy-XXXXXX', + DIR => dirname($artifact->{path}), UNLINK => 1); + close $fh or die "close $temporary: $!\n"; + write_binary($temporary, $artifact->{rendered}); + chmod($artifact->{mode}, $temporary) or die "chmod $temporary: $!\n"; + rename($temporary, $artifact->{path}) + or die "replace $artifact->{path}: $!\n"; + push @written, $artifact->{relative}; + } + return {mode => $mode, stale => \@stale, written => \@written, + messages => \@messages}; +} diff --git a/build-utils/sync-fips-policy b/build-utils/sync-fips-policy index 9009ac090e..48a8c5abbb 100755 --- a/build-utils/sync-fips-policy +++ b/build-utils/sync-fips-policy @@ -3,44 +3,12 @@ use strict; use warnings; use FindBin; -use File::Basename qw(dirname); -use File::Slurper qw(read_binary write_binary); -use File::Temp qw(tempfile); +use lib "$FindBin::Bin/lib"; +use XCAT::BuildUtils qw(sync_fips_policy); my $mode = shift // ''; die "Usage: $0 --check|--write\n" unless !@ARGV && ($mode eq '--check' || $mode eq '--write'); -my $root = "$FindBin::Bin/.."; -my $policy = read_binary("$FindBin::Bin/fips-policy.sh"); -die "FIPS policy is empty\n" unless length $policy; -die "FIPS policy must end with a newline\n" unless $policy =~ /\n\z/; -my $begin = "# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write\n"; -my $end = "# END generated FIPS policy\n"; -my $block = $begin . $policy . $end; -my $failed = 0; - -for my $relative ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', - 'xCAT/postscripts/xcatlib.sh') { - my $path = "$root/$relative"; - die "$relative must be a regular file\n" unless -f $path && !-l $path; - my $content = read_binary($path); - die "$relative must contain one FIPS policy block\n" - unless (() = $content =~ /\Q$begin\E/g) == 1 - && (() = $content =~ /\Q$end\E/g) == 1; - my $rendered = $content; - my $count = $rendered =~ s/\Q$begin\E.*?\Q$end\E/$block/sg; - die "$relative must contain one FIPS policy block\n" unless $count == 1; - next if $rendered eq $content; - if ($mode eq '--check') { - warn "$relative is out of date; run build-utils/sync-fips-policy --write\n"; - $failed = 1; - next; - } - my ($fh, $temporary) = tempfile('.fips-policy-XXXXXX', - DIR => dirname($path), UNLINK => 1); - close $fh or die "close $temporary: $!\n"; - write_binary($temporary, $rendered); - chmod((stat($path))[2] & oct('0777'), $temporary) or die "chmod $temporary: $!\n"; - rename($temporary, $path) or die "replace $path: $!\n"; -} -exit $failed; +my $result = sync_fips_policy(root => "$FindBin::Bin/..", mode => substr($mode, 2)); +warn "$_\n" for @{$result->{messages}}; +exit(@{$result->{stale}} && $result->{mode} eq 'check' ? 1 : 0); diff --git a/builddebs.pl b/builddebs.pl index 0a659d26a8..d82697fc81 100755 --- a/builddebs.pl +++ b/builddebs.pl @@ -113,8 +113,11 @@ for my $pkg ($opts{packages}->@*) { } if (grep { $_ eq 'xCAT' || $_ eq 'xCAT-genesis-scripts' } $opts{packages}->@*) { - system($^X, "$FindBin::Bin/build-utils/sync-fips-policy", '--check') == 0 - or die "FATAL: generated FIPS policy is not current\n"; + my $fips = XCAT::BuildUtils::sync_fips_policy(root => $FindBin::Bin, mode => 'check'); + if (@{$fips->{stale}}) { + warn "$_\n" for @{$fips->{messages}}; + die "FATAL: generated FIPS policy is not current\n"; + } } my $ROOT = abs_path($FindBin::Bin); diff --git a/buildrpms.pl b/buildrpms.pl index 2b19585c46..3cdec4d62e 100755 --- a/buildrpms.pl +++ b/buildrpms.pl @@ -990,8 +990,11 @@ sub main { if defined($native_subdir) && !$opts{source_only} && !$opts{gpg_sign}; if (grep { $_ eq 'xCAT' || $_ eq 'xCAT-genesis-scripts' } $opts{packages}->@*) { - system($^X, "$Bin/build-utils/sync-fips-policy", '--check') == 0 - or die "FATAL: generated FIPS policy is not current\n"; + my $fips = XCAT::BuildUtils::sync_fips_policy(root => $Bin, mode => 'check'); + if (@{$fips->{stale}}) { + warn "$_\n" for @{$fips->{messages}}; + die "FATAL: generated FIPS policy is not current\n"; + } } # ---- concurrency guard (mirrors cluster-test.pl's per-cluster lock) -------------------------- diff --git a/xCAT-test/bats/fips_policy_artifacts.bats b/xCAT-test/bats/fips_policy_artifacts.bats index dab4f3ba2c..e9337846b2 100644 --- a/xCAT-test/bats/fips_policy_artifacts.bats +++ b/xCAT-test/bats/fips_policy_artifacts.bats @@ -8,6 +8,7 @@ setup() ROOT="$BATS_TEST_TMPDIR/source" local relative for relative in build-utils/sync-fips-policy build-utils/fips-policy.sh \ + build-utils/lib/XCAT/BuildUtils.pm \ xCAT-genesis-scripts/usr/lib/xcat/fips.sh xCAT/postscripts/xcatlib.sh; do mkdir -p "$ROOT/$(dirname "$relative")" cp "$(repo_path "$relative")" "$ROOT/$relative" diff --git a/xCAT-test/unit/fips_policy_sources.t b/xCAT-test/unit/fips_policy_sources.t index 70fc141edb..5efa3c686a 100644 --- a/xCAT-test/unit/fips_policy_sources.t +++ b/xCAT-test/unit/fips_policy_sources.t @@ -1,84 +1,145 @@ #!/usr/bin/env perl +# XCAT::BuildUtils FIPS policy generation. +# +# buildrpms.pl and builddebs.pl call sync_fips_policy before they package xCAT or +# xCAT-genesis-scripts, so a runtime library that no longer matches the canonical +# policy cannot reach a package. Every assertion below runs the sub and reads its +# answer; none of them runs the build-utils/sync-fips-policy script, which is a +# thin caller with nothing of its own to measure. use strict; use warnings; -use FindBin; -use lib "$FindBin::Bin/../lib"; -use File::Copy qw(copy); use File::Basename qw(dirname); +use File::Copy qw(copy); use File::Path qw(make_path); use File::Slurper qw(read_binary write_binary); use File::Temp qw(tempdir); +use FindBin; +use lib "$FindBin::Bin/../lib"; +use lib "$FindBin::Bin/../../build-utils/lib"; use Test::More; +use XCAT::BuildUtils qw(fips_policy_artifacts fips_policy_plan sync_fips_policy); use XCAT::Test::File qw(repo_path); -use XCAT::Test::Process qw(run_command); -my $sync = 'build-utils/sync-fips-policy'; my $source = 'build-utils/fips-policy.sh'; -my @libraries = ('xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh'); -my ($rc, $output) = run_command($^X, repo_path($sync), '--check'); -is($rc, 0, 'committed runtime artifacts match the canonical policy') or diag($output); -is($output, '', 'a current artifact check is quiet'); +my @libraries = fips_policy_artifacts(); +is_deeply(\@libraries, + ['xCAT-genesis-scripts/usr/lib/xcat/fips.sh', 'xCAT/postscripts/xcatlib.sh'], + 'both runtime libraries carry a generated policy block'); +# The committed tree. A stale block here would ship in a package. +my $committed = sync_fips_policy(root => repo_path('.'), mode => 'check'); +is_deeply($committed->{stale}, [], 'the committed libraries match the canonical policy'); +is_deeply($committed->{messages}, [], 'a current check reports no diagnostic'); +is_deeply($committed->{written}, [], 'a check writes nothing'); +is($committed->{mode}, 'check', 'the answer names the mode that ran'); + +# Everything below runs against a copy, so a failed assertion cannot leave the +# checkout's own libraries rewritten. my $root = tempdir(CLEANUP => 1); -for my $relative ($sync, $source, @libraries) { +for my $relative ($source, @libraries) { make_path(dirname("$root/$relative")); copy(repo_path($relative), "$root/$relative") or die "copy $relative: $!"; } -my %modes = ($libraries[0] => oct('0644'), $libraries[1] => oct('0755')); -for my $relative (@libraries) { - chmod($modes{$relative}, "$root/$relative") or die "chmod $relative: $!"; -} -my @original = map { scalar read_binary("$root/$_") } @libraries; +my %mode = ($libraries[0] => oct('0644'), $libraries[1] => oct('0755')); +chmod($mode{$_}, "$root/$_") or die "chmod $_: $!" for @libraries; +my @committed_text = map { scalar read_binary("$root/$_") } @libraries; + my $policy = read_binary("$root/$source"); write_binary("$root/$source", $policy . "\nxcat_policy_probe() { printf canonical; }\n"); -($rc, $output) = run_command($^X, "$root/$sync", '--check'); -is($rc, 1, 'a changed canonical policy invalidates both artifacts'); -for my $relative (@libraries) { - like($output, qr/\Q$relative\E is out of date/, 'check identifies the stale artifact'); + +# A plan reports what generation would write and writes nothing itself. +my $plan = fips_policy_plan(root => $root); +is_deeply([map { $_->{relative} } @{$plan->{artifacts}}], \@libraries, + 'the plan covers both libraries in generation order'); +is_deeply([map { $_->{stale} } @{$plan->{artifacts}}], [1, 1], + 'a changed policy makes both libraries stale'); +like($plan->{block}, qr/xcat_policy_probe/, 'the block carries the changed policy'); +for my $artifact (@{$plan->{artifacts}}) { + like($artifact->{rendered}, qr/xcat_policy_probe/, + "the plan renders the changed policy into $artifact->{relative}"); + unlike($artifact->{current}, qr/xcat_policy_probe/, + "the plan leaves $artifact->{relative} unchanged on disk"); } -is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@original, - 'check does not modify stale artifacts'); -($rc, $output) = run_command($^X, "$root/$sync", '--write'); -is($rc, 0, 'write refreshes both artifacts') or diag($output); +is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@committed_text, + 'a plan writes nothing'); + +my $check = sync_fips_policy(root => $root, mode => 'check'); +is_deeply($check->{stale}, \@libraries, 'a check names every stale library'); +is_deeply($check->{written}, [], 'a check writes nothing when libraries are stale'); +is(scalar @{$check->{messages}}, scalar @libraries, 'a check reports one diagnostic per stale library'); for my $relative (@libraries) { - is((stat("$root/$relative"))[2] & oct('0777'), $modes{$relative}, - "generation preserves the mode of $relative"); + like((grep { /\Q$relative\E/ } @{$check->{messages}})[0], + qr/is out of date; run build-utils\/sync-fips-policy --write/, + "the diagnostic for $relative says how to fix it"); } -my @rendered = map { scalar read_binary("$root/$_") } @libraries; -($rc, $output) = run_command($^X, "$root/$sync", '--write'); -is($rc, 0, 'repeated generation succeeds'); -is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, +is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@committed_text, + 'a check does not modify a stale library'); + +my $write = sync_fips_policy(root => $root, mode => 'write'); +is($write->{mode}, 'write', 'the answer names the write mode'); +is_deeply($write->{written}, \@libraries, 'a write names every library it rewrote'); +is_deeply($write->{stale}, \@libraries, 'a write reports which libraries were stale'); +is_deeply([map { scalar read_binary("$root/$_") } @libraries], + [map { $_->{rendered} } @{$plan->{artifacts}}], + 'a write stores exactly what the plan rendered'); +is_deeply([map { (stat("$root/$_"))[2] & oct('0777') } @libraries], + [map { $mode{$_} } @libraries], 'a write preserves the mode of each library'); + +my @generated = map { scalar read_binary("$root/$_") } @libraries; +my $again = sync_fips_policy(root => $root, mode => 'write'); +is_deeply($again->{stale}, [], 'a second write finds nothing stale'); +is_deeply($again->{written}, [], 'a second write rewrites nothing'); +is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@generated, 'generation is idempotent'); -for my $relative (@libraries) { - is((stat("$root/$relative"))[2] & oct('0777'), $modes{$relative}, - "repeated generation preserves the mode of $relative"); -} -($rc, $output) = run_command($^X, "$root/$sync", '--check'); -is($rc, 0, 'refreshed artifacts pass the check'); +is_deeply([map { (stat("$root/$_"))[2] & oct('0777') } @libraries], + [map { $mode{$_} } @libraries], 'a second write preserves the mode of each library'); +is_deeply(sync_fips_policy(root => $root, mode => 'check')->{stale}, [], + 'a check passes on generated libraries'); -my $unterminated = $policy; -$unterminated =~ s/\n+\z//; -write_binary("$root/$source", $unterminated); -for my $mode (qw(--check --write)) { - ($rc, $output) = run_command($^X, "$root/$sync", $mode); - isnt($rc, 0, "$mode rejects a policy without a final newline"); - like($output, qr/FIPS policy must end with a newline/, - 'the error identifies the unterminated policy'); - is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, - 'an unterminated policy leaves both artifacts untouched'); +# Every rejected input leaves both libraries alone, in both modes. A write that +# stopped half way would ship one library generated and one stale. +for my $case ( + ['a policy with no final newline', sub { my $text = $policy; $text =~ s/\n+\z//; + write_binary("$root/$source", $text) }, qr/FIPS policy must end with a newline/], + ['an empty policy', sub { write_binary("$root/$source", '') }, + qr/FIPS policy is empty/], + ['a missing policy', sub { unlink "$root/$source" or die "unlink: $!" }, + qr/\Qfips-policy.sh\E/], + ['a library with no policy block', sub { write_binary("$root/$source", $policy); + write_binary("$root/$libraries[0]", "#!/bin/sh\n") }, + qr/\Q$libraries[0]\E must contain one FIPS policy block/], + ['a library with two policy blocks', sub { + write_binary("$root/$libraries[0]", $generated[0] . $generated[0]) }, + qr/\Q$libraries[0]\E must contain one FIPS policy block/], + ['a library that is a symbolic link', sub { + write_binary("$root/$libraries[0]", $generated[0]); + rename("$root/$libraries[0]", "$root/$libraries[0]-real") or die "rename: $!"; + symlink("$root/$libraries[0]-real", "$root/$libraries[0]") or die "symlink: $!" }, + qr/\Q$libraries[0]\E must be a regular file/], +) { + my ($name, $break, $expected) = @{$case}; + $break->(); + my @before = map { -l "$root/$_" ? '' : scalar read_binary("$root/$_") } @libraries; + for my $mode (qw(check write)) { + my $answer = eval { sync_fips_policy(root => $root, mode => $mode) }; + is($answer, undef, "$mode rejects $name"); + like($@, $expected, "the $mode error identifies $name"); + } + is_deeply([map { -l "$root/$_" ? '' : scalar read_binary("$root/$_") } @libraries], + \@before, "$name leaves both libraries untouched"); } -unlink "$root/$source" or die "unlink policy: $!"; -($rc, $output) = run_command($^X, "$root/$sync", '--write'); -isnt($rc, 0, 'a missing source fails generation'); -is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@rendered, - 'a missing source leaves both artifacts untouched'); -write_binary("$root/$source", $policy); -write_binary("$root/$libraries[0]", "#!/bin/sh\n"); -($rc, $output) = run_command($^X, "$root/$sync", '--write'); -isnt($rc, 0, 'a missing insertion block fails generation'); -is(read_binary("$root/$libraries[0]"), "#!/bin/sh\n", - 'generation does not replace a malformed artifact'); +unlink "$root/$libraries[0]" or die "unlink link: $!"; +rename("$root/$libraries[0]-real", "$root/$libraries[0]") or die "restore: $!"; +for my $mode ('', 'Write', 'check ', 'rewrite') { + my $answer = eval { sync_fips_policy(root => $root, mode => $mode) }; + is($answer, undef, "mode '$mode' is rejected"); + like($@, qr/FIPS policy mode must be check or write/, + "the error names the modes that exist, not '$mode'"); +} +my $answer = eval { fips_policy_plan() }; +is($answer, undef, 'a plan without a root is rejected'); +like($@, qr/fips_policy_plan needs a root/, 'the error asks for a root'); done_testing(); From 26be5646f9a0b81a92e491b7d33fa53e21543bc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 11:29:41 -0300 Subject: [PATCH 15/17] test(fips): preserve the policy command contract --- xCAT-test/bats/fips_policy_artifacts.bats | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/xCAT-test/bats/fips_policy_artifacts.bats b/xCAT-test/bats/fips_policy_artifacts.bats index e9337846b2..e0742a1131 100644 --- a/xCAT-test/bats/fips_policy_artifacts.bats +++ b/xCAT-test/bats/fips_policy_artifacts.bats @@ -44,3 +44,41 @@ check_generated_policy() @test "generated Cumulus policy runs in bash" { check_generated_policy bash xCAT/postscripts/xcatlib.sh } + +@test "policy check reports stale artifacts without changing them" { + run perl "$ROOT/build-utils/sync-fips-policy" --check + printf '%s\n' "$output" + [ "$status" -eq 1 ] + local expected + expected=$(printf '%s is out of date; run build-utils/sync-fips-policy --write\n' \ + xCAT-genesis-scripts/usr/lib/xcat/fips.sh xCAT/postscripts/xcatlib.sh) + [ "$output" = "$expected" ] + cmp "$(repo_path xCAT-genesis-scripts/usr/lib/xcat/fips.sh)" \ + "$ROOT/xCAT-genesis-scripts/usr/lib/xcat/fips.sh" + cmp "$(repo_path xCAT/postscripts/xcatlib.sh)" "$ROOT/xCAT/postscripts/xcatlib.sh" +} + +@test "policy check succeeds quietly after generation" { + run perl "$ROOT/build-utils/sync-fips-policy" --write + printf '%s\n' "$output" + [ "$status" -eq 0 ] + [ -z "$output" ] + run perl "$ROOT/build-utils/sync-fips-policy" --check + printf '%s\n' "$output" + [ "$status" -eq 0 ] + [ -z "$output" ] +} + +@test "policy commands reject invalid input without changing artifacts" { + printf '%s' no-final-newline >"$ROOT/build-utils/fips-policy.sh" + local mode + for mode in --check --write; do + run perl "$ROOT/build-utils/sync-fips-policy" "$mode" + printf '%s\n' "$output" + [ "$status" -ne 0 ] + [[ "$output" == *"FIPS policy must end with a newline"* ]] + cmp "$(repo_path xCAT-genesis-scripts/usr/lib/xcat/fips.sh)" \ + "$ROOT/xCAT-genesis-scripts/usr/lib/xcat/fips.sh" + cmp "$(repo_path xCAT/postscripts/xcatlib.sh)" "$ROOT/xCAT/postscripts/xcatlib.sh" + done +} From 0b42cb5fd44d4d5ebfed7e58e397f57cb9695005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 11:30:00 -0300 Subject: [PATCH 16/17] test(fips): reject malformed artifacts before writes --- xCAT-test/unit/fips_policy_sources.t | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/xCAT-test/unit/fips_policy_sources.t b/xCAT-test/unit/fips_policy_sources.t index 5efa3c686a..86a0e5891b 100644 --- a/xCAT-test/unit/fips_policy_sources.t +++ b/xCAT-test/unit/fips_policy_sources.t @@ -112,6 +112,17 @@ for my $case ( ['a library with two policy blocks', sub { write_binary("$root/$libraries[0]", $generated[0] . $generated[0]) }, qr/\Q$libraries[0]\E must contain one FIPS policy block/], + ['a library with an extra begin marker', sub { + write_binary("$root/$libraries[0]", + "# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write\n" . $generated[0]) }, + qr/\Q$libraries[0]\E must contain one FIPS policy block/], + ['a library with an extra end marker', sub { + write_binary("$root/$libraries[0]", $generated[0] . "# END generated FIPS policy\n") }, + qr/\Q$libraries[0]\E must contain one FIPS policy block/], + ['a library with reversed policy markers', sub { + write_binary("$root/$libraries[0]", "# END generated FIPS policy\n" + . "# BEGIN generated FIPS policy: build-utils/sync-fips-policy --write\n") }, + qr/\Q$libraries[0]\E must contain one FIPS policy block/], ['a library that is a symbolic link', sub { write_binary("$root/$libraries[0]", $generated[0]); rename("$root/$libraries[0]", "$root/$libraries[0]-real") or die "rename: $!"; @@ -132,6 +143,20 @@ for my $case ( unlink "$root/$libraries[0]" or die "unlink link: $!"; rename("$root/$libraries[0]-real", "$root/$libraries[0]") or die "restore: $!"; + +for my $mode (qw(check write)) { + write_binary("$root/$source", $policy); + write_binary("$root/$libraries[0]", $generated[0]); + write_binary("$root/$libraries[1]", "#!/bin/sh\n"); + my @before = map { scalar read_binary("$root/$_") } @libraries; + my $answer = eval { sync_fips_policy(root => $root, mode => $mode) }; + is($answer, undef, "$mode rejects an invalid second library"); + like($@, qr/\Q$libraries[1]\E must contain one FIPS policy block/, + "the $mode error identifies the second library"); + is_deeply([map { scalar read_binary("$root/$_") } @libraries], \@before, + "$mode leaves the stale first library untouched when the second is invalid"); +} + for my $mode ('', 'Write', 'check ', 'rewrite') { my $answer = eval { sync_fips_policy(root => $root, mode => $mode) }; is($answer, undef, "mode '$mode' is rejected"); From 88f8d3f92db205efb40ac6125900cb8ad3c28686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Ferr=C3=A3o?= <2031761+viniciusferrao@users.noreply.github.com> Date: Wed, 21 Oct 2026 11:30:00 -0300 Subject: [PATCH 17/17] docs(fips): remove misleading generation order comment --- build-utils/lib/XCAT/BuildUtils.pm | 3 --- 1 file changed, 3 deletions(-) diff --git a/build-utils/lib/XCAT/BuildUtils.pm b/build-utils/lib/XCAT/BuildUtils.pm index 389d4220c9..c5268a109c 100644 --- a/build-utils/lib/XCAT/BuildUtils.pm +++ b/build-utils/lib/XCAT/BuildUtils.pm @@ -1336,9 +1336,6 @@ sub fips_policy_plan { die "$relative must be a regular file\n" unless -f $path && !-l $path; my $current = read_binary($path); my $rendered = $current; - # A second block, or none, makes the insertion point ambiguous. Count - # before substituting: s///g on no match leaves the file unchanged and - # reads as current. my $count = $rendered =~ s/\Q$begin\E.*?\Q$end\E/$block/sg; die "$relative must contain one FIPS policy block\n" unless $count == 1