From b44018a79db6f790b59d8727a28224f791f8ffd1 Mon Sep 17 00:00:00 2001 From: jesus-ixatu Date: Wed, 11 Nov 2026 11:34:09 +0100 Subject: [PATCH] feat(update): maintain Azure CLI via official apt source (#28) --- docs/UPDATE.md | 16 ++- scripts/check-azure-tools.sh | 28 +++-- scripts/update/update-wsl.sh | 10 ++ tests/bats/system/azure-tools.bats | 69 +++++++++++ tests/bats/system/install-azure-cli.bats | 40 +++++-- tests/bats/system/update-workflow.bats | 144 +++++++++++++++++++++++ 6 files changed, 286 insertions(+), 21 deletions(-) diff --git a/docs/UPDATE.md b/docs/UPDATE.md index 6533d441..2cfa07c3 100644 --- a/docs/UPDATE.md +++ b/docs/UPDATE.md @@ -25,7 +25,7 @@ make update | `ixatu-day` | Prepara o recupera la sesión diaria declarativa `ixatu` de Herdr | | `make update` | Mismo flujo, invocado desde `~/dotfiles` | | `make update-windows` | Ejecuta WinGet y `wsl --update` desde PowerShell/Windows | -| `make update-wsl` | Ejecuta APT, Node/tooling IA, OpenCode, shell, uv, MCPs e imágenes Docker | +| `make update-wsl` | Ejecuta APT (incluido Azure CLI), Node/tooling IA, OpenCode, shell, uv, MCPs e imágenes Docker | | `make update-projects` | Actualiza proyectos personales como `~/proyectos/jesuserro` y RenderCV | | `make update-check` | Diagnóstico no mutante de requisitos de actualización y MCPs | | `make github-identity-check` / `github-identity-check` | Diagnóstico read-only de remotos, cuenta `gh` efectiva y tokens de entorno | @@ -48,6 +48,20 @@ make update `make update` no sustituye `chezmoi apply` y no regenera secretos. +En la fase APT, una Azure CLI ya instalada se actualiza mediante el `apt-get +upgrade` general. Si `az` no existe, el flujo llama al instalador idempotente +`scripts/install-azure-cli.sh`; no inicia sesión ni toca `~/.azure`. El resumen +registra la versión anterior y posterior, y conserva como incidente cualquier +fallo de instalación sin detener el resto del mantenimiento. + +Para consultar la sesión y suscripción seleccionada sin modificar Azure: +`bash scripts/check-azure-tools.sh`. Para autenticarte manualmente usa +`az login --tenant cafb7744-aedc-4c62-8a02-0474058bbfaa` y verifica la +suscripción con `az account show --query '{id:id,name:name,tenantId:tenantId}' --output table`. +Si necesitas cambiarla, usa `az account set --subscription ''` +**manualmente** y repite el diagnóstico. No sincronizar `~/.azure`, +tokens ni caches entre equipos, y no registrar credenciales en los logs. + ## Windows + WSL Desde Ubuntu/WSL, `make update` crea un directorio de ejecución en una ruta visible por Windows y WSL. La pestaña WSL ejecuta el mantenimiento Linux; una nueva pestaña PowerShell ejecuta WinGet y `wsl --update`. diff --git a/scripts/check-azure-tools.sh b/scripts/check-azure-tools.sh index 3fabf32d..366b1791 100755 --- a/scripts/check-azure-tools.sh +++ b/scripts/check-azure-tools.sh @@ -1,12 +1,14 @@ #!/usr/bin/env bash # Read-only Azure workstation readiness check for WSL2/Linux. # This script never logs in, selects subscriptions, creates resources, or deletes -# resources. It only checks local tooling and reports actionable next steps. +# resources. Azure account output is restricted to subscription ID and name for +# the expected tenant; users, credentials, and secrets are never printed. set -u critical_missing=0 warnings=0 +EXPECTED_AZURE_TENANT_ID="cafb7744-aedc-4c62-8a02-0474058bbfaa" section() { printf '\n==> %s\n' "$1" @@ -129,17 +131,25 @@ check_any_recommended_command "lsb-release" lsb_release lsb-release --version section "Azure CLI" if command -v az >/dev/null 2>&1; then - if az account show --output table >/dev/null 2>&1; then - line "OK" "Sesión Azure activa:" - az account show --output table 2>/dev/null + active_tenant="$(az account show --query tenantId --output tsv 2>/dev/null || true)" + if [[ -z "${active_tenant}" ]]; then + warn "No hay sesión Azure activa o no se pudo verificar el tenant actual." + printf ' Para iniciar sesión manualmente usa Azure CLI login.\n' + elif [[ "${active_tenant}" != "${EXPECTED_AZURE_TENANT_ID}" ]]; then + warn "La sesión Azure activa no pertenece al tenant esperado." else - warn "No hay sesión Azure activa o no se pudo leer la cuenta actual." - printf ' Para iniciar sesión manualmente: az %s\n' "login" + line "OK" "Tenant Azure esperado verificado." + selected_id="$(az account show --query id --output tsv 2>/dev/null || true)" + selected_name="$(az account show --query name --output tsv 2>/dev/null || true)" + if [[ -n "${selected_id}" && -n "${selected_name}" ]]; then + line "OK" "Suscripción seleccionada: ${selected_name} (${selected_id})" + else + warn "No fue posible identificar la suscripción seleccionada." + fi fi - if az extension show --name containerapp --output table >/dev/null 2>&1; then - line "OK" "Extensión Azure CLI 'containerapp' disponible:" - az extension show --name containerapp --output table 2>/dev/null + if az extension show --name containerapp --query name --output tsv >/dev/null 2>&1; then + line "OK" "Extensión Azure CLI 'containerapp' disponible." else warn "Extensión Azure CLI 'containerapp' no disponible." printf ' Recomendación: az extension add --name containerapp --upgrade\n' diff --git a/scripts/update/update-wsl.sh b/scripts/update/update-wsl.sh index 52e9d3c1..8a11b39a 100755 --- a/scripts/update/update-wsl.sh +++ b/scripts/update/update-wsl.sh @@ -153,17 +153,26 @@ run_versioned_step() { run_apt() { section "APT" + local azure_cli_before azure_cli_after + azure_cli_before="$(probe_named_version "Azure CLI" az version --query '"azure-cli"' --output tsv || true)" if is_truthy "${DOTFILES_UPDATE_MOCK:-}"; then result_ok "WSL" "APT" "mocked" + record_version_transition "WSL" "Azure CLI" "$azure_cli_before" "$azure_cli_before" return 0 fi if ! command -v sudo >/dev/null 2>&1; then result_fail "WSL" "APT" "sudo not found" + record_version_transition "WSL" "Azure CLI" "$azure_cli_before" "$azure_cli_before" return 0 fi run_step "WSL" "APT update" "${LOG_DIR}/wsl-apt-update.log" sudo apt-get update -y run_step "WSL" "APT upgrade" "${LOG_DIR}/wsl-apt-upgrade.log" sudo apt-get upgrade -y run_step "WSL" "APT autoremove" "${LOG_DIR}/wsl-apt-autoremove.log" sudo apt-get autoremove -y + if ! command -v az >/dev/null 2>&1; then + run_step "WSL" "Azure CLI install" "${LOG_DIR}/wsl-azure-cli.log" "${DOTFILES_ROOT}/scripts/install-azure-cli.sh" + fi + azure_cli_after="$(probe_named_version "Azure CLI" az version --query '"azure-cli"' --output tsv || true)" + record_version_transition "WSL" "Azure CLI" "$azure_cli_before" "$azure_cli_after" } ensure_node_runtime() { @@ -512,6 +521,7 @@ snapshot_current_tool() { } capture_workstation_snapshot() { + snapshot_current_tool "Azure CLI" az version --query '"azure-cli"' --output tsv snapshot_current_tool "Python" python3 --version snapshot_current_tool "Node.js" node --version snapshot_current_tool "npm" npm --version diff --git a/tests/bats/system/azure-tools.bats b/tests/bats/system/azure-tools.bats index b2baf38a..4df077f1 100644 --- a/tests/bats/system/azure-tools.bats +++ b/tests/bats/system/azure-tools.bats @@ -6,6 +6,11 @@ setup() { CHECK_SCRIPT="${DOTFILES_DIR}/scripts/check-azure-tools.sh" AZURE_ALIASES="${DOTFILES_DIR}/zsh/55-aliases-azure.zsh" AZURE_DOC="${DOTFILES_DIR}/docs/ops/azure-tooling.md" + setup_temp_dir +} + +teardown() { + teardown_temp_dir } @test "check-azure-tools.sh exists" { @@ -60,3 +65,67 @@ setup() { run grep -n '55-aliases-azure.zsh' "${DOTFILES_DIR}/zshrc" [[ "${status}" -eq 0 ]] } + +@test "Azure account check verifies expected tenant and selected subscription" { + local stub_dir="${TEST_TEMP_DIR}/bin" + local az_log="${TEST_TEMP_DIR}/az.log" + mkdir -p "$stub_dir" + for cmd in git make jq curl; do + ln -s "$(command -v "$cmd")" "$stub_dir/$cmd" + done + cat >"${stub_dir}/docker" <<'EOF' +#!/usr/bin/env bash +exit 0 +EOF + cat >"${stub_dir}/az" <<'EOF' +#!/usr/bin/env bash +printf '%s\n' "$*" >>"${AZ_TEST_LOG}" +case "$*" in + "version --query \"azure-cli\" --output tsv") printf '2.79.0\n' ;; + "account show --query tenantId --output tsv") printf 'cafb7744-aedc-4c62-8a02-0474058bbfaa\n' ;; + "account show --query id --output tsv") printf '00000000-0000-0000-0000-000000000001\n' ;; + "account show --query name --output tsv") printf 'Production\n' ;; + "extension show --name containerapp --query name --output tsv") printf 'containerapp\n' ;; +esac +EOF + chmod +x "${stub_dir}/az" "${stub_dir}/docker" + + run env AZ_TEST_LOG="$az_log" PATH="${stub_dir}:/usr/bin:/bin" "$CHECK_SCRIPT" + [[ "$status" -eq 0 ]] + [[ "$output" == *"Production (00000000-0000-0000-0000-000000000001)"* ]] + [[ "$output" != *"Development"* ]] + [[ "$output" != *"operator@example.com"* ]] + [[ "$output" != *"client-secret"* ]] + grep -Fq "account show --query tenantId --output tsv" "$az_log" + grep -Fq "account show --query id --output tsv" "$az_log" +} + +@test "Azure account check does not print account details for a different tenant" { + local stub_dir="${TEST_TEMP_DIR}/bin-mismatch" + mkdir -p "$stub_dir" + for cmd in git make jq curl; do + ln -s "$(command -v "$cmd")" "$stub_dir/$cmd" + done + cat >"${stub_dir}/docker" <<'EOF' +#!/usr/bin/env bash +exit 0 +EOF + cat >"${stub_dir}/az" <<'EOF' +#!/usr/bin/env bash +case "$*" in + "version --query \"azure-cli\" --output tsv") printf '2.79.0\n' ;; + "account show --query tenantId --output tsv") printf '11111111-1111-1111-1111-111111111111\n' ;; + "account show --query id --output tsv") printf 'SHOULD-NOT-PRINT\n' ;; + "account show --query name --output tsv") printf 'Secret Subscription\n' ;; + "extension show --name containerapp --query name --output tsv") printf 'containerapp\n' ;; +esac +EOF + chmod +x "${stub_dir}/az" "${stub_dir}/docker" + + run env PATH="${stub_dir}:/usr/bin:/bin" "$CHECK_SCRIPT" + [[ "$status" -eq 0 ]] + [[ "$output" == *"no pertenece al tenant esperado"* ]] + [[ "$output" != *"SHOULD-NOT-PRINT"* ]] + [[ "$output" != *"Secret Subscription"* ]] + [[ "$output" != *"11111111-1111-1111-1111-111111111111"* ]] +} diff --git a/tests/bats/system/install-azure-cli.bats b/tests/bats/system/install-azure-cli.bats index d8066cc7..e1c626dd 100644 --- a/tests/bats/system/install-azure-cli.bats +++ b/tests/bats/system/install-azure-cli.bats @@ -29,7 +29,7 @@ make_install_stubs() { #!/usr/bin/env bash echo "curl $*" >>"${AZURE_TEST_LOG}" case "$*" in - *"dists/resolute/Release"*) + *"dists/futurecodename/Release"*) exit 22 ;; *"dists/noble/Release"*) @@ -103,6 +103,22 @@ done exit 0 EOF chmod +x "${stub_dir}/curl" "${stub_dir}/sudo" "${stub_dir}/apt-get" "${stub_dir}/dpkg" "${stub_dir}/gpg" + # Test-only isolation: a workstation with /usr/bin/az must still exercise + # the missing-tool installer path. Only the temporary stub can satisfy az. + cat >"${stub_dir}/isolate-az.bash" <<'AZENV' +command() { + if [[ "${1:-}" == "-v" && "${2:-}" == "az" ]]; then + local found + found="$(builtin command -v az 2>/dev/null)" || return 1 + case "${found}" in + "${AZURE_TEST_STUB_DIR}"/*) printf '%s\n' "${found}"; return 0 ;; + *) return 1 ;; + esac + fi + builtin command "$@" +} +AZENV + export BASH_ENV="${stub_dir}/isolate-az.bash" } @test "install-azure-cli.sh exists and passes bash -n" { @@ -136,7 +152,7 @@ EOF local source_dir="${TEST_TEMP_DIR}/sources" local keyring="${TEST_TEMP_DIR}/keyrings/microsoft.gpg" local log="${TEST_TEMP_DIR}/commands.log" - write_os_release "resolute" + write_os_release "futurecodename" make_install_stubs "${stub_dir}" mkdir -p "${source_dir}" "$(dirname "${keyring}")" : >"${log}" @@ -152,9 +168,9 @@ EOF [[ "${status}" -ne 0 ]] [[ "${output}" == *"Microsoft no publica Azure CLI"* ]] - [[ "${output}" == *"resolute"* ]] + [[ "${output}" == *"futurecodename"* ]] [[ ! -e "${source_dir}/azure-cli.list" ]] - [[ "$(cat "${log}")" == *"dists/resolute/Release"* ]] + [[ "$(cat "${log}")" == *"dists/futurecodename/Release"* ]] [[ "$(cat "${log}")" != *"apt-get update"* ]] [[ "$(cat "${log}")" != *"apt-get install -y azure-cli"* ]] } @@ -164,7 +180,7 @@ EOF local source_dir="${TEST_TEMP_DIR}/sources" local keyring="${TEST_TEMP_DIR}/keyrings/microsoft.gpg" local log="${TEST_TEMP_DIR}/commands.log" - write_os_release "noble" + write_os_release "resolute" make_install_stubs "${stub_dir}" mkdir -p "${source_dir}" "$(dirname "${keyring}")" : >"${log}" @@ -179,11 +195,11 @@ EOF bash "${INSTALL_AZURE}" [[ "${status}" -eq 0 ]] - [[ "${output}" == *"Microsoft publica Azure CLI para 'noble'"* ]] + [[ "${output}" == *"Microsoft publica Azure CLI para 'resolute'"* ]] [[ "${output}" == *"Azure CLI instalado"* ]] [[ -f "${source_dir}/azure-cli.list" ]] - [[ "$(cat "${source_dir}/azure-cli.list")" == *" noble main"* ]] - [[ "$(cat "${log}")" == *"dists/noble/Release"* ]] + [[ "$(cat "${source_dir}/azure-cli.list")" == *" resolute main"* ]] + [[ "$(cat "${log}")" == *"dists/resolute/Release"* ]] [[ "$(cat "${log}")" == *"apt-get install -y azure-cli"* ]] } @@ -225,10 +241,10 @@ EOF local source_dir="${TEST_TEMP_DIR}/sources" local keyring="${TEST_TEMP_DIR}/keyrings/microsoft.gpg" local log="${TEST_TEMP_DIR}/commands.log" - write_os_release "resolute" + write_os_release "futurecodename" make_install_stubs "${stub_dir}" mkdir -p "${source_dir}" "$(dirname "${keyring}")" - printf 'deb https://packages.microsoft.com/repos/azure-cli/ resolute main\n' >"${source_dir}/azure-cli.list" + printf 'deb https://packages.microsoft.com/repos/azure-cli/ futurecodename main\n' >"${source_dir}/azure-cli.list" printf 'deb https://packages.microsoft.com/repos/edge/ stable main\n' >"${source_dir}/microsoft-edge.list" : >"${log}" @@ -355,7 +371,9 @@ EOF @test "deps-check default does not include optional azure-cli" { run python3 "${HELPER_SCRIPT}" list --inventory "${DOTFILES_DIR}/system/packages/tooling.yaml" [[ "${status}" -eq 0 ]] - [[ "${output}" != *"azure-cli"* ]] + # Match package-column boundaries, not the worktree path + # (which may itself be named azure-cli-update). + [[ "${output}" != *$'\tazure-cli\t'* ]] [[ "${output}" != *$'\taz\t'* ]] } diff --git a/tests/bats/system/update-workflow.bats b/tests/bats/system/update-workflow.bats index ec0c6d02..12cc3ff6 100644 --- a/tests/bats/system/update-workflow.bats +++ b/tests/bats/system/update-workflow.bats @@ -11,6 +11,28 @@ teardown() { teardown_temp_dir } + +# Hermetic Azure CLI tests: production /usr/bin/az must never mask a missing +# mock binary or make the test depend on workstation installation state. +make_azure_cli_isolation() { + local shim="${TEST_TEMP_DIR}/azure-cli-isolation.bash" + cat >"$shim" <<'AZENV' +az() { + [[ -x "${AZURE_MOCK_BIN}/az" ]] || return 127 + "${AZURE_MOCK_BIN}/az" "$@" +} +command() { + if [[ "${1:-}" == "-v" && "${2:-}" == "az" ]]; then + [[ -x "${AZURE_MOCK_BIN}/az" ]] || return 1 + printf '%s\n' "${AZURE_MOCK_BIN}/az" + return 0 + fi + builtin command "$@" +} +AZENV + printf '%s\n' "$shim" +} + make_node_stub() { local dir="$1" version="$2" mkdir -p "$dir" @@ -2148,6 +2170,128 @@ EOF grep -q 'runtime-managed via uvx' "${DOTFILES_DIR}/scripts/update/update-wsl.sh" } +@test "APT flow installs Azure CLI only when missing and snapshots the installed version" { + local runner="${TEST_TEMP_DIR}/azure-cli-install-runner.sh" + local stub_dir="${TEST_TEMP_DIR}/azure-cli-install-bin" + local run_dir="${TEST_TEMP_DIR}/run-azure-cli-install" + mkdir -p "$stub_dir" + cat >"${stub_dir}/sudo" <<'EOF' +#!/usr/bin/env bash +exit 0 +EOF + chmod +x "${stub_dir}/sudo" + cat >"$runner" <"${stub_dir}/az" <<'AZEOF' +#!/usr/bin/env bash +printf '2.79.0\n' +AZEOF + chmod +x "${stub_dir}/az" + fi + RUN_STEP_LAST_RESULT_STATUS="OK" + result_ok "\$area" "\$name" "completed" +} +run_apt +EOF + chmod +x "$runner" + + run env BASH_ENV="$(make_azure_cli_isolation)" AZURE_MOCK_BIN="$stub_dir" "$runner" + [[ "$status" -eq 0 ]] + grep -q $'OK\tWSL\tAzure CLI install\tcompleted' "$run_dir/wsl-results.tsv" + grep -q $'INFO\tWSL\tAzure CLI version\tunavailable → 2.79.0' "$run_dir/wsl-results.tsv" + grep -q $'Azure CLI\t\t2.79.0\tinstalled' "$run_dir/tool-snapshot.tsv" +} + +@test "APT flow leaves an existing Azure CLI to apt upgrade and records its transition" { + local runner="${TEST_TEMP_DIR}/azure-cli-upgrade-runner.sh" + local stub_dir="${TEST_TEMP_DIR}/azure-cli-upgrade-bin" + local run_dir="${TEST_TEMP_DIR}/run-azure-cli-upgrade" + mkdir -p "$stub_dir" + cat >"${stub_dir}/az" <"${stub_dir}/sudo" <<'EOF' +#!/usr/bin/env bash +exit 0 +EOF + chmod +x "${stub_dir}/az" "${stub_dir}/sudo" + printf '2.78.0\n' >"${TEST_TEMP_DIR}/azure-cli-version" + cat >"$runner" <"${TEST_TEMP_DIR}/azure-cli-version" + fi + [[ "\$name" != "Azure CLI install" ]] + RUN_STEP_LAST_RESULT_STATUS="OK" + result_ok "\$area" "\$name" "completed" +} +run_apt +EOF + chmod +x "$runner" + + run env BASH_ENV="$(make_azure_cli_isolation)" AZURE_MOCK_BIN="$stub_dir" "$runner" + [[ "$status" -eq 0 ]] + assert_file_not_contains "$run_dir/wsl-results.tsv" "Azure CLI install" + grep -q $'INFO\tWSL\tAzure CLI version\t2.78.0 → 2.79.0' "$run_dir/wsl-results.tsv" + grep -q $'Azure CLI\t2.78.0\t2.79.0\tupdated' "$run_dir/tool-snapshot.tsv" +} + +@test "APT flow records Azure CLI installer failure and unavailable snapshot" { + local runner="${TEST_TEMP_DIR}/azure-cli-failure-runner.sh" + local stub_dir="${TEST_TEMP_DIR}/azure-cli-failure-bin" + local run_dir="${TEST_TEMP_DIR}/run-azure-cli-failure" + mkdir -p "$stub_dir" + cat >"${stub_dir}/sudo" <<'EOF' +#!/usr/bin/env bash +exit 0 +EOF + chmod +x "${stub_dir}/sudo" + cat >"$runner" <