Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 15fcd37

Browse files
committed
add modules for credstash reader/writer grants
These two modules are based on the existing credstash-grant module, but replaces the grant.sh script and local_exec provisioner that was necessary to use in that module with the aws_kms_grant resource that terraform now provides. The one module is now split into two for directness in use and for the simplicity of variables.
1 parent 23cf983 commit 15fcd37

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Credstash Grant Reader Access
2+
3+
This module will make it possible for anybody assuming the supplied IAM Role to read
4+
secrets from a credstash store.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
variable "kms_key_arn" {
2+
description = "ARN for the KMS Master key created by 'credstash-setup' module"
3+
type = "string"
4+
}
5+
6+
// Roles count is only necessary to circumvent [terraform #4149](https://github.com/hashicorp/terraform/issues/4149) issue.
7+
variable "role_count" {
8+
description = "Number of roles that will be used during a grant process, i.e. how many roles_names there is."
9+
type = "string"
10+
}
11+
12+
variable "role_names" {
13+
type = "list"
14+
description = "Role Name for which reading secrets will be enabled. Must correspond 1:1 with roles_arns"
15+
}
16+
17+
variable "role_arns" {
18+
type = "list"
19+
description = "Role ARN for which reading secrets will be enabled. Must correspond 1:1 with roles_names"
20+
}
21+
22+
variable "reader_policy_arn" {
23+
description = "Secrets Reader Policy ARN that was created by 'credstash-setup' module"
24+
type = "string"
25+
}
26+
27+
variable "context_keys" {
28+
default = []
29+
description = "list of keys to be zipped with the context_values to set an 'encryption context' for additional granularity that clients are required to provide to read encrypted values. Eg. for env=dev svc=db, this would be [env, svc]. All readers get this context map."
30+
type = "list"
31+
}
32+
33+
variable "context_values" {
34+
default = []
35+
description = "list of values to be zipped with the context_keys to set an 'encryption context' for additional granularity that clients are required to provide to read encrypted values. Eg. for env=dev svc=db, this would be [dev, db]. All readers get this context map."
36+
type = "list"
37+
}
38+
39+
resource "aws_iam_role_policy_attachment" "credstash-reader-policy-attachment" {
40+
count = "${var.role_count}"
41+
role = "${var.role_names[count.index]}"
42+
policy_arn = "${var.reader_policy_arn}"
43+
}
44+
45+
resource "aws_kms_grant" "credstash-reader" {
46+
count = "${var.role_count}"
47+
name = "${var.role_names[count.index]}-credstash-reader"
48+
grantee_principal = "${var.role_arns[count.index]}"
49+
key_id = "${var.kms_key_arn}"
50+
operations = ["Decrypt"]
51+
52+
constraints {
53+
encryption_context_equals = "${map(element(var.context_keys, count.index),
54+
element(var.context_values, count.index))}"
55+
}
56+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Credstash Grant Writer Access
2+
3+
This module will make it possible for anybody assuming the supplied IAM Role to
4+
write secrets to a credstash store.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
variable "kms_key_arn" {
2+
description = "ARN for the KMS Master key created by 'credstash-setup' module"
3+
type = "string"
4+
}
5+
6+
// Roles count is only necessary to circumvent [terraform #4149](https://github.com/hashicorp/terraform/issues/4149) issue.
7+
variable "role_count" {
8+
description = "Number of roles that will be used during a grant process, i.e. how many roles_names there is."
9+
type = "string"
10+
}
11+
12+
variable "role_names" {
13+
type = "list"
14+
description = "Role Name for which reading secrets will be enabled. Must correspond 1:1 with roles_arns"
15+
}
16+
17+
variable "role_arns" {
18+
type = "list"
19+
description = "Role ARN for which reading secrets will be enabled. Must correspond 1:1 with roles_names"
20+
}
21+
22+
variable "writer_policy_arn" {
23+
description = "Secrets Reader Policy ARN that was created by 'credstash-setup' module"
24+
type = "string"
25+
}
26+
27+
variable "context_keys" {
28+
default = []
29+
description = "list of keys to be zipped with the context_values to set an 'encryption context' for additional granularity that clients are required to provide to read encrypted values. Eg. for env=dev svc=db, this would be [env, svc]. All writers get this context map."
30+
type = "list"
31+
}
32+
33+
variable "context_values" {
34+
default = []
35+
description = "list of values to be zipped with the context_keys to set an 'encryption context' for additional granularity that clients are required to provide to read encrypted values. Eg. for env=dev svc=db, this would be [dev, db]. All writers get this context map."
36+
type = "list"
37+
}
38+
39+
resource "aws_iam_role_policy_attachment" "credstash-writer-policy-attachment" {
40+
count = "${var.role_count}"
41+
role = "${var.role_names[count.index]}"
42+
policy_arn = "${var.writer_policy_arn}"
43+
}
44+
45+
resource "aws_kms_grant" "credstash-writer" {
46+
count = "${var.role_count}"
47+
name = "${var.role_names[count.index]}-credstash-writer"
48+
grantee_principal = "${var.role_arns[count.index]}"
49+
key_id = "${var.kms_key_arn}"
50+
operations = ["GenerateDataKey"]
51+
52+
constraints {
53+
encryption_context_equals = "${map(element(var.context_keys, count.index),
54+
element(var.context_values, count.index))}"
55+
}
56+
}

0 commit comments

Comments
 (0)