fix: another service's account of the same name is left alone (NetOrk/netork#1817)
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 59s
driver / lowest (pull_request) Successful in 30s
driver / security (pull_request) Successful in 55s
driver / build (pull_request) Successful in 26s
driver / test (3.12) (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 56s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m1s
driver / test (3.13) (pull_request) Successful in 59s
driver / lowest (pull_request) Successful in 30s
driver / security (pull_request) Successful in 55s
driver / build (pull_request) Successful in 26s
driver / test (3.12) (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 56s
driver / publish (pull_request) Skipped
On bw-01, netork is the satellite's own user (nologin, in docker). add_service_account opened it, gave it the passwordless sudo rule and netOrk's key, and only the login as it failed. An account that is there and cannot log in (nologin, false) is now refused before anything changes.
This commit is contained in:
1 parent
3f59023f46
commit
47751d12d6
3 files changed
+49
-9
No files matched your search
+3
-1
@@ -21,7 +21,9 @@ recorded here; their history is the git log.
|
||||
account that is there is opened again and keeps its other keys; root is refused
|
||||
(NetOrk/netork#1817). With `uid=`, a new account gets that uid once no other account
|
||||
has it; one that is there keeps its own. A driver supplies
|
||||
`_account_create_with_uid`.
|
||||
`_account_create_with_uid`. An account that is there and cannot log in (`nologin`,
|
||||
`false`) is another service's, such as a netOrk satellite's `netork`, and is refused
|
||||
before anything changes.
|
||||
- `LocalAccountsMixin` (`napalm_device_types.local_accounts`): personal login accounts
|
||||
on a host. `get_local_accounts(names)` reads them back (groups, lock, sudo, key
|
||||
files), `add_local_account(name, public_key, password_hash=, admin=)` creates or
|
||||
|
||||
@@ -85,6 +85,9 @@ _RUN_ALL_NOPASSWD_RE = re.compile(
|
||||
re.MULTILINE,
|
||||
)
|
||||
|
||||
#: Login shells that refuse every login: an account with one belongs to a service.
|
||||
_NO_LOGIN_SHELLS = frozenset({"nologin", "false"})
|
||||
|
||||
#: Stages stdin as a file anyone may read -- the person reads the key file
|
||||
#: from it -- and prints its path. The content is a public key or a sudo rule.
|
||||
_STAGE = 't=$(mktemp) && chmod 644 "$t" && cat > "$t" && printf %s "$t"'
|
||||
@@ -409,17 +412,15 @@ class LocalAccountsMixin:
|
||||
Steps as in :meth:`add_local_account`: ``account``, then ``sudo`` and
|
||||
``keys``. The new login itself is for the caller to try.
|
||||
|
||||
:raises ValueError: for a name or key no host takes, root, or a uid of 0 or less.
|
||||
An account that is there and cannot log in (``nologin``, ``false``) is
|
||||
another service's -- a satellite's ``netork``, say -- and is left alone.
|
||||
|
||||
:raises ValueError: for a name or key no host takes, root, a uid of 0 or
|
||||
less, or another service's account.
|
||||
:raises RuntimeError: when the login user cannot be told.
|
||||
"""
|
||||
check_account_name(name)
|
||||
check_public_key(public_key)
|
||||
if uid is not None and uid <= 0:
|
||||
raise ValueError(f"not a uid for netOrk's own account: {uid}")
|
||||
login = self._accounts_login()
|
||||
existing = self._accounts_passwd(name)
|
||||
if existing is not None and int(existing[2]) == 0:
|
||||
raise ValueError(f"{name} is root")
|
||||
login, existing = self._accounts_service_target(name, uid)
|
||||
if name == login:
|
||||
step: FixStepDict = {
|
||||
"step": "account",
|
||||
@@ -439,6 +440,24 @@ class LocalAccountsMixin:
|
||||
]
|
||||
return self._accounts_result(name, parts)
|
||||
|
||||
def _accounts_service_target(self, name: str, uid: int | None) -> tuple[str, list[str] | None]:
|
||||
"""The login user and *name*'s passwd line, once *name* may become netOrk's.
|
||||
|
||||
:raises ValueError: as :meth:`add_service_account` says.
|
||||
"""
|
||||
check_account_name(name)
|
||||
if uid is not None and uid <= 0:
|
||||
raise ValueError(f"not a uid for netOrk's own account: {uid}")
|
||||
login = self._accounts_login()
|
||||
existing = self._accounts_passwd(name)
|
||||
if existing is not None and int(existing[2]) == 0:
|
||||
raise ValueError(f"{name} is root")
|
||||
if existing is not None and existing[6].rsplit("/", 1)[-1] in _NO_LOGIN_SHELLS:
|
||||
raise ValueError(
|
||||
f"{name} is there and cannot log in ({existing[6]}): another service's account"
|
||||
)
|
||||
return login, existing
|
||||
|
||||
def _accounts_service_account(
|
||||
self, name: str, passwd: list[str] | None, uid: int | None
|
||||
) -> FixStepOutcomeDict:
|
||||
|
||||
@@ -280,3 +280,22 @@ def test_a_driver_that_cannot_set_a_uid_says_so(host: _ServiceHost, driver: _Dri
|
||||
|
||||
assert not result["success"]
|
||||
assert "uid" in result["output"]
|
||||
|
||||
|
||||
# -- another service's account of the same name (bw-01: the satellite's) ------------------
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"shell", ["/usr/sbin/nologin", "/sbin/nologin", "/bin/false", "/usr/bin/false"]
|
||||
)
|
||||
def test_an_account_that_cannot_log_in_is_another_services_and_left_alone(
|
||||
host: _ServiceHost, driver: _Driver, shell: str
|
||||
) -> None:
|
||||
host.accounts["netork"] = _Account(
|
||||
999, "/home/netork", shell=shell, groups=["netork", "docker"]
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="cannot log in"):
|
||||
driver.add_service_account("netork", NETORK_KEY)
|
||||
assert not _root_commands(host)
|
||||
assert service_sudoers_rule_path(_Driver.SUDOERS_DIR, "netork") not in host.files
|
||||
Reference in new issue
Block a user