From c9f2d12156e2542cfdcae4e2c77ed3202bd45301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20ROBERT?= Date: Mon, 16 Nov 2026 11:42:30 +0100 Subject: [PATCH] =?UTF-8?q?fix(doctor):=20--fix=20demande=20le=20mot=20de?= =?UTF-8?q?=20passe=20sudo=20m=C3=AAme=20juste=20apr=C3=A8s=20un=20sudo=20?= =?UTF-8?q?en=20cache=20(0.9.6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signalé sur Pop!_OS 24.04 le 2026-11-16 : `sudo -n true` réussissait grâce au mot de passe que sudo garde quelques minutes, dsoxlab omettait --ask-become-pass, et le sudo d Ansible échouait (« il est nécessaire de saisir un mot de passe »). La sonde est désormais `sudo -n -k true`. Les étapes facultatives se numérotent à la suite (« 0. Installer Docker »). Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.fr.md | 16 ++++++++++++++++ CHANGELOG.md | 15 +++++++++++++++ pyproject.toml | 2 +- src/dsoxlab/cli/diagnostic.py | 7 ++++++- src/dsoxlab/services/doctor_etapes.py | 4 ++++ tests/test_doctor_fix.py | 25 +++++++++++++++++++++++++ tests/test_doctor_guide.py | 15 +++++++++++++++ uv.lock | 2 +- 8 files changed, 83 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.fr.md b/CHANGELOG.fr.md index 17a8bfd..ea054f9 100644 --- a/CHANGELOG.fr.md +++ b/CHANGELOG.fr.md @@ -7,6 +7,22 @@ Toutes les modifications notables du projet sont documentées dans ce fichier. Le format s'appuie sur [Keep a Changelog](https://keepachangelog.com/fr/1.1.0/), et le projet suit le [versionnage sémantique](https://semver.org/lang/fr/). +## [0.9.6] - 2026-11-16 + +### Corrigé + +- **`doctor --fix` demande le mot de passe sudo quand sudo en exige un, même + juste après un sudo tapé dans le même terminal.** Pour décider s'il passe + `--ask-become-pass` à `ansible-playbook`, dsoxlab lançait `sudo -n true`, qui + réussit aussi grâce au mot de passe que sudo garde quelques minutes. Le + playbook partait alors sans rien demander, et le sudo lancé par Ansible, qui + ne voit pas ce cache, échouait : « sudo: il est nécessaire de saisir un mot de + passe » (signalé sur Pop!_OS 24.04). La sonde est désormais + `sudo -n -k true` : `-k` ignore le mot de passe gardé en mémoire, et seul un + vrai sudo sans mot de passe évite la question. +- **Les étapes facultatives se numérotent à la suite des étapes requises.** + `doctor --fix --clients` affichait « 0. Installer Docker ». + ## [0.9.5] - 2026-11-16 ### Ajouté diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c3924e..22864fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,21 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.9.6] - 2026-11-16 + +### Fixed + +- **`doctor --fix` asks for the sudo password when sudo needs one, even right + after a sudo typed in the same terminal.** To decide whether to pass + `--ask-become-pass` to `ansible-playbook`, dsoxlab ran `sudo -n true`, which + also succeeds thanks to the password sudo keeps for a few minutes. The + playbook then started without asking, and the sudo Ansible runs, which does + not see that cache, failed: "sudo: a password is required" (reported on + Pop!_OS 24.04). The probe is now `sudo -n -k true`: `-k` ignores the cached + password, so only a real passwordless sudo skips the question. +- **Optional steps are numbered after the required ones.** `doctor --fix + --clients` showed "0. Install Docker". + ## [0.9.5] - 2026-11-16 ### Added diff --git a/pyproject.toml b/pyproject.toml index fa72c99..7d59cf8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "dsoxlab" -version = "0.9.5" +version = "0.9.6" description = "Turn declarative exercises into reproducible, runnable and verifiable lab environments" readme = "README.md" requires-python = ">=3.11" diff --git a/src/dsoxlab/cli/diagnostic.py b/src/dsoxlab/cli/diagnostic.py index 9108c60..b3bdfeb 100644 --- a/src/dsoxlab/cli/diagnostic.py +++ b/src/dsoxlab/cli/diagnostic.py @@ -352,8 +352,13 @@ def _commande_playbook(chemin: Path) -> list[str]: """L'appel d'ansible-playbook, avec la demande de mot de passe si sudo en exige un.""" binaire = shutil.which("ansible-playbook") or str(Path.home() / ".local/bin/ansible-playbook") commande = [binaire, "-i", "localhost,", "-c", "local", str(chemin)] + # `-k` : sudo ignore le mot de passe tapé récemment dans ce terminal. Sans + # lui, `sudo -n true` réussissait grâce à ce cache, dsoxlab omettait + # `--ask-become-pass`, et le sudo d'Ansible, qui ne voit pas ce cache, + # échouait sur « il est nécessaire de saisir un mot de passe » (Pop!_OS + # 24.04, 2026-11-16). Avec `-k`, seul un vrai NOPASSWD passe. sans_mot_de_passe = subprocess.run( - ["sudo", "-n", "true"], capture_output=True, check=False, + ["sudo", "-n", "-k", "true"], capture_output=True, check=False, ).returncode == 0 if shutil.which("sudo") else False if not sans_mot_de_passe: commande.insert(1, "--ask-become-pass") diff --git a/src/dsoxlab/services/doctor_etapes.py b/src/dsoxlab/services/doctor_etapes.py index b7e94bf..2b51423 100644 --- a/src/dsoxlab/services/doctor_etapes.py +++ b/src/dsoxlab/services/doctor_etapes.py @@ -134,4 +134,8 @@ def construire(report: DoctorReport) -> Plan: if not c.ok and c.remede is not None and c.key in _CLIENTS ] plan.facultatives = _regrouper(facultatifs, facultative=True) + # Numérotées à la suite des étapes requises : `--fix --clients` les montrait + # en « 0. Installer Docker » (Pop!_OS 24.04, 2026-11-16). + for numero, etape in enumerate(plan.facultatives, start=len(plan.etapes) + 1): + etape.numero = numero return plan diff --git a/tests/test_doctor_fix.py b/tests/test_doctor_fix.py index 302e931..f0bb9fe 100644 --- a/tests/test_doctor_fix.py +++ b/tests/test_doctor_fix.py @@ -351,3 +351,28 @@ def test_le_display_requote_ce_qui_doit_l_etre() -> None: assert correctif.display == "echo 'deux mots' && touch fichier" assert not correctif.requires_sudo assert Fix((("sudo", "apt", "install", "x"),)).requires_sudo + + +@pytest.mark.parametrize(("code_sudo", "demande"), [(0, False), (1, True)]) +def test_le_mot_de_passe_se_demande_sauf_vrai_nopasswd( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, code_sudo: int, demande: bool, +) -> None: + """La sonde ignore le mot de passe gardé en mémoire (`sudo -k`). + + Sans `-k`, `sudo -n true` réussissait grâce au cache du terminal : dsoxlab + omettait `--ask-become-pass`, et le sudo d'Ansible échouait sur « il est + nécessaire de saisir un mot de passe » (Pop!_OS 24.04, 2026-11-16). + """ + sondes: list[list[str]] = [] + + class Resultat: + returncode = code_sudo + + monkeypatch.setattr(diagnostic.shutil, "which", lambda nom: f"/usr/bin/{nom}") + monkeypatch.setattr(diagnostic.subprocess, "run", + lambda cmd, **kw: (sondes.append(cmd), Resultat())[1]) + + commande = diagnostic._commande_playbook(tmp_path / "poste.yml") + + assert sondes == [["sudo", "-n", "-k", "true"]] + assert ("--ask-become-pass" in commande) is demande diff --git a/tests/test_doctor_guide.py b/tests/test_doctor_guide.py index 4cb21e4..039a1d3 100644 --- a/tests/test_doctor_guide.py +++ b/tests/test_doctor_guide.py @@ -479,3 +479,18 @@ def test_clients_n_installe_que_docker_et_incus() -> None: doctor._check("docker", False, "absent", remede=Remede("docker", ("sudo apt install y",), root=True)), ]) assert [e.cle for e in construire(report).facultatives] == ["docker"] + + +def test_les_etapes_facultatives_se_numerotent_a_la_suite() -> None: + """`--fix --clients` montrait « 0. Installer Docker » (Pop!_OS 24.04).""" + from dsoxlab.services.doctor_controles.modele import DoctorReport + from dsoxlab.services.doctor_controles.remedes import Remede + + report = DoctorReport() + report.required.append(doctor._check("git", False, "absent", + remede=Remede("git", ("sudo apt install git",), root=True))) + report.optional.append(doctor._check("docker", False, "absent", + remede=Remede("docker", ("sudo apt install y",), root=True))) + plan = construire(report) + assert [e.numero for e in plan.etapes] == [1] + assert [e.numero for e in plan.facultatives] == [2] diff --git a/uv.lock b/uv.lock index 7031d38..35c85d2 100644 --- a/uv.lock +++ b/uv.lock @@ -331,7 +331,7 @@ wheels = [ [[package]] name = "dsoxlab" -version = "0.9.5" +version = "0.9.6" source = { editable = "." } dependencies = [ { name = "ansible-core" },