Skip to content

Commit c5788d0

Browse files
feat: add org policies to confidential computing example (#427)
1 parent bf8c7cb commit c5788d0

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

examples/confidential_computing/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
This is an example of a vm creation with confidential computing,
44
encrypted disk using a multiregion (US by default) Cloud HSM key
5-
and a custom service account with cloud-platform scope.
5+
and a custom service account with cloud-platform scope. It also
6+
creates org policies enforcing the use of CMEK encrypted instances
7+
and confidential computing to all newly created VMs within the project.
8+
Also, an additional org policy constraint is created, which only allows
9+
Cloud KMS keys (used for CMEK protection) that come from the provided input project.
10+
Note: existing VM instances won't be affected by the new org policy.
611

712
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
813
## Inputs
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
module "confidential-computing-org-policy" {
18+
source = "terraform-google-modules/org-policy/google"
19+
version = "~> 5.3"
20+
21+
project_id = var.project_id
22+
policy_for = "project"
23+
constraint = "constraints/compute.restrictNonConfidentialComputing"
24+
policy_type = "list"
25+
deny = ["compute.googleapis.com"]
26+
deny_list_length = 1
27+
}
28+
29+
module "enforce-cmek-org-policy" {
30+
source = "terraform-google-modules/org-policy/google"
31+
version = "~> 5.3"
32+
33+
project_id = var.project_id
34+
policy_for = "project"
35+
constraint = "constraints/gcp.restrictNonCmekServices"
36+
policy_type = "list"
37+
deny = ["compute.googleapis.com"]
38+
deny_list_length = 1
39+
}
40+
41+
module "restrict-cmek-cryptokey-projects-policy" {
42+
source = "terraform-google-modules/org-policy/google"
43+
version = "~> 5.3"
44+
45+
project_id = var.project_id
46+
policy_for = "project"
47+
constraint = "constraints/gcp.restrictCmekCryptoKeyProjects"
48+
policy_type = "list"
49+
allow = ["projects/${var.project_id}"]
50+
allow_list_length = 1
51+
}

test/integration/confidential_compute_instance/confidential_compute_instance_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"github.com/stretchr/testify/assert"
2424
)
2525

26-
func TestConfidentialInstanceTemplate(t *testing.T) {
26+
func TestConfidentialComputeInstance(t *testing.T) {
2727
const instanceNamePrefix = "confidential-encrypted-instance"
2828

2929
confCompInst := tft.NewTFBlueprintTest(t)
@@ -49,6 +49,22 @@ func TestConfidentialInstanceTemplate(t *testing.T) {
4949
assert.Len(disks, 1)
5050
defaultSuffix := confCompInst.GetStringOutput("suffix")
5151
assert.Equal(fmt.Sprintf("projects/%s/locations/us/keyRings/key-ring-test-%s/cryptoKeys/key-test-%s/cryptoKeyVersions/1", projectId, defaultSuffix, defaultSuffix), disks[0].Get("diskEncryptionKey").Get("kmsKeyName").String())
52+
53+
org_policy_cmek_constraint := gcloud.Runf(t, "resource-manager org-policies list --project=%s --format=json --filter constraint='constraints/gcp.restrictNonCmekServices'", projectId).Array()
54+
assert.Len(org_policy_cmek_constraint, 1)
55+
cmek_denied_values_list := org_policy_cmek_constraint[0].Get("listPolicy.deniedValues").Array()
56+
assert.Len(cmek_denied_values_list, 1)
57+
assert.Equal("compute.googleapis.com", cmek_denied_values_list[0].String())
58+
org_policy_cmek_projects := gcloud.Runf(t, "resource-manager org-policies list --project=%s --format=json --filter constraint='constraints/gcp.restrictCmekCryptoKeyProjects'", projectId).Array()
59+
assert.Len(org_policy_cmek_projects, 1)
60+
cmek_allowed_projects := org_policy_cmek_projects[0].Get("listPolicy.allowedValues").Array()
61+
assert.Len(cmek_allowed_projects, 1)
62+
assert.Equal(fmt.Sprintf("projects/%s", projectId), cmek_allowed_projects[0].String())
63+
org_policy_confidential_constraint := gcloud.Runf(t, "resource-manager org-policies list --project=%s --format=json --filter constraint='constraints/compute.restrictNonConfidentialComputing'", projectId).Array()
64+
assert.Len(org_policy_confidential_constraint, 1)
65+
cc_denied_values_list := org_policy_confidential_constraint[0].Get("listPolicy.deniedValues").Array()
66+
assert.Len(cc_denied_values_list, 1)
67+
assert.Equal("compute.googleapis.com", cc_denied_values_list[0].String())
5268
})
5369
confCompInst.Test()
5470
}

test/setup/iam.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ resource "google_project_iam_member" "ci_vm_account" {
3838
member = "serviceAccount:${google_service_account.ci_vm_account.email}"
3939
}
4040

41+
resource "google_organization_iam_member" "ci_vm_account_organization" {
42+
org_id = var.org_id
43+
role = "roles/orgpolicy.policyAdmin"
44+
member = "serviceAccount:${google_service_account.ci_vm_account.email}"
45+
}
46+
4147
resource "google_service_account_key" "ci_vm_account" {
4248
service_account_id = google_service_account.ci_vm_account.id
4349
}

0 commit comments

Comments
 (0)