diff --git a/secure-delivery/drills/Jenkinsfile.staging-drill b/secure-delivery/drills/Jenkinsfile.staging-drill new file mode 100644 index 0000000..48268f7 --- /dev/null +++ b/secure-delivery/drills/Jenkinsfile.staging-drill @@ -0,0 +1,33 @@ +pipeline { + agent { label 'chapter6-trusted' } + options { + disableConcurrentBuilds() + disableRestartFromStage() + skipDefaultCheckout() + timeout(time: 10, unit: 'MINUTES') + } + stages { + stage('Exercise staging startup failure') { + steps { + script { + int result = sh(returnStatus: true, script: 'mkdir -p drill && sudo -n /opt/chapter6-tools/staging-drill fail > drill/failure.txt 2>&1') + echo "Staging drill command exited ${result}" + if (result == 42) { + error('Expected failure verified: wrong-port staging container was unhealthy. Restoring staging in post.') + } + error("Unexpected drill result: ${result}. Inspect the failure and recovery logs.") + } + } + } + } + post { + always { + script { + int restored = sh(returnStatus: true, script: 'mkdir -p drill && sudo -n /opt/chapter6-tools/staging-drill restore > drill/recovery.txt 2>&1') + archiveArtifacts artifacts: 'drill/*.txt', allowEmptyArchive: false + if (restored != 0) { error("Staging restoration failed: ${restored}") } + echo 'Staging restored and verified over HTTPS.' + } + } + } +} diff --git a/secure-delivery/drills/README.md b/secure-delivery/drills/README.md new file mode 100644 index 0000000..6589df6 --- /dev/null +++ b/secure-delivery/drills/README.md @@ -0,0 +1,14 @@ +# Jenkins staging failure drill + +The live controller runs this Pipeline on the restricted Chapter 6 agent. +Its two sudo entries permit only the literal arguments `fail` and `restore` +to the root-owned wrapper at `/opt/chapter6-tools/staging-drill`. +The root-owned deployment record is fixed to v1 staging. +The failure stage uses the ordinary Ansible playbook with the wrong-port +template from this directory. Exit 42 means Ansible rejected the unhealthy +staging container as expected. The Pipeline records FAILURE and its post +action restores and verifies the original v1 staging deployment. + +[Observed Jenkins drill #1](https://ci.165.227.90.215.sslip.io/job/chapter6-staging-drill/1/). +The checkout does not install any sudo rules. Coordinate with the controller +owner for grader access to the archived failure and recovery logs. diff --git a/secure-delivery/drills/compose.yaml.j2 b/secure-delivery/drills/compose.yaml.j2 new file mode 100644 index 0000000..0f58399 --- /dev/null +++ b/secure-delivery/drills/compose.yaml.j2 @@ -0,0 +1,20 @@ +services: + app: + image: "{{ release_image }}" + command: ["gunicorn", "--bind", "0.0.0.0:8001", "app:app"] + restart: unless-stopped + read_only: true + tmpfs: + - /tmp + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + networks: + edge: + aliases: + - "pricing-{{ target_env }}" +networks: + edge: + external: true + name: "{{ proxy_network }}" diff --git a/secure-delivery/drills/staging-drill.sh b/secure-delivery/drills/staging-drill.sh new file mode 100644 index 0000000..cd1bffe --- /dev/null +++ b/secure-delivery/drills/staging-drill.sh @@ -0,0 +1,38 @@ +#!/bin/bash +set -euo pipefail +umask 077 +export PATH=/usr/sbin:/usr/bin:/sbin:/bin +export ANSIBLE_COLLECTIONS_PATH=/opt/chapter6-tools/collections ANSIBLE_NOCOLOR=1 +base=/opt/chapter6-tools +known=$base/v1/secure-delivery/ansible +record=$base/promotions/v1-staging.yml +[[ $# = 1 ]] || exit 64 +exec 9>/run/lock/chapter6-promote.lock +flock -n 9 +run() { + "$base/ansible/bin/ansible-playbook" -i "$known/inventory.ini" "$1" -e ansible_connection=local -e "@$record" +} +date -u +%FT%TZ +case "$1" in + fail) + [[ $(docker inspect -f '{{.State.Health.Status}}' pricing-staging-app-1) = healthy ]] + output=$(mktemp) + trap 'rm -f "$output"' EXIT + rc=0 + run "$base/failure-rehearsal/deploy.yml" >"$output" 2>&1 || rc=$? + cat "$output" + [[ $rc = 2 ]] || exit 65 + grep -q 'pricing-staging-app-1 is unhealthy' "$output" || exit 66 + docker inspect -f '{{json .Config.Cmd}}' pricing-staging-app-1 | grep -q '0.0.0.0:8001' || exit 67 + date -u +%FT%TZ + exit 42 + ;; + restore) + run "$known/deploy.yml" + for path in / /health '/price?quantity=3'; do + curl -fsS --max-time 10 "https://staging.165.227.90.215.sslip.io$path" + done + date -u +%FT%TZ + ;; + *) exit 64 ;; +esac