diff --git a/cisco_sccfm_scripts/setup_ci_environment.sh b/cisco_sccfm_scripts/setup_ci_environment.sh index 9b8dbac..1955cb6 100755 --- a/cisco_sccfm_scripts/setup_ci_environment.sh +++ b/cisco_sccfm_scripts/setup_ci_environment.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash -# CI environment setup for Amazon Linux 2. +# CI environment setup for Amazon Linux 2023. # -# Uses system packages (yum) and pyenv (from git) to build Python, +# Uses system packages (dnf) and pyenv (from git) to build Python, # then Poetry for project dependencies. No Homebrew needed. # # For local macOS development, use setup_environment.sh instead. @@ -13,17 +13,17 @@ PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" VENV_DIR="${PROJECT_ROOT}/.venv" install_system_packages() { - if ! command -v yum >/dev/null 2>&1; then - echo "This script requires yum (Amazon Linux 2)." >&2 + if ! command -v dnf >/dev/null 2>&1; then + echo "This script requires dnf (Amazon Linux 2023)." >&2 exit 1 fi echo "Installing system build dependencies..." - sudo yum groupinstall -y 'Development Tools' - sudo yum install -y \ + sudo dnf groupinstall -y 'Development Tools' + sudo dnf install -y \ bzip2-devel \ libffi-devel \ - openssl11-devel \ + openssl-devel \ readline-devel \ sqlite-devel \ xz-devel \ @@ -59,22 +59,14 @@ build_python() { echo "Building Python ${PYTHON_VERSION}..." - # Amazon Linux 2 ships OpenSSL 1.0.2 by default but Python 3.12+ - # requires >= 1.1.1. The openssl11-devel package puts headers in - # /usr/include/openssl11 and libs in /usr/lib64/openssl11. - local build_cppflags="" build_ldflags="" - if [[ -d /usr/include/openssl11 ]]; then - build_cppflags="-I/usr/include/openssl11" - fi - if [[ -d /usr/lib64/openssl11 ]]; then - build_ldflags="-L/usr/lib64/openssl11" - fi - - if ! CPPFLAGS="${build_cppflags}" LDFLAGS="${build_ldflags}" \ - pyenv install "${PYTHON_VERSION}"; then + # AL2023 provides OpenSSL 3 in the standard compiler search paths. + # Hardened runners mount /tmp with noexec; build beside the installed interpreters. + local build_tmpdir="${PYENV_ROOT}/tmp" + mkdir -p "${build_tmpdir}" + if ! TMPDIR="${build_tmpdir}" pyenv install "${PYTHON_VERSION}"; then echo "Python ${PYTHON_VERSION} build failed." >&2 local log - log="$(ls -t /tmp/python-build.*.log 2>/dev/null | head -1)" + log="$(ls -t "${build_tmpdir}"/python-build.*.log 2>/dev/null | head -1 || true)" if [[ -n "${log}" && -f "${log}" ]]; then echo "=== Last 40 lines of build log ===" >&2 tail -40 "${log}" >&2 @@ -92,6 +84,11 @@ create_venv() { exit 1 fi + if ! "${python_bin}" -c 'import ssl'; then + echo "Python ${PYTHON_VERSION} cannot import ssl." >&2 + exit 1 + fi + if [[ ! -d "${VENV_DIR}" ]]; then echo "Creating virtualenv at ${VENV_DIR}" "${python_bin}" -m venv "${VENV_DIR}" diff --git a/cisco_sccfm_scripts/verify_public_release.py b/cisco_sccfm_scripts/verify_public_release.py index 72f36b2..46f7726 100644 --- a/cisco_sccfm_scripts/verify_public_release.py +++ b/cisco_sccfm_scripts/verify_public_release.py @@ -472,7 +472,8 @@ def _verify_profile_handoff(controller: _Controller, lookup_name: str) -> None: def verify_public_release(requested: str = "") -> PublicReleaseSummary: """Install and smoke-test one matching release from the public registries.""" version = resolve_public_version(requested) - with tempfile.TemporaryDirectory(prefix="sccfm-public-release-") as temporary: + # The controller executes installed entry points; hardened /tmp mounts forbid that. + with tempfile.TemporaryDirectory(prefix="sccfm-public-release-", dir=Path.cwd()) as temporary: _log("Creating an isolated controller with a clean home and collection path") controller = _create_controller(Path(temporary)) _install_public_artifacts(controller, version) diff --git a/sccfm-ansible/CHANGELOG.rst b/sccfm-ansible/CHANGELOG.rst index 31c69fd..cef0347 100644 --- a/sccfm-ansible/CHANGELOG.rst +++ b/sccfm-ansible/CHANGELOG.rst @@ -4,6 +4,17 @@ Cisco SCCFM Collection Release Notes .. contents:: Topics +v0.44.2 +======== + +Bugfixes +-------- + +- Fixed Python environment setup on Amazon Linux 2023 runners by using system OpenSSL 3 + build dependencies and executable build directories. +- Kept public-release verification controllers off system temporary directories that prohibit + executing installed commands. + v0.44.1 ======== diff --git a/sccfm-ansible/changelogs/changelog.yaml b/sccfm-ansible/changelogs/changelog.yaml index 850cb80..3ed04fb 100644 --- a/sccfm-ansible/changelogs/changelog.yaml +++ b/sccfm-ansible/changelogs/changelog.yaml @@ -2,6 +2,15 @@ ancestor: null # sccfm-release-retarget-seed: 0.39.0 releases: + 0.44.2: + changes: + bugfixes: + - Fixed Python environment setup on Amazon Linux 2023 runners by using + system OpenSSL 3 build dependencies and executable build directories. + - Kept public-release verification controllers off system temporary + directories that prohibit executing installed commands. + fragments: [] + release_date: '2026-11-17' 0.44.1: changes: bugfixes: diff --git a/tests/test_environment_setup.py b/tests/test_environment_setup.py index ab99a41..b587138 100644 --- a/tests/test_environment_setup.py +++ b/tests/test_environment_setup.py @@ -7,9 +7,12 @@ from __future__ import annotations import re +import subprocess import tomllib from pathlib import Path +import pytest + PROJECT_ROOT = Path(__file__).resolve().parents[1] SETUP_SCRIPTS = ( PROJECT_ROOT / "cisco_sccfm_scripts" / "setup_ci_environment.sh", @@ -76,3 +79,136 @@ def test_local_setup_validates_runtime_dependencies() -> None: assert "python -m pip check" in source assert 'importlib.metadata.version("poetry")' in source + + +@pytest.mark.parametrize("install_status", [0, 73]) +def test_ci_setup_installs_al2023_dependencies_and_propagates_failures( + tmp_path: Path, install_status: int +) -> None: + """Run the real installer with a controlled AL2023 package manager.""" + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + log = tmp_path / "packages.log" + commands = { + "sudo": '#!/bin/bash\nexec "$@"\n', + "dnf": ( + "#!/bin/bash\n" + 'printf "%s\\n" "$*" >> "$SCCFM_TEST_LOG"\n' + 'if [[ "$1" == groupinstall ]]; then exit 0; fi\n' + 'for arg in "$@"; do\n' + ' if [[ "$arg" == openssl11-devel ]]; then\n' + ' echo "No match for argument: openssl11-devel" >&2; exit 1\n' + " fi\n" + "done\n" + f"exit {install_status}\n" + ), + } + for name, content in commands.items(): + executable = bin_dir / name + executable.write_text(content, encoding="utf-8") + executable.chmod(0o755) + (bin_dir / "yum").symlink_to(bin_dir / "dnf") + + result = subprocess.run( + ["/bin/bash", "-c", f"{_ci_setup_functions()}\ninstall_system_packages"], + env={"PATH": str(bin_dir), "SCCFM_TEST_LOG": str(log)}, + text=True, + capture_output=True, + timeout=10, + check=False, + ) + + assert result.returncode == install_status, result.stderr + assert "openssl-devel" in log.read_text(encoding="utf-8") + + +def _ci_setup_functions() -> str: + """Load real definitions without running the script's orchestration.""" + source = SETUP_SCRIPTS[0].read_text(encoding="utf-8") + functions = source[source.index("install_system_packages() {") :] + return "set -euo pipefail\n" + functions[: functions.index("\ninstall_system_packages\n")] + + +def test_ci_setup_rejects_python_without_ssl_before_creating_venv( + tmp_path: Path, +) -> None: + python = tmp_path / "versions/3.12.4/bin/python3" + python.parent.mkdir(parents=True) + python.write_text( + "#!/bin/bash\n" + 'if [[ "$1" == -c ]]; then echo "No module named ssl" >&2; exit 1; fi\n' + 'echo "venv creation attempted"; exit 87\n', + encoding="utf-8", + ) + python.chmod(0o755) + result = subprocess.run( + [ + "/bin/bash", + "-c", + f"{_ci_setup_functions()}\n" + "PYTHON_VERSION=3.12.4\n" + 'VENV_DIR="$1/venv"\n' + 'pyenv() { printf "%s\\n" "$SCCFM_TEST_PYENV_ROOT"; }\n' + "create_venv", + "setup-test", + str(tmp_path), + ], + env={"SCCFM_TEST_PYENV_ROOT": str(tmp_path)}, + text=True, + capture_output=True, + timeout=10, + check=False, + ) + assert result.returncode == 1, result.stderr + assert "No module named ssl" in result.stderr + assert "venv creation attempted" not in result.stdout + + +@pytest.mark.parametrize("build_status", [0, 73]) +def test_ci_python_build_avoids_noexec_tmp_and_reads_its_build_log( + tmp_path: Path, build_status: int +) -> None: + pyenv_root = tmp_path / "pyenv" + pyenv_root.mkdir() + noexec_tmp = tmp_path / "noexec-tmp" + noexec_tmp.mkdir() + fake_pyenv = r""" +pyenv() { + case "$1" in + versions) return 0 ;; + root) printf '%s\n' "$PYENV_ROOT" ;; + install) + if [[ "$TMPDIR" == "$SCCFM_TEST_NOEXEC_TMP" ]]; then + echo 'python-build: TMPDIR cannot hold executables' >&2 + return 1 + fi + [[ -d "$TMPDIR" ]] || return 90 + printf 'compiler diagnostic\n' > "$TMPDIR/python-build.fixture.log" + return "$SCCFM_TEST_BUILD_STATUS" ;; + esac +} +""" + result = subprocess.run( + [ + "/bin/bash", + "-c", + f"{_ci_setup_functions()}\n{fake_pyenv}\nPYTHON_VERSION=3.12.4\nbuild_python", + ], + env={ + "PATH": "/usr/bin:/bin", + "PYENV_ROOT": str(pyenv_root), + "TMPDIR": str(noexec_tmp), + "SCCFM_TEST_NOEXEC_TMP": str(noexec_tmp), + "SCCFM_TEST_BUILD_STATUS": str(build_status), + }, + text=True, + capture_output=True, + timeout=10, + check=False, + ) + + assert "cannot hold executables" not in result.stderr + assert result.returncode == (0 if build_status == 0 else 1), result.stderr + assert (pyenv_root / "tmp/python-build.fixture.log").is_file() + if build_status: + assert "compiler diagnostic" in result.stderr diff --git a/tests/test_verify_public_release.py b/tests/test_verify_public_release.py index 737c314..e0cac20 100644 --- a/tests/test_verify_public_release.py +++ b/tests/test_verify_public_release.py @@ -15,6 +15,7 @@ import pytest import cisco_sccfm_scripts.verify_public_release as verifier +from cisco_sccfm_scripts.verify_clean_controller import _Controller _VERSION = "1.2.3" @@ -31,7 +32,7 @@ def read(self, limit: int) -> bytes: return self._payload[:limit] -def _controller(tmp_path: Path) -> verifier._Controller: +def _controller(tmp_path: Path) -> _Controller: work = tmp_path / "work" work.mkdir() binaries = tmp_path / "bin" @@ -259,10 +260,10 @@ def test_profile_handoff_configures_offline_and_asserts_lookup( ) -> None: controller = _controller(tmp_path) plugin = "cisco.sccfm.profile" - calls: list[tuple[verifier._Controller, list[str | Path]]] = [] + calls: list[tuple[_Controller, list[str | Path]]] = [] def run( - selected: verifier._Controller, + selected: _Controller, command: list[str | Path], *, check: bool = True, @@ -380,3 +381,30 @@ def test_main_writes_machine_readable_summary( "offline_probe": "cisco.sccfm.list_devices", "version": _VERSION, } + + +def test_public_controller_uses_workspace_and_cleans_up_on_failure( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + workspace = tmp_path / "workspace" + workspace.mkdir() + system_tmp = tmp_path / "noexec-tmp" + system_tmp.mkdir() + monkeypatch.chdir(workspace) + monkeypatch.setattr(verifier.tempfile, "tempdir", str(system_tmp)) + monkeypatch.setattr(verifier, "resolve_public_version", lambda requested: _VERSION) + created: list[Path] = [] + + def check_controller_location(directory: Path) -> _Controller: + created.append(directory) + assert directory.parent == workspace + assert directory.is_dir() + raise RuntimeError("stop before registry installation") + + monkeypatch.setattr(verifier, "_create_controller", check_controller_location) + + with pytest.raises(RuntimeError, match="stop before registry installation"): + verifier.verify_public_release(_VERSION) + + assert len(created) == 1 + assert not created[0].exists()