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

Fix: remove default vpc consistency errors #1344

Draft
wants to merge 20 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 0-bootstrap/cb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module "tf_source" {
}

# Remove after github.com/terraform-google-modules/terraform-google-bootstrap/issues/160
depends_on = [module.seed_bootstrap]
depends_on = [module.seed_bootstrap, time_sleep.wait_organization_policies]
}

resource "google_project_service_identity" "workflows_identity" {
Expand Down
3 changes: 1 addition & 2 deletions 0-bootstrap/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,5 @@ module "seed_bootstrap" {

sa_org_iam_permissions = []

depends_on = [module.required_group]
depends_on = [module.required_group, time_sleep.wait_organization_policies]
}

45 changes: 45 additions & 0 deletions 0-bootstrap/org_policy.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

locals {
organization_id = var.parent_folder != "" ? null : var.org_id
folder_id = var.parent_folder != "" ? var.parent_folder : null
policy_for = var.parent_folder != "" ? "folder" : "organization"

boolean_type_organization_policies = toset([
"compute.skipDefaultNetworkCreation"
])
}

module "organization_policies_type_boolean" {
source = "terraform-google-modules/org-policy/google"
version = "~> 5.1"
for_each = local.boolean_type_organization_policies

organization_id = local.organization_id
folder_id = local.folder_id
policy_for = local.policy_for
policy_type = "boolean"
enforce = "true"
constraint = "constraints/${each.value}"
}

resource "time_sleep" "wait_organization_policies" {
create_duration = "60s"
depends_on = [
module.organization_policies_type_boolean
]
}
1 change: 0 additions & 1 deletion 1-org/envs/shared/org_policy.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ locals {
boolean_type_organization_policies = toset([
"compute.disableNestedVirtualization",
"compute.disableSerialPortAccess",
"compute.skipDefaultNetworkCreation",
"compute.restrictXpnProjectLienRemoval",
"compute.disableVpcExternalIpv6",
"compute.setNewProjectDefaultToZonalDNSOnly",
Expand Down
16 changes: 16 additions & 0 deletions 1-org/envs/shared/projects.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module "org_audit_logs" {
folder_id = google_folder.common.id
deletion_policy = var.project_deletion_policy
activate_apis = ["logging.googleapis.com", "bigquery.googleapis.com", "billingbudgets.googleapis.com"]
auto_create_network = true

labels = {
environment = "common"
Expand Down Expand Up @@ -79,6 +80,7 @@ module "org_billing_export" {
folder_id = google_folder.common.id
deletion_policy = var.project_deletion_policy
activate_apis = ["logging.googleapis.com", "bigquery.googleapis.com", "billingbudgets.googleapis.com"]
auto_create_network = true

labels = {
environment = "common"
Expand Down Expand Up @@ -113,6 +115,8 @@ module "common_kms" {
folder_id = google_folder.common.id
deletion_policy = var.project_deletion_policy
activate_apis = ["logging.googleapis.com", "cloudkms.googleapis.com", "billingbudgets.googleapis.com"]
auto_create_network = true


labels = {
environment = "common"
Expand Down Expand Up @@ -148,6 +152,8 @@ module "org_secrets" {
folder_id = google_folder.common.id
deletion_policy = var.project_deletion_policy
activate_apis = ["logging.googleapis.com", "secretmanager.googleapis.com", "billingbudgets.googleapis.com"]
auto_create_network = true


labels = {
environment = "common"
Expand Down Expand Up @@ -182,6 +188,8 @@ module "interconnect" {
folder_id = google_folder.network.id
deletion_policy = var.project_deletion_policy
activate_apis = ["billingbudgets.googleapis.com", "compute.googleapis.com"]
auto_create_network = true


labels = {
environment = "network"
Expand Down Expand Up @@ -216,6 +224,8 @@ module "scc_notifications" {
folder_id = google_folder.common.id
deletion_policy = var.project_deletion_policy
activate_apis = ["logging.googleapis.com", "pubsub.googleapis.com", "securitycenter.googleapis.com", "billingbudgets.googleapis.com", "cloudkms.googleapis.com"]
auto_create_network = true


labels = {
environment = "common"
Expand Down Expand Up @@ -249,6 +259,8 @@ module "dns_hub" {
billing_account = local.billing_account
folder_id = google_folder.network.id
deletion_policy = var.project_deletion_policy
auto_create_network = true


activate_apis = [
"compute.googleapis.com",
Expand Down Expand Up @@ -292,6 +304,8 @@ module "base_network_hub" {
billing_account = local.billing_account
folder_id = google_folder.network.id
deletion_policy = var.project_deletion_policy
auto_create_network = true


activate_apis = [
"compute.googleapis.com",
Expand Down Expand Up @@ -343,6 +357,8 @@ module "restricted_network_hub" {
billing_account = local.billing_account
folder_id = google_folder.network.id
deletion_policy = var.project_deletion_policy
auto_create_network = true


activate_apis = [
"compute.googleapis.com",
Expand Down
2 changes: 2 additions & 0 deletions 1-org/modules/network/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "base_shared_vpc_host_project" {
folder_id = var.folder_id
disable_services_on_destroy = false
deletion_policy = var.project_deletion_policy
auto_create_network = true

activate_apis = [
"compute.googleapis.com",
Expand Down Expand Up @@ -67,6 +68,7 @@ module "restricted_shared_vpc_host_project" {
folder_id = var.folder_id
disable_services_on_destroy = false
deletion_policy = var.project_deletion_policy
auto_create_network = true

activate_apis = [
"compute.googleapis.com",
Expand Down
1 change: 1 addition & 0 deletions 2-environments/modules/env_baseline/kms.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module "env_kms" {
disable_services_on_destroy = false
depends_on = [time_sleep.wait_60_seconds]
activate_apis = ["logging.googleapis.com", "cloudkms.googleapis.com", "billingbudgets.googleapis.com"]
auto_create_network = true
deletion_policy = var.project_deletion_policy

labels = {
Expand Down
1 change: 1 addition & 0 deletions 2-environments/modules/env_baseline/secrets.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module "env_secrets" {
disable_services_on_destroy = false
depends_on = [time_sleep.wait_60_seconds]
activate_apis = ["logging.googleapis.com", "secretmanager.googleapis.com"]
auto_create_network = true
deletion_policy = var.project_deletion_policy

labels = {
Expand Down
1 change: 1 addition & 0 deletions 4-projects/modules/single_project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module "project" {
org_id = var.org_id
billing_account = var.billing_account
folder_id = var.folder_id
auto_create_network = true
deletion_policy = var.project_deletion_policy

svpc_host_project_id = var.shared_vpc_host_project_id
Expand Down
11 changes: 11 additions & 0 deletions test/integration/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ func TestBootstrap(t *testing.T) {
bootstrap.DefineVerify(
func(assert *assert.Assertions) {

parentFolder := terraform.OutputMap(t, bootstrap.GetTFOptions(), "common_config")["parent_folder"]

// cloud build project
cbProjectID := bootstrap.GetStringOutput("cloudbuild_project_id")
artifactsBktName := terraform.OutputMap(t, bootstrap.GetTFOptions(), "gcs_bucket_cloudbuild_artifacts")
Expand Down Expand Up @@ -306,8 +308,17 @@ func TestBootstrap(t *testing.T) {
assert.Subset(listRoles, sa.orgRoles, fmt.Sprintf("service account %s should have organization level roles", terraformSAEmail))
}
}
// boolean organization policies
for _, booleanConstraint := range []string{
"constraints/compute.skipDefaultNetworkCreation",
} {
orgPolicy := gcloud.Runf(t, "resource-manager org-policies describe %s --folder %s", booleanConstraint, parentFolder)
assert.True(orgPolicy.Get("booleanPolicy.enforced").Bool(), fmt.Sprintf("org policy %s should be enforced", booleanConstraint))
}
})



bootstrap.DefineTeardown(func(assert *assert.Assertions) {
// configure options to pull state from GCS bucket
cwd, err := os.Getwd()
Expand Down
1 change: 0 additions & 1 deletion test/integration/org/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ func TestOrg(t *testing.T) {
for _, booleanConstraint := range []string{
"constraints/compute.disableNestedVirtualization",
"constraints/compute.disableSerialPortAccess",
"constraints/compute.skipDefaultNetworkCreation",
"constraints/compute.restrictXpnProjectLienRemoval",
"constraints/sql.restrictPublicIp",
"constraints/sql.restrictAuthorizedNetworks",
Expand Down
1 change: 1 addition & 0 deletions test/setup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module "project" {
folder_id = var.folder_id
billing_account = var.billing_account
deletion_policy = "DELETE"
auto_create_network = true

activate_apis = [
"cloudresourcemanager.googleapis.com",
Expand Down