From ae7cb5101545e8ce0eefad85f093c4a441ba2cb7 Mon Sep 17 00:00:00 2001 From: devppratik Date: Mon, 21 Sep 2026 15:01:33 +0530 Subject: [PATCH 1/2] app-sre/infra: add terraform validate and speculative plan presubmits (APPSRE-14610) Both jobs authenticate to HCP Terraform with a team token from the app-sre-infra-terraform-cloud secret collection. validate: our workloads resolve modules from the private registry at app.terraform.io, which is why 'make validate' could not run in CI before. terraform-plan: backs a single context for both PR sources. Same-repository PRs wait on HCP Terraform's native VCS statuses and propagate them; fork PRs run a CLI-driven speculative plan, which HCP Terraform SaaS refuses to do natively and offers no override for. The planner is read from the base commit rather than the checkout, so a fork cannot modify the code that runs with credentials, and the job exits early if the context is not app-sre/infra. prowgen.disable_sparse_checkout is required: the planner reads the base tree via git show and git grep. Both jobs are gated on run_if_changed for terraform paths. --- .../app-sre/infra/app-sre-infra-main.yaml | 106 ++++++++++ .../infra/app-sre-infra-main-presubmits.yaml | 200 ++++++++++++++++++ 2 files changed, 306 insertions(+) diff --git a/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml b/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml index b4ab29f4375a..3cf95eaef053 100644 --- a/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml +++ b/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml @@ -2,6 +2,7 @@ build_root: project_image: dockerfile_path: ci/build-root/Dockerfile prowgen: + disable_sparse_checkout: true enable_secrets_store_csi_driver: true expose: true private: true @@ -15,6 +16,111 @@ tests: commands: make lint container: from: src +- as: validate + run_if_changed: ^(terraform|hcp-terraform)/ + steps: + test: + - as: validate + commands: | + git config --global url."https://github.com/".insteadOf "git@github.com:" + git config --global credential.helper '!f() { + if [ "$1" = "get" ]; then + host="" proto="" + while IFS= read -r line; do + [ -z "$line" ] && break + case "$line" in + host=*) host="${line#host=}" ;; + protocol=*) proto="${line#protocol=}" ;; + esac + done + if [ "$proto" = "https" ] && [ "$host" = "github.com" ]; then + echo "username=x-access-token" + echo "password=$(cat /etc/github-private/oauth)" + else + exit 1 + fi + fi + }; f' + umask 077 + cat > "$HOME/.terraformrc" < "$HOME/.terraformrc" < Date: Mon, 21 Sep 2026 15:17:53 +0530 Subject: [PATCH 2/2] app-sre/infra: isolate the planner interpreter and trust validate's tooling (APPSRE-14610) Two review findings. Run the base-revision planner with 'python3 -I -'. For 'python3 -' CPython sets sys.path[0] to the working directory, which is the PR checkout, so a pull request adding json.py (or any other stdlib module name) at the repository root would have it imported by the trusted planner and executed before any of the planner's checks ran. That defeated the point of reading the planner from the base commit. Reproduced locally: a planted json.py runs under 'python3 -' and does not under 'python3 -I -'. Restore Makefile and hack/ from the base revision before running validate, and add the same REPO_OWNER/REPO_NAME guard the plan job has. Credentials are mounted for that step, so a pull request could otherwise edit hack/lint.sh to read the Terraform Cloud token. The Terraform configuration under test remains the pull request's. --- .../config/app-sre/infra/app-sre-infra-main.yaml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml b/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml index 3cf95eaef053..d22c23594580 100644 --- a/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml +++ b/ci-operator/config/app-sre/infra/app-sre-infra-main.yaml @@ -49,6 +49,15 @@ tests: TFRC export TF_PLUGIN_CACHE_DIR="${TMPDIR:-/tmp}/terraform-plugin-cache" mkdir -p "${TF_PLUGIN_CACHE_DIR}" + if [ "${REPO_OWNER}/${REPO_NAME}" != "app-sre/infra" ]; then + echo "Skipping validate: job context is not app-sre/infra" + exit 0 + fi + # Credentials are mounted here, so the tooling that drives Terraform is + # restored from the trusted base revision. Without this a pull request + # could simply edit hack/lint.sh or the Makefile to read the token. The + # Terraform configuration under test stays as the PR wrote it. + git checkout "${PULL_BASE_SHA}" -- Makefile hack/ make validate credentials: - as: oauth @@ -106,7 +115,12 @@ tests: exit 0 fi set -o pipefail - git show "${PULL_BASE_SHA}:hack/tfc-speculative-plan.py" | python3 - + # -I (isolated) is required, not cosmetic: for `python3 -`, CPython sets + # sys.path[0] to the working directory, which is the PR checkout. A + # PR-added json.py (or any other stdlib name) at the repository root + # would then be imported by the trusted planner and execute before any + # of its checks ran. + git show "${PULL_BASE_SHA}:hack/tfc-speculative-plan.py" | python3 -I - credentials: - as: oauth collection: test-platform-infra