-
Notifications
You must be signed in to change notification settings - Fork 16
Add cache validation #625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inteon
wants to merge
1
commit into
main
Choose a base branch
from
add_cache_validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+255
−0
Open
Add cache validation #625
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Copyright 2026 The cert-manager Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
| set -o pipefail | ||
|
|
||
| SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" | ||
|
|
||
| # Verify the integrity of a restored cache directory before it is trusted. | ||
| # | ||
| # Intended to run after restoring a cache in CI from a low-trust source | ||
| # (e.g. a node-local hostPath shared with presubmit jobs that could plant | ||
| # trojan binaries). It performs three checks: | ||
| # | ||
| # 1. For each file directly under <cache-dir>/tools/: if the file name is | ||
| # not in the supplied allow-list, it is removed; otherwise its SHA-256 | ||
| # (via hash.sh) must match the expected hash or the file is removed. | ||
| # 2. Anything in <cache-dir>/ other than the tools/ directory is removed | ||
| # outright — only the tools/ subtree is considered part of the cache. | ||
| # 3. If go, go.mod and go.sum are present in the working directory, run | ||
| # `go mod verify` to validate the Go module cache. NOTE: `go mod verify` | ||
| # checks the modules in $GOMODCACHE (defaults to $GOPATH/pkg/mod); the | ||
| # caller must point GOMODCACHE at the restored cache for this to | ||
| # verify it. | ||
| # | ||
| # Exits 0 if nothing was removed (cache was clean), 1 if any file or | ||
| # directory was removed (cache was corrupted/tampered and CI should treat | ||
| # the restore as a miss). | ||
| # | ||
| # Usage: verify_cache.sh <cache-dir> [<file>=<hash> ...] | ||
| # <cache-dir> Path to an existing cache directory containing tools/. | ||
| # <file>=<hash> Allow-listed file under tools/ and its expected SHA-256. | ||
| # <hash> must be non-empty; every allow-listed file is | ||
| # hash-checked. | ||
|
|
||
| if [[ $# -lt 1 ]]; then | ||
| echo "Usage: $(basename "$0") <cache-dir> [<file>=<hash> ...]" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| cache_dir="$1" | ||
| shift | ||
|
|
||
| if [[ ! -d "${cache_dir}" ]]; then | ||
| echo "error: cache directory does not exist: ${cache_dir}" >&2 | ||
| exit 2 | ||
| fi | ||
|
|
||
| tools_dir="${cache_dir}/tools" | ||
|
|
||
| declare -A hashes | ||
| for pair in "$@"; do | ||
| if [[ "${pair}" != *=* ]]; then | ||
| echo "error: expected <file>=<hash>, got: ${pair}" >&2 | ||
| exit 2 | ||
| fi | ||
| name="${pair%%=*}" | ||
| hash="${pair#*=}" | ||
| if [[ -z "${name}" ]]; then | ||
| echo "error: empty file name in argument: ${pair}" >&2 | ||
| exit 2 | ||
| fi | ||
| if [[ -z "${hash}" ]]; then | ||
| echo "error: empty hash for file '${name}'; every allow-listed file must have a hash" >&2 | ||
| exit 2 | ||
| fi | ||
| hashes["${name}"]="${hash}" | ||
| done | ||
|
|
||
| removed=0 | ||
|
|
||
| # Verify tools/ directory | ||
| for file in "${tools_dir}"/*; do | ||
| [[ -f "${file}" ]] || continue | ||
| name=$(basename "${file}") | ||
|
|
||
| # Remove unknown files | ||
| if [[ ! -v hashes["${name}"] ]]; then | ||
| rm -f "${file}" | ||
| removed=1 | ||
| continue | ||
| fi | ||
|
|
||
| # Remove files with wrong hash | ||
| if [[ $("${SCRIPT_DIR}/hash.sh" "${file}") != "${hashes[${name}]}" ]]; then | ||
| rm -f "${file}" | ||
| removed=1 | ||
| fi | ||
| done | ||
|
|
||
| # Remove all unexpected files/directories in cache root (not tools/) | ||
| for item in "${cache_dir}"/*; do | ||
| [[ -e "${item}" ]] || continue | ||
| [[ "$(basename "${item}")" == "tools" ]] && continue | ||
| rm -rf "${item}" | ||
| removed=1 | ||
| done | ||
|
|
||
| if [[ -f go.mod ]] && [[ -f go.sum ]] && command -v go >/dev/null 2>&1; then | ||
| echo "## Verifying Go module cache" | ||
| go mod verify | ||
| fi | ||
|
|
||
| exit "${removed}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Copyright 2026 The cert-manager Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| set -o errexit | ||
| set -o nounset | ||
| set -o pipefail | ||
|
|
||
| script_dir=$(dirname "$(realpath "$0")") | ||
| verify_cache="${script_dir}/../modules/tools/util/verify_cache.sh" | ||
| hash_script="${script_dir}/../modules/tools/util/hash.sh" | ||
|
|
||
| echo "Testing verify_cache.sh" | ||
|
|
||
| # Test 1: Empty cache passes | ||
| echo "Test 1: Empty cache" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" | ||
| "${verify_cache}" "${tmp_dir}" file1=abc123 file2=def456 && echo "✓ pass" || echo "✗ fail" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| # Test 2: Valid files pass | ||
| echo "Test 2: Valid files" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" | ||
| echo "content1" > "${tmp_dir}/tools/file1" | ||
| echo "content2" > "${tmp_dir}/tools/file2" | ||
| hash1=$("${hash_script}" "${tmp_dir}/tools/file1") | ||
| hash2=$("${hash_script}" "${tmp_dir}/tools/file2") | ||
| "${verify_cache}" "${tmp_dir}" "file1=${hash1}" "file2=${hash2}" && echo "✓ pass" || echo "✗ fail" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| # Test 3: Unknown file in tools/ removed | ||
| echo "Test 3: Unknown file removed" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" | ||
| echo "content1" > "${tmp_dir}/tools/file1" | ||
| echo "trojan" > "${tmp_dir}/tools/evil" | ||
| hash1=$("${hash_script}" "${tmp_dir}/tools/file1") | ||
| if "${verify_cache}" "${tmp_dir}" "file1=${hash1}" 2>/dev/null; then | ||
| echo "✗ fail - should have exited 1" | ||
| rm -rf "${tmp_dir}" | ||
| exit 1 | ||
| fi | ||
| [[ ! -f "${tmp_dir}/tools/evil" ]] && echo "✓ pass" || echo "✗ fail - evil not removed" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| # Test 4: Wrong hash removed | ||
| echo "Test 4: Wrong hash removed" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" | ||
| echo "content1" > "${tmp_dir}/tools/file1" | ||
| if "${verify_cache}" "${tmp_dir}" "file1=wronghash" 2>/dev/null; then | ||
| echo "✗ fail - should have exited 1" | ||
| rm -rf "${tmp_dir}" | ||
| exit 1 | ||
| fi | ||
| [[ ! -f "${tmp_dir}/tools/file1" ]] && echo "✓ pass" || echo "✗ fail - file1 not removed" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| # Test 5: Empty hash is rejected | ||
| echo "Test 5: Empty hash rejected" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" | ||
| echo "content" > "${tmp_dir}/tools/etcd" | ||
| if "${verify_cache}" "${tmp_dir}" "etcd=" 2>/dev/null; then | ||
| echo "✗ fail - should have exited non-zero" | ||
| rm -rf "${tmp_dir}" | ||
| exit 1 | ||
| fi | ||
| echo "✓ pass - empty hash rejected" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| # Test 6: Mixed scenario in tools/ | ||
| echo "Test 6: Mixed scenario" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" | ||
| echo "good" > "${tmp_dir}/tools/kind" | ||
| echo "bad" > "${tmp_dir}/tools/helm" | ||
| echo "trojan" > "${tmp_dir}/tools/evil" | ||
| hash_kind=$("${hash_script}" "${tmp_dir}/tools/kind") | ||
| if "${verify_cache}" "${tmp_dir}" "kind=${hash_kind}" "helm=wronghash" 2>/dev/null; then | ||
| echo "✗ fail - should have exited 1" | ||
| rm -rf "${tmp_dir}" | ||
| exit 1 | ||
| fi | ||
| [[ -f "${tmp_dir}/tools/kind" ]] && echo "✓ pass - kind kept" || echo "✗ fail - kind removed" | ||
| [[ ! -f "${tmp_dir}/tools/helm" ]] && echo "✓ pass - helm removed" || echo "✗ fail - helm kept" | ||
| [[ ! -f "${tmp_dir}/tools/evil" ]] && echo "✓ pass - evil removed" || echo "✗ fail - evil kept" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| # Test 7: Removes untrusted files/dirs from cache root | ||
| echo "Test 7: Cache root cleanup" | ||
| tmp_dir=$(mktemp -d) | ||
| mkdir -p "${tmp_dir}/tools" "${tmp_dir}/evil_dir" | ||
| echo "good" > "${tmp_dir}/tools/kind" | ||
| echo "trojan1" > "${tmp_dir}/trojan_root" | ||
| echo "trojan2" > "${tmp_dir}/evil_dir/payload" | ||
| hash_kind=$("${hash_script}" "${tmp_dir}/tools/kind") | ||
| if "${verify_cache}" "${tmp_dir}" "kind=${hash_kind}" 2>/dev/null; then | ||
| echo "✗ fail - should have exited 1" | ||
| rm -rf "${tmp_dir}" | ||
| exit 1 | ||
| fi | ||
| [[ -f "${tmp_dir}/tools/kind" ]] && echo "✓ pass - kind kept" || echo "✗ fail - kind removed" | ||
| [[ ! -f "${tmp_dir}/trojan_root" ]] && echo "✓ pass - trojan_root removed" || echo "✗ fail - trojan_root kept" | ||
| [[ ! -d "${tmp_dir}/evil_dir" ]] && echo "✓ pass - evil_dir removed" || echo "✗ fail - evil_dir kept" | ||
| rm -rf "${tmp_dir}" | ||
|
|
||
| echo "All tests passed" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean we need to run
make verify-cachein every single Prow job? I imagine that's not a big downside, but I wish it was a bit more automaticUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll update the prowjob definitions to first call
make verify-cache.