fix(vhd-scanning): install trivy via PMC with GitHub fallback for unsupported SKUs#8248
fix(vhd-scanning): install trivy via PMC with GitHub fallback for unsupported SKUs#8248
Conversation
There was a problem hiding this comment.
Pull request overview
Updates the VHD post-build scanning script to stop downloading Trivy from GitHub releases and instead install it via the OS package manager (PMC-backed), then run scans using the system trivy binary.
Changes:
- Removed hardcoded Trivy version/arch and the GitHub
curl/tarinstall flow. - Added
install_trivy()to install Trivy viaapt(Ubuntu) ordnf(AzureLinux/Mariner). - Switched scan invocations from
./trivytotrivyand removed the local binary cleanup.
Replace the direct GitHub curl download of trivy with OS-native package manager install (apt-get for Ubuntu, dnf for AzureLinux/Mariner). Changes: - Remove hardcoded TRIVY_VERSION and TRIVY_ARCH variables - Add install_trivy() function using OS package managers (PMC) - Replace ./trivy references with trivy (installed to PATH) - Remove manual binary cleanup (rm ./trivy) - Keep GO_ARCH detection for vuln-to-kusto-vhd binary This eliminates GitHub rate-limiting issues and manual version bumps, leveraging the trivy packages already available on PMC. AB#37444537 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Ubuntu: install from PMC prod repo (deb packages) - AzureLinux: install from PMC via dnf (azlinux3 RPMs) - CBLMariner, Flatcar, AzureContainerLinux, AzureLinuxOSGuard: fall back to GitHub release download (no PMC packages available) - Restore rm -f ./trivy cleanup for the GitHub fallback path Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- GitHub fallback: wrap curl with retrycmd_if_failure 5 10 60 - AzureLinux: add dnf_install helper with retry/timeout (matches apt_get_install pattern) - Ubuntu: already uses apt_get_install with retries Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Ubuntu: replace deprecated apt-key with gpg --dearmor + signed-by - Ubuntu: use /etc/os-release instead of lsb_release for codename - Ubuntu: ensure ca-certificates, curl, gnupg are installed first - GitHub fallback: make version configurable via TRIVY_VERSION env var Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Remove local dnf_install() that shadowed the already-sourced helper from provision_source_distro.sh (cse_helpers_mariner.sh), which includes dnf_makecache on retry for better reliability - Wrap Ubuntu GPG key download with retrycmd_if_failure for network resilience (curl to temp file, then gpg --dearmor) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@copilot can you review |
The current state of the code looks good to me. Here's my review summary: ✅ All CI checks passing (validate-pull-request-source, golangci-lint, Lint Shell/Bash Scripts, Go Unit Tests, Shell/Bash Script Unit Tests, Validate Components, etc.) ✅ No new shellcheck issues — the only warnings ( ✅ ✅ Ubuntu PMC path — uses modern ✅ GitHub fallback — ✅ No issues found — LGTM! 🚀 |
The VHD base image may ship with a stale Microsoft signing key after PMC key rotations, causing apt-get update to fail with NO_PUBKEY EB3E94ADBE1229CF. This affects both install_azure_cli and install_trivy. Fix: download and install the latest microsoft.asc key to both /etc/apt/trusted.gpg.d/ (for repos without signed-by) and /etc/apt/keyrings/ (for repos with signed-by directive) before any apt operations. Also simplify install_trivy to skip redundant key import since the key is already refreshed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
vhdbuilder/packer/trivy-scan.sh
Outdated
| # For Ubuntu, refresh the Microsoft GPG signing key before any apt operations. | ||
| # The VHD base image may ship with a stale key after PMC key rotations, | ||
| # causing apt-get update to fail with NO_PUBKEY errors. | ||
| if [ "$OS_SKU" = "Ubuntu" ]; then | ||
| echo "Refreshing Microsoft GPG signing keys" | ||
| retrycmd_if_failure 5 10 60 curl -fsSL -o /tmp/microsoft.asc https://packages.microsoft.com/keys/microsoft.asc | ||
| # trusted.gpg.d covers repos configured without a signed-by directive | ||
| cat /tmp/microsoft.asc | sudo gpg --yes --dearmor -o /etc/apt/trusted.gpg.d/microsoft.gpg | ||
| sudo chmod 644 /etc/apt/trusted.gpg.d/microsoft.gpg | ||
| # keyrings covers repos configured with signed-by=/etc/apt/keyrings/microsoft.gpg | ||
| sudo install -d -m 0755 /etc/apt/keyrings | ||
| cat /tmp/microsoft.asc | sudo gpg --yes --dearmor -o /etc/apt/keyrings/microsoft.gpg | ||
| sudo chmod 644 /etc/apt/keyrings/microsoft.gpg |
There was a problem hiding this comment.
The Ubuntu key refresh block assumes curl, gpg, and sudo are available and uses sudo even though this script is typically executed as root via az vm run-command. This introduces a new hard dependency for the Ubuntu 22.04 arm64 path (which installs Azure CLI via pip and doesn’t otherwise ensure these tools exist).
Consider (a) using a SUDO wrapper that’s empty when running as root / when sudo isn’t present, and (b) adding explicit preflight checks with a clear error if curl/gpg are missing (or a fallback download mechanism) so scans fail with an actionable message instead of a generic command-not-found.
| # For Ubuntu, refresh the Microsoft GPG signing key before any apt operations. | |
| # The VHD base image may ship with a stale key after PMC key rotations, | |
| # causing apt-get update to fail with NO_PUBKEY errors. | |
| if [ "$OS_SKU" = "Ubuntu" ]; then | |
| echo "Refreshing Microsoft GPG signing keys" | |
| retrycmd_if_failure 5 10 60 curl -fsSL -o /tmp/microsoft.asc https://packages.microsoft.com/keys/microsoft.asc | |
| # trusted.gpg.d covers repos configured without a signed-by directive | |
| cat /tmp/microsoft.asc | sudo gpg --yes --dearmor -o /etc/apt/trusted.gpg.d/microsoft.gpg | |
| sudo chmod 644 /etc/apt/trusted.gpg.d/microsoft.gpg | |
| # keyrings covers repos configured with signed-by=/etc/apt/keyrings/microsoft.gpg | |
| sudo install -d -m 0755 /etc/apt/keyrings | |
| cat /tmp/microsoft.asc | sudo gpg --yes --dearmor -o /etc/apt/keyrings/microsoft.gpg | |
| sudo chmod 644 /etc/apt/keyrings/microsoft.gpg | |
| require_command() { | |
| local cmd="$1" | |
| if ! command -v "$cmd" >/dev/null 2>&1; then | |
| echo "required command '$cmd' is not installed or not in PATH" | |
| exit 1 | |
| fi | |
| } | |
| get_sudo_wrapper() { | |
| if [ "$(id -u)" -eq 0 ]; then | |
| echo "" | |
| elif command -v sudo >/dev/null 2>&1; then | |
| echo "sudo" | |
| else | |
| echo "sudo is required to write Microsoft apt keys when not running as root" >&2 | |
| exit 1 | |
| fi | |
| } | |
| # For Ubuntu, refresh the Microsoft GPG signing key before any apt operations. | |
| # The VHD base image may ship with a stale key after PMC key rotations, | |
| # causing apt-get update to fail with NO_PUBKEY errors. | |
| if [ "$OS_SKU" = "Ubuntu" ]; then | |
| SUDO="$(get_sudo_wrapper)" | |
| require_command curl | |
| require_command gpg | |
| echo "Refreshing Microsoft GPG signing keys" | |
| retrycmd_if_failure 5 10 60 curl -fsSL -o /tmp/microsoft.asc https://packages.microsoft.com/keys/microsoft.asc | |
| # trusted.gpg.d covers repos configured without a signed-by directive | |
| cat /tmp/microsoft.asc | ${SUDO:+$SUDO }gpg --yes --dearmor -o /etc/apt/trusted.gpg.d/microsoft.gpg | |
| ${SUDO:+$SUDO }chmod 644 /etc/apt/trusted.gpg.d/microsoft.gpg | |
| # keyrings covers repos configured with signed-by=/etc/apt/keyrings/microsoft.gpg | |
| ${SUDO:+$SUDO }install -d -m 0755 /etc/apt/keyrings | |
| cat /tmp/microsoft.asc | ${SUDO:+$SUDO }gpg --yes --dearmor -o /etc/apt/keyrings/microsoft.gpg | |
| ${SUDO:+$SUDO }chmod 644 /etc/apt/keyrings/microsoft.gpg |
| retrycmd_if_failure 5 10 60 curl -fL -o "trivy_${trivy_version}_${trivy_arch}.tar.gz" \ | ||
| "https://github.com/aquasecurity/trivy/releases/download/v${trivy_version}/trivy_${trivy_version}_${trivy_arch}.tar.gz" | ||
| tar -xzf "trivy_${trivy_version}_${trivy_arch}.tar.gz" --no-same-owner trivy | ||
| rm "trivy_${trivy_version}_${trivy_arch}.tar.gz" | ||
| chmod a+x trivy |
There was a problem hiding this comment.
The GitHub fallback downloads and executes a Trivy tarball without any integrity verification (checksum/signature). Since this runs with elevated privileges during image scanning, it’s a supply-chain risk.
If the fallback must remain, consider validating the downloaded artifact (e.g., pinned SHA256 per version/arch, or verifying upstream signatures with a pinned public key) before extracting/executing it.
The VHD build already installs packages-microsoft-prod.deb which configures the PMC repo and signing keys. No need to import keys or add repo entries manually — just apt_get_update + apt_get_install. The GPG error in the prior run was a transient PMC key rotation issue. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
trivy-scan.shdownloaded trivy directly from GitHub releases with a hardcodedTRIVY_VERSION=0.69.2, bypassing package management and subject to rate limiting.Changes
packages.microsoft.com/ubuntu/${os_version}/prod) with moderngpg --dearmor+signed-by=keyring; GPG key download wrapped inretrycmd_if_failurednf_install(fromcse_helpers_mariner.shviaprovision_source_distro.sh) which includesdnf_makecacheon retryTRIVY_VERSIONenv var (default0.69.2)./trivy→trivy;rm -f ./trivyat end is a no-op for PMC installs, cleanup for fallback