Run lab VMs as a dedicated least-privilege account
What was wrong
create_vm passed no --service-account and set --scopes=https://www.googleapis.com/auth/cloud-platform. Every lab VM ran as the project's default compute account, so any process on it could fetch a token from the metadata server and act on the whole project: create VMs, read secrets, read the project IAM policy.
What changes
up creates, idempotently, a sim-lab-vm account and two custom roles, then starts the VM as that account. The path an in-VM process now gets:
flowchart LR
P[process on the VM] -->|metadata token| SA[sim-lab-vm]
SA -->|simulationLabVm, bound on this instance| I[delete and read this lab instance]
SA -->|simulationLabList, bound on the project| L[list instances]
SA -.->|no other role| X[create VM, secrets, IAM policy: 403]| Role | Permissions | Bound on |
|---|---|---|
simulationLabVm |
compute.instances.delete, compute.instances.getGuestAttributes |
each lab instance |
simulationLabList |
compute.instances.list |
the project |
The list permission is project-level only: the compute API rejects it in an instance-level test, and a rig finds its peers by label. I measured the narrowing options on a rig. Label-based IAM conditions are refused ("Can't set conditional policy on this resource"). A name condition is accepted, but instance names are free text, so it would not bound anything a caller could not satisfy. The instance-level binding is the narrowest scope GCE allows. Measured at the project level, the account holds compute.instances.list and nothing else. On another project VM, its instance-level test returns {}.
Things that used the default identity:
- Idle self-delete tests
compute.instances.deleteon the instance itself. - The AWS federation trust pins
subto the account's unique id.setup-federation.shnow targets the lab account, so re-runsetup-federation.sh uponce to move the trust. - A Vertex arm used the VM account for ADC. It now grants
roles/aiplatform.useron the billed project when that arm runs, and no earlier. SIM_LAB_SERVICE_ACCOUNT=nonestarts a VM with no identity and no idle self-delete.upnames that in its log.preflightreports read-only whether the account and roles exist. The firstupcreates them, and it needs IAM role and service-account create rights on the project.
Proof: 2x2 on GCE rigs
Each arm was probed from inside the VM. The token is projected through jq -r 'has("access_token")' and never printed.
| Probe | main (control) | this change |
|---|---|---|
| Metadata token fetch | true, default compute account, cloud-platform scope |
true, sim-lab-vm, cloud-platform scope |
| Create a throwaway VM | 200, created, deleted at once | 403 |
| List secrets | 200 | 403 |
| Read project IAM policy | 200 | 403 |
| Delete own instance (permission test) | granted | granted |
| Delete another project VM (permission test) | not probed | {} |
The scope stays cloud-platform because the roles are the boundary, and a narrower scope would only restrict API families the account cannot reach anyway.
On the fix arm, a two-member --rig with SIM_IDLE_MINUTES=2: both members went idle, and both deleted together at the same second (sim-lab-vm is the caller on both delete operations). Every VM of the proof is gone: gcloud compute instances list --filter='name~labsa' returns nothing.
Tests
lab-identity-selftest.sh (new), idle-delete-selftest.sh, preflight-selftest.sh and engine-selftest.sh pass offline with stubs. shellcheck at warning, prose-check, vale-check and word-budget pass. The word budget sits at its ceiling, so I cut three phrases in operations.md to offset the one sentence added.