Skip to content

Commit 1edf793

Browse files
Feat: enable kms centralized key usage tracking (#1402)
Co-authored-by: eeaton <[email protected]>
1 parent 35202f1 commit 1edf793

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

1-org/envs/shared/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
| create\_unique\_tag\_key | Creates unique organization-wide tag keys by adding a random suffix to each key. | `bool` | `false` | no |
99
| domains\_to\_allow | The list of domains to allow users from in IAM. Used by Domain Restricted Sharing Organization Policy. Must include the domain of the organization you are deploying the foundation. To add other domains you must also grant access to these domains to the Terraform Service Account used in the deploy. | `list(string)` | n/a | yes |
1010
| enable\_hub\_and\_spoke | Enable Hub-and-Spoke architecture. | `bool` | `false` | no |
11+
| enable\_kms\_key\_usage\_tracking | Enable KMS centralized key usage tracking system. | `bool` | `true` | no |
1112
| enable\_scc\_resources\_in\_terraform | Create Security Command Center resources in Terraform. If your organization has newly enabled any preview features for SCC and get an error related to the v2 API, you must set this variable to false because the v2 API does not yet support Terraform resources. See [issue 1189](https://github.com/terraform-google-modules/terraform-example-foundation/issues/1189) for context. | `bool` | `false` | no |
1213
| enforce\_allowed\_worker\_pools | Whether to enforce the organization policy restriction on allowed worker pools for Cloud Build. | `bool` | `false` | no |
1314
| essential\_contacts\_domains\_to\_allow | The list of domains that email addresses added to Essential Contacts can have. | `list(string)` | n/a | yes |
1415
| essential\_contacts\_language | Essential Contacts preferred language for notifications, as a ISO 639-1 language code. See [Supported languages](https://cloud.google.com/resource-manager/docs/managing-notification-contacts#supported-languages) for a list of supported languages. | `string` | `"en"` | no |
1516
| folder\_deletion\_protection | Prevent Terraform from destroying or recreating the folder. | `string` | `true` | no |
16-
| gcp\_groups | Groups to grant specific roles in the Organization.<br> platform\_viewer: Google Workspace or Cloud Identity group that have the ability to view resource information across the Google Cloud organization.<br> security\_reviewer: Google Workspace or Cloud Identity group that members are part of the security team responsible for reviewing cloud security<br> network\_viewer: Google Workspace or Cloud Identity group that members are part of the networking team and review network configurations.<br> scc\_admin: Google Workspace or Cloud Identity group that can administer Security Command Center.<br> audit\_viewer: Google Workspace or Cloud Identity group that members are part of an audit team and view audit logs in the logging project.<br> global\_secrets\_admin: Google Workspace or Cloud Identity group that members are responsible for putting secrets into Secrets Manage | <pre>object({<br> audit_viewer = optional(string, null)<br> security_reviewer = optional(string, null)<br> network_viewer = optional(string, null)<br> scc_admin = optional(string, null)<br> global_secrets_admin = optional(string, null)<br> kms_admin = optional(string, null)<br> })</pre> | `{}` | no |
17+
| gcp\_groups | Groups to grant specific roles in the Organization.<br> platform\_viewer: Google Workspace or Cloud Identity group that have the ability to view resource information across the Google Cloud organization.<br> security\_reviewer: Google Workspace or Cloud Identity group that members are part of the security team responsible for reviewing cloud security<br> network\_viewer: Google Workspace or Cloud Identity group that members are part of the networking team and review network configurations.<br> scc\_admin: Google Workspace or Cloud Identity group that can administer Security Command Center.<br> audit\_viewer: Google Workspace or Cloud Identity group that members are part of an audit team and view audit logs in the logging project.<br> global\_secrets\_admin: Google Workspace or Cloud Identity group that members are responsible for putting secrets into Secrets Management. | <pre>object({<br> audit_viewer = optional(string, null)<br> security_reviewer = optional(string, null)<br> network_viewer = optional(string, null)<br> scc_admin = optional(string, null)<br> global_secrets_admin = optional(string, null)<br> kms_admin = optional(string, null)<br> })</pre> | `{}` | no |
1718
| log\_export\_storage\_force\_destroy | (Optional) If set to true, delete all contents when destroying the resource; otherwise, destroying the resource will fail if contents are present. | `bool` | `false` | no |
1819
| log\_export\_storage\_location | The location of the storage bucket used to export logs. | `string` | `null` | no |
1920
| log\_export\_storage\_retention\_policy | Configuration of the bucket's data retention policy for how long objects in the bucket should be retained. | <pre>object({<br> is_locked = bool<br> retention_period_days = number<br> })</pre> | `null` | no |

1-org/envs/shared/iam.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,34 @@ resource "google_organization_iam_member" "billing_viewer" {
6262
member = "group:${local.required_groups["billing_data_users"]}"
6363
}
6464

65+
/******************************************
66+
Enable KMS Usage Tracking
67+
*****************************************/
68+
69+
module "create_kms_organization_service_agent" {
70+
source = "terraform-google-modules/gcloud/google"
71+
version = "~> 3.1"
72+
upgrade = false
73+
74+
create_cmd_triggers = {
75+
org_id = local.org_id
76+
}
77+
78+
create_cmd_body = "beta services identity create --service cloudkms.googleapis.com --organization ${local.org_id}"
79+
}
80+
81+
resource "google_organization_iam_member" "kms_usage_tracking" {
82+
count = var.enable_kms_key_usage_tracking ? 1 : 0
83+
84+
depends_on = [
85+
module.create_kms_organization_service_agent,
86+
]
87+
88+
org_id = local.org_id
89+
role = "roles/cloudkms.orgServiceAgent"
90+
member = "serviceAccount:service-org-${local.org_id}@gcp-sa-cloudkms.iam.gserviceaccount.com"
91+
}
92+
6593
/******************************************
6694
Groups permissions
6795
*****************************************/
@@ -143,6 +171,13 @@ resource "google_project_iam_member" "kms_admin" {
143171
member = "group:${var.gcp_groups.kms_admin}"
144172
}
145173

174+
resource "google_organization_iam_member" "kms_protected_resources_viewer" {
175+
count = var.gcp_groups.kms_admin != null && var.enable_kms_key_usage_tracking ? 1 : 0
176+
org_id = local.org_id
177+
role = "roles/cloudkms.protectedResourcesViewer"
178+
member = "group:${var.gcp_groups.kms_admin}"
179+
}
180+
146181
resource "google_project_iam_member" "cai_monitoring_builder" {
147182
project = module.scc_notifications.project_id
148183
for_each = toset(var.enable_scc_resources_in_terraform ?

1-org/envs/shared/variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ variable "enable_scc_resources_in_terraform" {
2626
default = false
2727
}
2828

29+
variable "enable_kms_key_usage_tracking" {
30+
description = "Enable KMS centralized key usage tracking system."
31+
type = bool
32+
default = true
33+
}
34+
2935
variable "domains_to_allow" {
3036
description = "The list of domains to allow users from in IAM. Used by Domain Restricted Sharing Organization Policy. Must include the domain of the organization you are deploying the foundation. To add other domains you must also grant access to these domains to the Terraform Service Account used in the deploy."
3137
type = list(string)
@@ -141,7 +147,7 @@ variable "gcp_groups" {
141147
network_viewer: Google Workspace or Cloud Identity group that members are part of the networking team and review network configurations.
142148
scc_admin: Google Workspace or Cloud Identity group that can administer Security Command Center.
143149
audit_viewer: Google Workspace or Cloud Identity group that members are part of an audit team and view audit logs in the logging project.
144-
global_secrets_admin: Google Workspace or Cloud Identity group that members are responsible for putting secrets into Secrets Manage
150+
global_secrets_admin: Google Workspace or Cloud Identity group that members are responsible for putting secrets into Secrets Management.
145151
EOT
146152
type = object({
147153
audit_viewer = optional(string, null)

test/integration/testutils/retry.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,8 @@ var (
4545

4646
// Error waiting for creating service network connection. This happens randomly for development, production and non-production environments
4747
".*Error code 16.*Error waiting for Create Service Networking Connection*": "Request had invalid authentication credentials",
48+
49+
// Error 400: The eTag provided {} does not match the eTag of the current version of the Access Policy, which is {}.
50+
".*Error 400: The eTag provided.*does not match the eTag of the current version of the Access Policy, which is.*": "Conflict during Access Policy configuration.",
4851
}
4952
)

0 commit comments

Comments
 (0)