Loading
Keep SSH keys on the instance only, never in project metadata
Closes #49 (closed)
What was wrong
up set no ssh-keys, no block-project-ssh-keys and no OS Login. So gcloud compute ssh and scp wrote the operator's shared ~/.ssh/google_compute_engine public key into PROJECT metadata. That entry lands on every VM in the project, and writing it needs compute.projects.setCommonInstanceMetadata. A hosted caller with only instance-level rights cannot do it, and a caller that can has put its key on every other VM.
What changes
upmakes an ed25519 pair per lab,<instance>.sshand<instance>.ssh.pub, mode 600, beside<instance>.root-password. The state records the path asssh_key_file, andstatus --jsonshows the path, never the key (schema 17).- Every VM is created with
ssh-keys=<user>:<public line>andblock-project-ssh-keys=TRUE. That is one path:create_vmforup,restore, geo, praefect and two-plane, plusprov_nested_createfor openshift-local. - Every gcloud ssh, scp and IAP path passes
--ssh-key-fileand--strict-host-key-checking=yes. The raw ssh in geo and praefect and the multiplexer use the same key. - A lab recorded before this gets its key added to that instance's own metadata once, on its next ssh. Never to the project.
downandreapremove the key files.down --keep-snapshotkeeps them, sorestorereuses the key.- Preflight checks for
ssh-keygeninstead of the shared key. No step needssetCommonInstanceMetadata. - The project's existing common-metadata
ssh-keysentry is not touched. Its cleanup is a separate call.
flowchart TD
up["up: create_vm"] --> gen["ssh-keygen: ed25519 pair<br/>state/INSTANCE.ssh, .ssh.pub, mode 600"]
gen --> rec["state records ssh_key_file<br/>status --json shows the path only"]
gen --> meta["instances create, instance metadata:<br/>ssh-keys=USER:PUBKEY<br/>block-project-ssh-keys=TRUE"]
meta --> vm[("VM")]
rec --> use["every ssh, scp and IAP path"]
use --> flags["--ssh-key-file=state/INSTANCE.ssh<br/>--strict-host-key-checking=yes<br/>--tunnel-through-iap"]
flags --> iap["IAP tunnel to port 22"]
iap --> vm
use --> raw["geo, praefect: raw ssh<br/>-i key, known_hosts in state dir"]
raw --> iap
use --> mux["multiplexer: same key<br/>refuses a lab with no key"]
mux --> iap
old["lab recorded before this change:<br/>no key in state"] -->|"next ssh, once"| add["add-metadata on that instance:<br/>ssh-keys + block-project-ssh-keys"]
add --> vm
add -. "never" .-> proj["project metadata"]
flags -. "no write: key already on instance,<br/>project keys blocked" .-> proj
vm --> snap["down --keep-snapshot:<br/>VM goes, key files stay"]
snap --> restore["restore: create_vm reuses the key<br/>same metadata on the new VM"]
restore --> vm
vm --> down["down, reap: key files removed<br/>with the state"]Threat matrix
| Threat | Before | After | Control proven red when reverted |
|---|---|---|---|
| The operator's shared key lands on every VM in the project | Yes, through project metadata | No. The key is per lab and on one instance | Create metadata, for up and restore |
A caller without compute.projects.setCommonInstanceMetadata cannot ssh |
Blocked | Works with instance rights alone | --ssh-key-file on every ssh and scp path |
| gcloud falls back to project metadata | Always | Never: the key is on the instance and project keys are blocked | Create metadata; live arms (a) to (d) |
| One lab's key opens another lab | Shared key opens all | Each lab has its own pair | Per-instance key file |
| A record outlives its VM, or an older VM lacks the block flag, and gcloud writes our key to the project | Yes, on the normal ssh path | lab_key_ready reads the live instance before every ssh and scp. It runs only when the instance blocks project keys and carries our exact line. Otherwise the owner-gated migration sets both, or it refuses and gcloud ssh is never called |
Live-gate cases: not ours refused, ours migrated, scp too; live arm (3) |
| A lab recorded before this change has no key | n/a | Key appended to that instance's metadata once, keeping every other entry, never the project's. Only when a state record of ours names the instance and its live owner label is ours; otherwise it refuses and makes no metadata call |
Older-lab migration; refusal cases |
| A recreated VM keeps a stale pinned host key | n/a | create_vm and the restore recreate remove <instance>.known_hosts |
Create and restore cases |
| IAP lags a new VM and ssh fails once | n/a | vm_ssh retries Failed to lookup instance as it retries exit 255, under set -e as its callers run, and removes its temp file on every path |
Retry and temp-file cases under set -e |
| Key files outlive the lab | n/a | down and reap remove them; --keep-snapshot keeps them for restore |
down removes, --keep-snapshot keeps, reap removes |
| A new ssh path skips the key | n/a | Grep gate fails any compute ssh or scp call without --ssh-key-file, any use of the shared key, and any prov_ssh, prov_scp or prov_ssh_exec in a function that has not called lab_key_ready |
provider-gate.sh, eleven probes |
| The key leaks through status | n/a | status --json carries the path only |
status --json check |
Proof
Each control has a selftest case that goes red when the control is reverted, and green with it:
| Revert | Red |
|---|---|
${keys_metadata} out of the create metadata |
2 cases |
--ssh-key-file out of prov_ssh, prov_ssh_exec and prov_scp |
6 cases |
lab_key_ready made a no-op (older-lab migration) |
4 cases |
lab_key_ready back to the record-only check (no live read) |
8 cases |
vm_ssh back to the pipeline that set -e ends before rc= |
4 cases |
remove_lab_ssh_key out of state_clear (down) |
1 case |
remove_lab_ssh_key added to suspend_rig (--keep-snapshot) |
2 cases |
| The four source files at the branch's first commit, with the new tests: refusal for another owner and for no record (no metadata call, no key left), migration keeps entries, known_hosts removed at create and recreate, lookup retry | 16 cases |
Live, on sim-ssk1 (omnibus --no-gitlab, SIM_OWNER=ssh-keys, SIM_PIN_ZONE=false), with the project's ssh-keys entry at 4 lines before and after every step:
| Arm | Result |
|---|---|
(a) up |
Instance metadata holds ssh-keys (the local public key) and block-project-ssh-keys=TRUE. Project count 4. |
(b) ssh, scp, status --json --live, a compute ssh --dry-run as the multiplexer runs it |
All work. Verbose gcloud logs no Updating instance ssh metadata or project write. Project count 4. |
(c) snapshot, down --keep-snapshot, restore |
Key files kept, same checksum after restore. The new VM carries the key and the flag. ssh works. Project count 4. |
| (d) older lab: key removed from state and disk, then ssh | Logs the migration, makes a new key, instance metadata holds it. Project count 4. |
(e) down |
No sim-ssk instance, disk, snapshot, address, firewall rule or key file remains. |
(1) sim-ssk3, bare, owner=someone-else; just lab ssh |
Refuses (no state record of ours names it). The instance's ssh-keys sha256 is identical before and after. Project count 4. |
(3) sim-ssk5, bare, owner=ssh-keys, no block flag, a record of ours with a key; just lab ssh |
The instance now carries block-project-ssh-keys=TRUE and our line. Project count 4 before and after. Instance, record and key deleted afterwards. |
(2) sim-ssk4, caproni --no-gitlab; snapshot, down --keep-snapshot, restore |
Host key fingerprints of all three types changed with the new instance id, so the pin does go stale. A planted known_hosts survived --keep-snapshot and was gone after the recreate. Project count 4. down left no sim-ssk resource or key file. |
Notes for the reviewer
- A first live run showed gcloud writing the key to the instance again on the first ssh. It compares the
.publine verbatim, comment included, and the metadata had the comment stripped. The metadata now carries the line as is, and the selftest pins it. - gcloud hard-codes
UserKnownHostsFile=~/.ssh/google_compute_known_hostsandHostKeyAlias=compute.<instance id>, and its own option wins over an extra--ssh-flag. So a per-lab known_hosts cannot apply to gcloud-driven ssh. The gcloud paths use--strict-host-key-checking=yeswith host keys read from guest attributes, keyed by instance id. The raw ssh in geo and praefect uses the per-lab<instance>.known_hosts. - After
restore, the first ssh can fail once with the IAP errorFailed to lookup instancewhile IAP catches up with the recreated VM. The next try works. check_ssh_keybecomescheck_ssh_keygen, so preflight keeps its step count.
Edited by Andrew Dunn