How do you get into a private server?
Builds on29. SSH
| # | Approach | Shape | Cost |
|---|---|---|---|
| 1 | Public SSH | Port 22 open to 0.0.0.0/0; SSH straight to each VM's external IP | Every VM's SSH port is exposed and probed all day. Only the SSH login stands between the internet and a command line on the VM |
| 2 | Bastion host | One locked-down VM with a public IP whose only job is accepting SSH. You log in to it first, then hop on to the other VMs, which have no public SSH | Better, but not solved (below) |
| 3 | IAP TCP forwarding | No public SSH anywhere. Google checks who you are, then carries your SSH connection to the VM through its own network | Section 02 |
- kade-vpc and its private VMs
- Your SSH hops
- The bastion: the one VM exposed to SSH
The bastion pattern. You SSH to the bastion, then hop from it to the VMs behind it.
Wide by design - scroll it sideways.
A bastion has three real costs, numbered in the drawing:
- It is a VM you run. You keep it updated, lock it down and pay for it, and it is only as safe as that upkeep.
- It is still an SSH port open to the internet, just one instead of many. Every scanner finds it and keeps trying.
- If an attacker takes it over, they are already inside the network. That can happen through a stolen SSH key, a guessed password or an unpatched bug. From the bastion they can reach every VM behind it, exactly as you do.
A bastion reduces exposure to one box. It does not remove it.
What IAP TCP forwarding is
Identity-Aware Proxy (IAP) is a service Google runs for you. It is not a VM in your project: there is nothing to install, size or patch. It runs on Google's own servers, inside Google's network, and Google keeps it running and up to date. Your part is to switch it on (enable its API) and decide who may use it (IAM). It is called a proxy because it stands in the middle: you connect to IAP, and IAP makes the connection to your VM on your behalf. Its TCP forwarding feature does that for a VM's SSH port, or any other TCP port.
Here is the trip. Your laptop connects to IAP over ordinary HTTPS. IAP checks your Google sign-in and asks IAM whether you may reach that VM on that port. Only then does it open a connection to the VM's internal IP, from its own range 35.235.240.0/20, across Google's network and never the public internet. The VM needs no external IP, and its firewall lets port 22 in from IAP's range only. An attacker has two ways to try, and both end early: IAP turns away anyone who is not an allowed identity, and going straight at the VM fails because it has no public address to connect to.
- Google's network, which holds IAP and kade-vpc alike
- kade-vpc: the part you configure
- Your connection
- The firewall rule for IAP
A bastion asks where you connect from: if you can reach its IP and have a key, you are in. IAP asks who you are: you must sign in as a Google identity that IAM allows. That is what "identity-aware" means.
Wide by design - scroll it sideways.
What gcloud compute ssh kade-db --tunnel-through-iap actually does:
| # | Step | Detail |
|---|---|---|
| 1 | gcloud proves who you are | Using your gcloud login |
| 2 | It opens a tunnel to IAP | An ordinary HTTPS connection on port 443 (technically a WebSocket), so it works even from office or café networks that block outgoing port 22 |
| 3 | IAP checks IAM | Does your identity have permission to tunnel to this VM and port? |
| 4 | IAP connects to the VM | A normal TCP connection to 10.10.2.5:22, from an address in 35.235.240.0/20 |
| 5 | SSH runs inside the tunnel | Your SSH client talks to the VM's SSH server through the tunnel, encrypted end to end. IAP passes the bytes along but cannot read them |
The fixed range: 35.235.240.0/20
IAP's connections never come from your laptop's IP or a random Google address. They always come from 35.235.240.0/20, so the firewall rule for IAP SSH names that range and nothing else: allow tcp:22 from 35.235.240.0/20. Compare the wide version, allow tcp:22 from 0.0.0.0/0, which lets in the whole internet.
- Google's network, with IAP and kade-vpc inside it
- kade-vpc
- Your connection, through IAP
- The IAP-only firewall rule
Two layers guard port 22. You get through because you sign in to IAP and IAP connects for you, from its own range. Google's edge stops anyone faking IAP's addresses, and the firewall stops everyone else. The scanner only gets this far when someone has given the VM an external IP, which is exactly the team's habit from the top of this chapter.
Wide by design - scroll it sideways.
This range is one of the special paths from chapter 4. Nobody else can send from it: Google's edge drops any packet from the internet that fakes one of these addresses, and you cannot use the range for your own subnets. So a connection from this range has always passed IAP's identity check first, and a firewall rule can safely trust it. The firewall narrows the network path to IAP only, and IAP narrows access to allowed identities only: two narrow layers instead of one wide-open one.
The three things that must all be true
IAP SSH works only when all three are true:
- The IAP API is enabled on the project (
iap.googleapis.com). - Your identity holds the IAM role
roles/iap.tunnelResourceAccessor(orroles/owner, which includes it). - A firewall rule allows IAP's range: tcp:22 from
35.235.240.0/20.
Each one is checked at a different point on the connection's path, and that decides which error you see and how fast:
- Google's network; checks 1 and 2 are made by IAP
- Checked in kade-vpc and on the VM
- The path, when every check passes
- The firewall rule
Checks 1 and 2 happen at IAP, so IAP refuses you straight away with 4033. Checks 3 and 4 happen on the last hop, after IAP has already accepted you, so you wait while IAP tries to reach the VM, then see 4003.
Wide by design - scroll it sideways.
They are independent, and a failure often looks like a different problem:
| # | Missing | What you see | Why it confuses |
|---|---|---|---|
| 1 | The API | An authorization error, 4033: not authorized | IAM and firewall can be perfect and it still fails. A disabled API is invisible unless you check for it |
| 2 | The IAM role | The same 4033: not authorized | roles/editor does not include IAP tunnel access. People assume broad access covers it |
| 3 | The firewall rule | A pause, then 4003: failed to connect to backend | IAP accepted you; it is the last hop, IAP to the VM, that fails. So the error shows up later than the real cause |
| 4 | (bonus) SSH is not running on the VM | Also 4003 | Same symptom as a missing rule. Check the VM's serial output |
Try it: why does my IAP SSH fail?
Shell open. IAP gate passed.
- 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.
Giving each person only what they need
The IAM role can be granted at different levels, and with conditions:
| # | Grant | Effect |
|---|---|---|
| 1 | On the project | Can open tunnels to every VM, on any port the firewall allows from IAP |
| 2 | On one VM | Can open tunnels to that VM only |
| 3 | With an IAM condition on the port | A condition is an extra test attached to the grant. For example: only port 22, never 5432 |
A tunnel only works when both gates say yes: IAM (may this person tunnel to this VM and port?) and the firewall (may IAP's range reach that port at all?). Each tab is one grant, and the drawing shows every VM and port it opens:
user:you@kade.lk · opens 4 of 5 targets
One grant on the project opens a tunnel to every VM, on any port. IAM is not the only gate, though: kade-db's PostgreSQL port has no firewall rule from IAP's range, so that tunnel would open and then fail with 4003. IAM decides who; the firewall decides which ports can be reached at all.
Show as config
you@kade.lk → roles/iap.tunnelResourceAccessor on project kade-prod → can tunnel to every VM, on 22 and on any other port IAP's range is allowed to reach
IAP for more than SSH
TCP forwarding carries any TCP port through the same authenticated tunnel:
| # | Port | Use |
|---|---|---|
| 1 | 22 | SSH: the main use, replacing bastions and public SSH |
| 2 | 3389 | RDP: the same pattern for Windows VMs |
| 3 | Anything else | A database, an internal admin panel, any internal TCP service. Tunnel to it instead of exposing it |
Each tab follows one connection from an app on your laptop, through IAP, to a port on a VM. The piece on your laptop is always gcloud: it either carries an SSH session, or listens on a local port and forwards whatever arrives there.
The everyday case. gcloud starts your SSH client and carries its bytes through IAP to port 22. Nothing listens on your laptop, and SSH encrypts everything end to end.
Show as config
gcloud compute ssh kade-api-1 --zone=asia-southeast1-a --tunnel-through-iap
IAP tunnels are for admin work, not heavy traffic: do not route an application through one. Chapter 9 moves the database to Cloud SQL, which has its own private path.
For a web page that many people use, there is a better fit than a tunnel: IAP for HTTPS, in the next section.
IAP for web pages: IAP on the load balancer
IAP has a second form. TCP forwarding (sections 02 to 06) guards a port, and you reach it with gcloud. IAP for HTTPS guards a web page, and people reach it with nothing but a browser. You switch it on for a load balancer's backend service, and from then on every request to that backend must pass IAP before the load balancer forwards it. It also works directly on a Cloud Run service, with no load balancer (chapter 10).
It is the right tool when many people need a page every day: an internal dashboard, a staging site, an admin tool. Kadé's example is kade-worker's job-queue dashboard on port 8080, published for the whole team as https://admin.kade.lk. It can share the shop's existing load balancer: one more host in the URL map and one more certificate. Chapters 11 and 12.1 explain the load balancer's parts in full; here only the IAP step is new.
- Google's network, where the load balancer and IAP run
- kade-vpc and kade-worker
- An allowed request
- Load balancer parts and the firewall rule
One request to admin.kade.lk, numbered in the order it meets each part.
Wide by design - scroll it sideways.
- DNS turns
admin.kade.lkinto the load balancer's IP,34.120.88.10. - The forwarding rule is that IP and port 443: where the request arrives.
- The target proxy ends TLS, with a certificate for
admin.kade.lk. From here the request can be read. - The URL map sends requests for
admin.kade.lkto the backend servicekade-admin, which points at an instance group holding kade-worker (chapter 12.2). - IAP, on that backend service, checks that you are signed in and that IAM grants you
roles/iap.httpsResourceAccessoron it. If you pass, it forwards the request with a signed header saying who you are. If you are not signed in yet, it sends you to Google sign-in first. - The firewall lets port 8080 in from the load balancer's ranges,
35.191.0.0/16and130.211.0.0/22. Not IAP's35.235.240.0/20: that range is only for TCP forwarding. - The dashboard checks the signed header before trusting it.
Who gets the page
Each tab is one request, and the drawing shows how far it gets. The last tab is the one people forget: IAP only guards the path through the load balancer.
Staff member sees: Google sign-in page. IAP stopped the request before it reached kade-vpc.
IAP finds no session, so it does not send the request on. It redirects the browser to Google sign-in. After signing in, the browser comes back to admin.kade.lk with a session cookie, and the next request carries it. Nothing reached kade-worker.
Show as config
GET https://admin.kade.lk/ → 302 redirect to accounts.google.com (sign in) → back to admin.kade.lk with an IAP session cookie
The two kinds of IAP
They share a name and a sign-in, and almost everything else differs:
- IAP: the identity check
- The VM
- The firewall rule each one needs
Same idea, different gates. The role, the firewall rule and the source address the VM sees all change with the kind of IAP.
Wide by design - scroll it sideways.
Use TCP forwarding for your own admin work: SSH, a database query, a one-off look at a port. Use IAP for HTTPS for pages that a team opens in a browser every day.
Why it beats the alternatives
| # | Public SSH | Bastion host | IAP | |
|---|---|---|---|---|
| 1 | SSH ports exposed to the internet | Every VM | One (the bastion) | None |
| 2 | An extra VM to run, patch and secure | No | Yes | No |
| 3 | Access decided by | SSH login only | Reaching the bastion, plus SSH login | Identity, IAM and firewall |
| 4 | Every access tied to a named person | No | Partly: SSH keys are often shared | Yes, and logged |
| 5 | Works with no external IP on the VM | No | Not for the bastion itself | Yes |
Back to Kadé
Commands in this chapter
Commands 1 to 4 are read-only or only open your own session. Commands 5 to 10 change the project.
Check the IAP API is on
- What it shows: Whether
iap.googleapis.comis enabled in the project. - What to look for: One line of output. Empty means it is off, and IAP will answer
4033whatever IAM says.
gcloud services list --enabled --filter="name:iap.googleapis.com"Test every requirement at once
- What it shows: A pass or fail for the API, IAM, the firewall path and the VM state.
- What to look for: The first failed check. Fix that one before looking at anything else.
gcloud compute ssh kade-db --zone=asia-southeast1-b --tunnel-through-iap --troubleshootSSH through IAP and run one command
- What it shows: Whether IAP SSH works end to end, without opening an interactive shell.
- What to look for:
ok. Run it for every VM before removing the old rule.
gcloud compute ssh kade-api-1 --zone=asia-southeast1-a --tunnel-through-iap --command="echo ok"Tunnel another port to your laptop
- What it shows: Makes kade-db's PostgreSQL appear at
localhost:5433while the command runs. - What to look for: "Listening on port [5433]". Needs a firewall rule for 5432 from IAP's range; otherwise use SSH forwarding (section 06, the "Database, through SSH" tab).
gcloud compute start-iap-tunnel kade-db 5432 \ --local-host-port=localhost:5433 --zone=asia-southeast1-bEnable the IAP API (changes the project)
- What it shows: Turns on requirement 1.
- What to look for: Command 1 now prints the service.
gcloud services enable iap.googleapis.comGrant IAP tunnel access to one person on one VM (changes the project)
- What it shows: Section 05, tab B: the freelancer may tunnel to kade-worker only. For project-wide access, use
gcloud projects add-iam-policy-bindingwith the same role. - What to look for: The binding appears on the VM's IAM policy. Remove it with
remove-iam-policy-bindingwhen the work ends.
gcloud compute instances add-iam-policy-binding kade-worker \ --zone=asia-southeast1-a \ --member=user:dev@freelance.example \ --role=roles/iap.tunnelResourceAccessorAdd the IAP-only SSH rule (changes the project)
- What it shows: Step 1 in Back to Kadé. Adds a path; opens nothing new to the internet.
- What to look for: The rule in the firewall list, and command 3 working for every VM.
gcloud compute firewall-rules create kade-ssh-from-iap \ --network=kade-vpc --direction=INGRESS --action=ALLOW --priority=900 \ --rules=tcp:22 --source-ranges=35.235.240.0/20 --enable-loggingRemove the world-open rule (changes the project)
- What it shows: Step 3 in Back to Kadé: disable, check again, then delete.
- What to look for: Command 3 still works after the disable. Only then run the delete.
gcloud compute firewall-rules update kade-allow-ssh --disabledgcloud compute firewall-rules delete kade-allow-sshSwitch on IAP for the admin page (changes the project)
- What it shows: Turns on IAP for HTTPS for the backend service behind
admin.kade.lk(section 07, step 5). - What to look for: Opening
https://admin.kade.lkin a private window now sends you to Google sign-in first.
gcloud iap web enable --resource-type=backend-services --service=kade-adminLet the team open the admin page (changes the project)
- What it shows: Grants the HTTPS role to a group on that one backend service. Note the role:
httpsResourceAccessor, not the tunnel role. - What to look for: A member of the group sees the dashboard after signing in; anyone else gets a 403 from IAP.
gcloud iap web add-iam-policy-binding --resource-type=backend-services --service=kade-admin \ --member=group:staff@kade.lk --role=roles/iap.httpsResourceAccessor