Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 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
18 changes: 17 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ runs:
env:
GITHUB_ACTION_PATH: ${{ github.action_path }}

# Create and Clear Trivy Envs file
# See #422 for context
- name: Clear Trivy Envs file
shell: bash
run: |
rm -f trivy_envs.txt
touch trivy_envs.txt

- name: Set Trivy environment variables
shell: bash
run: |
Expand All @@ -161,7 +169,7 @@ runs:
local default_value="$3"

if [ ! -z "$input_value" ] && [ "$input_value" != "$default_value" ]; then
echo "$var_name=$input_value" >> $GITHUB_ENV
echo "export $var_name=$input_value" >> trivy_envs.txt

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ending up removing the extra logic for defaults I previously added as realised this wasn't needed, and would cause behaviour to deviate from configuration priority that is documented/intended for the action

The weird behaviour I'd seen in tests of defaults apparently not being honoured was actually due to the tests repository having a trivy.yaml file present which was automatically getting picked up and throwing off behaviour for the default output test

fi
}

Expand Down Expand Up @@ -202,3 +210,11 @@ runs:

# For Trivy
TRIVY_CACHE_DIR: ${{ inputs.cache-dir }} # Always set

# Remove Trivy envs to keep envs within this action and avoid env leaks
# See #422 for context
- name: Remove Trivy Envs file
if: ${{ always() }}
shell: bash
run: |
rm -f trivy_envs.txt
9 changes: 8 additions & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/bin/bash
set -euo pipefail

echo "export TRIVY_DEBUG=true" >> ./trivy_envs.txt
Comment thread
rvesse marked this conversation as resolved.
Outdated
Comment thread
rvesse marked this conversation as resolved.
Outdated
# Read TRIVY_* envs from file, previously they were written to the GITHUB_ENV file but GitHub Actions automatically
# injects those into subsequent job steps which means inputs from one trivy-action invocation were leaking over to
# any subsequent invocation which led to unexpected/undesireable behaviour from a user perspective
# See #422 for more context around this
source ./trivy_envs.txt

# Set artifact reference
scanType="${INPUT_SCAN_TYPE:-image}"
scanRef="${INPUT_SCAN_REF:-.}"
Expand Down Expand Up @@ -54,4 +61,4 @@ if [ "${TRIVY_FORMAT:-}" = "github" ]; then
fi
fi

exit $returnCode
exit $returnCode
28 changes: 28 additions & 0 deletions test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function reset_envs() {
for var in $(env | grep '^TRIVY_\|^INPUT_' | cut -d= -f1); do
unset "$var"
done
rm -f trivy_envs.txt
}

function compare_files() {
Expand Down Expand Up @@ -149,4 +150,31 @@ function compare_files() {
./entrypoint.sh
compare_files tfvars.json ./test/data/with-tf-vars/report.json
reset_envs
}

@test "trivy image via environment file" {
# trivy image --severity CRITICAL --output image.test knqyf263/vuln-image:1.2.3
# Action injects inputs into the script via environment variables
echo "export TRIVY_OUTPUT=image.test" >> trivy_envs.txt
echo "export TRIVY_SEVERITY=CRITICAL" >> trivy_envs.txt
echo "export INPUT_SCAN_TYPE=image" >> trivy_envs.txt
echo "export INPUT_SCAN_REF=knqyf263/vuln-image:1.2.3" >> trivy_envs.txt
./entrypoint.sh
compare_files image.test ./test/data/image-scan/report
reset_envs
}

@test "trivy image via environment file overrides env leakages" {
# trivy image --severity CRITICAL --output image.test knqyf263/vuln-image:1.2.3
# Action injects inputs into the script via environment variables
# If caller mixes old and new trivy-action version they could still have env leakage so verify that env vars already
# in the env are overridden by those from the envs file
export INPUT_SCAN_REF=no/such-image:1.2.3
echo "export TRIVY_OUTPUT=image.test" >> trivy_envs.txt
echo "export TRIVY_SEVERITY=CRITICAL" >> trivy_envs.txt
echo "export INPUT_SCAN_TYPE=image" >> trivy_envs.txt
echo "export INPUT_SCAN_REF=knqyf263/vuln-image:1.2.3" >> trivy_envs.txt
./entrypoint.sh
compare_files image.test ./test/data/image-scan/report
reset_envs
}