Chapters

Admin access 1: IAP TCP forwarding

Chapter 5.1 fixed who can reach the app and the database, but left port 22 open to the whole internet, because the team still needs a way in. This chapter replaces that open port with a path where access depends on who you are, not where you connect from.

Controlling access · chapter 8 of 26 · 10 min read

How do you get into a private server?

Builds on29. SSH

#ApproachShapeCost
1Public SSHPort 22 open to 0.0.0.0/0; SSH straight to each VM's external IPEvery 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
2Bastion hostOne 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 SSHBetter, but not solved (below)
3IAP TCP forwardingNo public SSH anywhere. Google checks who you are, then carries your SSH connection to the VM through its own networkSection 02
internetYouSSH keySSH scannersprobe :22 all daykade-vpcbastionexternal IPport 22 opento the internetsshkade-api-1no public SSHsshkade-dbno public SSHan attacker who takes over the bastion is already inside123
  • 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:

  1. 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.
  2. It is still an SSH port open to the internet, just one instead of many. Every scanner finds it and keeps trying.
  3. 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.

internetYour laptopgcloud, signed inas you@kade.lkAttackera scanner, or astolen SSH keyGoogle's networkIAPrun by Google, outside your VPC1 who are you?(Google sign-in)2 may you reach this VMon this port? (IAM)3 yes: connect for youHTTPSinside Google's networkfrom 35.235.240.0/20kade-vpc (yours)firewall22 only fromIAP's rangekade-db10.10.2.5no external IP✕refused by IAP:not an allowed identity✕straight at kade-db: it has no external IP, so there is no address to reach
  • 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:

#StepDetail
1gcloud proves who you areUsing your gcloud login
2It opens a tunnel to IAPAn ordinary HTTPS connection on port 443 (technically a WebSocket), so it works even from office or café networks that block outgoing port 22
3IAP checks IAMDoes your identity have permission to tunnel to this VM and port?
4IAP connects to the VMA normal TCP connection to 10.10.2.5:22, from an address in 35.235.240.0/20
5SSH runs inside the tunnelYour 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.

internetYousigned in, over HTTPSScannerfrom 203.0.113.99Fakerclaims 35.235.240.7Google's networkIAPconnects for you from35.235.240.9kade-vpcfirewallallow tcp:22from35.235.240.0/20kade-dbport 22✓✕not IAP's range: droppedwith 0.0.0.0/0, itwould reach SSH✕faked IAP address:dropped at Google's edge
  • 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:

  1. The IAP API is enabled on the project (iap.googleapis.com).
  2. Your identity holds the IAM role roles/iap.tunnelResourceAccessor (or roles/owner, which includes it).
  3. 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:

Your laptopgcloud sshGoogle's networkIAP · run by Googlekade-vpc1IAP APIenabled?✕ 4033not authorized2IAM roletunnel access?✕ 4033not authorized3firewall22 from IAP?✕ 4003failed to connectafter a pause4kade-dbSSH running?✕ 4003same as 3after a pauseIAP refuses you itself: the answer is quickIAP let you in; the last hop fails
  • 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:

#MissingWhat you seeWhy it confuses
1The APIAn authorization error, 4033: not authorizedIAM and firewall can be perfect and it still fails. A disabled API is invisible unless you check for it
2The IAM roleThe same 4033: not authorizedroles/editor does not include IAP tunnel access. People assume broad access covers it
3The firewall ruleA pause, then 4003: failed to connect to backendIAP 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 VMAlso 4003Same symptom as a missing rule. Check the VM's serial output

Try it: why does my IAP SSH fail?

IAP API
Your IAM role
Firewall for port 22
SSH server on the VM

Shell open. IAP gate passed.

  1. gcloud logs you in and opens an HTTPS tunnel to IAP on port 443.
  2. IAP accepts you: API on, IAM role present.
  3. Firewall lets 35.235.240.0/20 reach port 22.
  4. 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:

#GrantEffect
1On the projectCan open tunnels to every VM, on any port the firewall allows from IAP
2On one VMCan open tunnels to that VM only
3With an IAM condition on the portA 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:

WHOIAM GRANTVM AND PORTYouuser:you@kade.lkiap.tunnelResourceAccessoron project kade-prodany portkade-api-1 :22can tunnelIAM yes · firewall yeskade-api-2 :22can tunnelIAM yes · firewall yeskade-db :22can tunnelIAM yes · firewall yeskade-worker :22can tunnelIAM yes · firewall yeskade-db :5432firewall blocksIAM yes · no rule from IAP for this port✕

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:

#PortUse
122SSH: the main use, replacing bastions and public SSH
23389RDP: the same pattern for Windows VMs
3Anything elseA 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.

your laptopsshyour terminalgcloud compute sshcarries ssh to IAPGoogle's networkIAPchecks youkade-vpcfirewalltcp:22 from IAPkade-ssh-from-iapkade-api-1:22sshd

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.

DNSadmin.kade.lk→ 34.120.88.10Browseradmin.kade.lkGoogle's network: the load balancer runs on Google's front endsforwarding rule34.120.88.10port 443target proxyTLS ends hereURL mapadmin.kade.lk→ kade-adminbackend servicekade-adminIAP is onsigned in?IAM role?adds who you arenot signed in yet: sent to Google sign-in, then back with a session cookiekade-vpcfirewall8080 from LB rangeskade-worker :8080checks the header1234567
  • 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.

  1. DNS turns admin.kade.lk into the load balancer's IP, 34.120.88.10.
  2. The forwarding rule is that IP and port 443: where the request arrives.
  3. The target proxy ends TLS, with a certificate for admin.kade.lk. From here the request can be read.
  4. The URL map sends requests for admin.kade.lk to the backend service kade-admin, which points at an instance group holding kade-worker (chapter 12.2).
  5. IAP, on that backend service, checks that you are signed in and that IAM grants you roles/iap.httpsResourceAccessor on 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.
  6. The firewall lets port 8080 in from the load balancer's ranges, 35.191.0.0/16 and 130.211.0.0/22. Not IAP's 35.235.240.0/20: that range is only for TCP forwarding.
  7. 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 memberno session yetsees:Google sign-in pageGoogle's networkload balancer34.120.88.10 · TLSURL map → kade-adminbackend servicekade-admin · IAP on1 signed in?2 IAM role?✕not checked yetredirect to Google sign-inkade-vpcAnother VMin kade-vpcforged headerfirewall8080 fromLB rangeskade-worker :8080job-queue dashboard

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 TCP FORWARDING · sections 02 to 06Your laptopgcloud opensa tunnelIAPiap.tunnelResource-Accessor · per connectionfirewall22 from35.235.240.0/20VM :22or any TCP portsees IAP's rangeIAP FOR HTTPS · this sectionBrowserjust a URL,no gcloudLoad balancer + IAPiap.httpsResource-Accessor · per requestfirewall8080 from 35.191.0.0/16,130.211.0.0/22VM :8080a web appsees LB ranges
  • 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 SSHBastion hostIAP
1SSH ports exposed to the internetEvery VMOne (the bastion)None
2An extra VM to run, patch and secureNoYesNo
3Access decided bySSH login onlyReaching the bastion, plus SSH loginIdentity, IAM and firewall
4Every access tied to a named personNoPartly: SSH keys are often sharedYes, and logged
5Works with no external IP on the VMNoNot for the bastion itselfYes

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.com is enabled in the project.
  • What to look for: One line of output. Empty means it is off, and IAP will answer 4033 whatever 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 --troubleshoot

SSH 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:5433 while 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-b

Enable 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.com

Grant 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-binding with the same role.
  • What to look for: The binding appears on the VM's IAM policy. Remove it with remove-iam-policy-binding when the work ends.
gcloud compute instances add-iam-policy-binding kade-worker \  --zone=asia-southeast1-a \  --member=user:dev@freelance.example \  --role=roles/iap.tunnelResourceAccessor

Add 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-logging

Remove 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-ssh

Switch 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.lk in a private window now sends you to Google sign-in first.
gcloud iap web enable --resource-type=backend-services --service=kade-admin

Let 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

Spotted a mistake, or something unclear? Suggest an edit on GitHub

See it broken

Harbour is a platform built wrong on purpose. These defects are this chapter's ideas, left undone: what each one breaks, how you would find it, and the change that closes it.

  1. D2
  2. D3a
    Bastion carries an external IP

    mediumcloses in phase 2, Secure access

    Goes with How do you get into a private server?