-
Notifications
You must be signed in to change notification settings - Fork 70
feat: Add scopes, update directional rules variable definition and dry-run lifecycle ignore_changes
#199
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
Merged
apeabody
merged 16 commits into
terraform-google-modules:main
from
daniel-cit:fix-directional-rules
Jun 3, 2025
Merged
feat: Add scopes, update directional rules variable definition and dry-run lifecycle ignore_changes
#199
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c24c5bc
fix Directional Policies ignore_changes
daniel-cit cf117ed
add egress rule example
daniel-cit 2d434e4
update ingress example
daniel-cit 8fc2373
update Directional Policies rules
daniel-cit e79b287
Merge remote-tracking branch 'origin/master' into fix-directional-rules
daniel-cit 33694b2
fix tflint issue
daniel-cit e7bff95
Add scopes to access policy and tests for directional rules
daniel-cit 7329221
add fixture
daniel-cit a6027ba
fix fixture variables
daniel-cit 0947d3b
remove extra random suffix
daniel-cit d0dce29
increase retry count due to vpc-sc propagation
daniel-cit 37bb163
change creation order in the directional example
daniel-cit 01ad905
update retry count
daniel-cit ba7dbf0
fix duplication
daniel-cit 2429670
fix egress rule title check
daniel-cit e1d5991
code review fixes
daniel-cit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Scoped Example with Egress Rule | ||
|
||
This example illustrates how to use the `vpc-service-controls` module to configure a scoped org policy, a regular perimeter and external storage buckets that can be access in it from inside read only via egress rule. | ||
|
||
# Requirements | ||
|
||
1. Make sure you've gone through the root [Requirement Section](../../README.md#requirements) on any project in your organization. | ||
2. If you need to run integration tests for this example, select a second project in your organization. The project you already configured will be referred as the protected project that will be inside of the regular service perimeter. The second project will be the public project, which will be outside of the regular service perimeter. | ||
3. Grant the service account the following permissions on the public project: | ||
- roles/storage.Admin | ||
|
||
You may use the following gcloud commands: | ||
`gcloud projects add-iam-policy-binding <project-id> --member=serviceAccount:<service-account-email> --role=roles/storage.Admin` | ||
|
||
1. Enable Storage API on the protected project. | ||
2. If you want to run the integration tests for this example, repeat step #3 and #4 on the protected project. | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| access\_level\_name | Access level name of the Access Policy. | `string` | `"terraform_members_e"` | no | | ||
| buckets\_names | Buckets Names as list of strings | `list(string)` | <pre>[<br> "bucket1-e",<br> "bucket2-e"<br>]</pre> | no | | ||
| buckets\_prefix | Bucket Prefix | `string` | `"test-bucket-e"` | no | | ||
| members | An allowed list of members (users, service accounts). The signed-in identity originating the request must be a part of one of the provided members. If not specified, a request may come from any user (logged in/not logged in, etc.). Formats: user:{emailid}, serviceAccount:{emailid} | `list(string)` | n/a | yes | | ||
| parent\_id | The parent of this AccessPolicy in the Cloud Resource Hierarchy. As of now, only organization are accepted as parent. | `string` | n/a | yes | | ||
| perimeter\_name | Perimeter name of the Access Policy.. | `string` | `"regular_perimeter_e"` | no | | ||
| policy\_name | The policy's name. | `string` | n/a | yes | | ||
| protected\_project\_ids | Project id and number of the project INSIDE the regular service perimeter. This map variable expects an "id" for the project id and "number" key for the project number. | `object({ id = string, number = number })` | n/a | yes | | ||
| public\_project\_ids | Project id and number of the project OUTSIDE the regular service perimeter. This map variable expects an "id" for the project id and "number" key for the project number. | `object({ id = string, number = number })` | n/a | yes | | ||
| regions | The request must originate from one of the provided countries/regions. Format: A valid ISO 3166-1 alpha-2 code. | `list(string)` | `[]` | no | | ||
| scopes | Folder or project on which this policy is applicable. Format: 'folders/FOLDER\_ID' or 'projects/PROJECT\_NUMBER' | `list(string)` | `[]` | no | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| policy\_id | Resource name of the AccessPolicy. | | ||
| policy\_name | Name of the parent policy | | ||
| protected\_project\_id | Project id of the project INSIDE the regular service perimeter | | ||
| service\_perimeter\_name | Service perimeter name | | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
|
||
To provision this example, run the following from within this directory: | ||
- `terraform init` to get the plugins | ||
- `terraform plan` to see the infrastructure plan | ||
- `terraform apply` to apply the infrastructure build | ||
- `terraform destroy` to destroy the built infrastructure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
module "access_context_manager_policy" { | ||
source = "terraform-google-modules/vpc-service-controls/google" | ||
version = "~> 7.1" | ||
|
||
parent_id = var.parent_id | ||
policy_name = var.policy_name | ||
scopes = var.scopes | ||
|
||
depends_on = [module.gcs_buckets] | ||
} | ||
|
||
module "access_level_members" { | ||
source = "terraform-google-modules/vpc-service-controls/google//modules/access_level" | ||
version = "~> 7.1" | ||
|
||
description = "Simple Example Access Level" | ||
policy = module.access_context_manager_policy.policy_id | ||
name = var.access_level_name | ||
members = var.members | ||
regions = var.regions | ||
} | ||
|
||
resource "time_sleep" "wait_for_members" { | ||
create_duration = "90s" | ||
destroy_duration = "90s" | ||
|
||
depends_on = [module.access_level_members] | ||
} | ||
|
||
module "regular_service_perimeter_1" { | ||
source = "terraform-google-modules/vpc-service-controls/google//modules/regular_service_perimeter" | ||
version = "~> 7.1" | ||
|
||
|
||
policy = module.access_context_manager_policy.policy_id | ||
perimeter_name = var.perimeter_name | ||
|
||
description = "Perimeter shielding bigquery project" | ||
resources = [var.protected_project_ids["number"]] | ||
access_levels = [module.access_level_members.name] | ||
|
||
restricted_services = ["bigquery.googleapis.com", "storage.googleapis.com"] | ||
|
||
egress_policies = [ | ||
{ | ||
title = "Read outside buckets from project" | ||
from = { | ||
sources = { | ||
resources = ["projects/${var.protected_project_ids["number"]}"] | ||
}, | ||
identity_type = "ANY_SERVICE_ACCOUNT" | ||
} | ||
to = { | ||
resources = [ | ||
"projects/${var.public_project_ids["number"]}" | ||
] | ||
operations = { | ||
"storage.googleapis.com" = { | ||
methods = [ | ||
"google.storage.objects.get", | ||
"google.storage.objects.list" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
title = "Use permissions for Big Query access" # See https://cloud.google.com/vpc-service-controls/docs/supported-method-restrictions | ||
from = { | ||
sources = { | ||
resources = ["projects/${var.protected_project_ids["number"]}"] | ||
}, | ||
identity_type = "ANY_SERVICE_ACCOUNT" | ||
} | ||
to = { | ||
resources = [ | ||
"projects/${var.public_project_ids["number"]}" | ||
] | ||
operations = { | ||
"bigquery.googleapis.com" = { | ||
permissions = [ | ||
"bigquery.datasets.get", | ||
"bigquery.models.getData", | ||
"bigquery.models.getMetadata", | ||
"bigquery.models.list", | ||
"bigquery.tables.get", | ||
"bigquery.tables.getData", | ||
"bigquery.tables.list" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
] | ||
|
||
shared_resources = { | ||
all = [var.protected_project_ids["number"]] | ||
} | ||
|
||
depends_on = [ | ||
module.gcs_buckets, | ||
time_sleep.wait_for_members | ||
] | ||
} | ||
|
||
module "gcs_buckets" { | ||
source = "terraform-google-modules/cloud-storage/google" | ||
version = "~> 10.0" | ||
project_id = var.public_project_ids["id"] | ||
names = var.buckets_names | ||
randomize_suffix = true | ||
prefix = var.buckets_prefix | ||
set_admin_roles = true | ||
admins = var.members | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
output "policy_id" { | ||
description = "Resource name of the AccessPolicy." | ||
value = module.access_context_manager_policy.policy_id | ||
} | ||
|
||
output "policy_name" { | ||
description = "Name of the parent policy" | ||
value = var.policy_name | ||
} | ||
|
||
|
||
output "protected_project_id" { | ||
description = "Project id of the project INSIDE the regular service perimeter" | ||
value = var.protected_project_ids["id"] | ||
} | ||
|
||
output "service_perimeter_name" { | ||
description = "Service perimeter name" | ||
value = module.regular_service_perimeter_1.perimeter_name | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Copyright 2025 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. | ||
*/ | ||
|
||
variable "parent_id" { | ||
description = "The parent of this AccessPolicy in the Cloud Resource Hierarchy. As of now, only organization are accepted as parent." | ||
type = string | ||
} | ||
|
||
variable "policy_name" { | ||
description = "The policy's name." | ||
type = string | ||
} | ||
|
||
variable "protected_project_ids" { | ||
description = "Project id and number of the project INSIDE the regular service perimeter. This map variable expects an \"id\" for the project id and \"number\" key for the project number." | ||
type = object({ id = string, number = number }) | ||
} | ||
|
||
variable "public_project_ids" { | ||
description = "Project id and number of the project OUTSIDE the regular service perimeter. This map variable expects an \"id\" for the project id and \"number\" key for the project number." | ||
type = object({ id = string, number = number }) | ||
} | ||
|
||
variable "members" { | ||
description = "An allowed list of members (users, service accounts). The signed-in identity originating the request must be a part of one of the provided members. If not specified, a request may come from any user (logged in/not logged in, etc.). Formats: user:{emailid}, serviceAccount:{emailid}" | ||
type = list(string) | ||
} | ||
|
||
variable "regions" { | ||
description = "The request must originate from one of the provided countries/regions. Format: A valid ISO 3166-1 alpha-2 code." | ||
type = list(string) | ||
default = [] | ||
} | ||
|
||
variable "perimeter_name" { | ||
description = "Perimeter name of the Access Policy.." | ||
type = string | ||
default = "regular_perimeter_e" | ||
} | ||
|
||
variable "access_level_name" { | ||
description = "Access level name of the Access Policy." | ||
type = string | ||
default = "terraform_members_e" | ||
} | ||
|
||
variable "buckets_prefix" { | ||
description = "Bucket Prefix" | ||
type = string | ||
default = "test-bucket-e" | ||
} | ||
|
||
variable "buckets_names" { | ||
description = "Buckets Names as list of strings" | ||
type = list(string) | ||
default = ["bucket1-e", "bucket2-e"] | ||
} | ||
|
||
variable "scopes" { | ||
description = "Folder or project on which this policy is applicable. Format: 'folders/FOLDER_ID' or 'projects/PROJECT_NUMBER'" | ||
type = list(string) | ||
default = [] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.