-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to generate secrets for the application (#602)
⚠️ breaking change (change to way secrets are defined in app-config in environment-variables.tf). secrets are now defined as a map: ``` secrets = { ENV_VAR_NAME = { manage_method = "code" or "manual" secret_store_path = "/ssm/param/name" } } ``` It was previously defined as a list: ``` secrets = [ { name = "ENV_VAR_NAME", ssm_param_name = "/ssm/param/name" } ] ``` * Add new module modules/secret for generating new secrets or referencing existing secrets * Refactor interface To migrate: * In app-config's environment-variables.tf, update secret definitions to use the new format. * For secrets managed outside of the project's codebase, set manage_method = "manual" * For secrets created within the project's codebase but defined elsewhere, move (using [terraform mv](https://developer.hashicorp.com/terraform/cli/commands/state/mv)) the aws_ssm_parameter to module.secret[ENV_VAR_NAME].aws_ssm_parameter.secret
- Loading branch information
Showing
14 changed files
with
113 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module "secrets" { | ||
for_each = local.service_config.secrets | ||
|
||
source = "../../modules/secret" | ||
|
||
# When generating secrets and storing them in parameter store, append the | ||
# terraform workspace to the secret store path if the environment is temporary | ||
# to avoid conflicts with existing environments. | ||
# Don't do this for secrets that are managed manually since the temporary | ||
# environments will need to share those secrets. | ||
secret_store_name = (each.value.manage_method == "generated" && local.is_temporary ? | ||
"${each.value.secret_store_name}/${terraform.workspace}" : | ||
each.value.secret_store_name | ||
) | ||
manage_method = each.value.manage_method | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
locals { | ||
secret = var.manage_method == "generated" ? aws_ssm_parameter.secret[0] : data.aws_ssm_parameter.secret[0] | ||
access_policy_name = "${trimprefix(replace(local.secret.name, "/", "-"), "/")}-access" | ||
} | ||
|
||
resource "random_password" "secret" { | ||
count = var.manage_method == "generated" ? 1 : 0 | ||
|
||
length = 64 | ||
special = true | ||
override_special = "!#$%&*()-_=+[]{}<>:?" | ||
} | ||
|
||
resource "aws_ssm_parameter" "secret" { | ||
count = var.manage_method == "generated" ? 1 : 0 | ||
|
||
name = var.secret_store_name | ||
type = "SecureString" | ||
value = random_password.secret[0].result | ||
} | ||
|
||
data "aws_ssm_parameter" "secret" { | ||
count = var.manage_method == "manual" ? 1 : 0 | ||
|
||
name = var.secret_store_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "secret_arn" { | ||
value = local.secret.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
variable "manage_method" { | ||
type = string | ||
description = <<EOT | ||
Method to manage the secret. Options are 'manual' or 'generated'. | ||
Set to 'generated' to generate a random secret. | ||
Set to 'manual' to reference a secret that was manually created and stored in AWS parameter store. | ||
Defaults to 'generated'." | ||
EOT | ||
default = "generated" | ||
validation { | ||
condition = can(regex("^(manual|generated)$", var.manage_method)) | ||
error_message = "Invalid manage_method. Must be 'manual' or 'generated'." | ||
} | ||
} | ||
|
||
variable "secret_store_name" { | ||
type = string | ||
description = <<EOT | ||
If manage_method is 'generated', path to store the secret in AWS parameter store. | ||
If manage_method is 'manual', path to reference the secret in AWS parameter store. | ||
EOT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters