Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow internet egress by default & reroute accounts.google.com -> private.googleapis.com #284

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/networking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ No modules.
| Name | Type |
|------|------|
| [google_compute_network.this](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network) | resource |
| [google_compute_route.egress-inet](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_route) | resource |
| [google_compute_route.private-google-access](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_route) | resource |
| [google_compute_subnetwork.regional](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_subnetwork) | resource |
| [google_dns_managed_zone.cloud-run-internal](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_managed_zone) | resource |
| [google_dns_managed_zone.private-google-apis](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_managed_zone) | resource |
| [google_dns_managed_zone.private-google-com](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_managed_zone) | resource |
| [google_dns_record_set.cloud-run-cname](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set) | resource |
| [google_dns_record_set.private-google-com-cname](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set) | resource |
| [google_dns_record_set.private-googleapis-a-record](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dns_record_set) | resource |
| [random_string.suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |

Expand Down
29 changes: 29 additions & 0 deletions modules/networking/dns.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,35 @@ resource "google_dns_record_set" "cloud-run-cname" {
rrdatas = ["private.googleapis.com."]
}

// Create a special DNS zone attached to the network in which
// we will operate our services that reroutes *.google.com
// that we control.
resource "google_dns_managed_zone" "private-google-com" {
project = var.project_id
name = "private-google-com-${random_string.suffix.result}"
dns_name = "google.com."
description = "This reroutes google.com requests to private.googleapis.com"

visibility = "private"

private_visibility_config {
networks {
network_url = google_compute_network.this.id
}
}
}

// Create a record for *.google.com that points to private.googleapis.com
resource "google_dns_record_set" "private-google-com-cname" {
project = var.project_id
name = "*.google.com."
managed_zone = google_dns_managed_zone.private-google-com.name
type = "CNAME"
ttl = 60

rrdatas = ["private.googleapis.com."]
}

// Create a special DNS zone attached to the network in which
// we will operate our services that reroutes private.googleapis.com
// to records that we control.
Expand Down
12 changes: 9 additions & 3 deletions modules/networking/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ resource "google_compute_network" "this" {
delete_default_routes_on_create = true
}

// Create a default route to the Internet.
resource "google_compute_route" "egress-inet" {
// Allow private Google access from the VPC.
resource "google_compute_route" "private-google-access" {
tcnghia marked this conversation as resolved.
Show resolved Hide resolved
name = var.name
network = google_compute_network.this.name

dest_range = "0.0.0.0/0"
# https://cloud.google.com/vpc/docs/configure-private-google-access-hybrid#config
dest_range = "199.36.153.8/30"
next_hop_gateway = "default-internet-gateway"
}

moved {
from = google_compute_route.egress-inet
to = google_compute_route.private-google-access
}

// Create regional subnets in each of the specified regions,
// which we will use to operate Cloud Run services.
resource "google_compute_subnetwork" "regional" {
Expand Down