Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSOS-2406: align secretsmanager options with ssm #256

Merged
merged 10 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ locals {
}) if value.value == null && value.random == null
}

secretsmanager_random_passwords = {
for key, value in var.secretsmanager_secrets != null ? var.secretsmanager_secrets : {} :
key => value.random if value.random != null
}
secretsmanager_secrets_value = {
for key, value in var.secretsmanager_secrets != null ? var.secretsmanager_secrets : {} :
key => value if value.value != null
}
secretsmanager_secrets_random = {
for key, value in var.secretsmanager_secrets != null ? var.secretsmanager_secrets : {} :
key => merge(value, {
value = random_password.secrets[key].result
}) if value.value == null && value.random != null
}
secretsmanager_secrets_default = {
for key, value in var.secretsmanager_secrets != null ? var.secretsmanager_secrets : {} :
key => value if value.value == null && value.random == null
}

ami_block_device_mappings = {
for bdm in data.aws_ami.this.block_device_mappings : bdm.device_name => bdm
}
Expand Down
51 changes: 45 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,54 @@ resource "aws_ssm_parameter" "placeholder" {
}
}

#------------------------------------------------------------------------------
# SecretManager Secrets
#------------------------------------------------------------------------------

resource "random_password" "secrets" {
for_each = local.secretsmanager_random_passwords

length = each.value.length
special = each.value.special
}

resource "aws_secretsmanager_secret" "fixed" {
# skipped check as the secret value is defined by terraform so cannot be rotated by AWS
#checkov:skip=CKV2_AWS_57: Ensure Secrets Manager secrets should have automatic rotation enabled
for_each = merge(
local.secretsmanager_secrets_value,
local.secretsmanager_secrets_random,
)

name = "/${var.secretsmanager_secrets_prefix}${var.name}/${each.key}"
description = each.value.description
kms_key_id = each.value.kms_key_id
recovery_window_in_days = each.value.recovery_window_in_days

tags = merge(local.tags, {
Name = "${var.name}-${each.key}"
})
}

resource "aws_secretsmanager_secret_version" "fixed" {
for_each = merge(
local.secretsmanager_secrets_value,
local.secretsmanager_secrets_random,
)

secret_id = aws_secretsmanager_secret.fixed[each.key].id
secret_string = each.value.value
}

resource "aws_secretsmanager_secret" "placeholder" {
# skipped check to keep consistent behaviour between ssm params and secrets
# Rotation can be added later as a configurable option. Some will want it, for some it will break things
# Rotation can be added later as a configurable option
#checkov:skip=CKV2_AWS_57: Ensure Secrets Manager secrets should have automatic rotation enabled
for_each = var.secretsmanager_secrets
for_each = local.secretsmanager_secrets_default

name = "/${var.secretsmanager_secrets_prefix}${var.name}/${each.key}"
description = each.value.description
kms_key_id = each.value.kms_key_id
name = "/${var.secretsmanager_secrets_prefix}${var.name}/${each.key}"
description = each.value.description
kms_key_id = each.value.kms_key_id
recovery_window_in_days = each.value.recovery_window_in_days

tags = merge(local.tags, {
Name = "${var.name}-${each.key}"
Expand Down
2 changes: 2 additions & 0 deletions test/unit-test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ module "ec2_test_instance" {
ebs_volume_config = lookup(each.value, "ebs_volume_config", {})
ebs_volumes = lookup(each.value, "ebs_volumes", {})
ebs_volume_tags = lookup(each.value, "ebs_volume_tags", {})
secretsmanager_secrets_prefix = lookup(each.value, "secretsmanager_secrets_prefix", "test/")
secretsmanager_secrets = lookup(each.value, "secretsmanager_secrets", null)
ssm_parameters_prefix = lookup(each.value, "ssm_parameters_prefix", "test/")
ssm_parameters = lookup(each.value, "ssm_parameters", null)
route53_records = merge(local.ec2_test.route53_records, lookup(each.value, "route53_records", {}))
Expand Down
14 changes: 10 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,18 @@ variable "ssm_parameters" {
}

variable "secretsmanager_secrets" {
description = "A map of secretsmanager secrets to create. No value is created, add a value outside of terraform"
description = "A map of secretsmanager secrets to create. Set a specific value or a randomly generated value. If neither random or value are set, a placeholder value is created which can be updated outside of terraform"
type = map(object({
description = optional(string)
kms_key_id = optional(string)
description = optional(string)
kms_key_id = optional(string)
recovery_window_in_days = optional(number)
random = optional(object({
length = number
special = optional(bool)
}))
value = optional(string)
}))
default = {}
default = null
}

variable "cloudwatch_metric_alarms" {
Expand Down
Loading