From 0d53a258381453c5488856b83751bab922f33656 Mon Sep 17 00:00:00 2001 From: driazati Date: Thu, 25 Aug 2022 11:09:21 -0700 Subject: [PATCH] [ci] Add mechanism for trust on certain CI scripts This makes it so changes to certain files from users not listed in `CONTRIBUTING.md` are not tested in CI. This is necessary since these scripts run on the baremetal EC2 instances and not inside Docker containers, so they can affect other builds and potentially grab Jenkins secrets. This checks out the version from the upstream for the listed files after running `git checkout`. --- Jenkinsfile | 27 ++++++++++++++++++++++++++- ci/jenkins/Prepare.groovy.j2 | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index 1b615e38304c..50eee01fa974 100755 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -45,7 +45,7 @@ // 'python3 jenkins/generate.py' // Note: This timestamp is here to ensure that updates to the Jenkinsfile are // always rebased on main before merging: -// Generated at 2022-08-26T15:48:19.597592 +// Generated at 2022-08-30T11:58:06.036509 import org.jenkinsci.plugins.pipeline.modeldefinition.Utils // NOTE: these lines are scanned by docker/dev_common.sh. Please update the regex as needed. --> @@ -169,6 +169,7 @@ def init_git() { """, label: 'Update git submodules', ) + checkout_trusted_files() } def docker_init(image) { @@ -248,6 +249,30 @@ def cancel_previous_build() { } } +def checkout_trusted_files() { + // trust everything from branch builds + if (!env.BRANCH_NAME.startsWith('PR-')) { + return; + } + + // trust peoople listed in CONTRIBUTING.md + grep_code = sh( + returnStatus: true, + script: "git show '${upstream_revision}:CONTRIBUTORS.md' | grep '@${env.CHANGE_AUTHOR}'", + label: 'Check if change is from a contributor', + ) + + if (grep_code == 1) { + // Any scripts that run on the bare host and not inside a Docker container + // (especially those that access secrets) should be checked out here so + // only trusted versions are used in CI + sh( + script: "git checkout ${upstream_revision} ci/scripts/.", + label: 'Check out trusted files', + ) + } +} + def should_skip_ci(pr_number) { if (env.BRANCH_NAME == null || !env.BRANCH_NAME.startsWith('PR-')) { // never skip CI on build sourced from a branch diff --git a/ci/jenkins/Prepare.groovy.j2 b/ci/jenkins/Prepare.groovy.j2 index 404d2870c9e2..94575a7b4b64 100644 --- a/ci/jenkins/Prepare.groovy.j2 +++ b/ci/jenkins/Prepare.groovy.j2 @@ -38,6 +38,7 @@ def init_git() { """, label: 'Update git submodules', ) + checkout_trusted_files() } def docker_init(image) { @@ -98,6 +99,30 @@ def cancel_previous_build() { } } +def checkout_trusted_files() { + // trust everything from branch builds + if (!env.BRANCH_NAME.startsWith('PR-')) { + return; + } + + // trust peoople listed in CONTRIBUTING.md + grep_code = sh( + returnStatus: true, + script: "git show '${upstream_revision}:CONTRIBUTORS.md' | grep '@${env.CHANGE_AUTHOR}'", + label: 'Check if change is from a contributor', + ) + + if (grep_code == 1) { + // Any scripts that run on the bare host and not inside a Docker container + // (especially those that access secrets) should be checked out here so + // only trusted versions are used in CI + sh( + script: "git checkout ${upstream_revision} ci/scripts/.", + label: 'Check out trusted files', + ) + } +} + def should_skip_ci(pr_number) { if (env.BRANCH_NAME == null || !env.BRANCH_NAME.startsWith('PR-')) { // never skip CI on build sourced from a branch