|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +TRIVY_VERSION="${VERSION:-"latest"}" |
| 4 | +TRIVY_PLUGINS="${PLUGINS:-""}" |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Clean up |
| 9 | +rm -rf /var/lib/apt/lists/* |
| 10 | + |
| 11 | +if [ "$(id -u)" -ne 0 ]; then |
| 12 | + echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.' |
| 13 | + exit 1 |
| 14 | +fi |
| 15 | + |
| 16 | +# Checks if packages are installed and installs them if not |
| 17 | +check_packages() { |
| 18 | + if ! dpkg -s "$@" >/dev/null 2>&1; then |
| 19 | + if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then |
| 20 | + echo "Running apt-get update..." |
| 21 | + apt-get update -y |
| 22 | + fi |
| 23 | + apt-get -y install --no-install-recommends "$@" |
| 24 | + fi |
| 25 | +} |
| 26 | + |
| 27 | +# Resolve "latest" to the actual latest version tag, and validate that the |
| 28 | +# requested version exists in the GitHub releases. |
| 29 | +resolve_and_validate_version() { |
| 30 | + local requested_version=$1 |
| 31 | + |
| 32 | + if [ "${requested_version}" = "latest" ]; then |
| 33 | + requested_version=$(curl -sL https://api.github.com/repos/aquasecurity/trivy/releases/latest | jq -r ".tag_name") |
| 34 | + if [ -z "${requested_version}" ] || [ "${requested_version}" = "null" ]; then |
| 35 | + echo "Failed to fetch the latest Trivy version." >&2 |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + fi |
| 39 | + |
| 40 | + # Ensure the version starts with "v" |
| 41 | + if [[ "${requested_version}" != v* ]]; then |
| 42 | + requested_version="v${requested_version}" |
| 43 | + fi |
| 44 | + |
| 45 | + # Validate the version exists |
| 46 | + local version_list |
| 47 | + version_list=$(curl -sL https://api.github.com/repos/aquasecurity/trivy/releases | jq -r ".[].tag_name") |
| 48 | + if ! echo "${version_list}" | grep -qx "${requested_version}"; then |
| 49 | + echo -e "Invalid Trivy version: ${requested_version}\nValid recent versions:\n${version_list}" >&2 |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | + |
| 53 | + echo "${requested_version}" |
| 54 | +} |
| 55 | + |
| 56 | +# Install Trivy plugins. Accepts a comma or space separated list of plugin names. |
| 57 | +install_plugins() { |
| 58 | + local plugins_input=$1 |
| 59 | + |
| 60 | + # Replace commas with spaces to normalize the separator |
| 61 | + local plugins |
| 62 | + plugins=$(echo "${plugins_input}" | tr ',' ' ') |
| 63 | + |
| 64 | + for plugin in ${plugins}; do |
| 65 | + # Trim whitespace |
| 66 | + plugin=$(echo "${plugin}" | xargs) |
| 67 | + if [ -n "${plugin}" ]; then |
| 68 | + echo "Installing Trivy plugin: ${plugin}..." |
| 69 | + trivy plugin install "${plugin}" |
| 70 | + fi |
| 71 | + done |
| 72 | +} |
| 73 | + |
| 74 | +# Make sure we have required dependencies |
| 75 | +check_packages curl jq ca-certificates |
| 76 | + |
| 77 | +# Resolve and validate the version |
| 78 | +echo "Resolving Trivy version '${TRIVY_VERSION}'..." |
| 79 | +TRIVY_VERSION=$(resolve_and_validate_version "${TRIVY_VERSION}") |
| 80 | +echo "Installing Trivy ${TRIVY_VERSION}..." |
| 81 | + |
| 82 | +# Install Trivy using the official convenience script |
| 83 | +curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin "${TRIVY_VERSION}" |
| 84 | + |
| 85 | +# Verify installation |
| 86 | +trivy --version |
| 87 | + |
| 88 | +# Install plugins if specified |
| 89 | +if [ -n "${TRIVY_PLUGINS}" ]; then |
| 90 | + echo "Installing Trivy plugins..." |
| 91 | + install_plugins "${TRIVY_PLUGINS}" |
| 92 | +fi |
| 93 | + |
| 94 | +# Clean up |
| 95 | +rm -rf /var/lib/apt/lists/* |
| 96 | + |
| 97 | +echo "Done!" |
0 commit comments