Skip to content

Commit 124d452

Browse files
committed
feat: Support policies for secrets
BREAKING CHANGE: Input format for the secrets has changed
1 parent e5aed27 commit 124d452

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

locals.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
locals {
2+
secrets = { for secret in var.secrets : secret.name => secret.value }
3+
arns = { for secret in var.secrets : secret.name => secret.allowed_arns if length(secret.allowed_arns) > 0 }
4+
}

main.tf

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
data "aws_iam_policy_document" "access" {
4+
for_each = local.arns
5+
6+
statement {
7+
principals {
8+
type = "AWS"
9+
identifiers = each.value
10+
}
11+
12+
actions = ["secretsmanager:GetSecretValue"]
13+
resources = ["arn:aws:secretsmanager:${var.aws_region}:${data.aws_caller_identity.current.account_id}:secret:${var.app_name}-${each.key}*"]
14+
}
15+
}
16+
117
resource "aws_secretsmanager_secret" "app" {
2-
for_each = var.secrets
18+
for_each = local.secrets
319

420
name_prefix = "${var.app_name}-${each.key}"
521
description = "The ${title(replace(each.key, "-", " "))} secret for ${var.app_name} application"
622

23+
policy = lookup(local.arns, each.key, null) == null ? null : data.aws_iam_policy_document.access[each.key].json
24+
725
tags = merge(var.tags, { "service" = var.app_name })
826
}
927

1028
resource "aws_secretsmanager_secret_version" "app" {
11-
for_each = var.secrets
29+
for_each = local.secrets
1230

1331
secret_id = aws_secretsmanager_secret.app[each.key].id
14-
secret_string = each.value
32+
secret_string = each.value != "" ? each.value : "[value required]"
1533

1634
lifecycle {
1735
ignore_changes = [secret_string]

outputs.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "all" {
22
description = "Map of names and arns of created secrets"
33
value = [
4-
for k in keys(var.secrets) : {
5-
name = upper(replace(k, "-", "_"))
6-
arn = aws_secretsmanager_secret.app[k].id
4+
for name in keys(local.secrets) : {
5+
name = upper(replace(name, "-", "_"))
6+
arn = aws_secretsmanager_secret.app[name].id
77
}
88
]
99
}

variables.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@ variable "app_name" {
33
type = string
44
}
55

6+
variable "aws_region" {
7+
description = "AWS region"
8+
type = string
9+
10+
default = "us-east-2"
11+
}
12+
613
variable "secrets" {
7-
description = "Key-value map of secrets"
8-
type = map(string)
14+
description = "List of objects of secrets"
15+
type = list(
16+
object({
17+
name = string
18+
value = string
19+
allowed_arns = list(string)
20+
})
21+
)
922
}
1023

1124
variable "tags" {

0 commit comments

Comments
 (0)