-
Notifications
You must be signed in to change notification settings - Fork 94
/
main.tf
94 lines (79 loc) · 3.29 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* Copyright 2018 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 {
keys_by_name = zipmap(var.keys, var.prevent_destroy ? slice(google_kms_crypto_key.key[*].id, 0, length(var.keys)) : slice(google_kms_crypto_key.key_ephemeral[*].id, 0, length(var.keys)))
}
resource "google_kms_key_ring" "key_ring" {
name = var.keyring
project = var.project_id
location = var.location
}
resource "google_kms_crypto_key" "key" {
count = var.prevent_destroy ? length(var.keys) : 0
name = var.keys[count.index]
key_ring = google_kms_key_ring.key_ring.id
rotation_period = var.key_rotation_period
purpose = var.purpose
import_only = var.import_only
skip_initial_version_creation = var.skip_initial_version_creation
crypto_key_backend = var.crypto_key_backend
lifecycle {
prevent_destroy = true
}
destroy_scheduled_duration = var.key_destroy_scheduled_duration
version_template {
algorithm = var.key_algorithm
protection_level = var.key_protection_level
}
labels = var.labels
}
resource "google_kms_crypto_key" "key_ephemeral" {
count = var.prevent_destroy ? 0 : length(var.keys)
name = var.keys[count.index]
key_ring = google_kms_key_ring.key_ring.id
rotation_period = var.key_rotation_period
purpose = var.purpose
import_only = var.import_only
skip_initial_version_creation = var.skip_initial_version_creation
crypto_key_backend = var.crypto_key_backend
lifecycle {
prevent_destroy = false
}
destroy_scheduled_duration = var.key_destroy_scheduled_duration
version_template {
algorithm = var.key_algorithm
protection_level = var.key_protection_level
}
labels = var.labels
}
resource "google_kms_crypto_key_iam_binding" "owners" {
count = length(var.set_owners_for)
role = "roles/owner"
crypto_key_id = local.keys_by_name[var.set_owners_for[count.index]]
members = compact(split(",", var.owners[count.index]))
}
resource "google_kms_crypto_key_iam_binding" "decrypters" {
count = length(var.set_decrypters_for)
role = "roles/cloudkms.cryptoKeyDecrypter"
crypto_key_id = local.keys_by_name[var.set_decrypters_for[count.index]]
members = compact(split(",", var.decrypters[count.index]))
}
resource "google_kms_crypto_key_iam_binding" "encrypters" {
count = length(var.set_encrypters_for)
role = "roles/cloudkms.cryptoKeyEncrypter"
crypto_key_id = local.keys_by_name[element(var.set_encrypters_for, count.index)]
members = compact(split(",", var.encrypters[count.index]))
}