├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── img
├── .DS_Store
├── 000-IAP-gce-demo-main godemos.png
├── 000-IAP-gce-demo-main.png
├── 010-Google-SDK-sign-in.png
├── 011-Google-SDK-password.png
├── 012-Google-SDK-access.png
├── 020-AuthAppDef-sign-in.png
├── 021-AuthAppDef-access.png
├── 030-test-web.png
├── 040-test-ssh.png
├── 041-show-gcloud.png
├── 042-ssh-login.png
├── 070-vm-line.png
├── 071-instance-info.png
├── 072-instance-group.png
├── 073-backend-service.png
├── 075-url-map.png
├── 076-ssl-policy.png
├── 077-ssl-cert.png
├── 080-iap-https.png
└── 081-iap-ssh.png
└── modules
├── 00-global-variables
├── .gitignore
└── outputs.tf.example
├── 10-enable-apis
├── main.tf
├── output.tf
└── providers.tf
├── 20-create-iap-brand
├── main.tf
├── outputs.tf
└── providers.tf
└── 90-build-demo
├── main.tf
├── outputs.tf
└── providers.tf
/.gitignore:
--------------------------------------------------------------------------------
1 | # Mac specific items
2 | .DS_Store
3 |
4 | # Local .terraform directories
5 | **/.terraform/*
6 |
7 | # .tfstate files
8 | *.tfstate
9 | *.tfstate.*
10 |
11 | # Terraform generated lock files
12 | .terraform.lock.hcl
13 |
14 | # Crash log files
15 | crash.log
16 |
17 | # Vi editor swap files
18 | *.swp
19 |
20 | # Ignore any .tfvars files that are generated automatically for each Terraform run. Most
21 | # .tfvars files are managed as part of configuration and so should be included in
22 | # version control.
23 | #
24 | # example.tfvars
25 |
26 | # Ignore override files as they are usually used to override resources locally and so
27 | # are not checked in
28 | override.tf
29 | override.tf.json
30 | *_override.tf
31 | *_override.tf.json
32 |
33 | # Include override files you do wish to add to version control using negated pattern
34 | #
35 | # !example_override.tf
36 |
37 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
38 | # example: *tfplan*
39 | plan.out
40 |
41 | terraform.tfvars
42 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # How to Contribute
2 |
3 | We'd love to accept your patches and contributions to this project. There are
4 | just a few small guidelines you need to follow.
5 |
6 | ## Contributor License Agreement
7 |
8 | Contributions to this project must be accompanied by a Contributor License
9 | Agreement. You (or your employer) retain the copyright to your contribution;
10 | this simply gives us permission to use and redistribute your contributions as
11 | part of the project. Head over to
Figure 1 - Identity-Aware Proxy demo architecture
17 | 18 | Figure 1 shows a web environment with an HTTPS Global Load Balancer frontend and an unmanaged instance group with a Compute Engine instance as the backend.. 19 | The instance runs the Ubuntu operating system and uses Apache to provide web services. 20 | The instance uses [Cloud NAT](https://cloud.google.com/nat/docs/overview) for outbound internet access in order to download packages and updates. 21 | The instance has no external IP address to support access from the internet. 22 | All inbound access, both for web (HTTPS) and ssh (TCP) will be provided by IAP. 23 | 24 | The left section of Figure 1 shows the IAP TCP flow. A user wanting to SSH into the back end instance clicks the SSH but on the Compute Engine console or uses "gcloud compute ssh --tunnel-through-iap" to begin the session. 25 | IAP then prompts the user for an identity. 26 | If the identity has the "IAP-secured Tunnel User" role, an encrypted tunnel is created between the console or the gcloud client to the Google endpoint tunnel.cloudproxy.app. After the tunnel is created, SSH then begins an encrypted session through the encrypted tunnel to the instance providing two layers of encryption. 27 | 28 | The right portion of Figure 1 shows the HTTPS flow. 29 | A user browses to a URL secured with an SSL/TLS certificate that points to the IP address of a forwarding rule on the HTTPS Global Load Balancer. 30 | The forwarding rule points to a backend protected by IAP. 31 | IAP prompts the user for an identity. 32 | If the identity has the "IAP-secured Web App User" role, the session passes to the backend web server. 33 | 34 | ## Prerequisites 35 | 36 | 1. You will the project ID of a new Google Cloud project linked to a billing account. You should use a new project to avoid interfering with other workloads. IAP branding, which is used in this demonstration, can only be deleted by deleting the GCP project. 37 | 38 | 2. You will need a workstation with [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) and the [Google Cloud SDK](https://cloud.google.com/sdk/docs/install). Here are the versions used to create this demo: 39 | 40 | - Terraform: v1.1.7 41 | - Google Cloud SDK: 379.0.0 42 | 43 | You can use newer versions of these tools but older versions may not include all of the features used in this demo. Add the terraform and gcloud programs to your command search path. 44 | 45 | 3. You will need a fully qualified DNS hostname and the ability to update its IP address (the "A" record).. 46 | 47 | 4. You will need an SSL certificate and private key associated with the DNS hostname. These files should be accessible from your workstation. The files must be in PEM format. 48 | 49 | 5. You will need a user ID in the Google Cloud project that has accepted the Google Cloud Terms of Service. The user must have the following IAM roles: 50 | 51 | - Compute Admin (for setting up the network, instance, and load balancer) 52 | - IAP Policy Admin (for setting up the Identity Aware Proxy) 53 | - Logging Admin (for managing logs) 54 | - OAuth Config Editor (for updating the OAuth information) 55 | - Service Usage Admin (for allowing Terraform to enable APIs and to use a project for billing and quota purposes) 56 | 57 | User IDs in Google Cloud are in the form of email addresses. 58 | This user ID will also become part of the OAuth and IAP configurations. 59 | The email address must belong to the currently logged in user account. 60 | 61 | 6. There must not be any organization constraints that restrict the ability to deploy the services. You can change these settings in the Google Cloud console under IAM & Admin > Organization Policies at the project level. Examples of such constraints include (but are not limited to): 62 | 63 | - The policy "Restrict Load Balancer Creation Based on Load Balancer Types" must allow the EXTERNAL_HTTP_HTTPS load balancer (the classic Global Load Balancer) type. 64 | 65 | 7. You should be familiar with using the Google Cloud console and Terraform. 66 | 67 | 8. There are minor charges for the resources used, such as an e2.micro Compute Engine instance. To avoid future charges, follow the cleanup instructions. 68 | 69 | 70 | ## Build instructions 71 | 72 | ### Configure authentication 73 | 74 | 1. Launch a terminal session. 75 | 76 | 2. Sign on to your Google Cloud account as the user for the Google Cloud project using the command below. 77 | 78 | ``` 79 | gcloud auth login 80 | ``` 81 | 82 | In addition to the user ID and password, you may also be asked to grant access to the Google Cloud SDK. See Figure 2 for more details. 83 | 84 | 85 | | Enter user | Enter password | Grant access | 86 | | :-: | :-: | :-: | 87 | |  |  |  | 88 | 89 |Figure 2 - Google SDK sign in process
90 | 91 | 92 | 2. Set your default project ID using the command below, using your project ID in place of *INSERT-PROJECT-ID-HERE*. 93 | 94 | ``` 95 | gcloud config set project *INSERT-PROJECT-ID-HERE* 96 | ``` 97 | 98 | 3. Set your application default credentials so Terraform has credentials to run. 99 | 100 | ``` 101 | gcloud auth application-default login 102 | ``` 103 | You may be prompted for a user ID and password and also to grant access to the Google Auth Library as shown in Figure 3. 104 | | Enter user | Grant access | 105 | | :-: | :-: | 106 | |  |  | 107 | 108 | ### Download the demo repository 109 | 110 | 1. Clone this repository to your workstation. 111 | 112 | ``` 113 | git clone https://github.com/google/iap-compute-engine-demo.git 114 | ``` 115 | 116 | 2. Change your working directory to the newly-cloned repository. 117 | 118 | ``` 119 | cd iap-compute-engine-demo 120 | ``` 121 | 122 | 3. Set the environment variable to the current directory for easy navigation. 123 | 124 | ``` 125 | export DEMOHOME=`pwd` 126 | ``` 127 | 128 | 4. Here's a diagram of the repository. 129 | 130 | ``` 131 | . 132 | ├── LICENSE 133 | ├── README.md 134 | ├── img 135 | │ └── 000-IAP-gce-demo-main.png 136 | └── modules 137 | ├── 00-global-variables 138 | │ └── outputs.tf.example 139 | ├── 10-enable-apis 140 | │ ├── main.tf 141 | │ └── providers.tf 142 | ├── 20-create-iap-brand 143 | │ ├── main.tf 144 | │ └── providers.tf 145 | └── 90-build-demo 146 | ├── main.tf 147 | ├── outputs.tf 148 | └── providers.tf 149 | ``` 150 | 151 |Figure 5 - Test of web IAP flow
309 | 310 | 4. To test the SSH flow, on the Google Cloud console, go to the Compute Engine->VM Instances menu. Look at the line containing the demo-web-server instance. Notice that there is no external IP address for the instance. 311 | 312 | To the right of the demo-web-server instance, click the down arrow next to SSH and then click the *View gcloud command* option as shown in Figure 6. 313 | 314 |  315 |Figure 6 - Display options for ssh IAP flow
316 | 317 | 5. You will see The gcloud command as shown in Figure 7. 318 | 319 |  320 |Figure 7 - gcloud command for accessing the instance
321 | 322 | The *gcloud compute ssh* command selects the instance by name, zone, and project. The command also includes a *--tunnel-through-iap* argument to cause the connection to the instance to be brokered by IAP. 323 | 324 | 6. Click the *copy-to-clipboard* icon next to the gcloud command and then bring up the Cloud Shell and paste the gcloud command into the Cloud Shell window and press *enter*. 325 | 326 | 7. You will be asked to provide authentication information. You may be asked to choose a passphrase. If so, make a note of your passphrase in case you need it later. You will then be taken to an ssh session on the web server instance as shown in Figure 8. 327 | 328 |  329 |Figure 8 - SSH session on web server
330 | 331 | You have successfully completed the testing of the web and SSH flows through the Identity-Aware Proxy. In both flows, you were asked to authenticate to establish your identity. After authenticating, you were granted access to the web page and ssh session. 332 | 333 | ### Behind the scenes 334 | 335 | Now that you have built and tested the demo environment, you will now explore some of the underlying services to see how they are configured. You will first examine the configuration of the load balancer backend and work your way up through the load balancer configuration. You will then examine the configuration of IAP. 336 | 337 | 1. In the Google Cloud console, select Compute Engine -> VM Instances. You will see an instance named *demo-web-server* as shown in Figure 9. 338 | 339 |  340 |Figure 9 - Web server instance
341 | 342 | The instance has no external IP address and can therefore not be accessed directly from the internet. The IAP will broker the external access attempts and provide connections to the instances's internal IP address. 343 | 344 | 2. Click on the name of the instance (*demo-web-server*). You will see information about the instance as shown in Figure 10. 345 | 346 |  347 |Figure 10 - Web server instance information
348 | 349 | You can see the instance ID that was displayed on the server home page. Also, the instance belongs to an instance group named *demo-web-server-group*. 350 | 351 | 3. Click on the instance group name *demo-web-server-group*. The instance group appears as shown in Figure 11. 352 | 353 |  354 |Figure 11 - Instance group information
355 | 356 | The instance group is unmanaged and contains only the single web server instance. The instance group is part of the load balancer backend service named **demo-web-server-backend-service*. 357 | 358 | 4. Click on *demo-web-server-backend-service*. The backend service appears as shown in Figure 12. 359 | 360 |  361 |Figure 12 - Backend service information
362 | 363 | 5. Click on *demo-load-balancer-url-map*. Figure 13 shows the load balancer URL map which defines the top level of the load balancer configuration. 364 | 365 |  366 |Figure 13 - Load balancer URL map
367 | 368 | 6. Open the *demo-ssl-policy* link in a new browser tab. In Figure 14, you can see the SSL policy has been set to MODERN which limits the ciphers that are accepted and that a minimum of TLS 1.2 is required. 369 | 370 |  371 |Figure 14 - Load balancer SSL policy
372 | 373 | 7. Go to the browser tab with the URL map and open *demo-certificate* in a new browser tab. In Figure 15, you can see the properties of the certificate that you provided. 374 | 375 |  376 |Figure 15 - Load balancer SSL certificate
377 | 378 | 379 | You have now seen how the load balancer backend service is configured with an instance group that contains a single instance. You will now look at the configuration of IAP. 380 | 381 | 8. In the Google Cloud Console menu click Security > Identity-Aware Proxy. 382 | 383 | 9. As shown in figure 16, do the following: 384 | 385 | - Click *HTTPS RESOURCES* 386 | - Check the box next to *demo-web-server-backend-service* 387 | - Open the toggle next to *IAP-secured Web App User* 388 | 389 |  390 |Figure 16 - IAP HTTPS configuration
391 | 392 | The line that contains *demo-web-server-backend-service* refers to the backend service that you had seen in a previous step. The slider is to the right which means that IAP is enabled on that backend service. Under IAP-secured Web App user, you can see that the name of the test user. The test user is allowed to access the backend service (which ultimately points to the web server) after successfully authenticating. 393 | 394 | 10. As shown in figure 17, do the following: 395 | 396 | - Click *SSH AND TCP RESOURCES* 397 | - Check the box next to *demo-web-server* 398 | - Open the toggle next to *IAP-secured Tunnel User* 399 | 400 | 401 |  402 |Figure 17 - IAP SSH configuration
403 | 404 | The line that contains *demo-web-server* refers to the web-server-instance. The slider is to the right which means that IAP is enabled on that instance. Under IAP-secured Tunnel User, you can see that the name of the test user. The test user is allowed to ssh to the web server after successfully authenticating. 405 | 406 | You will also see a warning next to demo-web-server. The reason for this warning is that the firewall rule in the VPC network only allows TCP port 22 for SSH. You can use IAP TCP forwarding to proxy access to any TCP port. The warning is saying that you may want to open additional ports on the firewall. Since the firewall rule only needs to allow port 22 for SSH tunneling, you can safely ignore the warning. 407 | 408 | You have now seen how IAP is configure for both the web and TCP flows. In the next section, you will clean up the demo environment. 409 | 410 | ### Clean up 411 | 412 | Please follow the steps below to remove the demonstration environment. 413 | 414 | 1. Change your working directory to the 90-build-demo module. 415 | 416 | ``` 417 | cd $DEMOHOME/modules/90-build-demo 418 | ``` 419 | 420 | 2. Use the command below to destroy the demo environment. 421 | 422 | ``` 423 | terraform destroy 424 | ``` 425 | 426 | Respond with *yes* when asked to confirm that you want to destroy the resources. 427 | 428 | 3. Upon successful completion, you will see "Destroy complete." You do not have to destroy any of the other modules. 429 | 430 | 4. If you want to delete the IAP OAuth branding information and are *not* using the GCP project for anything else, you can delete the GCP project. 431 | 432 | ## Error messages 433 | 434 | The table below lists errors you may encounter when deploying this demo and remediation steps. 435 | 436 | | Error | Remediation | 437 | |--|--| 438 | | "UREQ_TOS_NOT_ACCEPTED" | This error arises when deploying the demo with a newly-created user ID. You must log into the Google Cloud console with this user ID and accept the Google Cloud Terms of Service. | 439 | -------------------------------------------------------------------------------- /img/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/.DS_Store -------------------------------------------------------------------------------- /img/000-IAP-gce-demo-main godemos.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/000-IAP-gce-demo-main godemos.png -------------------------------------------------------------------------------- /img/000-IAP-gce-demo-main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/000-IAP-gce-demo-main.png -------------------------------------------------------------------------------- /img/010-Google-SDK-sign-in.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/010-Google-SDK-sign-in.png -------------------------------------------------------------------------------- /img/011-Google-SDK-password.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/011-Google-SDK-password.png -------------------------------------------------------------------------------- /img/012-Google-SDK-access.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/012-Google-SDK-access.png -------------------------------------------------------------------------------- /img/020-AuthAppDef-sign-in.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/020-AuthAppDef-sign-in.png -------------------------------------------------------------------------------- /img/021-AuthAppDef-access.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/021-AuthAppDef-access.png -------------------------------------------------------------------------------- /img/030-test-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/030-test-web.png -------------------------------------------------------------------------------- /img/040-test-ssh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/040-test-ssh.png -------------------------------------------------------------------------------- /img/041-show-gcloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/041-show-gcloud.png -------------------------------------------------------------------------------- /img/042-ssh-login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/042-ssh-login.png -------------------------------------------------------------------------------- /img/070-vm-line.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/070-vm-line.png -------------------------------------------------------------------------------- /img/071-instance-info.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/071-instance-info.png -------------------------------------------------------------------------------- /img/072-instance-group.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/072-instance-group.png -------------------------------------------------------------------------------- /img/073-backend-service.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/073-backend-service.png -------------------------------------------------------------------------------- /img/075-url-map.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/075-url-map.png -------------------------------------------------------------------------------- /img/076-ssl-policy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/076-ssl-policy.png -------------------------------------------------------------------------------- /img/077-ssl-cert.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/077-ssl-cert.png -------------------------------------------------------------------------------- /img/080-iap-https.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/080-iap-https.png -------------------------------------------------------------------------------- /img/081-iap-ssh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/google/iap-compute-engine-demo/6e290d5a6f5312b2fe456a7a045d3576bec47153/img/081-iap-ssh.png -------------------------------------------------------------------------------- /modules/00-global-variables/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore the outputs.tf file in this directory since it is modified to 2 | # define the variables used across modules. 3 | 4 | outputs.tf 5 | -------------------------------------------------------------------------------- /modules/00-global-variables/outputs.tf.example: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Begin - Required Values 16 | 17 | output "project_id" { 18 | value = "INSERT-PROJECT-ID-HERE" 19 | } 20 | 21 | output "ssl_certificate_file" { 22 | value = "/path/to/certificate" 23 | } 24 | 25 | output "ssl_private_key_file" { 26 | value = "/path/to/privatekey" 27 | } 28 | 29 | output "iap_test_user" { 30 | value = "testuser@example.com" 31 | } 32 | 33 | # End - Required Values 34 | 35 | # Begin - Optional Values 36 | 37 | output "machine_type" { 38 | value = "e2-micro" 39 | } 40 | 41 | output "image_project" { 42 | value = "ubuntu-os-cloud" 43 | } 44 | 45 | output "image_family" { 46 | value = "ubuntu-2004-lts" 47 | } 48 | 49 | output "region" { 50 | value = "us-central1" 51 | } 52 | 53 | output "zone" { 54 | value = "us-central1-a" 55 | } 56 | 57 | # End - Optional Value 58 | -------------------------------------------------------------------------------- /modules/10-enable-apis/main.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | locals { 16 | google_cloud_api_set = toset([ 17 | "compute.googleapis.com", 18 | "iam.googleapis.com", 19 | "iap.googleapis.com" 20 | ]) 21 | } 22 | 23 | module "global_variables" { 24 | source = "../00-global-variables" 25 | } 26 | 27 | resource "google_project_service" "google_cloud_api" { 28 | provider = google 29 | 30 | for_each = local.google_cloud_api_set 31 | 32 | service = each.key 33 | disable_dependent_services = false 34 | disable_on_destroy = false 35 | } 36 | -------------------------------------------------------------------------------- /modules/10-enable-apis/output.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | output "Google_Cloud_Enabled_APIs" { 16 | value = local.google_cloud_api_set 17 | } 18 | -------------------------------------------------------------------------------- /modules/10-enable-apis/providers.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | terraform { 16 | required_providers { 17 | google = { 18 | source = "hashicorp/google" 19 | version = ">= 4.16.0" 20 | } 21 | } 22 | } 23 | 24 | provider "google" { 25 | project = module.global_variables.project_id 26 | region = module.global_variables.region 27 | zone = module.global_variables.zone 28 | } 29 | -------------------------------------------------------------------------------- /modules/20-create-iap-brand/main.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # This module is used to create the IAP OAuth brand for the IAP demo. 16 | # There currently can only be one brand per Google Cloud project. Since 17 | # brands cannot be deleted from projects, building out this Terraform plan 18 | # will fail on subseequent attempts will trigger an error. Moving the brand 19 | # resource to a separate module prevents the build of the 90-build-demo module 20 | # from aborting. 21 | 22 | module "global_variables" { 23 | source = "../00-global-variables" 24 | } 25 | 26 | resource "google_iap_brand" "demo_iap_brand" { 27 | support_email = module.global_variables.iap_test_user 28 | application_title = "IAP Demo" 29 | } 30 | -------------------------------------------------------------------------------- /modules/20-create-iap-brand/outputs.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | output "user" { 16 | value = module.global_variables.iap_test_user 17 | } 18 | -------------------------------------------------------------------------------- /modules/20-create-iap-brand/providers.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | terraform { 16 | required_providers { 17 | google = { 18 | source = "hashicorp/google" 19 | version = ">= 4.16.0" 20 | } 21 | } 22 | } 23 | 24 | provider "google" { 25 | project = module.global_variables.project_id 26 | region = module.global_variables.region 27 | zone = module.global_variables.zone 28 | } 29 | -------------------------------------------------------------------------------- /modules/90-build-demo/main.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # https://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # Set up global variables, data sources, and local variables 16 | 17 | module "global_variables" { 18 | source = "../00-global-variables" 19 | } 20 | 21 | data "google_compute_image" "web_server_image" { 22 | provider = google 23 | 24 | family = module.global_variables.image_family 25 | project = module.global_variables.image_project 26 | } 27 | 28 | data "google_project" "this_project" { 29 | provider = google 30 | } 31 | 32 | locals { 33 | project_number = data.google_project.this_project.number 34 | } 35 | 36 | # Set up network resources 37 | 38 | resource "google_compute_network" "vpc_network" { 39 | provider = google 40 | 41 | name = "demo-vpc" 42 | description = "VPC for the resources for the IAP demo" 43 | auto_create_subnetworks = false 44 | } 45 | 46 | resource "google_compute_subnetwork" "vpc_subnet" { 47 | provider = google 48 | 49 | name = "demo-subnet" 50 | description = "Subnet for the web servers for the IAP demo VPC" 51 | ip_cidr_range = "10.100.10.0/24" 52 | network = google_compute_network.vpc_network.id 53 | } 54 | 55 | resource "google_compute_firewall" "fw_tunneled_ssh_traffic" { 56 | provider = google 57 | 58 | name = "fw-tunneled-ssh-traffic" 59 | description = "Firewall to allow tunneled SSH traffic" 60 | 61 | network = google_compute_network.vpc_network.name 62 | direction = "INGRESS" 63 | 64 | allow { 65 | protocol = "tcp" 66 | ports = ["22"] 67 | } 68 | 69 | source_ranges = [ "35.235.240.0/20" ] 70 | } 71 | 72 | resource "google_compute_firewall" "fw_healthcheck_and_proxied_traffic" { 73 | provider = google 74 | 75 | name = "fw-healthcheck-and-proxied-traffic" 76 | description = "Firewall rule to allow health checks and proxied traffic" 77 | 78 | network = google_compute_network.vpc_network.name 79 | direction = "INGRESS" 80 | 81 | allow { 82 | protocol = "tcp" 83 | ports = ["80"] 84 | } 85 | 86 | source_ranges = [ "130.211.0.0/22", "35.191.0.0/16" ] 87 | } 88 | 89 | resource "google_compute_router" "vpc_router" { 90 | provider = google 91 | 92 | name = "demo-router" 93 | region = google_compute_subnetwork.vpc_subnet.region 94 | network = google_compute_network.vpc_network.id 95 | } 96 | 97 | resource "google_compute_router_nat" "nat" { 98 | provider = google 99 | 100 | name = "my-router-nat" 101 | router = google_compute_router.vpc_router.name 102 | region = google_compute_router.vpc_router.region 103 | nat_ip_allocate_option = "AUTO_ONLY" 104 | source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES" 105 | 106 | log_config { 107 | enable = true 108 | filter = "ERRORS_ONLY" 109 | } 110 | } 111 | 112 | # Set up compute 113 | 114 | # web_server_template - instance template for creating a web server 115 | # 116 | # startup-script - Meta data key to install Apache and a home page 117 | # 118 | # The home page consists of the instance's name and internal IP address. 119 | # 120 | # Notes: 121 | # 122 | # (1) There are nested heredocs below, including the definition of the script 123 | # (delimited by SCRIPT) and the creation of the home page (delimited by 124 | # EOF). 125 | # (2) The SCRIPT heredoc begins with "<<-" to strip off leading spaces. 126 | # If you remove the "-" from "<<-", the heredoc will include the leading 127 | # spaces and will not load properly. 128 | 129 | resource "google_compute_instance_template" "web_server_template" { 130 | provider = google 131 | 132 | name_prefix = "demo-template-" 133 | description = "Instance template for the web servers" 134 | region = module.global_variables.region 135 | machine_type = module.global_variables.machine_type 136 | 137 | network_interface { 138 | subnetwork = google_compute_subnetwork.vpc_subnet.self_link 139 | } 140 | 141 | shielded_instance_config { 142 | enable_secure_boot = true 143 | } 144 | 145 | disk { 146 | boot = true 147 | source_image = data.google_compute_image.web_server_image.self_link 148 | } 149 | 150 | metadata = { 151 | enable_oslogin = "TRUE" 152 | startup-script = <<-SCRIPT 153 | #!/bin/bash 154 | MD_URL="http://metadata.google.internal/computeMetadata/v1/instance" 155 | MD_HEADER="Metadata-Flavor: Google" 156 | # 157 | INSTANCE_ID=$(curl $MD_URL/id -H "$MD_HEADER") 158 | INSTANCE_NAME=$(curl $MD_URL/name -H "$MD_HEADER") 159 | INTERNAL_IP=$(curl $MD_URL//network-interfaces/0/ip -H "$MD_HEADER") 160 | # 161 | apt update 162 | apt -y install apache2 163 | cat <