From 4110aa54c91a61353a5f17119dff548c3fec6ba9 Mon Sep 17 00:00:00 2001 From: Codex Agent Date: Mon, 12 Oct 2026 01:41:20 +0300 Subject: [PATCH] Make runner setup race-safe and resumable --- CHANGELOG.md | 2 +- docs/operations/CI_RUNNER.md | 12 +++++--- docs/operations/CURRENT_STATE.md | 6 ++-- infra/runner/install-runner-lib.sh | 26 +++++++++++++---- infra/runner/install-runner.sh | 4 +-- .../runner/test-install-runner-idempotency.sh | 28 +++++++++++++++++++ infra/runner/test-runner-config.sh | 2 ++ 7 files changed, 66 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68dd73f..662f80e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Не выпущено -- Runner installer отклоняет symlink-компоненты в путях конфигурации и повторно использует хранилище только при активных mount-опциях `nosuid,nodev`. +- Runner installer безопасно повторяет настройку каталогов от имени runner, включая восстановление промежуточных каталогов `0755`, и принимает хранилище только с активными mount-опциями `nosuid,nodev`. - Backend `0.11.3`: ключ подписи поисковых снимков читается из Docker secret-файла, а не из аргументов процесса. Добавлена базовая инфраструктура VPS, Caddy HTTPS proxy с разделёнными Compose-сетями и CI-проверка конфигурации; публикация образов и production-деплой остаются выключены до проверки runner, registry, backup/restore и ограниченного deploy-helper. - Frontend `0.1.2`: на планшетной ширине экран поиска переходит в одну колонку, чтобы фильтры, результаты и документ помещались в окно. - Backend `0.11.2`: смена пароля требует текущий пароль и запрещает оставить прежний, включая временный пароль при первом входе. diff --git a/docs/operations/CI_RUNNER.md b/docs/operations/CI_RUNNER.md index f178a1d..19bd1ea 100644 --- a/docs/operations/CI_RUNNER.md +++ b/docs/operations/CI_RUNNER.md @@ -57,10 +57,14 @@ a non-root system UID below `UID_MIN`, its expected home, primary group, and sole supplementary group. The mount is started only when it is not already active, and an active mount must be the expected ext4 loop device backed by the runner image with both `nosuid` and -`nodev` active. Before root creates runner configuration directories, it checks -every path component for symlinks and validates the existing runner-owned -directories' ownership and mode. Conflicting files or state stop the installer -for manual inspection. A new image requires at least 25 GiB free on `/`. +`nodev` active. Runner-home directories and user configuration files are +created or normalized as `akyldash-runner`, never by a privileged file-write +operation; this avoids a symlink swap turning setup into a root write outside +the home. The installer rejects static symlink components and validates +ownership and mode after setup. This also normalizes the `0755` intermediate +directories left by the first installer version. Conflicting files or state +stop the installer for manual inspection. A new image requires at least 25 GiB +free on `/`. If the reviewed checkout is not present at the inventoried path, first verify that the target is absent and clone the reviewed `main` there. Do not overwrite diff --git a/docs/operations/CURRENT_STATE.md b/docs/operations/CURRENT_STATE.md index e13dcf1..4dfe553 100644 --- a/docs/operations/CURRENT_STATE.md +++ b/docs/operations/CURRENT_STATE.md @@ -90,8 +90,10 @@ UFW rule details, and protected file metadata remain unverified. loop-backed store is active on `/dev/loop0` after manually enabling and starting its mount unit when the installer stopped. Remaining components, repository registration, and Online state are unverified; this PR makes no - VPS changes. The reviewed installer now rejects symlinks in runner config - directory paths and requires the active store mount to include `nosuid,nodev`. + VPS changes. The reviewed installer now performs runner-home directory and + config writes as `akyldash-runner` (including recovery from legacy `0755` + intermediate directories), and requires active store mounts to include + `nosuid,nodev`. - Gitea repository runner inventory could not be read by the current API principal (403). The owner has not supplied runner state, and no runner Online status or runner-hosted workflow is claimed. The manual diff --git a/infra/runner/install-runner-lib.sh b/infra/runner/install-runner-lib.sh index ad6ef9b..8dc516e 100644 --- a/infra/runner/install-runner-lib.sh +++ b/infra/runner/install-runner-lib.sh @@ -72,15 +72,31 @@ runner_ensure_private_directory() { fi [[ -d $current ]] || { runner_die "runner directory path component is not a directory: $current"; return 1; } if [[ $current == /var/lib/akyldash-runner || $current == /var/lib/akyldash-runner/* ]]; then - [[ $(stat -c '%U:%G:%a' "$current") == "$owner:$group:700" ]] || { runner_die "runner directory ancestor has unexpected ownership or permissions: $current"; return 1; } + [[ $(stat -c '%U:%G' "$current") == "$owner:$group" ]] || { runner_die "runner directory ancestor has unexpected ownership: $current"; return 1; } fi done - if [[ -e $directory ]]; then - [[ $(stat -c '%U:%G:%a' "$directory") == "$owner:$group:700" ]] || { runner_die "runner directory has unexpected ownership or permissions: $directory"; return 1; } + # These paths are runner-writable; make changes as that user so a path swap + # cannot turn a root operation into a write outside its home. + runuser -u "$owner" -- install -d -m 0700 "$directory" || return 1 + [[ -d $directory && ! -L $directory && $(stat -c '%U:%G:%a' "$directory") == "$owner:$group:700" ]] || { runner_die "runner directory failed validation: $directory"; return 1; } +} + +runner_install_user_content() { + local destination=$1 owner=$2 group=$3 mode=${4#0} temporary + temporary=$(mktemp) + cat >"$temporary" + if [[ -e $destination || -L $destination ]]; then + [[ -f $destination && ! -L $destination ]] && cmp -s "$temporary" "$destination" || { rm -f "$temporary"; runner_die "existing runner file differs from reviewed configuration: $destination"; return 1; } + [[ $(stat -c '%U:%G:%a' "$destination") == "$owner:$group:$mode" ]] || { rm -f "$temporary"; runner_die "existing runner file has unexpected ownership or permissions: $destination"; return 1; } + rm -f "$temporary" return fi - install -d -o "$owner" -g "$group" -m 0700 "$directory" || return 1 - [[ -d $directory && ! -L $directory && $(stat -c '%U:%G:%a' "$directory") == "$owner:$group:700" ]] || { runner_die "runner directory failed validation: $directory"; return 1; } + chown "$owner:$group" "$temporary" && chmod "0$mode" "$temporary" || { rm -f "$temporary"; return 1; } + local result=0 + runuser -u "$owner" -- install -m "0$mode" "$temporary" "$destination" || result=$? + rm -f "$temporary" + ((result == 0)) || return 1 + [[ -f $destination && ! -L $destination && $(stat -c '%U:%G:%a' "$destination") == "$owner:$group:$mode" ]] || { runner_die "runner file failed validation: $destination"; return 1; } } runner_validate_mount() { diff --git a/infra/runner/install-runner.sh b/infra/runner/install-runner.sh index 3c0282e..488d56d 100644 --- a/infra/runner/install-runner.sh +++ b/infra/runner/install-runner.sh @@ -113,14 +113,14 @@ else trap - EXIT fi -runner_install_content "$runner_home/.config/docker/daemon.json" "$runner_user" "$runner_user" 0600 </dev/null +{"data-root":"/unexpected"} +EOF +then + echo 'Conflicting runner user config was accepted' >&2 + exit 1 +fi + image=$tmp/docker-store.img runner_ensure_image "$image" 1M 1048576 runner_ensure_image "$image" 1M 1048576 diff --git a/infra/runner/test-runner-config.sh b/infra/runner/test-runner-config.sh index f7d9f51..b18a339 100644 --- a/infra/runner/test-runner-config.sh +++ b/infra/runner/test-runner-config.sh @@ -22,6 +22,8 @@ grep -Fq 'fallocate -l "$size" "$image"' infra/runner/install-runner-lib.sh grep -Fq 'mkfs.ext4 -F -q "$image"' infra/runner/install-runner-lib.sh grep -Fq 'systemctl enable "$unit_name"' infra/runner/install-runner-lib.sh grep -Fq 'systemctl start "$unit_name"' infra/runner/install-runner-lib.sh +grep -Fq 'runuser -u "$owner" -- install -d -m 0700 "$directory"' infra/runner/install-runner-lib.sh +grep -Fq 'runner_install_user_content "$runner_home/.config/docker/daemon.json"' "$installer" assert_absent 'basename.*runner_mount_unit|enable --now.*runner_mount_unit' "$installer" grep -Fq 'groups=$(id -nG "$user")' infra/runner/install-runner-lib.sh grep -Fq 'id -gn "$user"' infra/runner/install-runner-lib.sh