diff --git a/elements/oci/bluefin-server-ddi.bst b/elements/oci/bluefin-server-ddi.bst index c58f9d8e..a6915963 100644 --- a/elements/oci/bluefin-server-ddi.bst +++ b/elements/oci/bluefin-server-ddi.bst @@ -76,9 +76,14 @@ config: # Ensure all standard virtual, serial, and hypervisor consoles allow root login printf 'tty0\ntty1\ntty2\ntty3\ntty4\ntty5\ntty6\nttyS0\nttyS1\nconsole\nhvc0\n' > /layer/etc/securetty - # Seed default root password 'bluefin' into /etc/shadow for console and ssh login + # Seed default root password 'bluefin' for local console bring-up only. + # The last-change field is 0 (equivalent to `chage -d 0 root`), so the + # password is expired at first login and the console forces a per-install + # rotation before any shell is granted. The seeded credential must never + # be usable over the network: the sshd drop-in sets PermitRootLogin + # prohibit-password and PasswordAuthentication no. mkdir -p /layer/etc - printf 'root:$6$bluefin123$o2pQjPN1n3ZCnYUROlA01xAdnGO7mhVRAVw98x4xc8LAUIJv7b44bBNoER6fM4P.rrj4ePYFfkx9WvZICU8nt/:19700:0:99999:7:::\n' > /layer/etc/shadow + printf 'root:$6$bluefin123$o2pQjPN1n3ZCnYUROlA01xAdnGO7mhVRAVw98x4xc8LAUIJv7b44bBNoER6fM4P.rrj4ePYFfkx9WvZICU8nt/:0:0:99999:7:::\n' > /layer/etc/shadow chmod 0600 /layer/etc/shadow printf 'root:x:0:0:root:/root:/bin/bash\n' > /layer/etc/passwd chmod 0644 /layer/etc/passwd diff --git a/files/os/issue.d/40-kubestellar.issue b/files/os/issue.d/40-kubestellar.issue index 862fc3b5..cef7eaaf 100644 --- a/files/os/issue.d/40-kubestellar.issue +++ b/files/os/issue.d/40-kubestellar.issue @@ -1,5 +1,5 @@ Bluefin Server (\l) -Default login: root / bluefin +Default login: root / bluefin (password change forced at first login) KubeStellar Console: https://127.0.0.1:8080/ (loopback, TLS only) Remote access: ssh -L 8080:127.0.0.1:8080 @ diff --git a/tests/unit/test_root_credential_contract.py b/tests/unit/test_root_credential_contract.py new file mode 100644 index 00000000..8ee2ba95 --- /dev/null +++ b/tests/unit/test_root_credential_contract.py @@ -0,0 +1,71 @@ +"""Contracts for the seeded root credential and SSH login policy. + +The shipped DDI seeds a well-known default root password for local console +bring-up only. This file pins the two safety contracts around it: + +- the password is expired at first login (shadow last-change field 0, the + `chage -d 0` equivalent), so every install is forced onto a per-install + secret before any shell is granted; and +- the effective SSH policy never allows the seeded password (or any password) + over the network. +""" + +from __future__ import annotations + +import re +from pathlib import Path + +REPO_ROOT = Path(__file__).resolve().parents[2] +DDI_ELEMENT = REPO_ROOT / "elements" / "oci" / "bluefin-server-ddi.bst" +SSHD_DROPIN = REPO_ROOT / "files" / "os" / "ssh" / "sshd_config.d" / "bluefin-server.conf" +ISSUE_FILE = REPO_ROOT / "files" / "os" / "issue.d" / "40-kubestellar.issue" + + +def _root_shadow_fields(ddi_element: str) -> list[str]: + match = re.search( + r"printf '(root:[^']*)", ddi_element + ) + assert match, "DDI element must seed a root account into /etc/shadow" + shadow_line = match.group(1).split("\\n")[0] + fields = shadow_line.split(":") + assert fields[0] == "root", "seeded shadow line must be the root account" + assert len(fields) == 9, "seeded shadow line must have all 9 shadow fields" + return fields + + +def test_root_password_is_expired_for_first_login_rotation() -> None: + ddi_element = DDI_ELEMENT.read_text(encoding="utf-8") + fields = _root_shadow_fields(ddi_element) + + # shadow field 2 is the last-change date: 0 means expired, so login + # (console included) forces a password rotation before any shell. + assert fields[2] == "0", ( + "seeded root password must be expired at first login (last-change 0); " + "a fixed date leaves the well-known default usable indefinitely" + ) + + +def test_root_password_uses_no_fixed_build_date() -> None: + ddi_element = DDI_ELEMENT.read_text(encoding="utf-8") + fields = _root_shadow_fields(ddi_element) + + # Regression guard: a fixed last-change date (e.g. 19700) ships the same + # usable credential in every image with no rotation enforced. + assert fields[2] != "19700" + + +def test_sshd_dropin_never_permits_password_authentication() -> None: + sshd = SSHD_DROPIN.read_text(encoding="utf-8") + + assert re.search(r"^PermitRootLogin\s+prohibit-password$", sshd, re.MULTILINE) + assert re.search(r"^PasswordAuthentication\s+no$", sshd, re.MULTILINE) + assert re.search(r"^KbdInteractiveAuthentication\s+no$", sshd, re.MULTILINE) + assert not re.search(r"^PermitRootLogin\s+yes$", sshd, re.MULTILINE) + assert not re.search(r"^PasswordAuthentication\s+yes$", sshd, re.MULTILINE) + + +def test_console_banner_discloses_forced_password_change() -> None: + banner = ISSUE_FILE.read_text(encoding="utf-8") + + assert "Default login: root / bluefin" in banner + assert "password change forced at first login" in banner