diff --git a/.github/workflows/devsecops.yml b/.github/workflows/devsecops.yml index d0d0807..ecc5e7e 100644 --- a/.github/workflows/devsecops.yml +++ b/.github/workflows/devsecops.yml @@ -208,44 +208,10 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} - # ======================================================================== - # Update the deployment manifest with new image tag - # This triggers ArgoCD to sync the new version automatically - # ======================================================================== - - name: Update deployment manifest + - name: Publish the scanned image to the current main manifest env: DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} - run: | - NEW_IMAGE="${DOCKERHUB_USERNAME}/governor:${{ github.sha }}" - echo "📝 Updating deployment to: ${NEW_IMAGE}" - - # Use sed to update the image tag in deployment.yaml - sed -i "s|image: .*governor:.*|image: ${NEW_IMAGE}|g" manifests/deployment.yaml - - # Show the change - grep "image:" manifests/deployment.yaml - - - name: Commit and push changes - run: | - git config user.name "GitHub Actions Bot" - git config user.email "actions@github.com" - - git add manifests/deployment.yaml - - # Check if there are changes to commit - if git diff --staged --quiet; then - echo "✅ No changes to commit - manifest already up to date" - exit 0 - fi - - # Commit and push with retry logic - git commit -m "🚀 Deploy: Update image to ${{ github.sha }}" - - # Pull latest changes in case of race condition, then push - git pull --rebase origin main - git push origin main - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: bash scripts/publish-gitops-image.sh "$GITHUB_SHA" "$DOCKERHUB_USERNAME" validate-repo: name: Validate Repo Artifacts @@ -267,11 +233,16 @@ jobs: run: | bash -n chaos_monkey.sh bash -n demo-chaos.sh + bash -n scripts/publish-gitops-image.sh + bash -n scripts/test-gitops-publication.sh bash -n scripts/capture_incident_snapshot.sh bash -n scripts/remediate_pod_crash.sh bash -n scripts/remediate_high_latency.sh bash -n scripts/remediate_deployment_rollback.sh + - name: Test GitOps publication against disposable Git repositories + run: bash scripts/test-gitops-publication.sh + - name: Validate Python scripts run: | python3 -m py_compile scripts/generate_incident_report.py diff --git a/scripts/publish-gitops-image.sh b/scripts/publish-gitops-image.sh new file mode 100644 index 0000000..cafcf8d --- /dev/null +++ b/scripts/publish-gitops-image.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# Publish a scanned image to the current main manifest without rebasing stale work. +set -euo pipefail + +source_sha=$1 +registry_namespace=$2 +[[ "$source_sha" =~ ^[0-9a-f]{40}$ ]] || { echo "Invalid source commit" >&2; exit 2; } +[[ "$registry_namespace" =~ ^[a-z0-9][a-z0-9_-]*$ ]] || { echo "Invalid registry namespace" >&2; exit 2; } +cd "$(git rev-parse --show-toplevel)" +[[ -z "$(git status --porcelain)" ]] || { echo "GitOps publisher needs a clean checkout" >&2; exit 1; } + +refresh_main() { + git fetch --no-tags origin '+refs/heads/main:refs/remotes/origin/main' >&2 && + git rev-parse --verify refs/remotes/origin/main +} + +remote_tip=$(refresh_main) +if [[ "$remote_tip" != "$source_sha" ]]; then + echo "Superseded: main is $remote_tip; not deploying $source_sha" + exit 0 +fi + +git switch --detach "$source_sha" +manifest=manifests/deployment.yaml +pattern='^[[:space:]]+image:[[:space:]]+[^[:space:]]+/governor:[^[:space:]]+[[:space:]]*$' +image_count=$(awk -v image_pattern="$pattern" '$0 ~ image_pattern {count++} END {print count+0}' "$manifest") +[[ "$image_count" == 1 ]] || { echo "Expected exactly one Governor image in $manifest; found $image_count" >&2; exit 1; } +new_image="$registry_namespace/governor:$source_sha" +sed -E "s|^([[:space:]]+image:[[:space:]]+)[^[:space:]]+/governor:[^[:space:]]+[[:space:]]*$|\\1$new_image|" "$manifest" > "$manifest.tmp" +mv "$manifest.tmp" "$manifest" +grep -Eq "$pattern" "$manifest" +if git diff --quiet -- "$manifest"; then + echo "Already current: $new_image" + exit 0 +fi + +git add -- "$manifest" +git -c user.name='GitHub Actions Bot' -c user.email='actions@github.com' commit -m "Deploy: Update Governor image to $source_sha" + +# Only a regular fast-forward push is allowed. If someone moved main while this +# job prepared its commit, never rebase the old manifest onto their newer work. +if git push origin HEAD:refs/heads/main; then + echo "Published $new_image" + exit 0 +fi +remote_tip=$(refresh_main) +if [[ "$remote_tip" != "$source_sha" ]]; then + echo "Superseded during publication: main is $remote_tip; not overwriting it" + exit 0 +fi +echo "Failed to publish $new_image although main is unchanged" >&2 +exit 1 diff --git a/scripts/test-gitops-publication.sh b/scripts/test-gitops-publication.sh new file mode 100644 index 0000000..5f2ad33 --- /dev/null +++ b/scripts/test-gitops-publication.sh @@ -0,0 +1,82 @@ +#!/usr/bin/env bash +set -euo pipefail + +root=$(cd "$(dirname "$0")/.." && pwd) +publisher="$root/scripts/publish-gitops-image.sh" +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +export GIT_CONFIG_NOSYSTEM=1 +export GIT_CONFIG_GLOBAL=/dev/null +export GIT_AUTHOR_NAME='Governor test' GIT_AUTHOR_EMAIL='test@example.invalid' +export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL" +remote="$work/remote.git" +alpha="$work/alpha" +beta="$work/beta" +git init -q --initial-branch=main --bare "$remote" +git clone -q "$remote" "$alpha" +mkdir -p "$alpha/manifests" +cat > "$alpha/manifests/deployment.yaml" <<'YAML' +spec: + template: + spec: + containers: + - name: governor + image: test-image/governor:prior + - name: metrics + image: test-image/metrics:unchanged +YAML +git -C "$alpha" add . +git -C "$alpha" commit -qm 'Initial app source' +git -C "$alpha" push -q origin main +source_sha=$(git -C "$alpha" rev-parse HEAD) +push_candidate() { (cd "$alpha" && bash "$publisher" "$1" test-image); } +remote_main() { git --git-dir="$remote" rev-parse main; } +remote_manifest() { git --git-dir="$remote" show main:manifests/deployment.yaml; } + +# Deploy exactly the scanned SHA and preserve the unrelated image. +push_candidate "$source_sha" +published=$(remote_main) +[[ "$published" != "$source_sha" ]] +[[ "$(git --git-dir="$remote" rev-parse main^)" == "$source_sha" ]] +grep -q "image: test-image/governor:$source_sha" <<<"$(remote_manifest)" +grep -q 'image: test-image/metrics:unchanged' <<<"$(remote_manifest)" +push_candidate "$source_sha" +[[ "$(remote_main)" == "$published" ]] + +# A run whose source is already superseded cannot roll the manifest back. +git clone -q "$remote" "$beta" +git -C "$beta" commit -q --allow-empty -m 'Newer source' +git -C "$beta" push -q origin main +newer=$(remote_main) +push_candidate "$source_sha" +[[ "$(remote_main)" == "$newer" ]] +grep -q "image: test-image/governor:$source_sha" <<<"$(remote_manifest)" + +# Simulate main moving between fetch/check and push under the exact same +# non-fast-forward condition as the real GitOps failure. +git -C "$alpha" fetch -q origin main +git -C "$alpha" switch -q --detach "$newer" +git -C "$beta" commit -q --allow-empty -m 'Concurrent winner' +winner=$(git -C "$beta" rev-parse HEAD) +export GOVERNOR_RACE_REPO="$beta" +cat > "$alpha/.git/hooks/pre-push" <<'HOOK' +#!/usr/bin/env bash +set -euo pipefail +git -C "$GOVERNOR_RACE_REPO" push -q origin main +HOOK +chmod +x "$alpha/.git/hooks/pre-push" +push_candidate "$newer" +[[ "$(remote_main)" == "$winner" ]] +grep -q "image: test-image/governor:$source_sha" <<<"$(remote_manifest)" + +# If the remote did not move, do not disguise other push failures as races. +cat > "$alpha/.git/hooks/pre-push" <<'HOOK' +#!/usr/bin/env bash +exit 1 +HOOK +if push_candidate "$winner"; then + echo "Unrelated push failure was not propagated" >&2 + exit 1 +fi +[[ "$(remote_main)" == "$winner" ]] +echo "GitOps publication tests passed"