Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/grype-hab-package-scan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/validate-habitat-sudo-env.yml
Original file line number Diff line number Diff line change
@@ -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
77 changes: 77 additions & 0 deletions workflow-supporting-files/test_habitat_sudo_env.py
Original file line number Diff line number Diff line change
@@ -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).")
Loading