From 582f6d2c3d9756d560d50f302d4193209038abaf Mon Sep 17 00:00:00 2001 From: Thomas Powell <104777878+tpowell-progress@users.noreply.github.com> Date: Fri, 13 Nov 2026 17:11:40 -0500 Subject: [PATCH 1/2] Fix Habitat authentication through sudo Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Thomas Powell <104777878+tpowell-progress@users.noreply.github.com> --- .github/workflows/grype-hab-package-scan.yml | 22 +++--- .../test_habitat_sudo_env.py | 77 +++++++++++++++++++ 2 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 workflow-supporting-files/test_habitat_sudo_env.py diff --git a/.github/workflows/grype-hab-package-scan.yml b/.github/workflows/grype-hab-package-scan.yml index 011bcb99..41e08bfe 100644 --- a/.github/workflows/grype-hab-package-scan.yml +++ b/.github/workflows/grype-hab-package-scan.yml @@ -107,7 +107,7 @@ jobs: export HAB_AUTH_TOKEN="${{ secrets.HAB_AUTH_TOKEN }}" fi HABITAT_VERSION="1.6.1245" - curl https://raw.githubusercontent.com/habitat-sh/habitat/master/components/hab/install.sh | sudo bash -s -- -v "$HABITAT_VERSION" + curl https://raw.githubusercontent.com/habitat-sh/habitat/master/components/hab/install.sh | sudo env HAB_AUTH_TOKEN="${HAB_AUTH_TOKEN:-}" HAB_LICENSE=accept-no-persist bash -s -- -v "$HABITAT_VERSION" - name: Configure Habitat run: | @@ -152,7 +152,7 @@ jobs: hartifacts=$(ls results/*.hart) if [ -f "$hartifacts" ]; then echo "Built package artifact: $hartifacts" - sudo -E hab pkg install $hartifacts + sudo env HAB_AUTH_TOKEN="${HAB_AUTH_TOKEN:-}" HAB_LICENSE="$HAB_LICENSE" hab pkg install "$hartifacts" else echo "Error: No .hart file found in results/" exit 1 @@ -189,10 +189,10 @@ jobs: PACKAGE="${PACKAGE}/${{ inputs.hab_release }}" fi - INSTALL_CMD="sudo -E hab pkg install ${PACKAGE}" + INSTALL_CMD=(hab pkg install "$PACKAGE") if [ -n "${{ inputs.hab_channel }}" ]; then - INSTALL_CMD="${INSTALL_CMD} --channel ${{ inputs.hab_channel }}" + INSTALL_CMD+=(--channel "${{ inputs.hab_channel }}") fi if [ -n "${{ inputs.hab_auth_token }}" ]; then @@ -203,8 +203,8 @@ jobs: echo "Using token from repository secret" fi - echo "Installing: ${INSTALL_CMD}" - eval ${INSTALL_CMD} + echo "Installing: ${INSTALL_CMD[*]}" + sudo env HAB_AUTH_TOKEN="${HAB_AUTH_TOKEN:-}" HAB_LICENSE="$HAB_LICENSE" "${INSTALL_CMD[@]}" - name: Run Grype Scan on Habitat Package timeout-minutes: 15 @@ -497,7 +497,7 @@ jobs: elif [ -n "${{ secrets.HAB_AUTH_TOKEN }}" ]; then export HAB_AUTH_TOKEN="${{ secrets.HAB_AUTH_TOKEN }}" fi - curl https://raw.githubusercontent.com/habitat-sh/habitat/main/components/hab/install.sh | sudo -E bash + curl https://raw.githubusercontent.com/habitat-sh/habitat/main/components/hab/install.sh | sudo env HAB_AUTH_TOKEN="${HAB_AUTH_TOKEN:-}" HAB_LICENSE=accept-no-persist bash hab license accept hab version @@ -583,10 +583,10 @@ jobs: PACKAGE="${PACKAGE}/${{ inputs.hab_release }}" fi - INSTALL_CMD="sudo -E hab pkg install ${PACKAGE}" + INSTALL_CMD=(hab pkg install "$PACKAGE") if [ -n "${{ inputs.hab_channel }}" ]; then - INSTALL_CMD="${INSTALL_CMD} --channel ${{ inputs.hab_channel }}" + INSTALL_CMD+=(--channel "${{ inputs.hab_channel }}") fi if [ -n "${{ inputs.hab_auth_token }}" ]; then @@ -597,8 +597,8 @@ jobs: echo "Using token from repository secret" fi - echo "Installing: ${INSTALL_CMD}" - eval ${INSTALL_CMD} + echo "Installing: ${INSTALL_CMD[*]}" + sudo env HAB_AUTH_TOKEN="${HAB_AUTH_TOKEN:-}" HAB_LICENSE="$HAB_LICENSE" "${INSTALL_CMD[@]}" - name: Run Grype Scan on Habitat Package timeout-minutes: 15 diff --git a/workflow-supporting-files/test_habitat_sudo_env.py b/workflow-supporting-files/test_habitat_sudo_env.py new file mode 100644 index 00000000..199ff834 --- /dev/null +++ b/workflow-supporting-files/test_habitat_sudo_env.py @@ -0,0 +1,77 @@ +"""Run with python3 workflow-supporting-files/test_habitat_sudo_env.py.""" + +import os +from pathlib import Path +import re +import subprocess +import tempfile + + +workflow = (Path(__file__).resolve().parents[1] / + ".github/workflows/grype-hab-package-scan.yml").read_text() +blocks = re.findall(r" run: \|\n((?: {12}.*\n|\n)+)", workflow) +scripts = [block for block in blocks if "sudo" in block and "HAB_AUTH_TOKEN" in block] +assert len(scripts) == 5, "Exercise every privileged Habitat installation" + +for script in scripts: + if "hartifacts=" in script: + script = script[script.index("hartifacts="):] + for input_token, secret_token in [("input-token", "secret-token"), + ("", "secret-token"), ("", "")]: + values = { + "inputs.hab_auth_token": input_token, + "secrets.HAB_AUTH_TOKEN": secret_token, + "inputs.hab_origin": "chef", + "inputs.hab_package": "fauxhai", + "inputs.hab_version": "9.4.29", + "inputs.hab_release": "", + "inputs.hab_channel": "unstable", + } + rendered = re.sub(r"\$\{\{\s*([^}]+?)\s*\}\}", + lambda match: values[match[1]], script) + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + (root / "results").mkdir() + (root / "results/package.hart").touch() + commands = root / "commands" + bin_dir = root / "bin" + bin_dir.mkdir() + stubs = { + "sudo": '#!/bin/bash\n' + 'if [[ "$1" == -E ]]; then shift; fi\n' + 'exec env -i PATH="$PATH" "$@"\n', + "curl": '#!/bin/bash\n' + "echo 'hab pkg install chef/hab'\n", + "hab": '#!/bin/bash\n' + f'[[ "${{HAB_AUTH_TOKEN:-}}" == "{input_token or secret_token}" ]] || exit 10\n' + '[[ "$HAB_LICENSE" == accept-no-persist ]] || exit 11\n' + f'printf "%s\\n" "$*" >> "{commands}"\n', + } + for name, content in stubs.items(): + executable = bin_dir / name + executable.write_text(content) + executable.chmod(0o755) + environment = { + "PATH": f"{bin_dir}:{os.environ['PATH']}", + "HAB_LICENSE": "accept-no-persist", + } + if "hartifacts=" in script: + environment["HAB_AUTH_TOKEN"] = input_token or secret_token + result = subprocess.run(["bash", "-e", "-c", rendered], cwd=root, + env=environment, capture_output=True, text=True) + assert result.returncode == 0, result.stderr + installed = commands.read_text() + if "INSTALL_CMD=" in script: + assert installed == "pkg install chef/fauxhai/9.4.29 --channel unstable\n" + elif "hartifacts=" in script: + assert installed == "pkg install results/package.hart\n" + else: + assert "pkg install chef/hab\n" in installed + assert "input-token" not in result.stdout + assert "secret-token" not in result.stdout + (bin_dir / "hab").write_text("#!/bin/bash\nexit 17\n") + failed = subprocess.run(["bash", "-e", "-c", rendered], cwd=root, + env=environment, capture_output=True, text=True) + assert failed.returncode == 17, "Habitat failures must propagate" + +print("Habitat sudo environment checks passed (5 paths, 3 token cases).") From e4c52e1da52749b32abcda404c50d898dbea3a67 Mon Sep 17 00:00:00 2001 From: Thomas Powell <104777878+tpowell-progress@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:35:46 -0500 Subject: [PATCH 2/2] Run Habitat sudo regression test in CI Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Thomas Powell <104777878+tpowell-progress@users.noreply.github.com> --- .../workflows/validate-habitat-sudo-env.yml | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/validate-habitat-sudo-env.yml diff --git a/.github/workflows/validate-habitat-sudo-env.yml b/.github/workflows/validate-habitat-sudo-env.yml new file mode 100644 index 00000000..39c88b17 --- /dev/null +++ b/.github/workflows/validate-habitat-sudo-env.yml @@ -0,0 +1,25 @@ +name: Validate Habitat sudo environment + +on: + pull_request: + branches: [main] + paths: + - .github/workflows/grype-hab-package-scan.yml + - .github/workflows/validate-habitat-sudo-env.yml + - workflow-supporting-files/test_habitat_sudo_env.py + workflow_dispatch: + +permissions: + contents: read + +jobs: + validate: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-python@v6 + with: + python-version: "3.x" + - name: Test Habitat sudo environment handling + run: python3 workflow-supporting-files/test_habitat_sudo_env.py