Skip to content

Commit 2f56de7

Browse files
authored
Merge pull request Coalfire-CF#9 from Coalfire-CF/issue8
2 parents 1aba940 + 820d147 commit 2f56de7

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

README.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,31 @@ The below example is how you can call secrets manager module to create secrets a
3333
If secrets need to be shared between AWS accounts, set "shared = true" and also provide "cross_account_ids".
3434

3535
```hcl
36+
locals{
37+
secrets = [
38+
{
39+
secret_name = "test123"
40+
secret_description = "test service account for the 123 service"
41+
},
42+
{
43+
secret_name = "svc_test456"
44+
secret_description = ""
45+
}
46+
]
47+
}
48+
49+
3650
module "secrets" {
3751
source = "github.com/Coalfire-CF/terraform-aws-secretsmanager"
3852
3953
partition = var.partition
40-
names = [""]
54+
secrets = local.secrets
4155
length = 15
42-
special = ""
56+
special = true
4357
override_special = "$%&!"
4458
kms_key_id = data.terraform_remote_state.setup.sm_kms_key_id
4559
path = ""
46-
shared = true
60+
shared = false
4761
cross_account_ids = [""]
4862
}
4963
```
@@ -91,12 +105,12 @@ No modules.
91105
| <a name="input_min_numeric"></a> [min\_numeric](#input\_min\_numeric) | Minimum number of numeric characters | `number` | `1` | no |
92106
| <a name="input_min_special"></a> [min\_special](#input\_min\_special) | Minimum number of special characters | `number` | `1` | no |
93107
| <a name="input_min_upper"></a> [min\_upper](#input\_min\_upper) | Minimum number of upper case characters | `number` | `1` | no |
94-
| <a name="input_names"></a> [names](#input\_names) | Specifies the friendly name of the new secrets to be created | `list(string)` | n/a | yes |
95108
| <a name="input_override_special"></a> [override\_special](#input\_override\_special) | Provide your own list of special characters | `string` | `"_%@!"` | no |
96109
| <a name="input_partition"></a> [partition](#input\_partition) | The AWS partition to use | `string` | n/a | yes |
97110
| <a name="input_path"></a> [path](#input\_path) | Path to organize secrets | `string` | n/a | yes |
98111
| <a name="input_recovery_window_in_days"></a> [recovery\_window\_in\_days](#input\_recovery\_window\_in\_days) | Number of days that AWS Secrets Manager waits before it can delete the secret. | `number` | `30` | no |
99112
| <a name="input_regional_tags"></a> [regional\_tags](#input\_regional\_tags) | a map of strings that contains regional level tags | `map(string)` | `{}` | no |
113+
| <a name="input_secrets"></a> [secrets](#input\_secrets) | Specifies the friendly name of the new secrets to be created as key and an optional value field for descriptions | `list(map(string))` | n/a | yes |
100114
| <a name="input_shared"></a> [shared](#input\_shared) | Whether secrets should be shared across accounts. | `bool` | `false` | no |
101115
| <a name="input_special"></a> [special](#input\_special) | Include special characters in random password string | `bool` | `true` | no |
102116
| <a name="input_tags"></a> [tags](#input\_tags) | A mapping of tags to assign to the resource | `map(string)` | `{}` | no |

locals.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#locals {
2+
# secret_input = [for k, v in var.secret_naming_descrip : v if length(regexall(".*", k)) > 0]
3+
#}

main.tf

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
resource "aws_secretsmanager_secret" "this" {
2-
for_each = toset(var.names)
3-
name = "${var.path}${each.value}"
2+
for_each = {for s in var.secrets : s.secret_name => s}
3+
name = "${var.path}${each.key}"
4+
description = coalesce(each.value.secret_description, "secret for ${each.key}")
45
kms_key_id = var.kms_key_id
56
tags = merge(var.tags, var.global_tags, var.regional_tags)
67
policy = var.shared ? null : "{}"
78
recovery_window_in_days = var.recovery_window_in_days
89
}
10+
911
resource "aws_secretsmanager_secret_policy" "shared" {
10-
for_each = var.shared ? toset(var.names) : []
12+
for_each = var.shared ? {for s in var.secrets : s.secret_name => s} : {}
1113

12-
secret_arn = aws_secretsmanager_secret.this[each.key].arn
14+
secret_arn = aws_secretsmanager_secret.this["${each.key}"].arn
1315

1416
policy = data.aws_iam_policy_document.resource_policy_MA.json
1517
}
@@ -23,20 +25,18 @@ data "aws_iam_policy_document" "resource_policy_MA" {
2325
"secretsmanager:ListSecrets",
2426
"secretsmanager:GetSecretValue",
2527
"secretsmanager:DescribeSecret",
26-
"secretsmanager:ListSecretVersionIds"
27-
]
28+
"secretsmanager:ListSecretVersionIds" ]
2829
resources = values(aws_secretsmanager_secret.this)[*].arn
2930
principals {
30-
identifiers = [
31-
"arn:${var.partition}:iam::${statement.value}:root"]
31+
identifiers = [ "arn:${var.partition}:iam::${statement.value}:root"]
3232
type = "AWS"
3333
}
3434
}
3535
}
3636
}
3737

3838
resource "aws_secretsmanager_secret_version" "this" {
39-
for_each = var.empty_value ? [] : toset(var.names)
39+
for_each = var.empty_value ? {} : {for s in var.secrets : s.secret_name => s}
4040
secret_id = aws_secretsmanager_secret.this[each.key].id
4141
secret_string = random_password.password[each.key].result
4242

@@ -48,7 +48,7 @@ resource "aws_secretsmanager_secret_version" "this" {
4848
}
4949

5050
resource "random_password" "password" {
51-
for_each = var.empty_value ? [] : toset(var.names)
51+
for_each = var.empty_value ? {} : {for s in var.secrets : s.secret_name => s}
5252
length = var.length
5353
special = var.special
5454
override_special = var.override_special

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ output "secrets" {
1414
}
1515

1616
output "names" {
17-
value = var.names
17+
value = values(aws_secretsmanager_secret.this)[*].name
1818
description = "Returns list of secret names to be created."
1919
}
2020

variables.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
variable "names" {
2-
type = list(string)
3-
description = "Specifies the friendly name of the new secrets to be created"
1+
variable "secrets" {
2+
type = list(map(string))
3+
description = "Specifies the friendly name of the new secrets to be created as key and an optional value field for descriptions"
44
}
55

66
variable "length" {

0 commit comments

Comments
 (0)