Files
akyldash/infra/deploy/install-helper.sh

142 lines
8.8 KiB
Bash

#!/usr/bin/env bash
set -euo pipefail
if [[ $EUID -ne 0 ]]; then
echo "Run this installer as root." >&2
exit 1
fi
checkout=/opt/akyldash/current
rollback_source=$checkout/infra/deploy/rollback.py
release_refs_source=$checkout/infra/deploy/release_refs.py
release_state_source=$checkout/infra/deploy/release_state.py
deployment_lock_source=$checkout/infra/deploy/deployment_lock.py
backup_source=$checkout/infra/backup/archive.py
verify_source=$checkout/infra/bootstrap/verify-helper.sh
compose_source=$checkout/infra/deploy/compose-helper.sh
release_deploy_source=$checkout/infra/deploy/akyldash-release-deploy.sh
release_rollback_source=$checkout/infra/deploy/akyldash-release-rollback.sh
release_dispatch_source=$checkout/infra/deploy/akyldash-release-dispatch.sh
release_controller_source=$checkout/infra/deploy/external_deploy.py
release_status_source=$checkout/infra/deploy/external_status.py
gitea_helper_fixture=$checkout/infra/deploy/gitea-api-base.fixture
gitea_helper_patch=$checkout/infra/deploy/gitea-api-binary-download.patch
release_policy_source=$checkout/infra/deploy/release-policy.json
if [[ ! -d $checkout/.git || ! -f $rollback_source || ! -f $release_refs_source || ! -f $release_state_source || ! -f $deployment_lock_source || ! -f $backup_source || ! -f $verify_source || ! -f $compose_source || ! -f $release_deploy_source || ! -f $release_rollback_source || ! -f $release_dispatch_source || ! -f $release_controller_source || ! -f $release_status_source || ! -f "$checkout/infra/deploy/external_rollback.py" || ! -f "$checkout/infra/backup/external_session.py" || ! -f "$checkout/infra/deploy/create_release_manifest.py" || ! -f "$checkout/infra/deploy/deploy_release.py" || ! -f "$checkout/infra/deploy/release_handoff.py" || ! -f "$checkout/infra/deploy/release_source.py" || ! -f "$checkout/infra/deploy/validate_release.py" || ! -f $gitea_helper_fixture || ! -f $gitea_helper_patch || ! -f $release_policy_source ]]; then
echo "Install the approved repository checkout at $checkout first." >&2
exit 1
fi
if [[ -L $checkout ]]; then
echo "The checkout path must not be a symlink." >&2
exit 1
fi
for directory in /opt /opt/akyldash; do
mode=$(stat -c %a "$directory")
if [[ $(stat -c %U "$directory") != root ]] || (( (8#$mode & 022) != 0 )); then
echo "The checkout parent $directory must be root-owned and not group/world-writable." >&2
exit 1
fi
done
if ! id codex >/dev/null 2>&1 || id -nG codex | tr ' ' '\n' | grep -Eq '^(sudo|docker)$'; then
echo "Create codex without sudo or Docker-group membership before installing the helper." >&2
exit 1
fi
if grep -R -nE '(^|[[:space:],])codex([[:space:],]|$)' /etc/sudoers /etc/sudoers.d --exclude=akyldash-codex; then
echo "Existing codex sudoers rules need owner review before installing the helper." >&2
exit 1
fi
unsafe=$(find "$checkout" -xdev \( ! -user root -o -perm /022 \) -print -quit)
if [[ $(stat -c %U "$checkout") != root || -n $unsafe ]]; then
echo "The checkout must be root-owned and not group/world-writable." >&2
exit 1
fi
if ! command -v visudo >/dev/null 2>&1; then
echo "visudo is required to validate the restricted sudoers rule." >&2
exit 1
fi
if ! command -v patch >/dev/null 2>&1 || ! command -v sha256sum >/dev/null 2>&1; then
echo "patch and sha256sum are required to install the reviewed binary-safe Gitea helper." >&2
exit 1
fi
bash -n "$release_deploy_source" "$release_rollback_source" "$release_dispatch_source"
for path in /usr/local/sbin/akyldash-verify /usr/local/sbin/akyldash-rollback /usr/local/sbin/akyldash-compose /usr/local/sbin/akyldash-release-deploy /usr/local/sbin/akyldash-release-rollback /usr/local/sbin/akyldash-release-dispatch /usr/local/lib/akyldash/archive.py /usr/local/lib/akyldash/external_session.py /usr/local/lib/akyldash/create_release_manifest.py /usr/local/lib/akyldash/deploy_release.py /usr/local/lib/akyldash/deployment_lock.py /usr/local/lib/akyldash/external_deploy.py /usr/local/lib/akyldash/external_status.py /usr/local/lib/akyldash/external_rollback.py /usr/local/lib/akyldash/rollback.py /usr/local/lib/akyldash/release_handoff.py /usr/local/lib/akyldash/release_refs.py /usr/local/lib/akyldash/release_source.py /usr/local/lib/akyldash/release_state.py /usr/local/lib/akyldash/validate_release.py /usr/local/lib/akyldash/gitea-api /etc/akyldash/release-policy.json /etc/sudoers.d/akyldash-codex; do
if [[ -L $path ]]; then
echo "Refusing symlink at $path." >&2
exit 1
fi
done
install -d -o root -g root -m 0755 /usr/local/lib/akyldash
install -o root -g root -m 0644 "$backup_source" /usr/local/lib/akyldash/archive.py
install -o root -g root -m 0644 "$release_refs_source" /usr/local/lib/akyldash/release_refs.py
install -o root -g root -m 0644 "$release_state_source" /usr/local/lib/akyldash/release_state.py
install -o root -g root -m 0644 "$deployment_lock_source" /usr/local/lib/akyldash/deployment_lock.py
install -o root -g root -m 0644 "$checkout/infra/backup/external_session.py" /usr/local/lib/akyldash/external_session.py
install -o root -g root -m 0644 "$checkout/infra/deploy/create_release_manifest.py" /usr/local/lib/akyldash/create_release_manifest.py
install -o root -g root -m 0644 "$checkout/infra/deploy/deploy_release.py" /usr/local/lib/akyldash/deploy_release.py
install -o root -g root -m 0644 "$rollback_source" /usr/local/lib/akyldash/rollback.py
install -o root -g root -m 0644 "$checkout/infra/deploy/external_deploy.py" /usr/local/lib/akyldash/external_deploy.py
install -o root -g root -m 0644 "$release_status_source" /usr/local/lib/akyldash/external_status.py
install -o root -g root -m 0644 "$checkout/infra/deploy/external_rollback.py" /usr/local/lib/akyldash/external_rollback.py
install -o root -g root -m 0644 "$checkout/infra/deploy/release_handoff.py" /usr/local/lib/akyldash/release_handoff.py
install -o root -g root -m 0644 "$checkout/infra/deploy/release_source.py" /usr/local/lib/akyldash/release_source.py
install -o root -g root -m 0644 "$checkout/infra/deploy/validate_release.py" /usr/local/lib/akyldash/validate_release.py
if [[ -e /etc/akyldash/release-policy.json ]]; then
if [[ -L /etc/akyldash/release-policy.json || $(stat -c %U:%a /etc/akyldash/release-policy.json) != root:600 || ! -f /etc/akyldash/release-policy.json || $(cat /etc/akyldash/release-policy.json) != "$(cat "$release_policy_source")" ]]; then
echo "The root-owned release policy exists with unexpected contents; review it manually." >&2
exit 1
fi
else
install -o root -g root -m 0600 "$release_policy_source" /etc/akyldash/release-policy.json
fi
gitea_tmp=$(mktemp -d /run/akyldash-gitea-api.XXXXXX)
trap 'rm -rf -- "$gitea_tmp"' EXIT
cp "$gitea_helper_fixture" "$gitea_tmp/gitea-api"
if [[ $(sha256sum "$gitea_tmp/gitea-api" | cut -d ' ' -f 1) != 8227332d28e27bcce9b4e3801f6b8d4323636174a82c93a0177970f560629df0 ]]; then
echo "The inspected base gitea-api helper changed; review a new patch before installing." >&2
exit 1
fi
patch --batch --fuzz=0 -p1 -d "$gitea_tmp" < "$gitea_helper_patch"
bash -n "$gitea_tmp/gitea-api"
install -o root -g root -m 0750 "$gitea_tmp/gitea-api" /usr/local/lib/akyldash/gitea-api
rm -rf -- "$gitea_tmp"
install -o root -g root -m 0750 "$verify_source" /usr/local/sbin/akyldash-verify
install -o root -g root -m 0750 "$rollback_source" /usr/local/sbin/akyldash-rollback
install -o root -g root -m 0750 "$compose_source" /usr/local/sbin/akyldash-compose
install -o root -g root -m 0750 "$release_deploy_source" /usr/local/sbin/akyldash-release-deploy
install -o root -g root -m 0750 "$release_rollback_source" /usr/local/sbin/akyldash-release-rollback
install -o root -g root -m 0750 "$release_dispatch_source" /usr/local/sbin/akyldash-release-dispatch
sudoers_file=/etc/sudoers.d/akyldash-codex
expected=$'codex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-verify\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-rollback *\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-compose config\ncodex ALL=(root) NOPASSWD: /usr/local/sbin/akyldash-compose status'
created=false
if [[ -e $sudoers_file ]]; then
if [[ $(stat -c %U:%a "$sudoers_file") != root:440 || $(cat "$sudoers_file") != "$expected" ]]; then
echo "$sudoers_file exists with unexpected contents or permissions; review manually." >&2
exit 1
fi
else
temp=$(mktemp /etc/sudoers.d/.akyldash-codex.XXXXXX)
trap 'rm -f "$temp"' EXIT
printf '%s\n' "$expected" > "$temp"
chmod 0440 "$temp"
visudo -cf "$temp"
visudo -c -f /etc/sudoers
mv -n "$temp" "$sudoers_file"
if [[ $(cat "$sudoers_file") != "$expected" ]]; then
echo "Could not install the expected sudoers rule." >&2
exit 1
fi
created=true
fi
if ! visudo -c -f /etc/sudoers; then
if [[ $created == true ]]; then
rm -f "$sudoers_file"
fi
echo "Sudoers validation failed; the newly added rule was removed." >&2
exit 1
fi
echo "Installed fixed read-only verification/Compose commands and guarded rollback; codex receives no general sudo or Docker access."