Skip to content

Latest commit

 

History

History
294 lines (261 loc) · 14.9 KB

File metadata and controls

294 lines (261 loc) · 14.9 KB

3-networks

This repo is part of a multi-part guide that shows how to configure and deploy the example.com reference architecture described in Google Cloud security foundations guide (PDF). The following table lists the parts of the guide.

0-bootstrap Bootstraps a Google Cloud organization, creating all the required resources and permissions to start using the Cloud Foundation Toolkit (CFT). This step also configures a CI/CD pipeline for foundations code in subsequent stages.
1-org Sets up top level shared folders, monitoring and networking projects, and organization-level logging, and sets baseline security settings through organizational policy.
2-environments Sets up development, non-production, and production environments within the Google Cloud organization that you've created.
3-networks (this file) Sets up base and restricted shared VPCs with default DNS, NAT (optional), Private Service networking, VPC service controls, Dedicated or Partner Interconnect, and baseline firewall rules for each environment. It also sets up the global DNS hub.
4-projects Sets up a folder structure, projects, and application infrastructure pipeline for applications, which are connected as service projects to the shared VPC created in the previous stage.
5-app-infra Deploy a simple Compute Engine instance in one of the business unit projects using the infra pipeline set up in 4-projects.

For an overview of the architecture and the parts, see the terraform-example-foundation README.

Purpose

The purpose of this step is to:

  • Set up the global DNS Hub.
  • Set up base and restricted shared VPCs with default DNS, NAT (optional), Private Service networking, VPC service controls, on-premises Dedicated or Partner Interconnect, and baseline firewall rules for each environment.

Prerequisites

  1. 0-bootstrap executed successfully.
  2. 1-org executed successfully.
  3. 2-environments executed successfully.
  4. Obtain the value for the access_context_manager_policy_id variable. Can be obtained by running
gcloud access-context-manager policies list --organization YOUR_ORGANIZATION_ID --format="value(name)"

Troubleshooting

Please refer to troubleshooting if you run into issues during this step.

Usage

Networking Architecture

You need to set variables enable_hub_and_spoke and enable_hub_and_spoke_transitivity to true to be able to use the Hub-and-Spoke architecture detailed in the Networking section of the Google cloud security foundations guide.

Using Dedicated Interconnect

If you provisioned the prerequisites listed in the Dedicated Interconnect README, follow these steps to enable Dedicated Interconnect to access on-premises resources.

  1. Rename interconnect.tf.example to interconnect.tf in each environment folder in 3-networks/envs/<ENV>.
  2. Update the file interconnect.tf with values that are valid for your environment for the interconnects, locations, candidate subnetworks, vlan_tag8021q and peer info.
  3. The candidate subnetworks and vlan_tag8021q variables can be set to null to allow the interconnect module to auto generate these values.

Using Partner Interconnect

If you provisioned the prerequisites listed in the Partner Interconnect README follow this steps to enable Partner Interconnect to access on-premises resources.

  1. Rename partner_interconnect.tf.example to partner_interconnect.tf and interconnect.auto.tfvars.example to interconnect.auto.tfvars in the environment folder in 3-networks/envs/<environment> .
  2. Update the file partner_interconnect.tf with values that are valid for your environment for the VLAN attachments, locations, and candidate subnetworks.
  3. The candidate subnetworks variable can be set to null to allow the interconnect module to auto generate this value.

OPTIONAL - Using High Availability VPN

If you are not able to use Dedicated or Partner Interconnect, you can also use an HA Cloud VPN to access on-premises resources.

  1. Rename vpn.tf.example to vpn.tf in each environment folder in 3-networks/envs/<ENV>.
  2. Create secret for VPN private preshared key.
    echo '<YOUR-PRESHARED-KEY-SECRET>' | gcloud secrets create <VPN_PRIVATE_PSK_SECRET_NAME> --project <ENV_SECRETS_PROJECT> --replication-policy=automatic --data-file=-
    
  3. Create secret for VPN restricted preshared key.
    echo '<YOUR-PRESHARED-KEY-SECRET>' | gcloud secrets create <VPN_RESTRICTED_PSK_SECRET_NAME> --project <ENV_SECRETS_PROJECT> --replication-policy=automatic --data-file=-
    
  4. In the file vpn.tf, update the values for environment, vpn_psk_secret_name, on_prem_router_ip_address1, on_prem_router_ip_address2 and bgp_peer_asn.
  5. Verify other default values are valid for your environment.

Deploying with Cloud Build

  1. Clone repo.
    gcloud source repos clone gcp-networks --project=YOUR_CLOUD_BUILD_PROJECT_ID
    
  2. Change to the freshly cloned repo and change to non-master branch.
    git checkout -b plan
    
  3. Copy contents of foundation to new repo.
    cp -RT ../terraform-example-foundation/3-networks/ .
    
  4. Copy Cloud Build configuration files for Terraform.
    cp ../terraform-example-foundation/build/cloudbuild-tf-* .
    
  5. Copy Terraform wrapper script to the root of your new repository.
    cp ../terraform-example-foundation/build/tf-wrapper.sh .
    
  6. Ensure wrapper script can be executed.
    chmod 755 ./tf-wrapper.sh
    
  7. Rename common.auto.example.tfvars to common.auto.tfvars and update the file with values from your environment and bootstrap. See any of the envs folder README.md files for additional information on the values in the common.auto.tfvars file.
  8. Rename shared.auto.example.tfvars to shared.auto.tfvars and update the file with the target_name_server_addresses (the list of target name servers for the DNS forwarding zone in the DNS Hub).
  9. Rename access_context.auto.example.tfvars to access_context.auto.tfvars and update the file with the access_context_manager_policy_id.
  10. Commit changes
    git add .
    git commit -m 'Your message'
    
  11. You must manually plan and apply the shared environment (only once) since the development, non-production and production environments depend on it.
    1. Run cd ./envs/shared/.
    2. Update backend.tf with your bucket name from the bootstrap step.
    3. Run terraform init.
    4. Run terraform plan and review output.
    5. Run terraform apply.
    6. If you would like the bucket to be replaced by Cloud Build at run time, change the bucket name back to UPDATE_ME.
  12. Push your plan branch to trigger a plan.
    git push --set-upstream origin plan
    
  13. Review the plan output in your Cloud Build project https://console.cloud.google.com/cloud-build/builds?project=YOUR_CLOUD_BUILD_PROJECT_ID
  14. Merge changes to production.
    git checkout -b production
    git push origin production
    
  15. Review the apply output in your Cloud Build project https://console.cloud.google.com/cloud-build/builds?project=YOUR_CLOUD_BUILD_PROJECT_ID
  16. After production has been applied, apply development.
  17. Merge changes to development.
    git checkout -b development
    git push origin development
    
  18. Review the apply output in your Cloud Build project https://console.cloud.google.com/cloud-build/builds?project=YOUR_CLOUD_BUILD_PROJECT_ID
  19. After development has been applied, apply non-production.
  20. Merge changes to non-production.
    git checkout -b non-production
    git push origin non-production
    
  21. Review the apply output in your Cloud Build project https://console.cloud.google.com/cloud-build/builds?project=YOUR_CLOUD_BUILD_PROJECT_ID
  22. You can now move to the instructions in the step 4-projects.

Deploying with Jenkins

  1. Clone the repo you created manually in 0-bootstrap.
    git clone <YOUR_NEW_REPO-3-networks>
    
  2. Navigate into the repo and change to a non-production branch.
    cd YOUR_NEW_REPO_CLONE-3-networks
    git checkout -b plan
    
  3. Copy contents of foundation to new repo.
    cp -RT ../terraform-example-foundation/3-networks/ .
    
  4. Copy the Jenkinsfile script to the root of your new repository.
    cp ../terraform-example-foundation/build/Jenkinsfile .
    
  5. Update the variables located in the environment {} section of the Jenkinsfile with values from your environment:
    _TF_SA_EMAIL
    _STATE_BUCKET_NAME
    _PROJECT_ID (the cicd project id)
    
  6. Copy Terraform wrapper script to the root of your new repository.
    cp ../terraform-example-foundation/build/tf-wrapper.sh .
    
  7. Ensure wrapper script can be executed.
    chmod 755 ./tf-wrapper.sh
    
  8. Rename common.auto.example.tfvars to common.auto.tfvars and update the file with values from your environment and bootstrap. See any of the envs folder README.md files for additional information on the values in the common.auto.tfvars file.
  9. Rename shared.auto.example.tfvars to shared.auto.tfvars and update the file with the target_name_server_addresses.
  10. Rename access_context.auto.example.tfvars to access_context.auto.tfvars and update the file with the access_context_manager_policy_id.
  11. Commit changes.
    git add .
    git commit -m 'Your message'
    
  12. You must manually plan and apply the shared environment (only once) since the development, non-production and production environments depend on it.
    1. Run cd ./envs/shared/.
    2. Update backend.tf with your bucket name from the bootstrap step.
    3. Run terraform init.
    4. Run terraform plan and review output.
    5. Run terraform apply.
    6. If you would like the bucket to be replaced by Cloud Build at run time, change the bucket name back to UPDATE_ME.
  13. Push your plan branch.
     git push --set-upstream origin plan
    
    • Assuming you configured an automatic trigger in your Jenkins Master (see Jenkins sub-module README), this will trigger a plan. You can also trigger a Jenkins job manually. Given the many options to do this in Jenkins, it is out of the scope of this document see Jenkins website for more details.
  14. Review the plan output in your Master's web UI.
  15. Merge changes to production branch.
    git checkout -b production
    git push origin production
    
  16. Review the apply output in your Master's web UI (you might want to use the option to "Scan Multibranch Pipeline Now" in your Jenkins Master UI).
  17. After production has been applied, apply development and non-production.
  18. Merge changes to development
    git checkout -b development
    git push origin development
    
  19. Review the apply output in your Master's web UI (you might want to use the option to "Scan Multibranch Pipeline Now" in your Jenkins Master UI).
  20. Merge changes to non-production.
    git checkout -b non-production
    git push origin non-production
    
  21. Review the apply output in your Master's web UI (you might want to use the option to "Scan Multibranch Pipeline Now" in your Jenkins Master UI).

Run Terraform locally

  1. Change into the 3-networks folder.
  2. Run cp ../build/tf-wrapper.sh .
  3. Run chmod 755 ./tf-wrapper.sh.
  4. Rename common.auto.example.tfvars to common.auto.tfvars and update the file with values from your environment and bootstrap. See any of the envs folder README.md files for additional information on the values in the common.auto.tfvars file.
  5. Rename shared.auto.example.tfvars to shared.auto.tfvars and update the file with the target_name_server_addresses.
  6. Rename access_context.auto.example.tfvars to access_context.auto.tfvars and update the file with the access_context_manager_policy_id.
  7. Update backend.tf with your bucket name from the bootstrap step.
    for i in `find -name 'backend.tf'`; do sed -i 's/UPDATE_ME/<YOUR-BUCKET-NAME>/' $i; done
    
    You can run terraform output gcs_bucket_tfstate in the 0-bootstrap folder to obtain the bucket name.

We will now deploy each of our environments(development/production/non-production) using this script. When using Cloud Build or Jenkins as your CI/CD tool each environment corresponds to a branch in the repository for 3-networks step and only the corresponding environment is applied.

To use the validate option of the tf-wrapper.sh script, please follow the instructions in the Install Terraform Validator section and install version 2021-03-22 in your system. You will also need to rename the binary from terraform-validator-<your-platform> to terraform-validator and the terraform-validator binary must be in your PATH.

  1. Run ./tf-wrapper.sh init shared.
  2. Run ./tf-wrapper.sh plan shared and review output.
  3. Run ./tf-wrapper.sh validate shared $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID> and check for violations.
  4. Run ./tf-wrapper.sh apply shared.
  5. Run ./tf-wrapper.sh init production.
  6. Run ./tf-wrapper.sh plan production and review output.
  7. Run ./tf-wrapper.sh validate production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID> and check for violations.
  8. Run ./tf-wrapper.sh apply production.
  9. Run ./tf-wrapper.sh init non-production.
  10. Run ./tf-wrapper.sh plan non-production and review output.
  11. Run ./tf-wrapper.sh validate non-production $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID> and check for violations.
  12. Run ./tf-wrapper.sh apply non-production.
  13. Run ./tf-wrapper.sh init development.
  14. Run ./tf-wrapper.sh plan development and review output.
  15. Run ./tf-wrapper.sh validate development $(pwd)/../policy-library <YOUR_CLOUD_BUILD_PROJECT_ID> and check for violations.
  16. Run ./tf-wrapper.sh apply development.

If you received any errors or made any changes to the Terraform config or any .tfvarsyou must re-run ./tf-wrapper.sh plan <env> before run ./tf-wrapper.sh apply <env>.