Add sessions-ctl keygen to create prod's key on the dev box
keygen generates an ed25519 pair in RAM, writes the public half to config.toml and authorized_keys (replacing the previous sessions-manager line) and prints the web app's environment - dev host, port, user, pinned host key and the private key as one base64 line - then deletes the private key, so the pasted copy is the only one. Refuses to replace an existing key without --rotate. Local only, never reachable over SSH. Also make load_config() read CONFIG_PATH at call time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KKYfRST9cTJYae3f7Jf3ac
This commit is contained in:
parent
08636ddc25
commit
ca66db3aeb
4 changed files with 165 additions and 7 deletions
|
|
@ -126,5 +126,73 @@ class MainContract(unittest.TestCase):
|
|||
self.assertFalse(json.loads(buf.getvalue())["ok"])
|
||||
|
||||
|
||||
class TomlEdit(unittest.TestCase):
|
||||
BASE = 'max_running = 2\n\n[repos]\na = "x"\n\n[ssh_access]\nallow_from_host = "reudy.net"\npublic_key = "old"\n'
|
||||
|
||||
def test_replaces_in_place(self):
|
||||
out = sc.set_toml_string(self.BASE, "ssh_access", "public_key", "ssh-ed25519 AAAAnew c")
|
||||
self.assertIn('public_key = "ssh-ed25519 AAAAnew c"', out)
|
||||
self.assertNotIn('"old"', out)
|
||||
self.assertEqual(out.count("public_key"), 1)
|
||||
self.assertIn('a = "x"', out)
|
||||
|
||||
def test_adds_key_or_section(self):
|
||||
no_key = self.BASE.replace('public_key = "old"\n', "")
|
||||
self.assertIn('public_key = "k"', sc.set_toml_string(no_key, "ssh_access", "public_key", "k"))
|
||||
out = sc.set_toml_string('max_running = 2\n', "ssh_access", "public_key", "k")
|
||||
self.assertTrue(out.endswith('[ssh_access]\npublic_key = "k"\n'))
|
||||
|
||||
def test_same_key_in_other_section_untouched(self):
|
||||
text = '[other]\npublic_key = "keep"\n[ssh_access]\npublic_key = "old"\n'
|
||||
out = sc.set_toml_string(text, "ssh_access", "public_key", "new")
|
||||
self.assertIn('public_key = "keep"', out)
|
||||
self.assertIn('public_key = "new"', out)
|
||||
|
||||
def test_rejects_quotes(self):
|
||||
with self.assertRaises(sc.UserError):
|
||||
sc.set_toml_string("", "s", "k", 'a" \nx = "y')
|
||||
|
||||
|
||||
class Keygen(unittest.TestCase):
|
||||
def test_keygen_end_to_end_in_temp_dirs(self):
|
||||
import base64, os
|
||||
from unittest import mock
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
d = Path(d)
|
||||
cfg = d / "config.toml"
|
||||
cfg.write_text('[ssh_access]\nallow_from_host = "reudy.net"\npublic_key = "ssh-ed25519 AAAAold vps-manual"\n')
|
||||
ak = d / "authorized_keys"
|
||||
ak.write_text("ssh-ed25519 AAAAmine me@laptop\n" + 'from="1.1.1.1",restrict ssh-ed25519 AAAAold sessions-manager\n')
|
||||
hk = d / "host.pub"
|
||||
hk.write_text("ssh-ed25519 AAAAhostkey root@box\n")
|
||||
with mock.patch.object(sc, "CONFIG_PATH", cfg), mock.patch.object(sc, "AUTHORIZED_KEYS", ak), \
|
||||
mock.patch.object(sc, "HOST_KEY_PATH", str(hk)), mock.patch.object(sc, "resolve", lambda h: ["37.205.10.63"]), \
|
||||
mock.patch.dict(os.environ, {"XDG_RUNTIME_DIR": str(d)}):
|
||||
with self.assertRaises(sc.UserError): # existing key needs --rotate
|
||||
sc.cmd_keygen(sc.load_config(cfg), [])
|
||||
r = sc.cmd_keygen(sc.load_config(cfg), ["--rotate"])
|
||||
|
||||
env = dict(line.split("=", 1) for line in r["text"].splitlines())
|
||||
self.assertEqual(set(env), {"SESSIONS_DEV_HOST", "SESSIONS_DEV_PORT", "SESSIONS_DEV_USER",
|
||||
"SESSIONS_DEV_HOST_KEY", "SESSIONS_SSH_KEY_B64"})
|
||||
self.assertEqual(env["SESSIONS_DEV_HOST_KEY"], "ssh-ed25519 AAAAhostkey")
|
||||
priv = base64.b64decode(env["SESSIONS_SSH_KEY_B64"], validate=True)
|
||||
self.assertTrue(priv.startswith(b"-----BEGIN OPENSSH PRIVATE KEY-----"))
|
||||
# public half landed in config and authorized_keys; old key gone; other keys kept
|
||||
new_pub = sc.load_config(cfg)["ssh_access"]["public_key"]
|
||||
self.assertTrue(new_pub.startswith("ssh-ed25519 ") and "AAAAold" not in new_pub)
|
||||
keys = ak.read_text()
|
||||
self.assertIn("me@laptop", keys)
|
||||
self.assertNotIn("AAAAold", keys)
|
||||
self.assertIn(new_pub.split()[1], keys)
|
||||
self.assertIn('from="37.205.10.63"', keys)
|
||||
# nothing private left behind in the runtime dir
|
||||
self.assertEqual([p.name for p in d.iterdir() if p.name.startswith("sessions-keygen-")], [])
|
||||
|
||||
def test_keygen_is_local_only(self):
|
||||
with self.assertRaises(sc.UserError):
|
||||
sc.parse_remote("keygen --rotate")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue