Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf2909273b | ||
|
|
4a7b230f35 | ||
|
|
cfe11a0a04 | ||
|
|
35a21f8ab5 | ||
|
|
2d0deb8963 | ||
|
|
f0a79362eb | ||
|
|
27f86d4916 | ||
|
|
f728d31dbf |
No files matched your search
@@ -40,6 +40,15 @@ jobs:
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Detect release-tooling-only changes
|
||||
id: release_tooling
|
||||
env:
|
||||
GITEA_EVENT_NAME: ${{ gitea.event_name }}
|
||||
GITEA_EVENT_BEFORE: ${{ gitea.event.before }}
|
||||
GITEA_PR_BASE_SHA: ${{ gitea.event.pull_request.base.sha }}
|
||||
FORCE_FULL: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }}
|
||||
run: python3 scripts/ci-release-tooling-scope.py
|
||||
|
||||
- name: Install Rust toolchain
|
||||
run: |
|
||||
set -eu
|
||||
@@ -78,15 +87,18 @@ jobs:
|
||||
docker info
|
||||
|
||||
- name: Check typed runtime configuration contract
|
||||
if: steps.release_tooling.outputs.ci_only != 'true'
|
||||
run: |
|
||||
cargo run -p crank-config --bin crank-config-contract -- --check
|
||||
python3 scripts/check-runtime-config.py --root .
|
||||
python3 scripts/check-config-boundaries.py --root .
|
||||
|
||||
- name: Check canonical migration contract
|
||||
if: steps.release_tooling.outputs.ci_only != 'true'
|
||||
run: cargo run -p admin-api --bin crank-migrate -- plan --check
|
||||
|
||||
- name: Check typed metrics contract
|
||||
if: steps.release_tooling.outputs.ci_only != 'true'
|
||||
run: |
|
||||
cargo run -p crank-metrics --bin crank-metrics-contract -- --check
|
||||
python3 scripts/check-metrics-boundaries.py --root .
|
||||
@@ -142,6 +154,7 @@ jobs:
|
||||
python3 scripts/check-openapi-matrix-compatibility.py --base "$base_ref"
|
||||
|
||||
- name: Run Crank story ATDD
|
||||
if: steps.release_tooling.outputs.ci_only != 'true'
|
||||
run: scripts/ci-story-checks.sh
|
||||
ui:
|
||||
name: UI Checks
|
||||
|
||||
@@ -12,10 +12,20 @@ on:
|
||||
options: [story, feature]
|
||||
default: story
|
||||
required: true
|
||||
resume_tag:
|
||||
description: Existing draft release tag to resume with image-only steps
|
||||
type: string
|
||||
default: ""
|
||||
required: false
|
||||
resume_source_sha:
|
||||
description: Exact accepted source commit SHA for the resume tag
|
||||
type: string
|
||||
default: ""
|
||||
required: false
|
||||
|
||||
env:
|
||||
REGISTRY: git.itexp.me
|
||||
IMAGE_TAG: ${{ gitea.ref_name }}
|
||||
IMAGE_TAG: ${{ inputs.resume_tag || gitea.ref_name }}
|
||||
ADMIN_API_IMAGE: git.itexp.me/bsodfather/crank-community-admin-api
|
||||
MCP_SERVER_IMAGE: git.itexp.me/bsodfather/crank-community-mcp-server
|
||||
UI_IMAGE: git.itexp.me/bsodfather/crank-community-ui
|
||||
@@ -31,12 +41,47 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Verify existing release before resume
|
||||
id: resume
|
||||
if: inputs.resume_tag != ''
|
||||
env:
|
||||
GITEA_SERVER_URL: https://git.itexp.me
|
||||
GITEA_REPOSITORY: ${{ gitea.repository }}
|
||||
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
||||
RELEASE_TAG: ${{ inputs.resume_tag }}
|
||||
RESUME_SOURCE_SHA: ${{ inputs.resume_source_sha }}
|
||||
run: python3 scripts/ci-release-resume.py
|
||||
- name: Preserve updated image scanner for resume
|
||||
id: pin_resume_scanner
|
||||
if: inputs.resume_tag != ''
|
||||
run: |
|
||||
set -eu
|
||||
scanner_dir="$(mktemp -d "${RUNNER_TEMP:?RUNNER_TEMP is unavailable}/crank-release-scanner.XXXXXX")"
|
||||
cp scripts/scan-images.sh "$scanner_dir/scan-images.sh"
|
||||
cp scripts/check-image-scan.py "$scanner_dir/check-image-scan.py"
|
||||
chmod +x "$scanner_dir/scan-images.sh"
|
||||
printf 'path=%s\n' "$scanner_dir/scan-images.sh" >> "$GITHUB_OUTPUT"
|
||||
- name: Checkout validated release source
|
||||
if: inputs.resume_tag != ''
|
||||
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
|
||||
with:
|
||||
ref: ${{ steps.resume.outputs.release_tag }}
|
||||
fetch-depth: 0
|
||||
- name: Assert exact release source checkout
|
||||
if: inputs.resume_tag != ''
|
||||
env:
|
||||
EXPECTED_SOURCE_SHA: ${{ steps.resume.outputs.source_sha }}
|
||||
run: test "$(git rev-parse HEAD)" = "$EXPECTED_SOURCE_SHA"
|
||||
- name: Clear optional release evidence
|
||||
if: inputs.resume_tag == ''
|
||||
run: rm -f .tmp/openapi-playwright.json .tmp/openapi-ui-evidence.json
|
||||
|
||||
|
||||
|
||||
- name: Use preinstalled Rust toolchain
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
set -eu
|
||||
toolchain_dir="${RUSTUP_HOME:-$HOME/.rustup}/toolchains/1.96.1-x86_64-unknown-linux-gnu"
|
||||
@@ -54,6 +99,7 @@ jobs:
|
||||
"$toolchain_bin/cargo-clippy" --version
|
||||
|
||||
- name: Verify runner toolchain
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
python3 --version
|
||||
rustc --version
|
||||
@@ -65,19 +111,23 @@ jobs:
|
||||
bao version
|
||||
|
||||
- name: Install dependency policy tool
|
||||
if: inputs.resume_tag == ''
|
||||
run: cargo install cargo-deny --version 0.20.2 --locked
|
||||
|
||||
- name: Check formatting
|
||||
if: inputs.resume_tag == ''
|
||||
run: cargo fmt --all --check
|
||||
|
||||
- name: Audit Rust dependencies
|
||||
if: inputs.resume_tag == ''
|
||||
run: cargo deny --locked check advisories bans licenses sources
|
||||
|
||||
- name: Run tooling unit tests
|
||||
if: inputs.validation_level == 'feature'
|
||||
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
|
||||
run: python3 -m unittest discover -s tests/unit
|
||||
|
||||
- name: Check typed runtime configuration contract
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
cargo run -p crank-config --bin crank-config-contract -- --check
|
||||
python3 scripts/check-runtime-config.py --root .
|
||||
@@ -85,14 +135,17 @@ jobs:
|
||||
scripts/check-rust-boundaries.sh
|
||||
|
||||
- name: Check canonical migration contract
|
||||
if: inputs.resume_tag == ''
|
||||
run: cargo run -p admin-api --bin crank-migrate -- plan --check
|
||||
|
||||
- name: Check typed metrics contract
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
cargo run -p crank-metrics --bin crank-metrics-contract -- --check
|
||||
python3 scripts/check-metrics-boundaries.py --root .
|
||||
|
||||
- name: Check Capability Inventory
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
required_args=""
|
||||
for number in $(seq 1 54); do
|
||||
@@ -105,6 +158,7 @@ jobs:
|
||||
$required_args
|
||||
|
||||
- name: Check Capability Baseline
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
python3 scripts/validate-capability-baseline.py \
|
||||
--root . \
|
||||
@@ -112,40 +166,46 @@ jobs:
|
||||
--schema docs/schemas/capability-baseline.schema.json
|
||||
|
||||
- name: Check Community scope
|
||||
if: inputs.resume_tag == ''
|
||||
run: scripts/check-community-scope.sh
|
||||
|
||||
- name: Run Crank story ATDD
|
||||
if: inputs.resume_tag == ''
|
||||
run: scripts/ci-story-checks.sh
|
||||
|
||||
- name: Run release quality gates
|
||||
if: inputs.validation_level == 'feature'
|
||||
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
|
||||
run: |
|
||||
cargo clippy --workspace --all-targets --all-features -- -D warnings
|
||||
cargo test --workspace --all-targets -- --test-threads=1
|
||||
|
||||
- name: Build release binaries
|
||||
if: inputs.resume_tag == ''
|
||||
run: cargo build --release -p admin-api -p mcp-server
|
||||
|
||||
|
||||
- name: Install UI dependencies
|
||||
if: inputs.resume_tag == ''
|
||||
working-directory: apps/ui
|
||||
run: npm ci
|
||||
|
||||
- name: Audit UI dependencies
|
||||
if: inputs.resume_tag == ''
|
||||
working-directory: apps/ui
|
||||
run: npm audit --audit-level=high
|
||||
|
||||
- name: Build UI dist
|
||||
if: inputs.resume_tag == ''
|
||||
working-directory: apps/ui
|
||||
run: npm run build
|
||||
|
||||
- name: Install Playwright browser
|
||||
if: inputs.validation_level == 'feature'
|
||||
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
|
||||
working-directory: apps/ui
|
||||
run: npx playwright install --with-deps chromium
|
||||
|
||||
- name: Run release end-to-end tests
|
||||
if: inputs.validation_level == 'feature'
|
||||
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
|
||||
working-directory: apps/ui
|
||||
run: |
|
||||
mkdir -p ../../.tmp
|
||||
@@ -153,7 +213,7 @@ jobs:
|
||||
PLAYWRIGHT_JSON_OUTPUT=../../.tmp/openapi-playwright.json npm run e2e
|
||||
|
||||
- name: Collect sanitized OpenAPI UI release evidence
|
||||
if: inputs.validation_level == 'feature'
|
||||
if: inputs.resume_tag == '' && inputs.validation_level == 'feature'
|
||||
working-directory: apps/ui
|
||||
run: |
|
||||
trap 'rm -f ../../.tmp/openapi-playwright.json' EXIT
|
||||
@@ -175,12 +235,14 @@ jobs:
|
||||
--require-accepted
|
||||
|
||||
- name: Validate deployment manifests
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
docker compose -f docker-compose.yml --env-file .env.example config -q
|
||||
docker compose -f deploy/community/docker-compose.yml --env-file deploy/community/.env.example config -q
|
||||
docker compose -f deploy/community/docker-compose.images.yml --env-file deploy/community/.env.images.example --profile local-db config -q
|
||||
|
||||
- name: Package release artifacts
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
set -euo pipefail
|
||||
mkdir -p dist/release
|
||||
@@ -204,13 +266,14 @@ jobs:
|
||||
sha256sum "${files[@]}" > "dist/crank-community-${IMAGE_TAG}-checksums.txt"
|
||||
|
||||
- name: Generate SBOM
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
docker run --rm -v "$PWD:/workspace" anchore/syft:latest \
|
||||
dir:/workspace -o spdx-json \
|
||||
> dist/crank-community-${IMAGE_TAG}-sbom.spdx.json
|
||||
|
||||
- name: Publish release bundles, checksums, evidence, and SBOM to Gitea
|
||||
if: startsWith(gitea.ref, 'refs/tags/v')
|
||||
if: inputs.resume_tag == '' && startsWith(gitea.ref, 'refs/tags/v')
|
||||
env:
|
||||
# gitea.server_url inside the job container is the internal http://gitea:3000 origin;
|
||||
# the publisher only forwards its token to the public HTTPS origin.
|
||||
@@ -249,6 +312,8 @@ jobs:
|
||||
docker login '${{ env.REGISTRY }}' -u "$DEPLOY_REGISTRY_USER" --password-stdin
|
||||
|
||||
- name: Build and push release images
|
||||
env:
|
||||
RELEASE_IMAGE_SCANNER: ${{ steps.pin_resume_scanner.outputs.path || 'scripts/scan-images.sh' }}
|
||||
run: |
|
||||
docker build -f apps/admin-api/Dockerfile \
|
||||
-t '${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \
|
||||
@@ -285,7 +350,7 @@ jobs:
|
||||
--env-file .tmp/release-migration-smoke.env --profile local-db up -d --wait
|
||||
docker compose -f deploy/community/docker-compose.images.yml \
|
||||
--env-file .tmp/release-migration-smoke.env --profile local-db logs migrate | grep '"status":"applied"'
|
||||
scripts/scan-images.sh \
|
||||
"$RELEASE_IMAGE_SCANNER" \
|
||||
'${{ env.ADMIN_API_IMAGE }}:${{ env.IMAGE_TAG }}' \
|
||||
'${{ env.MCP_SERVER_IMAGE }}:${{ env.IMAGE_TAG }}' \
|
||||
'${{ env.UI_IMAGE }}:${{ env.IMAGE_TAG }}'
|
||||
@@ -297,5 +362,6 @@ jobs:
|
||||
docker push '${{ env.UI_IMAGE }}:latest'
|
||||
|
||||
- name: Show release bundle summary
|
||||
if: inputs.resume_tag == ''
|
||||
run: |
|
||||
ls -lh dist
|
||||
@@ -0,0 +1,267 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Validate an existing draft release before resuming its image build."""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.parse import quote, urlsplit
|
||||
from urllib.request import Request, build_opener
|
||||
|
||||
_HERE = Path(__file__).resolve().parent
|
||||
_ASSETS_SCRIPT = _HERE / "ci-release-assets.py"
|
||||
_spec = importlib.util.spec_from_file_location("ci_release_assets", _ASSETS_SCRIPT)
|
||||
if _spec is None or _spec.loader is None:
|
||||
raise RuntimeError("Could not load release asset helpers")
|
||||
_assets_module = importlib.util.module_from_spec(_spec)
|
||||
sys.modules[_spec.name] = _assets_module
|
||||
_spec.loader.exec_module(_assets_module)
|
||||
|
||||
PublishError = _assets_module.PublishError
|
||||
_NoRedirect = _assets_module._NoRedirect
|
||||
_assets = _assets_module._assets
|
||||
_inputs = _assets_module._inputs
|
||||
_json_request = _assets_module._json_request
|
||||
|
||||
_COMMIT = re.compile(r"[0-9a-fA-F]{40}\Z")
|
||||
_ARCHIVES = re.compile(r"crank-community-(?:admin-api|mcp-server|ui)-.+\.tar\.gz\Z")
|
||||
_MAX_ASSET_SIZE = 512 * 1024 * 1024
|
||||
_MAX_TOTAL_SIZE = 1024 * 1024 * 1024
|
||||
_REQUIRED_SOURCE_STATUSES = (
|
||||
"CI / Required Checks (push)",
|
||||
"CI / Rust Checks (push)",
|
||||
"CI / UI Checks (push)",
|
||||
)
|
||||
|
||||
|
||||
def _fail(message: str) -> None:
|
||||
raise PublishError(message)
|
||||
|
||||
|
||||
def _git(*args: str) -> str:
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", *args], check=False, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL, text=True,
|
||||
)
|
||||
except OSError:
|
||||
_fail("Could not inspect checked out Git history")
|
||||
if result.returncode:
|
||||
_fail("Resume source tag is unavailable or is not based on checked out main")
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def _check_source(tag: str, source_sha: str, env: dict[str, str]) -> None:
|
||||
if env.get("GITHUB_EVENT_NAME") != "workflow_dispatch" or env.get("GITHUB_REF") != "refs/heads/main":
|
||||
_fail("Resume is allowed only for manual dispatches from main")
|
||||
if not _COMMIT.fullmatch(source_sha):
|
||||
_fail("RESUME_SOURCE_SHA must be an exact 40-character commit SHA")
|
||||
tag_sha = _git("rev-parse", "--verify", f"refs/tags/{tag}^{{commit}}")
|
||||
if tag_sha.lower() != source_sha.lower():
|
||||
_fail("Release tag does not point to the requested source commit")
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", "merge-base", "--is-ancestor", tag_sha, "HEAD"],
|
||||
check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||||
)
|
||||
except OSError:
|
||||
_fail("Could not verify release source ancestry")
|
||||
if result.returncode:
|
||||
_fail("Release source is not an ancestor of checked out main")
|
||||
|
||||
|
||||
def _api_tag_commit(api: str, tag: str, token: str, opener) -> str:
|
||||
result, status = _json_request(
|
||||
opener, "GET", f"{api}/tags/{quote(tag, safe='')}", token
|
||||
)
|
||||
if status != 200 or not isinstance(result, dict) or result.get("name") != tag:
|
||||
_fail("Gitea did not return the requested tag")
|
||||
commit = result.get("commit")
|
||||
if not isinstance(commit, dict) or not isinstance(commit.get("sha"), str):
|
||||
_fail("Gitea returned a malformed tag commit")
|
||||
return commit["sha"]
|
||||
|
||||
|
||||
def _download(origin: str, item: dict, token: str, opener) -> bytes:
|
||||
url = urlsplit(item["browser_download_url"])
|
||||
trusted = urlsplit(origin)
|
||||
if (
|
||||
url.scheme != "https" or url.netloc != trusted.netloc
|
||||
or url.username or url.password or url.fragment or not url.path.startswith("/")
|
||||
):
|
||||
_fail(f"Release asset {item['name']} has an untrusted download URL")
|
||||
size = item["size"]
|
||||
if size < 0 or size > _MAX_ASSET_SIZE:
|
||||
_fail(f"Release asset {item['name']} exceeds the download size limit")
|
||||
request = Request(item["browser_download_url"], headers={"Authorization": f"token {token}"})
|
||||
chunks: list[bytes] = []
|
||||
total = 0
|
||||
try:
|
||||
with opener.open(request, timeout=30) as response:
|
||||
while True:
|
||||
chunk = response.read(min(1024 * 1024, size + 1 - total))
|
||||
if not chunk:
|
||||
break
|
||||
total += len(chunk)
|
||||
if total > size:
|
||||
_fail(f"Release asset {item['name']} size does not match Gitea metadata")
|
||||
chunks.append(chunk)
|
||||
except PublishError:
|
||||
raise
|
||||
except HTTPError as exc:
|
||||
status = exc.code
|
||||
exc.close()
|
||||
_fail(f"Could not download release asset (HTTP {status})")
|
||||
except (URLError, TimeoutError, OSError):
|
||||
_fail("Could not download release asset or request timed out")
|
||||
if total != size:
|
||||
_fail(f"Release asset {item['name']} size does not match Gitea metadata")
|
||||
return b"".join(chunks)
|
||||
|
||||
|
||||
def _sha_manifest(data: bytes, expected: set[str]) -> dict[str, str]:
|
||||
try:
|
||||
lines = data.decode("utf-8").splitlines()
|
||||
except UnicodeDecodeError:
|
||||
_fail("SHA256SUMS is not valid UTF-8")
|
||||
parsed: dict[str, str] = {}
|
||||
for line in lines:
|
||||
match = re.fullmatch(r"([0-9a-fA-F]{64}) [ *](?:dist/)?([^\s/\\]+)", line)
|
||||
if not match or match.group(2) in parsed:
|
||||
_fail("SHA256SUMS has an invalid or duplicate entry")
|
||||
parsed[match.group(2)] = match.group(1).lower()
|
||||
if set(parsed) != expected:
|
||||
_fail("SHA256SUMS does not cover exactly all release payload assets")
|
||||
return parsed
|
||||
|
||||
|
||||
def _verify_payloads(payload: dict[str, bytes], manifest_data: bytes, tag: str) -> dict[str, str]:
|
||||
archives = sorted(name for name in payload if _ARCHIVES.fullmatch(name))
|
||||
checksums = [name for name in payload if name.endswith("-checksums.txt")]
|
||||
sboms = [name for name in payload if name.endswith("-sbom.spdx.json")]
|
||||
expected_archives = {
|
||||
f"crank-community-admin-api-{tag}.tar.gz",
|
||||
f"crank-community-mcp-server-{tag}.tar.gz",
|
||||
f"crank-community-ui-{tag}.tar.gz",
|
||||
}
|
||||
if (
|
||||
set(archives) != expected_archives
|
||||
or checksums != [f"crank-community-{tag}-checksums.txt"]
|
||||
or sboms != [f"crank-community-{tag}-sbom.spdx.json"]
|
||||
):
|
||||
_fail("Draft release is missing an expected archive, checksums file, or SBOM")
|
||||
if len(payload) != 5:
|
||||
_fail("Draft release contains an unexpected payload asset set")
|
||||
manifest = _sha_manifest(manifest_data, set(payload))
|
||||
hashes = {name: hashlib.sha256(data).hexdigest() for name, data in payload.items()}
|
||||
if any(manifest[name] != digest for name, digest in hashes.items()):
|
||||
_fail("SHA256SUMS does not match a release payload asset")
|
||||
try:
|
||||
checksum_lines = payload[checksums[0]].decode("utf-8").splitlines()
|
||||
except UnicodeDecodeError:
|
||||
_fail("Release checksums file is not valid UTF-8")
|
||||
checksum_entries: dict[str, str] = {}
|
||||
for line in checksum_lines:
|
||||
match = re.fullmatch(r"([0-9a-fA-F]{64}) [ *](?:dist/)?([^\s/\\]+)", line)
|
||||
if not match or match.group(2) in checksum_entries:
|
||||
_fail("Release checksums file has an invalid or duplicate entry")
|
||||
checksum_entries[match.group(2)] = match.group(1).lower()
|
||||
if set(checksum_entries) != set(archives) or any(
|
||||
checksum_entries[name] != hashlib.sha256(payload[name]).hexdigest() for name in archives
|
||||
):
|
||||
_fail("Release checksums file does not match all three release archives")
|
||||
return hashes
|
||||
|
||||
|
||||
def _require_source_ci(status_data: object, status_code: int) -> None:
|
||||
if status_code != 200 or not isinstance(status_data, dict):
|
||||
_fail("Could not read required source CI statuses")
|
||||
statuses = status_data.get("statuses")
|
||||
if not isinstance(statuses, list):
|
||||
_fail("Gitea did not return source CI status records")
|
||||
for context in _REQUIRED_SOURCE_STATUSES:
|
||||
matches = [
|
||||
item for item in statuses
|
||||
if isinstance(item, dict) and item.get("context") == context
|
||||
]
|
||||
if not matches:
|
||||
_fail(f"Required source CI status is missing: {context}")
|
||||
if any(not isinstance(item.get("updated_at"), str) for item in matches):
|
||||
_fail(f"Required source CI status has invalid timestamp: {context}")
|
||||
latest = max(matches, key=lambda item: item["updated_at"])
|
||||
if latest.get("status") != "success":
|
||||
_fail(f"Required source CI status is not successful: {context}")
|
||||
|
||||
|
||||
def verify(env=None, opener=None) -> dict[str, object]:
|
||||
env = os.environ if env is None else env
|
||||
origin, api, tag, token = _inputs(env)
|
||||
source_sha = env.get("RESUME_SOURCE_SHA", "")
|
||||
_check_source(tag, source_sha, env)
|
||||
opener = opener or build_opener(_NoRedirect())
|
||||
|
||||
release, status = _json_request(
|
||||
opener, "GET", f"{api}/releases/tags/{quote(tag, safe='')}", token, allow_404=True
|
||||
)
|
||||
if status == 404 or not isinstance(release, dict):
|
||||
_fail("The requested release does not already exist")
|
||||
release_id = release.get("id")
|
||||
if (
|
||||
release.get("tag_name") != tag or release.get("draft") is not True
|
||||
or not isinstance(release_id, int) or release_id <= 0
|
||||
):
|
||||
_fail("The requested release is not the expected existing draft")
|
||||
if _api_tag_commit(api, tag, token, opener).lower() != source_sha.lower():
|
||||
_fail("Gitea tag does not point to the requested source commit")
|
||||
status_data, status_code = _json_request(
|
||||
opener, "GET", f"{api}/commits/{source_sha}/status", token
|
||||
)
|
||||
_require_source_ci(status_data, status_code)
|
||||
existing = _assets(api, release_id, token, opener)
|
||||
if len(existing) != 6:
|
||||
_fail("Draft release must contain exactly the five expected payloads and SHA256SUMS")
|
||||
sums_names = [name for name in existing if name == "SHA256SUMS"]
|
||||
if len(sums_names) != 1:
|
||||
_fail("Draft release must contain one canonical SHA256SUMS asset")
|
||||
if sum(item["size"] for item in existing.values()) > _MAX_TOTAL_SIZE:
|
||||
_fail("Draft release payload exceeds the total download size limit")
|
||||
payload: dict[str, bytes] = {}
|
||||
for name, item in existing.items():
|
||||
payload[name] = _download(origin, item, token, opener)
|
||||
if "SHA256SUMS" not in payload:
|
||||
_fail("Draft release must contain one canonical SHA256SUMS asset")
|
||||
sums = payload.pop("SHA256SUMS")
|
||||
hashes = _verify_payloads(payload, sums, tag)
|
||||
return {
|
||||
"tag": tag,
|
||||
"release_id": release_id,
|
||||
"source_sha": source_sha.lower(),
|
||||
"assets": hashes,
|
||||
}
|
||||
|
||||
|
||||
def main() -> int:
|
||||
try:
|
||||
result = verify()
|
||||
except PublishError as exc:
|
||||
print(f"Gitea release resume: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
output = os.environ.get("GITHUB_OUTPUT")
|
||||
if not output:
|
||||
print("GITHUB_OUTPUT is unavailable", file=sys.stderr)
|
||||
return 1
|
||||
with open(output, "a", encoding="utf-8") as target:
|
||||
target.write(f"release_id={result['release_id']}\n")
|
||||
target.write(f"source_sha={result['source_sha']}\n")
|
||||
target.write(f"release_tag={result['tag']}\n")
|
||||
print(f"Validated existing draft {result['tag']} (id {result['release_id']}) and source CI")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail-closed detector for release-tooling-only CI changes."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
_SHA = re.compile(r"[0-9a-fA-F]{40}\Z")
|
||||
_ALLOWED = {
|
||||
".gitea/workflows/release.yml",
|
||||
"scripts/scan-images.sh",
|
||||
"scripts/check-image-scan.py",
|
||||
"scripts/ci-release-assets.py",
|
||||
"scripts/ci-release-resume.py",
|
||||
"tests/unit/test_scan_images.py",
|
||||
"tests/unit/test_check_image_scan.py",
|
||||
"tests/unit/test_ci_release_assets.py",
|
||||
"tests/unit/test_ci_release_resume.py",
|
||||
}
|
||||
_ALLOWED_BYTES = {path.encode("utf-8") for path in _ALLOWED}
|
||||
|
||||
|
||||
def _git(repo: Path, *args: str) -> subprocess.CompletedProcess[bytes] | None:
|
||||
try:
|
||||
return subprocess.run(
|
||||
["git", "-C", str(repo), *args],
|
||||
check=False,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
|
||||
def release_tooling_only(
|
||||
repo: Path, event_name: str, before_sha: str, pr_base_sha: str, force_full: str
|
||||
) -> bool:
|
||||
if force_full.lower() != "false" or event_name not in {"push", "pull_request"}:
|
||||
return False
|
||||
raw_base = before_sha if event_name == "push" else pr_base_sha
|
||||
if not _SHA.fullmatch(raw_base):
|
||||
return False
|
||||
available = _git(repo, "cat-file", "-e", f"{raw_base}^{{commit}}")
|
||||
if available is None or available.returncode != 0:
|
||||
return False
|
||||
if event_name == "pull_request":
|
||||
merge_base = _git(repo, "merge-base", "HEAD", raw_base)
|
||||
if merge_base is None or merge_base.returncode != 0:
|
||||
return False
|
||||
raw_base = merge_base.stdout.decode("ascii", errors="ignore").strip()
|
||||
if not _SHA.fullmatch(raw_base):
|
||||
return False
|
||||
diff = _git(repo, "diff", "--no-renames", "--name-only", "-z", raw_base, "HEAD")
|
||||
if diff is None or diff.returncode != 0:
|
||||
return False
|
||||
paths = diff.stdout.split(b"\0")
|
||||
paths = [path for path in paths if path]
|
||||
return bool(paths) and all(path in _ALLOWED_BYTES for path in paths)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
env = os.environ
|
||||
ci_only = release_tooling_only(
|
||||
Path.cwd(),
|
||||
env.get("GITEA_EVENT_NAME", ""),
|
||||
env.get("GITEA_EVENT_BEFORE", ""),
|
||||
env.get("GITEA_PR_BASE_SHA", ""),
|
||||
env.get("FORCE_FULL", ""),
|
||||
)
|
||||
output = env.get("GITHUB_OUTPUT")
|
||||
if not output:
|
||||
print("Release-tooling CI scope unavailable; using full Rust checks.")
|
||||
return 0
|
||||
with open(output, "a", encoding="utf-8") as stream:
|
||||
stream.write(f"ci_only={'true' if ci_only else 'false'}\n")
|
||||
print("Release-tooling-only change detected." if ci_only else "Full Rust checks required.")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+39
-7
@@ -1,8 +1,8 @@
|
||||
#!/bin/sh
|
||||
# Scan the built Crank server images with a pinned Trivy and fail closed.
|
||||
#
|
||||
# The wrapper downloads and checksum-verifies one fixed Trivy release, downloads
|
||||
# one fresh vulnerability database, then scans every requested image by its
|
||||
# The wrapper loads or downloads and checksum-verifies one fixed Trivy release,
|
||||
# downloads one fresh vulnerability database, then scans every requested image by its
|
||||
# immutable Docker id with a fully pinned policy. It never trusts a local
|
||||
# trivy.yaml, .trivyignore, VEX file or skip list: --config and --ignorefile
|
||||
# point at /dev/null and every inherited TRIVY_* variable is removed before
|
||||
@@ -14,6 +14,7 @@
|
||||
#
|
||||
# db/metadata.json copy of the actual downloaded Trivy DB metadata
|
||||
# db-download.log database download log
|
||||
# scanner-cache-source.log archive source provenance
|
||||
# scanner.json pinned scanner version and scan policy receipt
|
||||
# <NN-slug>.json raw Trivy JSON report per image
|
||||
# <NN-slug>.scan.log Trivy scan log per image
|
||||
@@ -45,6 +46,12 @@ trivy_version="0.70.0"
|
||||
archive="trivy_${trivy_version}_Linux-64bit.tar.gz"
|
||||
expected_sha256="8b4376d5d6befe5c24d503f10ff136d9e0c49f9127a4279fd110b727929a5aa9"
|
||||
download_url="https://github.com/aquasecurity/trivy/releases/download/v${trivy_version}/${archive}"
|
||||
# Fixed GitHub asset ID for the Trivy 0.70.0 Linux 64-bit release archive.
|
||||
asset_api_url="https://api.github.com/repos/aquasecurity/trivy/releases/assets/398381981"
|
||||
tool_cache_root="${RUNNER_TOOL_CACHE:-/opt/hostedtoolcache}"
|
||||
cached_archive="$tool_cache_root/trivy/$trivy_version/$archive"
|
||||
db_repository_primary="ghcr.io/aquasecurity/trivy-db:2"
|
||||
db_repository_fallback="mirror.gcr.io/aquasec/trivy-db:2"
|
||||
|
||||
output_dir="${CRANK_IMAGE_SCAN_OUTPUT_DIR:-.tmp/image-scan-evidence}"
|
||||
if ! mkdir -p "$output_dir"; then
|
||||
@@ -100,11 +107,34 @@ PY
|
||||
exit 2
|
||||
}
|
||||
|
||||
# Download and checksum-verify the pinned scanner exactly once.
|
||||
if ! curl --fail --silent --show-error --location --max-time 300 \
|
||||
"$download_url" --output "$temp_dir/$archive" \
|
||||
>"$output_dir/scanner-download.log" 2>&1; then
|
||||
fail_all "could not download pinned Trivy ${trivy_version}"
|
||||
# Load the exact-version archive from the runner cache when preinstalled;
|
||||
# otherwise use the bounded network routes. The shared cache is read-only here.
|
||||
cache_source_log="$output_dir/scanner-cache-source.log"
|
||||
: > "$cache_source_log"
|
||||
if [ -f "$cached_archive" ]; then
|
||||
printf 'archive_source=runner-tool-cache\n' > "$cache_source_log"
|
||||
if ! cp "$cached_archive" "$temp_dir/$archive"; then
|
||||
fail_all "could not copy the pinned Trivy archive from runner tool cache"
|
||||
fi
|
||||
else
|
||||
printf 'archive_source=network\n' > "$cache_source_log"
|
||||
if ! curl --fail --silent --show-error --location --max-time 300 \
|
||||
"$download_url" --output "$temp_dir/$archive" \
|
||||
>"$output_dir/scanner-download.log" 2>&1; then
|
||||
rm -f "$temp_dir/$archive"
|
||||
if ! curl --fail --silent --show-error --location --max-time 300 \
|
||||
--header 'Accept: application/octet-stream' \
|
||||
"$asset_api_url" --output "$temp_dir/$archive" \
|
||||
>"$output_dir/scanner-api-download.log" 2>&1; then
|
||||
printf 'network_download=failed\n' >> "$cache_source_log"
|
||||
fail_all "could not download pinned Trivy ${trivy_version} from release URL or GitHub asset API"
|
||||
fi
|
||||
printf 'archive_source=github-release-asset-api\n' > "$cache_source_log"
|
||||
printf 'Downloaded pinned Trivy %s from GitHub release asset API.\n' "$trivy_version" \
|
||||
>>"$output_dir/scanner-api-download.log"
|
||||
else
|
||||
printf 'archive_source=github-release-download\n' > "$cache_source_log"
|
||||
fi
|
||||
fi
|
||||
if ! printf '%s %s\n' "$expected_sha256" "$temp_dir/$archive" \
|
||||
| sha256sum --check --status; then
|
||||
@@ -120,6 +150,8 @@ fi
|
||||
|
||||
# Download one fresh vulnerability database; every scan reuses it read-only.
|
||||
if ! "$trivy_bin" image --config /dev/null --download-db-only --cache-dir "$cache_dir" \
|
||||
--db-repository "$db_repository_primary" \
|
||||
--db-repository "$db_repository_fallback" \
|
||||
--no-progress --skip-version-check \
|
||||
>"$output_dir/db-download.log" 2>&1; then
|
||||
fail_all "could not download the Trivy vulnerability database"
|
||||
|
||||
@@ -0,0 +1,238 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SCRIPT = ROOT / "scripts" / "ci-release-resume.py"
|
||||
spec = importlib.util.spec_from_file_location("ci_release_resume", SCRIPT)
|
||||
assert spec and spec.loader
|
||||
resume = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(resume)
|
||||
|
||||
|
||||
TAG = "v0.4.0-rc.15"
|
||||
SHA = "29ee01b6efced04b027569fbeed2e865991c8f55"
|
||||
FILES = {
|
||||
f"crank-community-admin-api-{TAG}.tar.gz": b"admin archive",
|
||||
f"crank-community-mcp-server-{TAG}.tar.gz": b"mcp archive",
|
||||
f"crank-community-ui-{TAG}.tar.gz": b"ui archive",
|
||||
f"crank-community-{TAG}-checksums.txt": b"",
|
||||
f"crank-community-{TAG}-sbom.spdx.json": b"{}",
|
||||
}
|
||||
|
||||
|
||||
def payloads():
|
||||
files = dict(FILES)
|
||||
archives = [name for name in files if name.endswith(".tar.gz")]
|
||||
files[f"crank-community-{TAG}-checksums.txt"] = "".join(
|
||||
f"{hashlib.sha256(files[name]).hexdigest()} dist/{name}\n" for name in archives
|
||||
).encode()
|
||||
manifest = "".join(
|
||||
f"{hashlib.sha256(data).hexdigest()} {name}\n" for name, data in files.items()
|
||||
).encode()
|
||||
return files, manifest
|
||||
|
||||
|
||||
class ResumeValidationTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.env = {
|
||||
"GITEA_SERVER_URL": "https://git.itexp.me",
|
||||
"GITEA_REPOSITORY": "bsodfather/crank-community",
|
||||
"GITEA_TOKEN": "test-token",
|
||||
"RELEASE_TAG": TAG,
|
||||
"RESUME_SOURCE_SHA": SHA,
|
||||
"GITHUB_EVENT_NAME": "workflow_dispatch",
|
||||
"GITHUB_REF": "refs/heads/main",
|
||||
}
|
||||
self.files, self.manifest = payloads()
|
||||
self.calls = []
|
||||
|
||||
def api(self, _opener, method, url, token, payload=None, allow_404=False):
|
||||
self.calls.append((method, url, payload))
|
||||
if url.endswith("/releases/tags/" + TAG):
|
||||
return ({"id": 426, "tag_name": TAG, "draft": True}, 200)
|
||||
if url.endswith("/tags/" + TAG):
|
||||
return ({"name": TAG, "commit": {"sha": SHA}}, 200)
|
||||
if url.endswith("/commits/" + SHA + "/status"):
|
||||
contexts = {"CI / Required Checks (push)", "CI / Rust Checks (push)", "CI / UI Checks (push)"}
|
||||
return ({
|
||||
"state": "failure",
|
||||
"statuses": [
|
||||
{"context": context, "status": "success", "updated_at": "2026-11-07T10:00:00Z"}
|
||||
for context in contexts
|
||||
] + [
|
||||
{"context": "Community Release (push)", "status": "failure", "updated_at": "2026-11-07T10:01:00Z"}
|
||||
],
|
||||
}, 200)
|
||||
raise AssertionError(f"Unexpected API URL: {url}")
|
||||
|
||||
def assets(self, _api, release_id, token, opener):
|
||||
self.assertEqual(release_id, 426)
|
||||
result = {}
|
||||
for name, data in {**self.files, "SHA256SUMS": self.manifest}.items():
|
||||
result[name] = {"name": name, "id": len(result) + 1, "size": len(data),
|
||||
"browser_download_url": f"https://git.itexp.me/releases/download/{name}"}
|
||||
return result
|
||||
|
||||
def download(self, origin, item, token, opener):
|
||||
self.assertEqual(origin, "https://git.itexp.me")
|
||||
return self.files.get(item["name"], self.manifest)
|
||||
|
||||
def run_verify(self):
|
||||
with (
|
||||
patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://git.itexp.me/api/v1/repos/bsodfather/crank-community", TAG, "test-token")),
|
||||
patch.object(resume, "_check_source") as check_source,
|
||||
patch.object(resume, "_json_request", side_effect=self.api),
|
||||
patch.object(resume, "_assets", side_effect=self.assets),
|
||||
patch.object(resume, "_download", side_effect=self.download),
|
||||
):
|
||||
result = resume.verify(self.env, opener=object())
|
||||
check_source.assert_called_once_with(TAG, SHA, self.env)
|
||||
return result
|
||||
|
||||
def test_valid_draft_is_read_only_and_outputs_source_receipt(self):
|
||||
result = self.run_verify()
|
||||
self.assertEqual(result["release_id"], 426)
|
||||
self.assertEqual(result["source_sha"], SHA)
|
||||
self.assertEqual(set(result["assets"]), set(self.files))
|
||||
self.assertTrue(self.calls)
|
||||
self.assertTrue(all(call[0] == "GET" and call[2] is None for call in self.calls))
|
||||
|
||||
def test_missing_release_rejected(self):
|
||||
with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \
|
||||
patch.object(resume, "_check_source"), \
|
||||
patch.object(resume, "_json_request", return_value=(None, 404)):
|
||||
with self.assertRaisesRegex(resume.PublishError, "does not already exist"):
|
||||
resume.verify(self.env, opener=object())
|
||||
|
||||
def test_source_mismatch_rejected_before_api(self):
|
||||
with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \
|
||||
patch.object(resume, "_check_source", side_effect=resume.PublishError("tag mismatch")), \
|
||||
patch.object(resume, "_json_request") as request:
|
||||
with self.assertRaisesRegex(resume.PublishError, "tag mismatch"):
|
||||
resume.verify(self.env, opener=object())
|
||||
request.assert_not_called()
|
||||
|
||||
def test_api_tag_commit_mismatch_rejected(self):
|
||||
with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \
|
||||
patch.object(resume, "_check_source"), \
|
||||
patch.object(resume, "_json_request", side_effect=self.api), \
|
||||
patch.object(resume, "_api_tag_commit", return_value="0" * 40):
|
||||
with self.assertRaisesRegex(resume.PublishError, "Gitea tag does not point"):
|
||||
resume.verify(self.env, opener=object())
|
||||
|
||||
def test_unexpected_asset_set_rejected_before_download(self):
|
||||
assets = self.assets(None, 426, "token", None)
|
||||
assets["unexpected.txt"] = {
|
||||
"name": "unexpected.txt", "id": 99, "size": 0,
|
||||
"browser_download_url": "https://git.itexp.me/unexpected.txt",
|
||||
}
|
||||
with patch.object(resume, "_inputs", return_value=("https://git.itexp.me", "https://api", TAG, "token")), \
|
||||
patch.object(resume, "_check_source"), \
|
||||
patch.object(resume, "_json_request", side_effect=self.api), \
|
||||
patch.object(resume, "_api_tag_commit", return_value=SHA), \
|
||||
patch.object(resume, "_assets", return_value=assets), \
|
||||
patch.object(resume, "_download") as download:
|
||||
with self.assertRaisesRegex(resume.PublishError, "exactly the five expected"):
|
||||
resume.verify(self.env, opener=object())
|
||||
download.assert_not_called()
|
||||
|
||||
def test_required_source_status_missing_or_failed_is_rejected(self):
|
||||
cases = (
|
||||
{"state": "failure", "statuses": []},
|
||||
{"state": "failure", "statuses": [
|
||||
{"context": "CI / Required Checks (push)", "status": "failure", "updated_at": "2026-11-07T10:00:00Z"},
|
||||
{"context": "CI / Rust Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"},
|
||||
{"context": "CI / UI Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"},
|
||||
]},
|
||||
{"state": "failure", "statuses": [
|
||||
{"context": "CI / Required Checks (push)", "status": "success", "updated_at": "2026-11-07T09:00:00Z"},
|
||||
{"context": "CI / Required Checks (push)", "status": "failure", "updated_at": "2026-11-07T10:00:00Z"},
|
||||
{"context": "CI / Rust Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"},
|
||||
{"context": "CI / UI Checks (push)", "status": "success", "updated_at": "2026-11-07T10:00:00Z"},
|
||||
]},
|
||||
)
|
||||
for status_data in cases:
|
||||
with self.subTest(status_data=status_data):
|
||||
with self.assertRaises(resume.PublishError):
|
||||
resume._require_source_ci(status_data, 200)
|
||||
|
||||
def test_missing_expected_asset_rejected(self):
|
||||
files = dict(self.files)
|
||||
files.pop(f"crank-community-{TAG}-sbom.spdx.json")
|
||||
with self.assertRaisesRegex(resume.PublishError, "missing an expected"):
|
||||
resume._verify_payloads(files, self.manifest, TAG)
|
||||
|
||||
def test_checksum_manifest_rejects_missing_or_bad_payload_entries(self):
|
||||
for manifest in (b"", b"0" * 64 + b" wrong.tar.gz\n"):
|
||||
with self.subTest(manifest=manifest):
|
||||
with self.assertRaises(resume.PublishError):
|
||||
resume._verify_payloads(self.files, manifest, TAG)
|
||||
|
||||
def test_checksum_manifest_rejects_mismatched_archive_checksum(self):
|
||||
files = dict(self.files)
|
||||
checksum_name = f"crank-community-{TAG}-checksums.txt"
|
||||
files[checksum_name] = b"0" * 64 + b" crank-community-admin-api-" + TAG.encode() + b".tar.gz\n"
|
||||
updated_manifest = "".join(
|
||||
f"{hashlib.sha256(data).hexdigest()} {name}\n" for name, data in files.items()
|
||||
).encode()
|
||||
with self.assertRaisesRegex(resume.PublishError, "checksums file"):
|
||||
resume._verify_payloads(files, updated_manifest, TAG)
|
||||
|
||||
def test_download_rejects_untrusted_origin_before_network(self):
|
||||
item = {"name": "payload", "size": 1, "browser_download_url": "https://evil.example/payload"}
|
||||
opener = unittest.mock.Mock()
|
||||
with self.assertRaisesRegex(resume.PublishError, "untrusted download URL"):
|
||||
resume._download("https://git.itexp.me", item, "token", opener)
|
||||
opener.open.assert_not_called()
|
||||
|
||||
def test_download_rejects_redirect_without_following(self):
|
||||
class Redirecting:
|
||||
def open(self, request, timeout):
|
||||
raise resume.PublishError("redirect refused")
|
||||
item = {"name": "payload", "size": 1, "browser_download_url": "https://git.itexp.me/payload"}
|
||||
with self.assertRaisesRegex(resume.PublishError, "redirect refused"):
|
||||
resume._download("https://git.itexp.me", item, "token", Redirecting())
|
||||
|
||||
|
||||
class WorkflowResumeTests(unittest.TestCase):
|
||||
def test_resume_selects_tag_source_and_skips_asset_production(self):
|
||||
workflow = (ROOT / ".gitea/workflows/release.yml").read_text()
|
||||
self.assertIn("resume_tag:", workflow)
|
||||
self.assertIn("resume_source_sha:", workflow)
|
||||
self.assertIn("IMAGE_TAG: ${{ inputs.resume_tag || gitea.ref_name }}", workflow)
|
||||
self.assertIn("python3 scripts/ci-release-resume.py", workflow)
|
||||
self.assertIn("ref: ${{ steps.resume.outputs.release_tag }}", workflow)
|
||||
self.assertIn('test "$(git rev-parse HEAD)" = "$EXPECTED_SOURCE_SHA"', workflow)
|
||||
for step in ("Build release binaries", "Build UI dist", "Package release artifacts", "Generate SBOM"):
|
||||
start = workflow.index(f"- name: {step}")
|
||||
self.assertIn("if: inputs.resume_tag == ''", workflow[start : start + 180])
|
||||
self.assertIn("if: inputs.resume_tag == '' && startsWith(gitea.ref, 'refs/tags/v')", workflow)
|
||||
self.assertIn("if: inputs.resume_tag == ''\n run: |\n ls -lh dist", workflow)
|
||||
|
||||
def test_resume_preserves_updated_scanner_before_old_source_checkout(self):
|
||||
workflow = (ROOT / ".gitea/workflows/release.yml").read_text()
|
||||
pin = workflow.index("- name: Preserve updated image scanner for resume")
|
||||
old_checkout = workflow.index("- name: Checkout validated release source")
|
||||
source_assert = workflow.index("- name: Assert exact release source checkout")
|
||||
image_build = workflow.index("- name: Build and push release images")
|
||||
self.assertLess(pin, old_checkout)
|
||||
self.assertLess(old_checkout, source_assert)
|
||||
self.assertLess(source_assert, image_build)
|
||||
pin_step = workflow[pin:old_checkout]
|
||||
self.assertIn("if: inputs.resume_tag != ''", pin_step)
|
||||
self.assertIn('cp scripts/scan-images.sh "$scanner_dir/scan-images.sh"', pin_step)
|
||||
self.assertIn('cp scripts/check-image-scan.py "$scanner_dir/check-image-scan.py"', pin_step)
|
||||
build_step = workflow[image_build:]
|
||||
self.assertIn("RELEASE_IMAGE_SCANNER: ${{ steps.pin_resume_scanner.outputs.path || 'scripts/scan-images.sh' }}", build_step)
|
||||
self.assertIn('"$RELEASE_IMAGE_SCANNER" \\', build_step)
|
||||
self.assertIn("ref: ${{ steps.resume.outputs.release_tag }}", workflow)
|
||||
self.assertIn('test "$(git rev-parse HEAD)" = "$EXPECTED_SOURCE_SHA"', workflow)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,140 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SCRIPT = ROOT / "scripts" / "ci-release-tooling-scope.py"
|
||||
spec = importlib.util.spec_from_file_location("ci_release_tooling_scope", SCRIPT)
|
||||
assert spec and spec.loader
|
||||
scope = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(scope)
|
||||
|
||||
WORKFLOW = ROOT / ".gitea/workflows/ci.yml"
|
||||
_FALSE = "false"
|
||||
|
||||
|
||||
class ReleaseToolingScopeTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.repo = Path(self.temp.name)
|
||||
self.git("init", "-q")
|
||||
self.git("config", "user.email", "ci-scope@example.test")
|
||||
self.git("config", "user.name", "CI Scope Test")
|
||||
self.write("src/lib.rs", "pub fn initial() {}\n")
|
||||
self.write("scripts/scan-images.sh", "#!/bin/sh\nexit 0\n")
|
||||
self.write("tests/unit/test_scan_images.py", "# initial\n")
|
||||
self.git("add", ".")
|
||||
self.git("commit", "-qm", "baseline")
|
||||
self.base = self.head()
|
||||
|
||||
def git(self, *args: str) -> str:
|
||||
result = subprocess.run(
|
||||
["git", "-C", str(self.repo), *args],
|
||||
check=True,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
text=True,
|
||||
)
|
||||
return result.stdout.strip()
|
||||
|
||||
def head(self) -> str:
|
||||
return self.git("rev-parse", "HEAD")
|
||||
|
||||
def write(self, relative: str, content: str) -> None:
|
||||
path = self.repo / relative
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(content, encoding="utf-8")
|
||||
|
||||
def commit(self, message: str = "change") -> None:
|
||||
self.git("add", "-A")
|
||||
self.git("commit", "-qm", message)
|
||||
|
||||
def classify(self, event="push", before=None, base=None, force=_FALSE) -> bool:
|
||||
return scope.release_tooling_only(
|
||||
self.repo,
|
||||
event,
|
||||
self.base if before is None else before,
|
||||
self.base if base is None else base,
|
||||
force,
|
||||
)
|
||||
|
||||
def test_release_only_diff_is_safe_for_push_and_pull_request(self):
|
||||
self.write(".gitea/workflows/release.yml", "# release tooling update\n")
|
||||
self.write("scripts/scan-images.sh", "#!/bin/sh\n# scanner update\n")
|
||||
self.commit()
|
||||
self.assertTrue(self.classify("push", before=self.base))
|
||||
self.assertTrue(self.classify("pull_request", base=self.base))
|
||||
|
||||
def test_rust_or_mixed_diff_requires_full_ci(self):
|
||||
self.write("src/lib.rs", "pub fn changed() {}\n")
|
||||
self.commit("Rust change")
|
||||
self.assertFalse(self.classify("push", before=self.base))
|
||||
|
||||
self.write("scripts/scan-images.sh", "#!/bin/sh\n# tooling change\n")
|
||||
self.write("src/lib.rs", "pub fn changed_again() {}\n")
|
||||
self.commit("mixed change")
|
||||
self.assertFalse(self.classify("push", before=self.base))
|
||||
|
||||
def test_rename_from_rust_into_allowlist_is_not_safe(self):
|
||||
(self.repo / ".gitea/workflows").mkdir(parents=True)
|
||||
self.git("mv", "src/lib.rs", ".gitea/workflows/release.yml")
|
||||
self.commit("rename Rust source into release path")
|
||||
self.assertFalse(self.classify("push", before=self.base))
|
||||
|
||||
def test_unavailable_zero_empty_and_missing_bases_require_full_ci(self):
|
||||
self.write("scripts/scan-images.sh", "#!/bin/sh\n# allowed\n")
|
||||
self.commit()
|
||||
self.assertFalse(self.classify("push", before=""))
|
||||
self.assertFalse(self.classify("push", before="0" * 40))
|
||||
self.assertFalse(self.classify("push", before="1" * 40))
|
||||
self.assertFalse(self.classify("push", before=self.head()))
|
||||
self.assertFalse(self.classify("pull_request", base=""))
|
||||
|
||||
def test_manual_feature_and_unknown_force_metadata_require_full_ci(self):
|
||||
self.write("scripts/scan-images.sh", "#!/bin/sh\n# allowed\n")
|
||||
self.commit()
|
||||
self.assertFalse(self.classify("workflow_dispatch", before=self.base))
|
||||
self.assertFalse(self.classify("push", before=self.base, force="true"))
|
||||
self.assertFalse(self.classify("push", before=self.base, force=""))
|
||||
|
||||
def test_non_allowlisted_ci_self_change_requires_full_ci(self):
|
||||
self.write(".gitea/workflows/ci.yml", "# detector workflow change\n")
|
||||
self.commit()
|
||||
self.assertFalse(self.classify("push", before=self.base))
|
||||
|
||||
|
||||
class WorkflowScopeTests(unittest.TestCase):
|
||||
def test_only_four_heavy_rust_steps_are_skippable(self):
|
||||
workflow = WORKFLOW.read_text(encoding="utf-8")
|
||||
self.assertIn("run: python3 scripts/ci-release-tooling-scope.py", workflow)
|
||||
self.assertIn("FORCE_FULL: ${{ inputs.validation_level == 'feature' || contains(gitea.event.pull_request.labels.*.name, 'feature-ready') }}", workflow)
|
||||
conditional = "if: steps.release_tooling.outputs.ci_only != 'true'"
|
||||
for name in (
|
||||
"Check typed runtime configuration contract",
|
||||
"Check canonical migration contract",
|
||||
"Check typed metrics contract",
|
||||
"Run Crank story ATDD",
|
||||
):
|
||||
start = workflow.index(f"- name: {name}")
|
||||
self.assertIn(conditional, workflow[start : start + 180])
|
||||
for name in (
|
||||
"Install Rust toolchain",
|
||||
"Run tooling unit tests",
|
||||
"Check formatting",
|
||||
"Check Rust boundaries",
|
||||
"Check OpenAPI matrix and corpus compatibility versions",
|
||||
):
|
||||
start = workflow.index(f"- name: {name}")
|
||||
self.assertNotIn("if:", workflow[start : start + 160])
|
||||
self.assertIn("name: Rust Checks", workflow)
|
||||
self.assertIn("name: UI Checks", workflow)
|
||||
self.assertIn("name: Required Checks", workflow)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -27,9 +27,18 @@ import os
|
||||
import sys
|
||||
|
||||
args = sys.argv[1:]
|
||||
if os.environ.get("FAKE_CURL_FAIL") == "1":
|
||||
sys.stderr.write("curl: (7) failed to connect\\n")
|
||||
url = next((value for value in args if value.startswith("https://")), "")
|
||||
api_download = url == "https://api.github.com/repos/aquasecurity/trivy/releases/assets/398381981"
|
||||
failed = os.environ.get("FAKE_CURL_FAIL") == "1"
|
||||
failed = failed or (not api_download and os.environ.get("FAKE_CURL_DIRECT_FAIL") == "1")
|
||||
failed = failed or (api_download and os.environ.get("FAKE_CURL_API_FAIL") == "1")
|
||||
if failed:
|
||||
sys.stderr.write("curl: (22) fixture download failure for " + url + "\\n")
|
||||
sys.exit(7)
|
||||
if api_download:
|
||||
if "--header" not in args or args[args.index("--header") + 1] != "Accept: application/octet-stream":
|
||||
sys.stderr.write("curl: missing octet-stream Accept header\\n")
|
||||
sys.exit(2)
|
||||
output = None
|
||||
for index, value in enumerate(args):
|
||||
if value in ("--output", "-o"):
|
||||
@@ -37,7 +46,8 @@ for index, value in enumerate(args):
|
||||
if output is None:
|
||||
sys.stderr.write("curl: missing --output\\n")
|
||||
sys.exit(2)
|
||||
with open(os.environ["FAKE_CURL_ARCHIVE"], "rb") as source:
|
||||
archive_path = os.environ.get("FAKE_CURL_API_ARCHIVE", os.environ["FAKE_CURL_ARCHIVE"]) if api_download else os.environ["FAKE_CURL_ARCHIVE"]
|
||||
with open(archive_path, "rb") as source:
|
||||
payload = source.read()
|
||||
with open(output, "wb") as target:
|
||||
target.write(payload)
|
||||
@@ -176,6 +186,10 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
images=(VULN_TAG, CLEAN_TAG),
|
||||
trivy_fail_id=None,
|
||||
curl_fail=False,
|
||||
curl_direct_fail=False,
|
||||
curl_api_fail=False,
|
||||
api_checksum_mismatch=False,
|
||||
cache_archive=None,
|
||||
extra_env=None,
|
||||
workdir_files=None,
|
||||
run_in_workdir=False,
|
||||
@@ -214,6 +228,13 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
|
||||
archive_path = root / "trivy-fixture.tar.gz"
|
||||
archive_path.write_bytes(fixture_archive)
|
||||
api_archive_path = root / "trivy-api-fixture.tar.gz"
|
||||
api_archive_path.write_bytes(fixture_archive + b"tampered" if api_checksum_mismatch else fixture_archive)
|
||||
tool_cache = root / "runner-tool-cache"
|
||||
if cache_archive is not None:
|
||||
cached_path = tool_cache / "trivy" / "0.70.0" / "trivy_0.70.0_Linux-64bit.tar.gz"
|
||||
cached_path.parent.mkdir(parents=True)
|
||||
cached_path.write_bytes(fixture_archive if cache_archive == "valid" else b"invalid archive")
|
||||
|
||||
reports = root / "reports"
|
||||
reports.mkdir()
|
||||
@@ -246,8 +267,10 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
env.update(
|
||||
{
|
||||
"PATH": f"{bin_dir}:{env['PATH']}",
|
||||
"RUNNER_TOOL_CACHE": str(tool_cache),
|
||||
"CRANK_IMAGE_SCAN_OUTPUT_DIR": str(evidence),
|
||||
"FAKE_CURL_ARCHIVE": str(archive_path),
|
||||
"FAKE_CURL_API_ARCHIVE": str(api_archive_path),
|
||||
"FAKE_IMAGE_MAP": str(image_map),
|
||||
"FAKE_REPORT_DIR": str(reports),
|
||||
"FAKE_DOCKER_LOG": str(docker_log),
|
||||
@@ -258,6 +281,10 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
env["FAKE_TRIVY_FAIL_ID"] = trivy_fail_id
|
||||
if curl_fail:
|
||||
env["FAKE_CURL_FAIL"] = "1"
|
||||
if curl_direct_fail:
|
||||
env["FAKE_CURL_DIRECT_FAIL"] = "1"
|
||||
if curl_api_fail:
|
||||
env["FAKE_CURL_API_FAIL"] = "1"
|
||||
if extra_env:
|
||||
env.update(extra_env)
|
||||
|
||||
@@ -412,6 +439,18 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
self.assertEqual(result.returncode, 2, result.stderr)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
|
||||
def test_database_download_uses_official_priority_and_fallback(self):
|
||||
result, _, _, _, _, trivy_calls = self.run_scan(images=(CLEAN_TAG,))
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
downloads = [call["args"] for call in trivy_calls if "--download-db-only" in call["args"]]
|
||||
self.assertEqual(len(downloads), 1)
|
||||
args = downloads[0]
|
||||
repositories = [args[index + 1] for index, item in enumerate(args) if item == "--db-repository"]
|
||||
self.assertEqual(
|
||||
repositories,
|
||||
["ghcr.io/aquasecurity/trivy-db:2", "mirror.gcr.io/aquasec/trivy-db:2"],
|
||||
)
|
||||
|
||||
def test_rerun_cannot_reuse_an_old_successful_raw_report(self):
|
||||
result, _, evidence, receipt, _, _ = self.run_scan(images=(CLEAN_TAG,), repeat_without_output=True)
|
||||
self.assertEqual(result.returncode, 2, result.stderr)
|
||||
@@ -430,6 +469,68 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
self.assertEqual(docker_calls, [])
|
||||
self.assertEqual(trivy_calls, [])
|
||||
|
||||
def test_pinned_asset_api_fallback_downloads_and_verifies_same_archive(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), curl_direct_fail=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(receipt["overall"]["status"], "pass")
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("GitHub release asset API", (evidence / "scanner-api-download.log").read_text())
|
||||
self.assertTrue(any("--download-db-only" in call["args"] for call in trivy_calls))
|
||||
scanner = json.loads((evidence / "scanner.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(scanner["expected_version"], "0.70.0")
|
||||
|
||||
def test_pinned_asset_api_fallback_failure_fails_closed_with_both_logs(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), curl_direct_fail=True, curl_api_fail=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-api-download.log").read_text())
|
||||
self.assertEqual(trivy_calls, [])
|
||||
|
||||
def test_pinned_asset_api_fallback_checksum_mismatch_never_executes_trivy(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), curl_direct_fail=True, api_checksum_mismatch=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("checksum mismatch", result.stderr)
|
||||
self.assertEqual(trivy_calls, [])
|
||||
|
||||
def test_preloaded_pinned_archive_runs_without_network(self):
|
||||
result, root, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), cache_archive="valid", curl_fail=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(receipt["overall"]["status"], "pass")
|
||||
self.assertEqual(
|
||||
(evidence / "scanner-cache-source.log").read_text(),
|
||||
"archive_source=runner-tool-cache\n",
|
||||
)
|
||||
self.assertFalse((evidence / "scanner-download.log").exists())
|
||||
self.assertFalse((evidence / "scanner-api-download.log").exists())
|
||||
cached = root / "runner-tool-cache" / "trivy" / "0.70.0" / "trivy_0.70.0_Linux-64bit.tar.gz"
|
||||
self.assertEqual(cached.read_bytes(), (root / "trivy-fixture.tar.gz").read_bytes())
|
||||
self.assertTrue(any("--download-db-only" in call["args"] for call in trivy_calls))
|
||||
|
||||
def test_preloaded_archive_hash_mismatch_fails_before_network_or_execution(self):
|
||||
result, _, evidence, receipt, _, trivy_calls = self.run_scan(
|
||||
images=(CLEAN_TAG,), cache_archive="bad", curl_fail=True
|
||||
)
|
||||
self.assertEqual(result.returncode, 2)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("checksum mismatch", result.stderr)
|
||||
self.assertEqual(
|
||||
(evidence / "scanner-cache-source.log").read_text(),
|
||||
"archive_source=runner-tool-cache\n",
|
||||
)
|
||||
self.assertFalse((evidence / "scanner-download.log").exists())
|
||||
self.assertFalse((evidence / "scanner-api-download.log").exists())
|
||||
self.assertEqual(trivy_calls, [])
|
||||
|
||||
def test_unknown_image_is_error_and_is_never_scanned(self):
|
||||
missing = "crank/missing:ci"
|
||||
result, _, _, receipt, _, trivy_calls = self.run_scan(images=(missing, CLEAN_TAG))
|
||||
@@ -445,7 +546,8 @@ class ScanImagesWrapperTests(unittest.TestCase):
|
||||
result, _, evidence, receipt, _, _ = self.run_scan(curl_fail=True)
|
||||
self.assertEqual(result.returncode, 2, result.stderr)
|
||||
self.assertEqual(receipt["overall"]["status"], "error")
|
||||
self.assertIn("failed to connect", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-download.log").read_text())
|
||||
self.assertIn("fixture download failure", (evidence / "scanner-api-download.log").read_text())
|
||||
|
||||
def test_evidence_kept_even_when_gate_is_red(self):
|
||||
_, _, evidence, receipt, _, _ = self.run_scan()
|
||||
|
||||
Reference in new issue
Block a user