The older way: SSH keys in metadata
Builds on29. SSH keys
Without OS Login, a VM decides who may log in by a list of SSH public keys kept in metadata, either on the project (every VM) or on one VM. A small Google program on each VM, the guest agent, copies that list into the VM. Anyone who holds the matching private key can log in. Nothing asks whether that person should still have access.
- Kadé's VMs, each accepting every key
- The key list in project metadata
One list, copied to every VM. The freelancer's key is still on it, so the freelancer can still log in everywhere, and the VM's log names only the key.
Wide by design - scroll it sideways.
The part most people miss is how the keys got there. When OS Login is off and you run gcloud compute ssh for the first time, gcloud creates a key pair on your laptop and adds your public key to the project's metadata, if you have permission to edit it. So every person who has ever SSHed in with gcloud has usually left a key behind that works on every VM.
| # | Problem | What it means |
|---|---|---|
| 1 | Keys are not tied to people | A key is just a key. Someone leaves, and their key keeps working until a human removes it |
| 2 | Project keys spread everywhere | A key in project metadata works on every VM in the project. Giving access to one VM usually gives access to all of them |
| 3 | No central control | Managing access means editing metadata by hand, key by key, and hoping nobody forgets to clean up |
| 4 | Weak record | Logs show which key logged in, not which person |
block-project-ssh-keys=TRUE. That narrows the problem but does not fix it; OS Login does.The better way: OS Login
OS Login is a Google service that ties SSH login to Google identities and IAM, instead of keys kept in metadata. Like IAP, it is nothing you install or run as a server. It has two halves:
- At Google: OS Login keeps a profile for each person: their Linux username, their user ID and their public SSH keys, all tied to their Google account. IAM records who may log in and who may use sudo.
- On each VM: small parts of Google's guest environment, already installed in Google's public images, connect Linux to that profile. They let sshd fetch a person's keys from Google, and let Linux know accounts such as
you_kade_lkthat appear in no local user file. Settingenable-oslogin=TRUEin metadata switches them on.
The difference from metadata keys is when access is decided. With metadata keys, it was decided once, when someone added a key, maybe a year ago. With OS Login, the VM asks at every login: whose key is this, and does IAM let that person in right now? It asks through its metadata server, 169.254.169.254, a Google service that every VM reaches on its own host. So the check needs no internet access and no external IP, which matters for the rest of this chapter.
- Google's: OS Login, which holds your profile and asks IAM
- kade-vpc and kade-db
- Your login
Your key lives on your OS Login profile, tied to you@kade.lk, not in the VM's metadata. Each login is checked against IAM at that moment, so removing a role ends access on every VM at once.
Wide by design - scroll it sideways.
| # | Property | Detail |
|---|---|---|
| 1 | Controlled by IAM roles | roles/compute.osLogin for a normal user, roles/compute.osAdminLogin for a user with sudo |
| 2 | Revocation is instant and central | Remove the role or disable the Google account, and access to every VM ends at once. No stray keys to hunt |
| 3 | Logins map to real people | The Linux username comes from the Google identity, so logs show who logged in |
| 4 | Metadata keys are ignored | Once OS Login is on for a VM, keys in project or instance metadata no longer work there. That is how the freelancer's old key stops working |
| 5 | Optional second factor | OS Login can require 2-step verification on top of the key (enable-oslogin-2fa=TRUE) |
| 6 | Turned on with metadata | enable-oslogin=TRUE on the project (all VMs) or on one VM. An organization policy can require it everywhere (chapter 17) |
Why logging in needs the VM's service account
Logging in to a VM gives you more than a shell. It also gives you the VM's own Google identity:
- A VM runs as a service account. kade-db runs as
sa-kade-db, and Google API calls from kade-db carry that service account's permissions. - Any program on the VM can use it. The metadata server,
169.254.169.254, hands the service account's token to whoever asks from inside the VM. It cannot tell the database apart from a person who has logged in. - So a login can borrow those permissions. That is called privilege escalation: getting access your own roles never gave you.
- Google's: the metadata server and Secret Manager
- kade-vpc and kade-db
dev2 has no Secret Manager access, but one command on kade-db runs as sa-kade-db and reads the database password.
Wide by design - scroll it sideways.
Google closes this gap with one firm rule: nobody may use a service account's permissions unless IAM says they may act as it, which is what roles/iam.serviceAccountUser grants. Google checks it at every point where a service account could be borrowed:
| # | A way to borrow sa-kade-db | Where Google checks serviceAccountUser |
|---|---|---|
| 1 | Create a new VM that runs as sa-kade-db, then log in to it | When you create the VM |
| 2 | Deploy a Cloud Run service that runs as sa-kade-db | When you deploy (chapter 10) |
| 3 | Log in to kade-db, which already runs as sa-kade-db | When you log in, by OS Login |
Two habits follow:
- Grant
serviceAccountUseron each VM's own service account, not on the project. On the project, the person could act as every service account in it. - Keep each service account's permissions small, as chapter 5.1 did by giving each VM its own. Logging in to a VM always brings its permissions along.
Who can log in
Each tab is one person or key trying to log in. The four checks run in order, and the first one that fails decides:
you@kade.lk: logged in as you_kade_lk, sudo works.
Every check passes. osAdminLogin is the role with sudo, so you can run admin commands. The Linux user comes from your identity, and that is the name the VM's logs show.
Show as config
you@kade.lk → roles/compute.osAdminLogin on project kade-prod
roles/iam.serviceAccountUser on sa-kade-dbTwo gates, both about identity
- Google's network; gate 1 is IAP
- kade-vpc, where the VM runs
- Your connection
- Gate 2: OS Login on the VM
IAP controls reaching the VM by identity. OS Login controls logging in by identity. Together, access is decided entirely by who you are, and one IAM change removes it everywhere.
Wide by design - scroll it sideways.
Try both gates together. Each setting matches one requirement:
Shell open. Both gates passed on identity alone.
- gcloud logs you in and opens an HTTPS tunnel to IAP on port 443.
- IAP accepts you: API on, IAM role present.
- Firewall lets 35.235.240.0/20 reach port 22.
- The tunnel reaches the SSH server on the VM.
- OS Login accepts you as you_kade_lk, with sudo.
Switching to OS Login safely
Do this after chapter 6.1's IAP change is proven, not at the same time. Changing both the access path and the login method at once makes it hard to tell which one broke if something does.
| # | Step | Check |
|---|---|---|
| 1 | Grant the roles first: compute.osAdminLogin for you, serviceAccountUser on each VM's service account | The grants show in IAM |
| 2 | Turn on OS Login for one VM: enable-oslogin=TRUE on kade-api-2 | IAP SSH to kade-api-2 logs you in as you_kade_lk, and sudo works |
| 3 | Turn it on for the project | SSH works on every VM |
| 4 | Remove the old ssh-keys from project metadata | Nothing changes for anyone, because OS Login already ignores them. This is cleanup, and it removes the fallback if OS Login is ever turned off |
| 5 | Later: require OS Login with an organization policy | Chapter 17 |
Removing a VM's external IP: the full picture
Builds on10. NAT
Can a VM have no external IP and still do its job? Yes. Every kind of traffic a VM needs has its own path that does not use the VM's own public address. There are four, each from a different chapter:
- Google's network: IAP, the load balancer and Google APIs
- kade-vpc, its VMs and Cloud NAT
- Traffic coming in
Wide by design - scroll it sideways.
| # | Without an external IP, the VM would be… | Fixed by | Chapter |
|---|---|---|---|
| 1 | Unreachable for admin work | IAP TCP forwarding | 6.1 |
| 2 | Unable to reach Google APIs (Cloud Storage, Secret Manager, Logging) | Private Google Access | 9 |
| 3 | Unable to reach the internet (apt, PayGate) | Cloud NAT in its region | 7 |
| 4 | Unreachable for the public (a website or an API) | An external load balancer | 11, 12 |
Path 4 is how a VM serves the public without a public address of its own, and it is the one most people ask about:
- The public IP lives on the load balancer, not the VM. Shoppers connect to
34.120.88.10, which belongs to the load balancer's forwarding rule. The load balancer then connects to the VMs on their internal IPs, across Google's network (chapter 12.2 builds this for kade-media). - The firewall lets the app's port in from the load balancer's ranges only:
35.191.0.0/16and130.211.0.0/22, the same ranges its health checks use. Nobody else can reach port 8080. - Cloud NAT does not help here. NAT is outbound only: it lets a VM start connections out, and it never accepts a new connection in. Inbound public traffic always needs a load balancer, or an external IP.
- The VM sees the load balancer's address, not the shopper's. The shopper's real IP arrives in a header (chapter 12.1).
With all four paths in place, a VM does its whole job while nothing on the internet can start a connection to it directly. A VM becomes reachable from the internet in only two ways: an external IP of its own, or a place behind an external load balancer. Take away the first, and the only way in is the one Kadé builds on purpose (chapters 11 to 13). That makes no external IP the strongest position a VM can be in.
Back to Kadé
Commands in this chapter
Commands 1 to 3 are read-only. Commands 4 to 7 change the project.
See SSH keys and OS Login settings in project metadata
- What it shows: Project-wide metadata, including
ssh-keysandenable-oslogin. - What to look for: Keys whose owner you cannot name, and whether
enable-osloginisTRUE.
gcloud compute project-info describe \ --format="yaml(commonInstanceMetadata.items)"See one VM's own metadata
- What it shows: Instance-level keys and settings, which override the project's.
- What to look for:
enable-oslogin=FALSEon a single VM, which would switch it back to metadata keys.
gcloud compute instances describe kade-db --zone=asia-southeast1-b \ --format="yaml(metadata.items)"See your OS Login profile
- What it shows: Your Linux username and the keys registered to your identity.
- What to look for: The username, e.g.
you_kade_lk, which is what shows in the VM's logs.
gcloud compute os-login describe-profileGrant OS Login with sudo (changes the project)
- What it shows: Lets you log in to every VM as an admin.
- What to look for: Use
roles/compute.osLogininstead for people who do not need sudo.
gcloud projects add-iam-policy-binding kade-prod \ --member=user:you@kade.lk --role=roles/compute.osAdminLoginAllow using a VM's service account (changes the project)
- What it shows: The second role trap: needed to log in to a VM that runs as sa-kade-db.
- What to look for: Without it, OS Login refuses even with the login role.
gcloud iam service-accounts add-iam-policy-binding \ sa-kade-db@kade-prod.iam.gserviceaccount.com \ --member=user:you@kade.lk --role=roles/iam.serviceAccountUserTurn on OS Login for one VM, then the project (changes the project)
- What it shows: Steps 2 and 3 of section 04.
- What to look for: After the first line, test SSH to kade-api-2 before running the second.
gcloud compute instances add-metadata kade-api-2 --zone=asia-southeast1-b \ --metadata=enable-oslogin=TRUEgcloud compute project-info add-metadata --metadata=enable-oslogin=TRUERemove the old keys from project metadata (changes the project)
- What it shows: Step 4 of section 04: cleanup of every project-wide SSH key.
- What to look for:
ssh-keysgone from command 1. Save command 1's output first as a record.
gcloud compute project-info remove-metadata --keys=ssh-keys