Skip to content

Commit 5a57ba0

Browse files
authored
Merge pull request #2 from terraform-google-modules/initial-import
Initial import from internal version
2 parents 91b95da + 594a4cc commit 5a57ba0

File tree

4 files changed

+137
-14
lines changed

4 files changed

+137
-14
lines changed

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# terraform-google-kms
22

3-
This module was generated from [terraform-google-module-template](https://github.com/terraform-google-modules/terraform-google-module-template/), which by default generates a module that simply creates a GCS bucket. As the module develops, this README should be updated.
3+
Simple Cloud KMS module that allows managing a keyring, zero or more keys in the keyring, and IAM role bindings on individual keys.
44

55
The resources/services/activations/deletions that this module will create/trigger are:
66

7-
- Create a GCS bucket with the provided name
7+
- Create a KMS keyring in the provided project
8+
- Create zero or more keys in the keyring
9+
- Create IAM role bindings for owners, encrypters, decrypters
810

911
## Usage
1012

@@ -16,7 +18,14 @@ module "kms" {
1618
version = "~> 0.1"
1719
1820
project_id = "<PROJECT ID>"
19-
bucket_name = "gcs-test-bucket"
21+
location = "europe"
22+
name = "sample-keyring"
23+
keys = ["foo", "spam"]
24+
set_owners_for = ["foo", "spam"]
25+
owners = [
26+
27+
28+
]
2029
}
2130
```
2231

@@ -40,10 +49,11 @@ The following dependencies must be available:
4049

4150
### Service Account
4251

43-
A service account with the following roles must be used to provision
52+
A service account with one of the following roles must be used to provision
4453
the resources of this module:
4554

46-
- Storage Admin: `roles/storage.admin`
55+
- Cloud KMS Admin: `roles/cloudkms.admin` or
56+
- Owner: `roles/owner`
4757

4858
The [Project Factory module][project-factory-module] and the
4959
[IAM module][iam-module] may be used in combination to provision a
@@ -54,7 +64,7 @@ service account with the necessary roles applied.
5464
A project with the following APIs enabled must be used to host the
5565
resources of this module:
5666

57-
- Google Cloud Storage JSON API: `storage-api.googleapis.com`
67+
- Google Cloud Key Management Service: `cloudkms.googleapis.com`
5868

5969
The [Project Factory module][project-factory-module] can be used to
6070
provision a project with the necessary APIs enabled.

main.tf

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,65 @@ terraform {
1818
required_version = "~> 0.11.0"
1919
}
2020

21-
resource "google_storage_bucket" "main" {
22-
project = "${var.project_id}"
23-
name = "${var.bucket_name}"
21+
locals {
22+
keys_by_name = "${zipmap(var.keys, google_kms_crypto_key.key.*.self_link)}"
23+
}
24+
25+
resource "google_kms_key_ring" "key_ring" {
26+
name = "${var.keyring}"
27+
project = "${var.project_id}"
28+
location = "${var.location}"
29+
}
30+
31+
resource "google_kms_crypto_key" "key" {
32+
count = "${length(var.keys)}"
33+
name = "${element(var.keys, count.index)}"
34+
key_ring = "${google_kms_key_ring.key_ring.self_link}"
35+
rotation_period = "${var.key_rotation_period}"
36+
37+
lifecycle {
38+
prevent_destroy = true
39+
}
40+
}
41+
42+
resource "google_kms_crypto_key_iam_binding" "owners" {
43+
count = "${length(var.set_owners_for)}"
44+
role = "roles/owner"
45+
46+
crypto_key_id = "${lookup(
47+
local.keys_by_name,
48+
element(var.set_owners_for, count.index)
49+
)}"
50+
51+
members = [
52+
"${compact(split(",", element(var.owners, count.index)))}",
53+
]
54+
}
55+
56+
resource "google_kms_crypto_key_iam_binding" "decrypters" {
57+
count = "${length(var.set_decrypters_for)}"
58+
role = "roles/cloudkms.cryptoKeyDecrypter"
59+
60+
crypto_key_id = "${lookup(
61+
local.keys_by_name,
62+
element(var.set_decrypters_for, count.index)
63+
)}"
64+
65+
members = [
66+
"${compact(split(",", element(var.decrypters, count.index)))}",
67+
]
68+
}
69+
70+
resource "google_kms_crypto_key_iam_binding" "encrypters" {
71+
count = "${length(var.set_encrypters_for)}"
72+
role = "roles/cloudkms.cryptoKeyEncrypter"
73+
74+
crypto_key_id = "${lookup(
75+
local.keys_by_name,
76+
element(var.set_encrypters_for, count.index)
77+
)}"
78+
79+
members = [
80+
"${compact(split(",", element(var.encrypters, count.index)))}",
81+
]
2482
}

outputs.tf

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
output "bucket_name" {
18-
value = "${google_storage_bucket.main.name}"
17+
output "keyring" {
18+
description = "Self link of the keyring."
19+
value = "${google_kms_key_ring.key_ring.self_link}"
20+
}
21+
22+
output "keys" {
23+
description = "Map of key name => key self link."
24+
value = "${local.keys_by_name}"
25+
}
26+
27+
output "keyring_name" {
28+
description = "Name of the keyring."
29+
value = "${google_kms_key_ring.key_ring.name}"
1930
}

variables.tf

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,53 @@
1515
*/
1616

1717
variable "project_id" {
18-
description = "The project ID to deploy to"
18+
description = "Project id where the keyring will be created."
1919
}
2020

21-
variable "bucket_name" {
22-
description = "The name of the bucket to create"
21+
# cf https://cloud.google.com/kms/docs/locations
22+
variable "location" {
23+
description = "Location for the keyring."
24+
}
25+
26+
variable "keyring" {
27+
description = "Keyring name."
28+
}
29+
30+
variable "keys" {
31+
description = "Key names."
32+
default = []
33+
}
34+
35+
variable "set_owners_for" {
36+
description = "Name of keys for which owners will be set."
37+
default = []
38+
}
39+
40+
variable "owners" {
41+
description = "List of comma-separated owners for each key declared in set_owners_for."
42+
default = []
43+
}
44+
45+
variable "set_encrypters_for" {
46+
description = "Name of keys for which encrypters will be set."
47+
default = []
48+
}
49+
50+
variable "encrypters" {
51+
description = "List of comma-separated owners for each key declared in set_encrypters_for."
52+
default = []
53+
}
54+
55+
variable "set_decrypters_for" {
56+
description = "Name of keys for which decrypters will be set."
57+
default = []
58+
}
59+
60+
variable "decrypters" {
61+
description = "List of comma-separated owners for each key declared in set_decrypters_for."
62+
default = []
63+
}
64+
65+
variable "key_rotation_period" {
66+
default = "100000s"
2367
}

0 commit comments

Comments
 (0)