|
| 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