Cloud Security VMware Cloud

Intro to Google Cloud VMware Engine – Bastion Host Access with IAP

This post is the third in a series on Google Cloud VMware Engine and Google Cloud Platform. This post walks through the process of creating a bastion host and accessing it via Identity-Aware Proxy. Example Terraform code is provided to deploy this scenario. This post will build on the previous posts in this series by deploying a Windows Server 2019 bastion host to manage our Google Cloud VMware Engine SDDC. Access to the bastion host will be provided with Identity-Aware Proxy (IAP). Everything will be deployed and configured with Terraform, with all of the code referenced in this post is available at https://github.com/shamsway/gcp-terraform-examples in the gcve-bastion-iap sub-directory.

Other posts in this series:

Identity Aware Proxy Overview

Standing up initial cloud connectivity is challenging. I walked through the steps to deploy a client VPN in Deploying a Private Cloud with HCX, but this post will show how to use IAP as a method for accessing a new bastion host. Using IAP means that the bastion host will be accessible without having to configure a VPN or expose it to the internet. I am a massive fan of this approach, and while there are some tradeoffs to discuss, it is a simpler and more secure approach than traditional access methods.

IAP can be used to access various resources, including App Engine and GKE. Accessing the bastion host over RDP (TCP port 3389) will be accomplished using IAP for TCP forwarding. Once configured, IAP will allow us to establish a connection to our bastion host over an encrypted tunnel on demand. Configuring this feature will require some specific IAM roles, as well as some firewall rules in your VPC. If you have Owner permissions in your GCP project, then you’re good to go. Otherwise, you will need the following roles assigned to complete the tasks outlined in the rest of this post:

  • Compute Admin (roles/compute.admin)
  • Service Account Admin (roles/iam.serviceAccountAdmin)
  • Service Account User (roles/iam.serviceAccountUser)
  • IAP Policy Admin (roles/iap.admin)
  • IAP settings Admin (roles/iap.settingsAdmin)
  • IAP-secured Tunnel User (roles/iap.tunnelResourceAccessor)
  • Service Networking Admin (roles/servicenetworking.networksAdmin)
  • Project IAM Admin (roles/resourcemanager.projectIamAdmin)

The VPC firewall will need to allow traffic sourced from 35.235.240.0/20, which is the range that IAP uses for TCP forwarding. This rule can be further limited to specific TCP ports, like 3389 for RDP or 22 for SSH.

Bastion Host Deployment with Terraform

The example Terraform code linked at the beginning of the post will do the following:

  • Create a service account, which will be associated with the bastion host
  • Create Windows 2019 Server instance, which will be used as a bastion host
  • Create firewall rules for accessing the bastion host via IAP, and accessing resources from the bastion host
  • Assign IAM roles needed for IAP
  • Set a password on the bastion host using the gcloud tool

After Terraform completes configuration, you will be able to use the gcloud tool to enable TCP forwarding for RDP. Once connected to the bastion host, you will be able to log into your vSphere portal. To get started, clone the example repo with git clone https://github.com/shamsway/gcp-terraform-examples.git, then change to the gcve-bastion-iap sub-directory. You will find these files:

  • main.tf – Contains the primary Terraform code to complete the steps mentioned above
  • variables.tf – Defines the input variables that will be used in main.tf
  • terraform.tfvars – Supplies values for the input variables defined in variables.tf
  • outputs.tf – Defines the output variables to be returned from main.tf

Let’s take a closer look at what is happening in each of these files.

main.tf Contents

<span class="k">provider</span> <span class="s2">"google"</span> <span class="p">{</span>
  <span class="nx">project</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">project</span>
  <span class="nx">region</span>  <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">region</span>
  <span class="nx">zone</span>    <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">zone</span>
<span class="p">}</span>

<span class="k">data</span> <span class="s2">"google_compute_network"</span> <span class="s2">"network"</span> <span class="p">{</span>
  <span class="nx">name</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">network_name</span>
<span class="p">}</span>

<span class="k">data</span> <span class="s2">"google_compute_subnetwork"</span> <span class="s2">"subnet"</span> <span class="p">{</span>
  <span class="nx">name</span>   <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">subnet_name</span>
  <span class="nx">region</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">region</span>
<span class="p">}</span>

Just like the example from my last postmain.tf begins with a provider block to define the Google Cloud project, region, and zone in which Terraform will create resources. The following data blocks, google_compute_network.network and google_compute_network.subnet, reference an existing VPC network and subnetwork. These data blocks will provide parameters necessary for creating a bastion host and firewall rules.

<span class="k">resource</span> <span class="s2">"google_service_account"</span> <span class="s2">"bastion_host"</span> <span class="p">{</span>
  <span class="nx">project</span>      <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">project</span>
  <span class="nx">account_id</span>   <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">service_account_name</span>
  <span class="nx">display_name</span> <span class="p">=</span> <span class="s2">"Service Account for Bastion"</span>
<span class="p">}</span>

The first resource block creates a new service account that will be associated with our bastion host instance.

<span class="k">resource</span> <span class="s2">"google_compute_instance"</span> <span class="s2">"bastion_host"</span> <span class="p">{</span>
  <span class="nx">name</span>         <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">name</span>
  <span class="nx">machine_type</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">machine_type</span>

  <span class="nx">boot_disk</span> <span class="p">{</span>
    <span class="nx">initialize_params</span> <span class="p">{</span>
      <span class="nx">image</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">image</span>
    <span class="p">}</span>
  <span class="p">}</span>

  <span class="nx">network_interface</span> <span class="p">{</span>
    <span class="nx">subnetwork</span> <span class="p">=</span> <span class="k">data</span><span class="p">.</span><span class="nx">google_compute_subnetwork</span><span class="p">.</span><span class="nx">subnet</span><span class="p">.</span><span class="nx">self_link</span>
    <span class="nx">access_config</span> <span class="p">{}</span>
  <span class="p">}</span>

  <span class="nx">service_account</span> <span class="p">{</span>
    <span class="nx">email</span>  <span class="p">=</span> <span class="nx">google_service_account</span><span class="p">.</span><span class="nx">bastion_host</span><span class="p">.</span><span class="nx">email</span>
    <span class="nx">scopes</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">scopes</span>
  <span class="p">}</span>

  <span class="nx">tags</span>   <span class="p">=</span> <span class="p">[</span><span class="kd">var</span><span class="p">.</span><span class="nx">tag</span><span class="p">]</span>
  <span class="nx">labels</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">labels</span>
  <span class="nx">metadata</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">metadata</span>
<span class="p">}</span>

The google_compute_instance.bastion_host block creates the bastion host. There are a few things to take note of in this block. subnetwork is set based on one of the data blocks at the beginning of main.tfdata.google_compute_subnetwork.subnet.self_link. The self_link property provides a unique reference to the subnet that Terraform will use when submitting the API call to create the bastion host. Similarly, the service account created by google_service_account.bastion_host is assigned to the bastion host.

tagslabels, and metadata all serve similar, but distinct, purposes. tags are network tags, which will be used in firewall rules. labels are informational data that can be used for organizational or billing purposes. metadata has numerous uses, the most common of which is supplying a boot script that the instance will run on first boot.

<span class="k">resource</span> <span class="s2">"google_compute_firewall"</span> <span class="s2">"allow_from_iap_to_bastion"</span> <span class="p">{</span>
  <span class="nx">project</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">project</span>
  <span class="nx">name</span>    <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">fw_name_allow_iap_to_bastion</span>
  <span class="nx">network</span> <span class="p">=</span> <span class="k">data</span><span class="p">.</span><span class="nx">google_compute_network</span><span class="p">.</span><span class="nx">network</span><span class="p">.</span><span class="nx">self_link</span>

  <span class="nx">allow</span> <span class="p">{</span>
    <span class="nx">protocol</span> <span class="p">=</span> <span class="s2">"tcp"</span>
    <span class="nx">ports</span>    <span class="p">=</span> <span class="p">[</span><span class="s2">"3389"</span><span class="p">]</span>
  <span class="p">}</span>

  <span class="c1"># https://cloud.google.com/iap/docs/using-tcp-forwarding#before_you_begin</span>
  <span class="c1"># This range is needed to allow IAP to access the bastion host</span>
  <span class="nx">source_ranges</span> <span class="p">=</span> <span class="p">[</span><span class="s2">"35.235.240.0/20"</span><span class="p">]</span>

  <span class="nx">target_tags</span> <span class="p">=</span> <span class="p">[</span><span class="kd">var</span><span class="p">.</span><span class="nx">tag</span><span class="p">]</span>
<span class="p">}</span>

<span class="k">resource</span> <span class="s2">"google_compute_firewall"</span> <span class="s2">"allow_access_from_bastion"</span> <span class="p">{</span>
  <span class="nx">project</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">project</span>
  <span class="nx">name</span>    <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">fw_name_allow_mgmt_from_bastion</span>
  <span class="nx">network</span> <span class="p">=</span> <span class="k">data</span><span class="p">.</span><span class="nx">google_compute_network</span><span class="p">.</span><span class="nx">network</span><span class="p">.</span><span class="nx">self_link</span>

  <span class="nx">allow</span> <span class="p">{</span>
    <span class="nx">protocol</span> <span class="p">=</span> <span class="s2">"icmp"</span>
  <span class="p">}</span>

  <span class="nx">allow</span> <span class="p">{</span>
    <span class="nx">protocol</span> <span class="p">=</span> <span class="s2">"tcp"</span>
    <span class="nx">ports</span>    <span class="p">=</span> <span class="p">[</span><span class="s2">"22"</span><span class="p">,</span> <span class="s2">"80"</span><span class="p">,</span> <span class="s2">"443"</span><span class="p">,</span> <span class="s2">"3389"</span><span class="p">]</span>
  <span class="p">}</span>

  <span class="c1"># Allow management traffic from bastion</span>
  <span class="nx">source_tags</span> <span class="p">=</span> <span class="p">[</span><span class="kd">var</span><span class="p">.</span><span class="nx">tag</span><span class="p">]</span>
<span class="p">}</span>

The next two blocks create firewall rules: one for accessing the bastion host via IAP, and the other for accessing resources from the bastion host. google_compute_firewall.allow_from_iap_to_bastion allows traffic from 35.235.240.0/24 on tcp/3389 to instances that have the same network tag as the one that was assigned to the bastion host. google_compute_firewall.allow_access_from_bastion allows traffic from the bastion host by referencing the same network tag to anything else in our project, using common management ports/protocols.

<span class="k">resource</span> <span class="s2">"google_iap_tunnel_instance_iam_binding"</span> <span class="s2">"enable_iap"</span> <span class="p">{</span>
  <span class="nx">project</span>    <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">project</span>
  <span class="nx">zone</span>       <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">zone</span>
  <span class="nx">instance</span>   <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">name</span>
  <span class="nx">role</span>       <span class="p">=</span> <span class="s2">"roles/iap.tunnelResourceAccessor"</span>
  <span class="nx">members</span>    <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">members</span>
  <span class="nx">depends_on</span> <span class="p">=</span> <span class="p">[</span><span class="nx">google_compute_instance</span><span class="p">.</span><span class="nx">bastion_host</span><span class="p">]</span>
<span class="p">}</span>

The google_iap_tunnel_instance_iam_binding.enable_iap block assigns the roles/iap.tunnelResourceAccessor IAM role to the accounts defined in the members variable. This value could be any valid IAM resource like a specific account or a group. This role is required to be able to access the bastion host via IAP.

<span class="k">resource</span> <span class="s2">"google_service_account_iam_binding"</span> <span class="s2">"bastion_sa_user"</span> <span class="p">{</span>
  <span class="nx">service_account_id</span> <span class="p">=</span> <span class="nx">google_service_account</span><span class="p">.</span><span class="nx">bastion_host</span><span class="p">.</span><span class="nx">id</span>
  <span class="nx">role</span>               <span class="p">=</span> <span class="s2">"roles/iam.serviceAccountUser"</span>
  <span class="nx">members</span>            <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">members</span>
<span class="p">}</span>

The google_project_iam_member.bastion_sa_user block allows accounts specified in the members variable to use the newly created service account via the Service Account User role (roles/iam.serviceAccountUser). This allows the users or groups defined in the members variable to access all of the resources that the service account has rights to access. More information on this can be found here.

<span class="k">resource</span> <span class="s2">"google_project_iam_member"</span> <span class="s2">"bastion_sa_bindings"</span> <span class="p">{</span>
  <span class="nx">for_each</span> <span class="p">=</span> <span class="nx">toset</span><span class="p">(</span><span class="kd">var</span><span class="p">.</span><span class="nx">service_account_roles</span><span class="p">)</span>

  <span class="nx">project</span> <span class="p">=</span> <span class="kd">var</span><span class="p">.</span><span class="nx">project</span>
  <span class="nx">role</span>    <span class="p">=</span> <span class="nx">each</span><span class="p">.</span><span class="nx">key</span>
  <span class="nx">member</span>  <span class="p">=</span> <span class="s2">"serviceAccount:</span><span class="k">${</span><span class="nx">google_service_account</span><span class="p">.</span><span class="nx">bastion_host</span><span class="p">.</span><span class="nx">email</span><span class="k">}</span><span class="s2">"</span>
<span class="p">}</span>

google_project_iam_member.bastion_sa_bindings completes the IAM-related configuration by granting roles defined in the service_account_roles variable to the service account. This service account is assigned to the bastion host, which defines what the bastion host can do. The default roles assigned are listed below, but they can be modified in variables.tf.

  • Log Writer (roles/logging.logWriter)
  • Monitoring Metric Writer (roles/monitoring.metricWriter)
  • Monitoring Viewer (roles/monitoring.viewer)
  • Compute OS Login (roles/compute.osLogin)
<span class="k">resource</span> <span class="s2">"time_sleep"</span> <span class="s2">"wait_60_seconds"</span> <span class="p">{</span>
  <span class="nx">create_duration</span> <span class="p">=</span> <span class="s2">"60s"</span>
  <span class="nx">depends_on</span>      <span class="p">=</span> <span class="p">[</span><span class="nx">google_compute_instance</span><span class="p">.</span><span class="nx">bastion_host</span><span class="p">]</span>
<span class="p">}</span>

<span class="k">data</span> <span class="s2">"external"</span> <span class="s2">"gcloud_set_bastion_password"</span> <span class="p">{</span>
  <span class="nx">program</span>    <span class="p">=</span> <span class="p">[</span><span class="s2">"bash"</span><span class="p">,</span> <span class="s2">"-c"</span><span class="p">,</span> <span class="s2">"gcloud compute reset-windows-password </span><span class="k">${</span><span class="kd">var</span><span class="p">.</span><span class="nx">name</span><span class="k">}</span><span class="s2"> --user=</span><span class="k">${</span><span class="kd">var</span><span class="p">.</span><span class="nx">username</span><span class="k">}</span><span class="s2"> --format=json --quiet"</span><span class="p">]</span>
  <span class="nx">depends_on</span> <span class="p">=</span> <span class="p">[</span><span class="nx">time_sleep</span><span class="p">.</span><span class="nx">wait_60_seconds</span><span class="p">]</span>
<span class="p">}</span>

These final two blocks are what I refer to as “cool Terraform tricks.” The point of these blocks is to set the password on the bastion host. There are a few ways to do this, but unfortunately, there is no way to set a Windows instance password with a native Terraform resource. Instead, an external data source is used to run the appropriate gcloud command, with JSON formatted results returned (this is a requirement of the external data source.) The password cannot be set until the bastion host is fully booted, so external.gcloud_set_bastion_pasword depends on time_sleep.wait_60_seconds, which is a simple 60-second timer that gives the bastion host time to boot up before the gcloud command is run.

There is a chance that 60 seconds may not be long enough for the bastion host to boot. If you receive an error stating that the instance is not ready for use, you have two options:

  1. Run terraform destroy to remove the bastion host. Edit main.tf and increase the create_duration to a higher value, then run terraform apply again.
  2. Run the gcloud compute reset-windows-password command manually

Ideally, the password reset functionality would be built into the Google Cloud Terraform provider, and I wouldn’t be surprised to see it added in the future. If you’re reading this post in 2022 or beyond, it’s probably worth a quick investigation to see if this has happened.

output.tf Contents

<span class="k">output</span> <span class="s2">"bastion_username"</span> <span class="p">{</span>
  <span class="nx">value</span> <span class="p">=</span> <span class="k">data</span><span class="p">.</span><span class="nx">external</span><span class="p">.</span><span class="nx">gcloud_set_bastion_password</span><span class="p">.</span><span class="nx">result</span><span class="p">.</span><span class="nx">username</span>
<span class="p">}</span>

<span class="k">output</span> <span class="s2">"bastion_password"</span> <span class="p">{</span>
  <span class="nx">value</span> <span class="p">=</span> <span class="k">data</span><span class="p">.</span><span class="nx">external</span><span class="p">.</span><span class="nx">gcloud_set_bastion_password</span><span class="p">.</span><span class="nx">result</span><span class="p">.</span><span class="nx">password</span>
<span class="p">}</span>

These two outputs are the results of running the gcloud command. Once Terraform has completed running, it will display the username and password set on the bastion host. A password is sensitive data, so if you want to prevent it from being displayed, add sensitive = true to the bastion_password output block. Output values are stored in the Terraform state file, so you should take precautions to protect the state file from unauthorized access. Additional information on Terraform outputs is available here.

terraform.tfvars Contents

terraform.tfvars is the file that defines all the variables that are referenced in main.tf. All you need to do is supply the desired values for your environment, and you are good to go. Note that the variables below are all examples, so simply copying and pasting may not lead to the desired result.

<span class="n">members</span>              <span class="o">=</span> <span class="p">[</span><span class="s">"user:[email protected]"</span><span class="p">]</span>
<span class="n">project</span>              <span class="o">=</span> <span class="s">"your-gcp-project"</span>
<span class="n">region</span>               <span class="o">=</span> <span class="s">"us-west2"</span>
<span class="n">zone</span>                 <span class="o">=</span> <span class="s">"us-west2-a"</span>
<span class="n">service_account_name</span> <span class="o">=</span> <span class="s">"bastion-sa"</span>
<span class="n">name</span>                 <span class="o">=</span> <span class="s">"bastion-vm"</span>
<span class="n">username</span>             <span class="o">=</span> <span class="s">"bastionuser"</span>
<span class="n">labels</span>               <span class="o">=</span> <span class="p">{</span> <span class="n">owner</span> <span class="o">=</span> <span class="s">"Cloud Team"</span><span class="p">,</span> <span class="n">created_with</span> <span class="o">=</span> <span class="s">"terraform"</span> <span class="p">}</span>
<span class="n">image</span>                <span class="o">=</span> <span class="s">"gce-uefi-images/windows-2019"</span>
<span class="n">machine_type</span>         <span class="o">=</span> <span class="s">"n1-standard-1"</span>
<span class="n">network_name</span>         <span class="o">=</span> <span class="s">"gcve-usw2"</span>
<span class="n">subnet_name</span>          <span class="o">=</span> <span class="s">"gcve-usw2-mgmt"</span>
<span class="n">tag</span>                  <span class="o">=</span> <span class="s">"bastion"</span>

Additional information on the variables used is available in README.md. You can also find information on these variables, including their default values should one exist, in variables.tf.

Initializing and Running Terraform

Terraform will use Application Default Credentials to authenticate to Google Cloud. Assuming you have the gcloud cli tool installed, you can set these by running gcloud auth application-default. Additional information on authentication can be found in the Getting Started with the Google Provider Terraform documentation. To run the Terraform code, follow the steps below.

Following these steps will create resources in your Google Cloud project, and you will be billed for them.

  1. Run terraform init and ensure no errors are displayed
  2. Run terraform plan and review the changes that Terraform will perform
  3. Run terraform apply to apply the proposed configuration changes

Should you wish to remove everything created by Terraform, run terraform destroy and answer yes when prompted. This will only remove the VPC network and related configuration created by Terraform. Your Google Cloud VMware Engine environment will have to be deleted using these instructions, if desired.

Accessing the Bastion Host with IAP

Now, you should have a fresh Windows 2019 Server running in Google Cloud to serve as a bastion host. Use this command to create a tunnel to the bastion host:

gcloud compute start-iap-tunnel <span class="o">[</span>bastion-host-name] 3389 <span class="nt">--zone</span> <span class="o">[</span>zone]

You will see a message that says Listening on port [random number]. This random high port is proxied to your bastion host port 3389. Fire up your favorite RDP client and connect to localhost:[random number]. Login with the credentials that were output from running Terraform. Once you’re able to connect to the bastion host, install the vSphere-compatible browser of your choice, along with any other management tools you may need.

If you’re a Windows user, there is an IAP-enabled RDP client available here.

Accessing Google Cloud VMware Engine Resources from the Bastion Host

Open the Google Cloud VMware Engine portal, browse to Resources, and click on your SDDC, then vSphere Management Network. This will display the hostnames for your vCenter, NSX and HCX instances. Copy the hostname for vCenter and paste it into a browser in your bastion host to verify you can access your SDDC.

Cloud DNS forwarding config to enable resolution of Google Cloud VMware Engine resources

Access to Google Cloud VMware Engine from your VPC is made possible by private service access and a DNS forwarding configuration in Cloud DNS. The DNS forwarding configuration enables name resolution from your VPC for resources in Google Cloud VMware Engine. It is automatically created in Cloud DNS when private service access is configured between your VPC and Google Cloud VMware Engine. This is a relatively new feature and a nice improvement. Previously, name resolution for Google Cloud VMware Engine required manually changing resolvers on your bastion host or configuring a standalone DNS server.

Wrap Up

A quick recap of everything we’ve accomplished if you’ve been following this blog series from the beginning:

  • Deployed an SDDC in Google Cloud VMware Engine
  • Created a new VPC and configured private service access to your SDDC
  • Deployed a bastion host in your VPC, accessible via IAP

Clearly, we are just getting started! My next post will look at configuring Cloud Interconnect and standing up an HCX service mesh. With that in place, we can begin migrating some workloads into our SDDC.

Terraform Documentation Links