|
| 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 | +data "google_project" "kms_project" { |
| 18 | + project_id = var.project_id |
| 19 | +} |
| 20 | + |
| 21 | +#Create KMS Service Agent |
| 22 | +resource "google_project_service_identity" "kms_service_agent" { |
| 23 | + count = local.create_autokey_key_handles ? 1 : 0 |
| 24 | + provider = google-beta |
| 25 | + |
| 26 | + service = "cloudkms.googleapis.com" |
| 27 | + project = data.google_project.kms_project.number |
| 28 | +} |
| 29 | + |
| 30 | +# Wait delay after creating service agent. |
| 31 | +resource "time_sleep" "wait_service_agent" { |
| 32 | + count = local.create_autokey_key_handles ? 1 : 0 |
| 33 | + |
| 34 | + create_duration = "10s" |
| 35 | + depends_on = [google_project_service_identity.kms_service_agent] |
| 36 | +} |
| 37 | + |
| 38 | +#Grant the KMS Service Agent the Cloud KMS Admin role |
| 39 | +resource "google_project_iam_member" "autokey_project_admin" { |
| 40 | + count = local.create_autokey_key_handles ? 1 : 0 |
| 41 | + provider = google-beta |
| 42 | + |
| 43 | + project = var.project_id |
| 44 | + role = "roles/cloudkms.admin" |
| 45 | + member = "serviceAccount:service-${data.google_project.kms_project.number}@gcp-sa-cloudkms.iam.gserviceaccount.com" |
| 46 | + depends_on = [time_sleep.wait_service_agent] |
| 47 | +} |
| 48 | + |
| 49 | +# Wait delay after granting IAM permissions |
| 50 | +resource "time_sleep" "wait_srv_acc_permissions" { |
| 51 | + count = local.create_autokey_key_handles ? 1 : 0 |
| 52 | + |
| 53 | + create_duration = "10s" |
| 54 | + depends_on = [google_project_iam_member.autokey_project_admin] |
| 55 | +} |
0 commit comments