feat(accounts): clear a password, replace and remove keys #69

Merged
christianmanivong merged 1 commits from fix/local-account-key-replace into main 2026-11-04 11:13:54 +00:00
Owner

What

Personal accounts on Linux now follow what the person changes. See NetOrk/netork#2013.

Needs the napalm-device-types release first (NAPALM/napalm-device-types#90, assumed to be 4.28.0). The floor is raised to napalm-device-types>=4.28.0. CI pre-installs device-types from git main, so this PR goes green once #90 is merged.

  • Replace a key (add_local_account(..., replaces=[...])) and remove keys (remove_local_account_keys) use the mixin's key-file implementation. As the person (sudo -n -u <name>), it reads ~/.ssh/authorized_keys{,2}, stages the lines that are kept and runs install -m 600 -- <staged> <file>, or rm -f -- <file> when nothing is left. Root only runs test -e. The tests here pin those commands on Linux.
  • Clear the password (add_local_account(..., clear_password=True)): usermod -p '*' <name>. The account logs in with its key only. The sudo step removes /etc/sudoers.d/80-account-<name> and takes the account out of sudo/wheel/admin.

Why *

The value has to stop every password and still let the key log in on Debian, Ubuntu and RHEL with sshd's defaults (UsePAM yes) and without PAM.

  • Not passwd -d (an empty field). pam_unix's nullok, which is on by default in Debian's and Ubuntu's common-auth, takes an empty field as "no password needed" for su and the console.
  • Not ! or usermod -L. With UsePAM no, sshd takes a ! prefix for a locked account and refuses the key too. With PAM it makes no difference, but * works both ways.
  • Not the expiry. That is this driver's lock (usermod -L -e 1), and PAM's account check refuses an expired account its key login.

usermod -p leaves the expiry alone and records today as the last password change, so pam_unix does not ask for a new password at the next key login. * is already in the driver's set of "no password" fields, so opening the account again later does not try usermod -U.

Tests

Fixtures only, no device. Two tests failed first, because the device-types default raises NotImplementedError for the clear hook: test_clearing_the_password_is_a_star_no_hash_matches and test_removing_the_password_runs_usermod_and_takes_sudo_away. New tests also pin the replace and remove commands and the fact that a key-only account opens again without a password coming back. 362 tests pass against the device-types branch. ruff, mypy, napalm-standard check and ratchet are clean.

## What Personal accounts on Linux now follow what the person changes. See NetOrk/netork#2013. **Needs the napalm-device-types release first** (NAPALM/napalm-device-types#90, assumed to be 4.28.0). The floor is raised to `napalm-device-types>=4.28.0`. CI pre-installs device-types from git main, so this PR goes green once #90 is merged. - **Replace a key** (`add_local_account(..., replaces=[...])`) and **remove keys** (`remove_local_account_keys`) use the mixin's key-file implementation. As the person (`sudo -n -u <name>`), it reads `~/.ssh/authorized_keys{,2}`, stages the lines that are kept and runs `install -m 600 -- <staged> <file>`, or `rm -f -- <file>` when nothing is left. Root only runs `test -e`. The tests here pin those commands on Linux. - **Clear the password** (`add_local_account(..., clear_password=True)`): `usermod -p '*' <name>`. The account logs in with its key only. The `sudo` step removes `/etc/sudoers.d/80-account-<name>` and takes the account out of sudo/wheel/admin. ## Why `*` The value has to stop every password and still let the key log in on Debian, Ubuntu and RHEL with sshd's defaults (`UsePAM yes`) and without PAM. - **Not `passwd -d` (an empty field).** pam_unix's `nullok`, which is on by default in Debian's and Ubuntu's `common-auth`, takes an empty field as "no password needed" for `su` and the console. - **Not `!` or `usermod -L`.** With `UsePAM no`, sshd takes a `!` prefix for a locked account and refuses the key too. With PAM it makes no difference, but `*` works both ways. - **Not the expiry.** That is this driver's lock (`usermod -L -e 1`), and PAM's account check refuses an expired account its key login. `usermod -p` leaves the expiry alone and records today as the last password change, so pam_unix does not ask for a new password at the next key login. `*` is already in the driver's set of "no password" fields, so opening the account again later does not try `usermod -U`. ## Tests Fixtures only, no device. Two tests failed first, because the device-types default raises `NotImplementedError` for the clear hook: `test_clearing_the_password_is_a_star_no_hash_matches` and `test_removing_the_password_runs_usermod_and_takes_sudo_away`. New tests also pin the replace and remove commands and the fact that a key-only account opens again without a password coming back. 362 tests pass against the device-types branch. ruff, mypy, `napalm-standard check` and `ratchet` are clean.
christianmanivong added 1 commit 2026-11-04 10:50:34 +00:00
feat(accounts): clear a password, replace and remove keys
driver / lint (pull_request) Successful in 17s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m2s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 32s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m27s
driver / publish (pull_request) Skipped
d03fe2a2ba
napalm-device-types 4.28.0 lets add_local_account remove the old keys
(replaces) and the password (clear_password), and adds
remove_local_account_keys. The key changes run through the mixin's own
key files as the person; the password goes with usermod -p '*', which
leaves the expiry (the lock) alone and keeps the key login working with
and without PAM, where an empty password or a '!' would not.

See NetOrk/netork#2013
christianmanivong merged commit bd17fbd84d into main 2026-11-04 11:13:54 +00:00
christianmanivong deleted branch fix/local-account-key-replace 2026-11-04 11:13:54 +00:00
Sign in to join this conversation.