Skip to content

Commit 3d1d79b

Browse files
authored
Merge pull request #9 from staticfloat/sf/improved_blocking
Improve blocking with `depends_on` and some sanity checks!
2 parents 3869724 + 53b54bd commit 3d1d79b

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

hooks/post-command

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ done
3434
SHOULD_FAIL=false
3535
for PIPELINE_IDX in "${!SIGNED_PIPELINES[@]}"; do
3636
PIPELINE_PATH="${SIGNED_PIPELINES[${PIPELINE_IDX}]}"
37+
SANITIZED_PIPELINE_PATH="$(basename "${PIPELINE_PATH}" | tr '/' '-' | tr '.' '-' | tr ' ' '_')"
38+
39+
# Perform sanity checks such as ensuring that this pipeline will receive the key
40+
if [[ -z "$(grep "BUILDKITE_PLUGIN_CRYPTIC_BASE64_SIGNED_JOB_ID_SECRET" "${PIPELINE_PATH}")" ]]; then
41+
(die "Pipeline ${PIPELINE_PATH} does not contain an env mapping for BUILDKITE_PLUGIN_CRYPTIC_BASE64_SIGNED_JOB_ID_SECRET!"; ) || true
42+
SHOULD_FAIL=true
43+
continue
44+
fi
3745

3846
# Hash up the inputs
3947
readarray -d '' -t PIPELINE_INPUTS < <(collect_buildkite_array "BUILDKITE_PLUGIN_CRYPTIC_SIGNED_PIPELINES_${PIPELINE_IDX}_INPUTS")
@@ -66,12 +74,31 @@ for PIPELINE_IDX in "${!SIGNED_PIPELINES[@]}"; do
6674
base64dec <<<"${!SIGNATURE_VAR}" >"${SIGNATURE_FILE}"
6775
fi
6876
if [[ "$(decrypt_aes "${UNENCRYPTED_REPO_KEY_PATH}" <"${SIGNATURE_FILE}")" != "${FULL_TREEHASH}" ]]; then
69-
echo "Pipeline '${PIPELINE_PATH}' fails treehash siganture check! You may need to re-run cryptic/bin/sign_treehashes!"
77+
SIGNATURE_FAIL_MSG="Pipeline '${PIPELINE_PATH}' fails treehash siganture check! You may need to re-run cryptic/bin/sign_treehashes!"
78+
echo "${SIGNATURE_FAIL_MSG}" >&2
7079

7180
HASH_OVERRIDE_VAR="BUILDKITE_PLUGIN_CRYPTIC_SIGNED_PIPELINES_${PIPELINE_IDX}_ALLOW_HASH_OVERRIDE"
7281
if [[ -v "${HASH_OVERRIDE_VAR}" ]] && [[ "${!HASH_OVERRIDE_VAR}" == "true" ]]; then
7382
# If we allow committers to override the failing hash check, create a `block` step, then still launch it.
74-
cat "${PIPELINE_PATH}" | sed -e "s&^steps:\(.*\)&steps:\\1\\n - block: \"Bypass failed signature check for '${PIPELINE_PATH}'?\"\\n blocked_state: \"running\"\\n&" > "${PIPELINE_PATH}.block"
83+
# To do so, we require each of the pipeline's steps to contain a `depends_on` node:
84+
NUM_STEPS=$( (grep -E "^ - " "${PIPELINE_PATH}" || true) | wc -l)
85+
NUM_DEPENDS_ON=$( (grep -E "^ depends_on:" "${PIPELINE_PATH}" || true) | wc -l)
86+
if [[ "${NUM_DEPENDS_ON}" -lt "${NUM_STEPS}" ]]; then
87+
(die "Refusing to continue execution; pipeline '${PIPELINE_PATH}' looks like it lacks some 'depends_on' nodes!"; ) || true
88+
SHOULD_FAIL=true
89+
fi
90+
91+
# Notify the user that they probably need to re-sign something
92+
BLOCK_KEY="cryptic-block-${SANITIZED_PIPELINE_PATH}"
93+
buildkite-agent annotate --style=warning --context="${BLOCK_KEY}" "${SIGNATURE_FAIL_MSG}"
94+
95+
cat "${PIPELINE_PATH}" |
96+
# Insert a block step as the first step in this pipeline
97+
sed -e "s&^steps:\(.*\)&steps:\\1\n - block: \"Bypass failed signature check for '${PIPELINE_PATH}'?\"\n blocked_state: \"running\"\n key: \"${BLOCK_KEY}\"&" |
98+
# Each other step in the secure pipeline _must_ have a `depends_on`, which we then add to:
99+
sed -e "s&^ depends_on:& depends_on:\n - \"${BLOCK_KEY}\"\n&" > "${PIPELINE_PATH}.block"
100+
echo "Printing out altered pipeline:"
101+
cat "${PIPELINE_PATH}.block"
75102
PIPELINE_PATH="${PIPELINE_PATH}.block"
76103
else
77104
# Execute `die` in a subshell so that we can print out failure messages for each pipeline,
@@ -82,7 +109,7 @@ for PIPELINE_IDX in "${!SIGNED_PIPELINES[@]}"; do
82109
fi
83110
fi
84111

85-
# If we passed, launch the pipeline!
112+
# If we passed, try to launch the pipeline!
86113
echo " -> Launching ${PIPELINE_PATH}"
87114
buildkite-agent pipeline upload "${PIPELINE_PATH}"
88115
done

0 commit comments

Comments
 (0)