diff --git a/.github/workflows/xcat_test.yml b/.github/workflows/xcat_test.yml index 5874b752e6..064ab45dca 100644 --- a/.github/workflows/xcat_test.yml +++ b/.github/workflows/xcat_test.yml @@ -16,6 +16,15 @@ 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 xCAT-test/native/profile_package_payload.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 if: ${{ !cancelled() }} @@ -111,8 +120,11 @@ 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 perl-DBI perl-DBD-SQLite perl-JSON perl-XML-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 perl-DBI perl-DBD-SQLite perl-JSON perl-XML-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 xCAT-test/native/profile_package_payload.t' + - name: Test FIPS package artifacts + if: ${{ !cancelled() }} + run: su -s /bin/sh nobody -c 'prove -v xCAT-test/native/fips_package_payload.t' 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/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/lib/XCAT/BuildUtils.pm b/build-utils/lib/XCAT/BuildUtils.pm index e24d6a760f..c5268a109c 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,136 @@ 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; + 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 new file mode 100755 index 0000000000..48a8c5abbb --- /dev/null +++ b/build-utils/sync-fips-policy @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use FindBin; +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 $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 3c7778d692..d82697fc81 100755 --- a/builddebs.pl +++ b/builddebs.pl @@ -112,6 +112,14 @@ for my $pkg ($opts{packages}->@*) { unless grep { $_ eq $pkg } @PACKAGES; } +if (grep { $_ eq 'xCAT' || $_ eq 'xCAT-genesis-scripts' } $opts{packages}->@*) { + 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); 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..3cdec4d62e 100755 --- a/buildrpms.pl +++ b/buildrpms.pl @@ -989,6 +989,14 @@ 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}->@*) { + 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) -------------------------- # 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-test/bats/fips_policy_artifacts.bats b/xCAT-test/bats/fips_policy_artifacts.bats new file mode 100644 index 0000000000..e0742a1131 --- /dev/null +++ b/xCAT-test/bats/fips_policy_artifacts.bats @@ -0,0 +1,84 @@ +#!/usr/bin/env bats + +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 \ + 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" + 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 + 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" +} + +@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 +} + +@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 +} 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..8bec343910 --- /dev/null +++ b/xCAT-test/bats/fips_public_key_fallback.bats @@ -0,0 +1,109 @@ +#!/usr/bin/env bats + +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 +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' "$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() +{ + 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" + : >"$CALLS" + : >"$INVOCATIONS" + run "$shell" -c ' + exec >"$RESULT" 2>&1 + . "$1" + 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" + 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/lib/XCAT/Test/Lifecycle.pm b/xCAT-test/lib/XCAT/Test/Lifecycle.pm index 022ac35c6d..6b06801555 100644 --- a/xCAT-test/lib/XCAT/Test/Lifecycle.pm +++ b/xCAT-test/lib/XCAT/Test/Lifecycle.pm @@ -32,6 +32,12 @@ sub build_deb { my $work = tempdir(CLEANUP => 1); checked('tar', '-cf', "$work/source.tar", '-C', $source, $package); checked('tar', '-xpf', "$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/README.md b/xCAT-test/native/README.md index b300996e19..2779808d64 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. 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/fips_build_guard.t b/xCAT-test/native/fips_build_guard.t new file mode 100644 index 0000000000..bf51309e19 --- /dev/null +++ b/xCAT-test/native/fips_build_guard.t @@ -0,0 +1,47 @@ +#!/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; +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: $!"; +} +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'); + for my $relative (@libraries) { + like($output, qr/\Q$relative\E is out of date/, + "$builder identifies stale $relative for $package"); + } + } +} + +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/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"); 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_policy_sources.t b/xCAT-test/unit/fips_policy_sources.t new file mode 100644 index 0000000000..86a0e5891b --- /dev/null +++ b/xCAT-test/unit/fips_policy_sources.t @@ -0,0 +1,170 @@ +#!/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 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); + +my $source = 'build-utils/fips-policy.sh'; +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 ($source, @libraries) { + make_path(dirname("$root/$relative")); + copy(repo_path($relative), "$root/$relative") or die "copy $relative: $!"; +} +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"); + +# 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], \@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) { + 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"); +} +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'); +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'); + +# 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 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: $!"; + 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/$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"); + 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(); diff --git a/xCAT-test/unit/fips_shell_runtime.t b/xCAT-test/unit/fips_shell_runtime.t index f9382bd88b..4db6ec896f 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); @@ -18,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*$/; } @@ -27,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'); @@ -110,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 @@ -145,11 +148,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 +164,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 { 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(); 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() {