Skip to content

Commit 6de47d4

Browse files
authored
Merge pull request #14 from terraform-google-modules/ludo-v1.1.0-2
Allow setting prevent_destroy lifecycle value on keys (alternate version)
2 parents bc87966 + 5dbb7f2 commit 6de47d4

File tree

14 files changed

+100
-44
lines changed

14 files changed

+100
-44
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ and this project adheres to
99

1010
## [Unreleased]
1111

12+
## [1.1.0]
13+
1214
### Added
1315

16+
- Allow setting prevent_destroy lifecycle value on keys, add keyring resource output [#14]
1417
- Added continuous integration using Cloud Build. [#11]
1518

1619
## [1.0.0] - 2019-07-19
@@ -26,8 +29,10 @@ and this project adheres to
2629
- Initial release
2730

2831
[Unreleased]: https://github.com/terraform-google-modules/terraform-google-kms/compare/v1.0.0...HEAD
29-
[0.1.0]: https://github.com/terraform-google-modules/terraform-google-kms/releases/tag/v0.1.0
32+
[1.1.0]: https://github.com/terraform-google-modules/terraform-google-kms/releases/tag/v1.1.0
3033
[1.0.0]: https://github.com/terraform-google-modules/terraform-google-kms/releases/tag/v1.0.0
34+
[0.1.0]: https://github.com/terraform-google-modules/terraform-google-kms/releases/tag/v0.1.0
3135

32-
[#3]: https://github.com/terraform-google-modules/terraform-google-kms/pull/3
36+
[#14]: https://github.com/terraform-google-modules/terraform-google-kms/pull/11
3337
[#11]: https://github.com/terraform-google-modules/terraform-google-kms/pull/11
38+
[#3]: https://github.com/terraform-google-modules/terraform-google-kms/pull/3

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
SHELL := /usr/bin/env bash # Make will use bash instead of sh
1919

20-
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.0.1
20+
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.4.3
2121
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
2222
REGISTRY_URL := gcr.io/cloud-foundation-cicd
2323

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ Functional examples are included in the
4343

4444
| Name | Description | Type | Default | Required |
4545
|------|-------------|:----:|:-----:|:-----:|
46-
| decrypters | List of comma-separated owners for each key declared in set_decrypters_for. | list(string) | `<list>` | no |
47-
| encrypters | List of comma-separated owners for each key declared in set_encrypters_for. | list(string) | `<list>` | no |
46+
| decrypters | List of comma-separated owners for each key declared in set\_decrypters\_for. | list(string) | `<list>` | no |
47+
| encrypters | List of comma-separated owners for each key declared in set\_encrypters\_for. | list(string) | `<list>` | no |
4848
| key\_rotation\_period | | string | `"100000s"` | no |
4949
| keyring | Keyring name. | string | n/a | yes |
5050
| keys | Key names. | list(string) | `<list>` | no |
5151
| location | Location for the keyring. | string | n/a | yes |
52-
| owners | List of comma-separated owners for each key declared in set_owners_for. | list(string) | `<list>` | no |
52+
| owners | List of comma-separated owners for each key declared in set\_owners\_for. | list(string) | `<list>` | no |
53+
| prevent\_destroy | Set the prevent\_destroy lifecycle attribute on keys. | string | `"true"` | no |
5354
| project\_id | Project id where the keyring will be created. | string | n/a | yes |
5455
| set\_decrypters\_for | Name of keys for which decrypters will be set. | list(string) | `<list>` | no |
5556
| set\_encrypters\_for | Name of keys for which encrypters will be set. | list(string) | `<list>` | no |
@@ -61,6 +62,7 @@ Functional examples are included in the
6162
|------|-------------|
6263
| keyring | Self link of the keyring. |
6364
| keyring\_name | Name of the keyring. |
65+
| keyring\_resource | Keyring resource. |
6466
| keys | Map of key name => key self link. |
6567

6668
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

examples/simple_example/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This example illustrates how to use the `kms` module.
88
| Name | Description | Type | Default | Required |
99
|------|-------------|:----:|:-----:|:-----:|
1010
| keyring | Keyring name. | string | n/a | yes |
11+
| keys | Key names. | list(string) | `<list>` | no |
1112
| location | Location for the keyring. | string | `"global"` | no |
1213
| project\_id | The ID of the project in which to provision resources. | string | n/a | yes |
1314

@@ -16,6 +17,8 @@ This example illustrates how to use the `kms` module.
1617
| Name | Description |
1718
|------|-------------|
1819
| keyring | The name of the keyring. |
20+
| keys | List of created kkey names. |
21+
| location | The location of the keyring. |
1922

2023
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2124

examples/simple_example/main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ provider "google" {
1919
}
2020

2121
module "kms" {
22-
source = "../.."
23-
22+
source = "../.."
2423
project_id = var.project_id
2524
keyring = var.keyring
2625
location = "global"
26+
keys = var.keys
27+
# keys can be destroyed by Terraform
28+
prevent_destroy = false
2729
}
2830

examples/simple_example/outputs.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
output "keyring" {
1818
description = "The name of the keyring."
19-
value = module.kms.keyring_name
19+
value = module.kms.keyring_resource.name
2020
}
2121

22+
output "location" {
23+
description = "The location of the keyring."
24+
value = module.kms.keyring_resource.location
25+
}
26+
27+
output "keys" {
28+
description = "List of created kkey names."
29+
value = keys(module.kms.keys)
30+
}

examples/simple_example/variables.tf

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@
1515
*/
1616

1717
variable "project_id" {
18-
type = string
1918
description = "The ID of the project in which to provision resources."
19+
type = string
2020
}
2121

2222
variable "location" {
23-
type = string
2423
description = "Location for the keyring."
25-
26-
default = "global"
24+
type = string
25+
default = "global"
2726
}
2827

2928
variable "keyring" {
30-
type = string
3129
description = "Keyring name."
30+
type = string
31+
}
32+
33+
variable "keys" {
34+
description = "Key names."
35+
type = list(string)
36+
default = []
3237
}

main.tf

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
locals {
18-
keys_by_name = zipmap(var.keys, google_kms_crypto_key.key.*.self_link)
18+
keys_by_name = zipmap(var.keys, var.prevent_destroy ? google_kms_crypto_key.key[*].self_link : google_kms_crypto_key.key_ephemeral[*].self_link)
1919
}
2020

2121
resource "google_kms_key_ring" "key_ring" {
@@ -25,7 +25,7 @@ resource "google_kms_key_ring" "key_ring" {
2525
}
2626

2727
resource "google_kms_crypto_key" "key" {
28-
count = length(var.keys)
28+
count = var.prevent_destroy ? length(var.keys) : 0
2929
name = var.keys[count.index]
3030
key_ring = google_kms_key_ring.key_ring.self_link
3131
rotation_period = var.key_rotation_period
@@ -35,30 +35,35 @@ resource "google_kms_crypto_key" "key" {
3535
}
3636
}
3737

38-
resource "google_kms_crypto_key_iam_binding" "owners" {
39-
count = length(var.set_owners_for)
40-
role = "roles/owner"
38+
resource "google_kms_crypto_key" "key_ephemeral" {
39+
count = var.prevent_destroy ? 0 : length(var.keys)
40+
name = var.keys[count.index]
41+
key_ring = google_kms_key_ring.key_ring.self_link
42+
rotation_period = var.key_rotation_period
4143

42-
crypto_key_id = local.keys_by_name[var.set_owners_for[count.index]]
44+
lifecycle {
45+
prevent_destroy = false
46+
}
47+
}
4348

44-
members = compact(split(",", var.owners[count.index]))
49+
resource "google_kms_crypto_key_iam_binding" "owners" {
50+
count = length(var.set_owners_for)
51+
role = "roles/owner"
52+
crypto_key_id = local.keys_by_name[var.set_owners_for[count.index]]
53+
members = compact(split(",", var.owners[count.index]))
4554
}
4655

4756
resource "google_kms_crypto_key_iam_binding" "decrypters" {
48-
count = length(var.set_decrypters_for)
49-
role = "roles/cloudkms.cryptoKeyDecrypter"
50-
57+
count = length(var.set_decrypters_for)
58+
role = "roles/cloudkms.cryptoKeyDecrypter"
5159
crypto_key_id = local.keys_by_name[var.set_decrypters_for[count.index]]
52-
53-
members = compact(split(",", var.decrypters[count.index]))
60+
members = compact(split(",", var.decrypters[count.index]))
5461
}
5562

5663
resource "google_kms_crypto_key_iam_binding" "encrypters" {
57-
count = length(var.set_encrypters_for)
58-
role = "roles/cloudkms.cryptoKeyEncrypter"
59-
64+
count = length(var.set_encrypters_for)
65+
role = "roles/cloudkms.cryptoKeyEncrypter"
6066
crypto_key_id = local.keys_by_name[element(var.set_encrypters_for, count.index)]
61-
62-
members = compact(split(",", var.encrypters[count.index]))
67+
members = compact(split(",", var.encrypters[count.index]))
6368
}
6469

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ output "keyring" {
1919
value = google_kms_key_ring.key_ring.self_link
2020
}
2121

22+
output "keyring_resource" {
23+
description = "Keyring resource."
24+
value = google_kms_key_ring.key_ring
25+
}
26+
2227
output "keys" {
2328
description = "Map of key name => key self link."
2429
value = local.keys_by_name

test/fixtures/simple_example/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ resource "random_pet" "main" {
2525
}
2626

2727
module "example" {
28-
source = "../../../examples/simple_example"
29-
28+
source = "../../../examples/simple_example"
3029
project_id = var.project_id
3130
keyring = random_pet.main.id
3231
location = "global"
32+
keys = ["one", "two"]
3333
}

0 commit comments

Comments
 (0)