Add KMS CMK for carves (#198)

This commit is contained in:
Robert Fairburn authored and GitHub committed 2026-03-24 06:44:00 -05:00
1 parent e7faee93de
commit 5427830fea
18 files changed
+1147 -138

No files matched your search

+3
View File
@@ -129,6 +129,7 @@ No requirements.
| [aws_iam_policy_document.lambda_sweep_assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.s3_athena_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.s3_log_bucket](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
## Inputs
@@ -140,6 +141,8 @@ No requirements.
| <a name="input_extra_kms_policies"></a> [extra\_kms\_policies](#input\_extra\_kms\_policies) | n/a | `list(any)` | `[]` | no |
| <a name="input_extra_s3_athena_policies"></a> [extra\_s3\_athena\_policies](#input\_extra\_s3\_athena\_policies) | n/a | `list(any)` | `[]` | no |
| <a name="input_extra_s3_log_policies"></a> [extra\_s3\_log\_policies](#input\_extra\_s3\_log\_policies) | n/a | `list(any)` | `[]` | no |
| <a name="input_kms_base_policy"></a> [kms\_base\_policy](#input\_kms\_base\_policy) | Optional base KMS key-policy statements to apply to module-created CMKs before module-required service access statements are merged in. If null, the module defaults to the historical root `kms:*` statement. | <pre>list(object({<br/> sid = string<br/> effect = string<br/> principals = object({<br/> type = string<br/> identifiers = list(string)<br/> })<br/> actions = list(string)<br/> resources = list(string)<br/> conditions = optional(list(object({<br/> test = string<br/> variable = string<br/> values = list(string)<br/> })), [])<br/> }))</pre> | `null` | no |
| <a name="input_lambda_log_retention_in_days"></a> [lambda\_log\_retention\_in\_days](#input\_lambda\_log\_retention\_in\_days) | CloudWatch log retention in days for the re-encrypt and sweep Lambda functions | `number` | `365` | no |
| <a name="input_prefix"></a> [prefix](#input\_prefix) | n/a | `string` | `"fleet"` | no |
| <a name="input_s3_expiration_days"></a> [s3\_expiration\_days](#input\_s3\_expiration\_days) | n/a | `number` | `90` | no |
| <a name="input_s3_newer_noncurrent_versions"></a> [s3\_newer\_noncurrent\_versions](#input\_s3\_newer\_noncurrent\_versions) | n/a | `number` | `5` | no |
+53 -42
View File
@@ -57,15 +57,65 @@ locals {
}
]
kms_policies = concat(local.kms_base_policy_statements, local.kms_service_statements, var.extra_kms_policies)
s3_path_prefix = coalesce(var.alt_path_prefix, var.prefix)
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "kms" {
dynamic "statement" {
for_each = local.kms_policies
for_each = local.kms_base_policy_statements
content {
sid = try(statement.value.sid, "")
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
effect = try(statement.value.effect, null)
dynamic "principals" {
for_each = try([statement.value.principals], [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = local.kms_service_statements
content {
sid = try(statement.value.sid, "")
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
effect = try(statement.value.effect, null)
dynamic "principals" {
for_each = try([statement.value.principals], [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.extra_kms_policies
content {
sid = try(statement.value.sid, "")
actions = try(statement.value.actions, [])
@@ -239,19 +289,6 @@ data "aws_iam_policy_document" "lambda_reencrypt" {
]
}
statement {
sid = "UseKmsKey"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
resources = [
aws_kms_key.logs.arn,
]
}
statement {
sid = "WriteLogs"
actions = [
@@ -362,19 +399,6 @@ data "aws_iam_policy_document" "lambda_sweep" {
]
}
statement {
sid = "UseKmsKey"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:GenerateDataKey*",
"kms:DescribeKey",
]
resources = [
aws_kms_key.logs.arn,
]
}
statement {
sid = "CreateBatchJob"
actions = [
@@ -498,19 +522,6 @@ data "aws_iam_policy_document" "batch_reencrypt" {
"${module.s3_bucket_for_logs.s3_bucket_arn}/*",
]
}
statement {
sid = "UseKmsKey"
actions = [
"kms:Encrypt",
"kms:GenerateDataKey",
"kms:GenerateDataKeyWithoutPlaintext",
"kms:DescribeKey",
]
resources = [
aws_kms_key.logs.arn,
]
}
}
resource "aws_iam_role" "batch_reencrypt" {
+13
View File
@@ -1,2 +1,15 @@
# Osquery Carve Bucket Addon
This addon provides a S3 bucket for Osquery Carve results.
## KMS considerations
If `osquery_carve_s3_bucket.kms.create_kms_key = true`, this module now creates the KMS key first and manages the custom key policy through a separate attachment resource. If `fleet_role_name` is omitted, the module still creates the key but leaves the default KMS key policy in place and does not attach a custom policy.
When a module-created customer-managed key must trust the Fleet IAM role, Terraform needs to resolve that role through `data.aws_iam_role` in order to build the KMS key policy. Because that data source cannot read a role that is being created in the same apply, this is a technical limitation that requires a two-stage apply:
1. Apply once without `osquery_carve_s3_bucket.kms.fleet_role_name` so the Fleet IAM role and the KMS key are created.
2. Set `osquery_carve_s3_bucket.kms.fleet_role_name` to the existing Fleet role name and apply again so the module can resolve the role ARN and attach the KMS key policy.
The IAM policy exported by this addon is intentionally S3-only. KMS authorization is handled in the KMS key policy for module-created keys. If `osquery_carve_s3_bucket.kms.kms_key_arn` is set, this module does not manage the referenced key policy and does not export a generic KMS IAM policy for it.
When bringing your own KMS key, it is your responsibility to ensure that key policy and any separately managed IAM permissions allow the Fleet role to perform the required KMS actions for S3 object encryption and decryption.
+24 -3
View File
@@ -1,6 +1,19 @@
# Osquery Carve Bucket Addon
This addon provides a S3 bucket for Osquery Carve results.
## KMS considerations
If `osquery_carve_s3_bucket.kms.create_kms_key = true`, this module now creates the KMS key first and manages the custom key policy through a separate attachment resource. If `fleet_role_name` is omitted, the module still creates the key but leaves the default KMS key policy in place and does not attach a custom policy.
When a module-created customer-managed key must trust the Fleet IAM role, Terraform needs to resolve that role through `data.aws_iam_role` in order to build the KMS key policy. Because that data source cannot read a role that is being created in the same apply, this is a technical limitation that requires a two-stage apply:
1. Apply once without `osquery_carve_s3_bucket.kms.fleet_role_name` so the Fleet IAM role and the KMS key are created.
2. Set `osquery_carve_s3_bucket.kms.fleet_role_name` to the existing Fleet role name and apply again so the module can resolve the role ARN and attach the KMS key policy.
The IAM policy exported by this addon is intentionally S3-only. KMS authorization is handled in the KMS key policy for module-created keys. If `osquery_carve_s3_bucket.kms.kms_key_arn` is set, this module does not manage the referenced key policy and does not export a generic KMS IAM policy for it.
When bringing your own KMS key, it is your responsibility to ensure that key policy and any separately managed IAM permissions allow the Fleet role to perform the required KMS actions for S3 object encryption and decryption.
## Requirements
No requirements.
@@ -9,7 +22,7 @@ No requirements.
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.39.1 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.37.0 |
## Modules
@@ -20,21 +33,29 @@ No modules.
| Name | Type |
|------|------|
| [aws_iam_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_kms_alias.osquery_carve](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_key.osquery_carve](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
| [aws_kms_key_policy.osquery_carve](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy) | resource |
| [aws_s3_bucket.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_lifecycle_configuration.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_lifecycle_configuration) | resource |
| [aws_s3_bucket_public_access_block.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.osquery_carve_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_role.osquery_carve_fleet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_role) | data source |
| [aws_kms_key.osquery_carve_provided](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_osquery_carve_s3_bucket"></a> [osquery\_carve\_s3\_bucket](#input\_osquery\_carve\_s3\_bucket) | n/a | <pre>object({<br> name = optional(string, "fleet-osquery-results-archive")<br> expires_days = optional(number, 1)<br> })</pre> | <pre>{<br> "expires_days": 1,<br> "name": "fleet-osquery-results-archive"<br>}</pre> | no |
| <a name="input_osquery_carve_s3_bucket"></a> [osquery\_carve\_s3\_bucket](#input\_osquery\_carve\_s3\_bucket) | Configuration for the osquery carve S3 bucket, including optional customer-managed KMS settings. | <pre>object({<br/> name = optional(string, "fleet-osquery-results-archive")<br/> expires_days = optional(number, 1)<br/> kms = optional(object({<br/> kms_key_arn = optional(string, null)<br/> create_kms_key = optional(bool, false)<br/> kms_alias = optional(string, "osquery-carve")<br/> kms_base_policy = optional(list(object({<br/> sid = string<br/> effect = string<br/> principals = object({<br/> type = string<br/> identifiers = list(string)<br/> })<br/> actions = list(string)<br/> resources = list(string)<br/> conditions = optional(list(object({<br/> test = string<br/> variable = string<br/> values = list(string)<br/> })), [])<br/> })), null)<br/> extra_kms_policies = optional(list(any), [])<br/> fleet_role_name = optional(string, null)<br/> }), {<br/> kms_key_arn = null<br/> create_kms_key = false<br/> kms_alias = "osquery-carve"<br/> kms_base_policy = null<br/> extra_kms_policies = []<br/> fleet_role_name = null<br/> })<br/> })</pre> | <pre>{<br/> "expires_days": 1,<br/> "kms": {<br/> "create_kms_key": false,<br/> "extra_kms_policies": [],<br/> "fleet_role_name": null,<br/> "kms_alias": "osquery-carve",<br/> "kms_base_policy": null,<br/> "kms_key_arn": null<br/> },<br/> "name": "fleet-osquery-results-archive"<br/>}</pre> | no |
## Outputs
| Name | Description |
|------|-------------|
| <a name="output_fleet_extra_environment_variables"></a> [fleet\_extra\_environment\_variables](#output\_fleet\_extra\_environment\_variables) | n/a |
| <a name="output_fleet_extra_iam_policies"></a> [fleet\_extra\_iam\_policies](#output\_fleet\_extra\_iam\_policies) | n/a |
| <a name="output_fleet_extra_iam_policies"></a> [fleet\_extra\_iam\_policies](#output\_fleet\_extra\_iam\_policies) | IAM policies required for Fleet to access the osquery carve S3 bucket. |
+170 -4
View File
@@ -1,11 +1,86 @@
// Customer keys are not supported in our Fleet Terraforms at the moment. We will evaluate the
// possibility of providing this capability in the future.
// No versioning on this bucket is by design.
// Bucket logging is not supported in our Fleet Terraforms at the moment. It can be enabled by the
// organizations deploying Fleet, and we will evaluate the possibility of providing this capability
// in the future.
resource "aws_s3_bucket" "main" { #tfsec:ignore:aws-s3-encryption-customer-key:exp:2022-07-01 #tfsec:ignore:aws-s3-enable-versioning #tfsec:ignore:aws-s3-enable-bucket-logging:exp:2022-06-15
locals {
osquery_carve_provided_kms_key_ref = var.osquery_carve_s3_bucket.kms.kms_key_arn
osquery_carve_create_kms_key = local.osquery_carve_provided_kms_key_ref == null && var.osquery_carve_s3_bucket.kms.create_kms_key == true
osquery_carve_fleet_role_name = (
var.osquery_carve_s3_bucket.kms.fleet_role_name != null &&
trimspace(var.osquery_carve_s3_bucket.kms.fleet_role_name) != ""
) ? trimspace(var.osquery_carve_s3_bucket.kms.fleet_role_name) : null
osquery_carve_kms_key_id = local.osquery_carve_provided_kms_key_ref != null ? data.aws_kms_key.osquery_carve_provided[0].key_id : (
local.osquery_carve_create_kms_key == true ? aws_kms_key.osquery_carve[0].id : null
)
osquery_carve_kms_key_arn = local.osquery_carve_provided_kms_key_ref != null ? data.aws_kms_key.osquery_carve_provided[0].arn : (
local.osquery_carve_create_kms_key == true ? aws_kms_key.osquery_carve[0].arn : null
)
osquery_carve_create_kms_key_policy = local.osquery_carve_create_kms_key && (
local.osquery_carve_fleet_role_name != null ||
var.osquery_carve_s3_bucket.kms.kms_base_policy != null ||
length(var.osquery_carve_s3_bucket.kms.extra_kms_policies) > 0
)
kms_base_policy_statements = var.osquery_carve_s3_bucket.kms.kms_base_policy != null ? var.osquery_carve_s3_bucket.kms.kms_base_policy : [
{
sid = "EnableRootPermissions"
effect = "Allow"
actions = ["kms:*"]
resources = ["*"]
principals = {
type = "AWS"
identifiers = ["arn:${data.aws_partition.current.partition}:iam::${data.aws_caller_identity.current.account_id}:root"]
}
conditions = []
}
]
osquery_carve_kms_task_role_statements = local.osquery_carve_fleet_role_name != null ? [
{
sid = "AllowFleetRoleUseOfTheKey"
effect = "Allow"
actions = [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt*",
"kms:Describe*",
"kms:Decrypt*"
]
resources = ["*"]
principals = {
type = "AWS"
identifiers = [data.aws_iam_role.osquery_carve_fleet[0].arn]
}
conditions = []
}
] : []
}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
check "kms_base_policy_requires_module_managed_cmk" {
assert {
condition = var.osquery_carve_s3_bucket.kms.kms_base_policy == null || (
local.osquery_carve_create_kms_key == true
)
error_message = "osquery_carve_s3_bucket.kms.kms_base_policy is not used by osquery-carve unless this module is creating the osquery carve CMK. When kms_key_arn is provided, external key policies remain caller-managed."
}
}
data "aws_kms_key" "osquery_carve_provided" {
count = local.osquery_carve_provided_kms_key_ref != null ? 1 : 0
key_id = local.osquery_carve_provided_kms_key_ref
}
data "aws_iam_role" "osquery_carve_fleet" {
count = local.osquery_carve_create_kms_key && local.osquery_carve_fleet_role_name != null ? 1 : 0
name = local.osquery_carve_fleet_role_name
}
resource "aws_s3_bucket" "main" { #tfsec:ignore:aws-s3-encryption-customer-key:exp:2028-07-01 #tfsec:ignore:aws-s3-enable-versioning #tfsec:ignore:aws-s3-enable-bucket-logging:exp:2028-07-01
bucket = var.osquery_carve_s3_bucket.name
}
@@ -25,11 +100,102 @@ resource "aws_s3_bucket_server_side_encryption_configuration" "main" {
bucket = aws_s3_bucket.main.bucket
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = local.osquery_carve_kms_key_id
sse_algorithm = "aws:kms"
}
}
}
resource "aws_kms_key" "osquery_carve" {
count = local.osquery_carve_create_kms_key ? 1 : 0
description = "CMK for Fleet osquery carve S3 bucket object encryption."
enable_key_rotation = true
}
resource "aws_kms_alias" "osquery_carve" {
count = local.osquery_carve_create_kms_key ? 1 : 0
target_key_id = aws_kms_key.osquery_carve[0].id
name = "alias/${var.osquery_carve_s3_bucket.kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "osquery_carve_kms" {
count = local.osquery_carve_create_kms_key_policy ? 1 : 0
dynamic "statement" {
for_each = local.kms_base_policy_statements
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.osquery_carve_s3_bucket.kms.extra_kms_policies
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = local.osquery_carve_kms_task_role_statements
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
}
resource "aws_kms_key_policy" "osquery_carve" {
count = local.osquery_carve_create_kms_key_policy ? 1 : 0
key_id = aws_kms_key.osquery_carve[0].id
policy = data.aws_iam_policy_document.osquery_carve_kms[0].json
}
resource "aws_s3_bucket_public_access_block" "main" {
bucket = aws_s3_bucket.main.id
block_public_acls = true
+1
View File
@@ -6,6 +6,7 @@ output "fleet_extra_environment_variables" {
}
output "fleet_extra_iam_policies" {
description = "IAM policies required for Fleet to access the osquery carve S3 bucket."
value = [
aws_iam_policy.main.arn
]
+66
View File
@@ -2,9 +2,75 @@ variable "osquery_carve_s3_bucket" {
type = object({
name = optional(string, "fleet-osquery-results-archive")
expires_days = optional(number, 1)
kms = optional(object({
kms_key_arn = optional(string, null)
create_kms_key = optional(bool, false)
kms_alias = optional(string, "osquery-carve")
kms_base_policy = optional(list(object({
sid = string
effect = string
principals = object({
type = string
identifiers = list(string)
})
actions = list(string)
resources = list(string)
conditions = optional(list(object({
test = string
variable = string
values = list(string)
})), [])
})), null)
extra_kms_policies = optional(list(any), [])
fleet_role_name = optional(string, null)
}), {
kms_key_arn = null
create_kms_key = false
kms_alias = "osquery-carve"
kms_base_policy = null
extra_kms_policies = []
fleet_role_name = null
})
})
description = "Configuration for the osquery carve S3 bucket, including optional customer-managed KMS settings."
default = {
name = "fleet-osquery-results-archive"
expires_days = 1
kms = {
kms_key_arn = null
create_kms_key = false
kms_alias = "osquery-carve"
kms_base_policy = null
extra_kms_policies = []
fleet_role_name = null
}
}
validation {
condition = !(var.osquery_carve_s3_bucket.kms.kms_key_arn != null && var.osquery_carve_s3_bucket.kms.create_kms_key == true)
error_message = "osquery_carve_s3_bucket.kms.kms_key_arn and osquery_carve_s3_bucket.kms.create_kms_key are mutually exclusive; set one or the other, not both."
}
validation {
condition = (
length(var.osquery_carve_s3_bucket.kms.extra_kms_policies) == 0 ||
(
var.osquery_carve_s3_bucket.kms.create_kms_key == true &&
var.osquery_carve_s3_bucket.kms.kms_key_arn == null
)
)
error_message = "osquery_carve_s3_bucket.kms.extra_kms_policies can be set only when osquery-carve is creating the osquery carve CMK."
}
validation {
condition = (
var.osquery_carve_s3_bucket.kms.fleet_role_name == null ||
trimspace(var.osquery_carve_s3_bucket.kms.fleet_role_name) == "" ||
(
var.osquery_carve_s3_bucket.kms.create_kms_key == true &&
var.osquery_carve_s3_bucket.kms.kms_key_arn == null
)
)
error_message = "osquery_carve_s3_bucket.kms.fleet_role_name can be set only when osquery-carve is creating the osquery carve CMK."
}
}
+1 -1
View File
@@ -22,7 +22,7 @@ For each feature:
- If a key ARN is provided, this module uses that key and does not create a CMK.
- For software installers, setting `software_installers.kms_key_arn` is sufficient to use that key even if `create_kms_key = false`.
IAM permissions for using these keys are managed in-module where possible. If a task role ARN is provided and belongs to the same AWS account, KMS/S3 policy attachments and `extra_iam_policies` attachments are applied to that role.
IAM permissions for using these keys are managed in-module where possible. For module-created keys, the module prefers granting usage in the KMS key policy itself. If a caller-provided external key still needs IAM authorization and the task role ARN belongs to the same AWS account, the module attaches a separate resource-scoped KMS IAM policy to that role instead of mixing KMS access into the broader S3 or secret-access policy. `extra_iam_policies` attachments continue to be applied to the same-account task role when provided.
## Example: Use Module-Managed Keys
@@ -0,0 +1 @@
header-from: .header.md
+8 -2
View File
@@ -22,7 +22,7 @@ For each feature:
- If a key ARN is provided, this module uses that key and does not create a CMK.
- For software installers, setting `software_installers.kms_key_arn` is sufficient to use that key even if `create_kms_key = false`.
IAM permissions for using these keys are managed in-module where possible. If a task role ARN is provided and belongs to the same AWS account, KMS/S3 policy attachments and `extra_iam_policies` attachments are applied to that role.
IAM permissions for using these keys are managed in-module where possible. For module-created keys, the module prefers granting usage in the KMS key policy itself. If a caller-provided external key still needs IAM authorization and the task role ARN belongs to the same AWS account, the module attaches a separate resource-scoped KMS IAM policy to that role instead of mixing KMS access into the broader S3 or secret-access policy. `extra_iam_policies` attachments continue to be applied to the same-account task role when provided.
## Example: Use Module-Managed Keys
@@ -149,16 +149,20 @@ No modules.
| [aws_ecs_service.fleet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
| [aws_ecs_task_definition.backend](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
| [aws_iam_policy.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.execution_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_policy.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_iam_role.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.execution_extras](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.execution_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.extras](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.role_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_kms_alias.application_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_alias.private_key_secret](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_alias.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
@@ -179,8 +183,10 @@ No modules.
| [aws_iam_policy_document.assume_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.fleet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.fleet-execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.fleet-execution-kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.private_key_secret_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers_external_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.software_installers_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_kms_key.software_installers_provided](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/kms_key) | data source |
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
@@ -191,7 +197,7 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_ecs_cluster"></a> [ecs\_cluster](#input\_ecs\_cluster) | The name of the ECS cluster to use | `string` | n/a | yes |
| <a name="input_fleet_config"></a> [fleet\_config](#input\_fleet\_config) | The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. | <pre>object({<br/> task_mem = optional(number, null)<br/> task_cpu = optional(number, null)<br/> ephemeral_storage = optional(object({<br/> size_in_gib = number<br/> }), null)<br/> mem = optional(number, 4096)<br/> cpu = optional(number, 512)<br/> pid_mode = optional(string, null)<br/> image = optional(string, "fleetdm/fleet:v4.81.2")<br/> family = optional(string, "fleet")<br/> sidecars = optional(list(any), [])<br/> depends_on = optional(list(any), [])<br/> mount_points = optional(list(any), [])<br/> volumes = optional(list(any), [])<br/> extra_environment_variables = optional(map(string), {})<br/> extra_iam_policies = optional(list(string), [])<br/> extra_execution_iam_policies = optional(list(string), [])<br/> extra_secrets = optional(map(string), {})<br/> security_group_name = optional(string, "fleet")<br/> iam_role_arn = optional(string, null)<br/> repository_credentials = optional(string, "")<br/> private_key_secret_name = optional(string, "fleet-server-private-key")<br/> private_key_secret_kms = optional(object({<br/> cmk_enabled = optional(bool, null)<br/> enabled = optional(bool, null)<br/> kms_key_arn = optional(string, null)<br/> kms_alias = optional(string, "fleet-server-private-key")<br/> extra_kms_policies = optional(list(any), [])<br/> }), {<br/> cmk_enabled = null<br/> enabled = null<br/> kms_key_arn = null<br/> kms_alias = "fleet-server-private-key"<br/> extra_kms_policies = []<br/> })<br/> server_tls_enabled = optional(bool, false)<br/> service = optional(object({<br/> name = optional(string, "fleet")<br/> }), {<br/> name = "fleet"<br/> })<br/> database = object({<br/> password_secret_arn = string<br/> password_secret_kms_key_arn = optional(string, null)<br/> user = string<br/> database = string<br/> address = string<br/> rr_address = optional(string, null)<br/> })<br/> redis = object({<br/> address = string<br/> use_tls = optional(bool, true)<br/> })<br/> awslogs = optional(object({<br/> name = optional(string, null)<br/> region = optional(string, null)<br/> create = optional(bool, true)<br/> prefix = optional(string, "fleet")<br/> retention = optional(number, 5)<br/> kms = optional(object({<br/> cmk_enabled = optional(bool, null)<br/> enabled = optional(bool, null)<br/> kms_key_arn = optional(string, null)<br/> kms_alias = optional(string, "fleet-application-logs")<br/> extra_kms_policies = optional(list(any), [])<br/> }), {<br/> cmk_enabled = null<br/> enabled = null<br/> kms_key_arn = null<br/> kms_alias = "fleet-application-logs"<br/> extra_kms_policies = []<br/> })<br/> }), {<br/> name = null<br/> region = null<br/> create = true<br/> prefix = "fleet"<br/> retention = 5<br/> kms = {<br/> cmk_enabled = null<br/> enabled = null<br/> kms_key_arn = null<br/> kms_alias = "fleet-application-logs"<br/> extra_kms_policies = []<br/> }<br/> })<br/> loadbalancer = object({<br/> arn = string<br/> })<br/> extra_load_balancers = optional(list(any), [])<br/> networking = object({<br/> subnets = optional(list(string), null)<br/> security_groups = optional(list(string), null)<br/> ingress_sources = object({<br/> cidr_blocks = optional(list(string), [])<br/> ipv6_cidr_blocks = optional(list(string), [])<br/> security_groups = optional(list(string), [])<br/> prefix_list_ids = optional(list(string), [])<br/> })<br/> assign_public_ip = optional(bool, false)<br/> })<br/> autoscaling = optional(object({<br/> max_capacity = optional(number, 5)<br/> min_capacity = optional(number, 1)<br/> memory_tracking_target_value = optional(number, 80)<br/> cpu_tracking_target_value = optional(number, 80)<br/> }), {<br/> max_capacity Line truncated
| <a name="input_fleet_config"></a> [fleet\_config](#input\_fleet\_config) | The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`. | <pre>object({<br/> task_mem = optional(number, null)<br/> task_cpu = optional(number, null)<br/> ephemeral_storage = optional(object({<br/> size_in_gib = number<br/> }), null)<br/> mem = optional(number, 4096)<br/> cpu = optional(number, 512)<br/> pid_mode = optional(string, null)<br/> image = optional(string, "fleetdm/fleet:v4.82.1")<br/> family = optional(string, "fleet")<br/> sidecars = optional(list(any), [])<br/> depends_on = optional(list(any), [])<br/> mount_points = optional(list(any), [])<br/> volumes = optional(list(any), [])<br/> extra_environment_variables = optional(map(string), {})<br/> extra_iam_policies = optional(list(string), [])<br/> extra_execution_iam_policies = optional(list(string), [])<br/> extra_secrets = optional(map(string), {})<br/> security_group_name = optional(string, "fleet")<br/> iam_role_arn = optional(string, null)<br/> repository_credentials = optional(string, "")<br/> private_key_secret_name = optional(string, "fleet-server-private-key")<br/> private_key_secret_kms = optional(object({<br/> cmk_enabled = optional(bool, null)<br/> enabled = optional(bool, null)<br/> kms_key_arn = optional(string, null)<br/> kms_alias = optional(string, "fleet-server-private-key")<br/> extra_kms_policies = optional(list(any), [])<br/> }), {<br/> cmk_enabled = null<br/> enabled = null<br/> kms_key_arn = null<br/> kms_alias = "fleet-server-private-key"<br/> extra_kms_policies = []<br/> })<br/> server_tls_enabled = optional(bool, false)<br/> service = optional(object({<br/> name = optional(string, "fleet")<br/> }), {<br/> name = "fleet"<br/> })<br/> database = object({<br/> password_secret_arn = string<br/> password_secret_kms_key_arn = optional(string, null)<br/> user = string<br/> database = string<br/> address = string<br/> rr_address = optional(string, null)<br/> })<br/> redis = object({<br/> address = string<br/> use_tls = optional(bool, true)<br/> })<br/> awslogs = optional(object({<br/> name = optional(string, null)<br/> region = optional(string, null)<br/> create = optional(bool, true)<br/> prefix = optional(string, "fleet")<br/> retention = optional(number, 5)<br/> kms = optional(object({<br/> cmk_enabled = optional(bool, null)<br/> enabled = optional(bool, null)<br/> kms_key_arn = optional(string, null)<br/> kms_alias = optional(string, "fleet-application-logs")<br/> extra_kms_policies = optional(list(any), [])<br/> }), {<br/> cmk_enabled = null<br/> enabled = null<br/> kms_key_arn = null<br/> kms_alias = "fleet-application-logs"<br/> extra_kms_policies = []<br/> })<br/> }), {<br/> name = null<br/> region = null<br/> create = true<br/> prefix = "fleet"<br/> retention = 5<br/> kms = {<br/> cmk_enabled = null<br/> enabled = null<br/> kms_key_arn = null<br/> kms_alias = "fleet-application-logs"<br/> extra_kms_policies = []<br/> }<br/> })<br/> loadbalancer = object({<br/> arn = string<br/> })<br/> extra_load_balancers = optional(list(any), [])<br/> networking = object({<br/> subnets = optional(list(string), null)<br/> security_groups = optional(list(string), null)<br/> ingress_sources = object({<br/> cidr_blocks = optional(list(string), [])<br/> ipv6_cidr_blocks = optional(list(string), [])<br/> security_groups = optional(list(string), [])<br/> prefix_list_ids = optional(list(string), [])<br/> })<br/> assign_public_ip = optional(bool, false)<br/> })<br/> autoscaling = optional(object({<br/> max_capacity = optional(number, 5)<br/> min_capacity = optional(number, 1)<br/> memory_tracking_target_value = optional(number, 80)<br/> cpu_tracking_target_value = optional(number, 80)<br/> }), {<br/> max_capacity Line truncated
| <a name="input_kms_base_policy"></a> [kms\_base\_policy](#input\_kms\_base\_policy) | Optional base KMS key-policy statements to apply to module-created CMKs before module-required service access statements are merged in. If null, the module defaults to the historical root `kms:*` statement. | <pre>list(object({<br/> sid = string<br/> effect = string<br/> principals = object({<br/> type = string<br/> identifiers = list(string)<br/> })<br/> actions = list(string)<br/> resources = list(string)<br/> conditions = optional(list(object({<br/> test = string<br/> variable = string<br/> values = list(string)<br/> })), [])<br/> }))</pre> | `null` | no |
| <a name="input_migration_config"></a> [migration\_config](#input\_migration\_config) | The configuration object for Fleet's migration task. | <pre>object({<br/> mem = number<br/> cpu = number<br/> })</pre> | <pre>{<br/> "cpu": 1024,<br/> "mem": 2048<br/>}</pre> | no |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | n/a | `string` | `null` | no |
+7 -20
View File
@@ -6,19 +6,19 @@ locals {
task_role_name = var.fleet_config.iam_role_arn == null ? var.fleet_config.iam.role.name : (
can(split(":", var.fleet_config.iam_role_arn)[4]) && split(":", var.fleet_config.iam_role_arn)[4] == data.aws_caller_identity.current.account_id && can(split("role/", var.fleet_config.iam_role_arn)[1]) ? split("role/", var.fleet_config.iam_role_arn)[1] : null
)
software_installers_kms_policy = local.software_installers_kms_key_arn != null ? [{
software_installers_kms_policy = local.software_installers_kms_key_arn != null && local.software_installers_create_kms_key == false ? [{
sid = "AllowSoftwareInstallersKMSAccess"
actions = [
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Encrypt*",
"kms:Describe*",
"kms:Decrypt*"
"kms:Encrypt",
"kms:DescribeKey",
"kms:Decrypt"
]
resources = [local.software_installers_kms_key_arn]
effect = "Allow"
}] : []
private_key_secret_kms_policy = local.private_key_secret_kms_key_arn != null ? [{
private_key_secret_kms_policy = local.private_key_secret_kms_key_arn != null && local.private_key_secret_create_kms_key == false ? [{
sid = "AllowFleetPrivateKeySecretKMSAccess"
actions = [
"kms:Decrypt",
@@ -55,6 +55,7 @@ data "aws_iam_policy_document" "software_installers" {
]
resources = [aws_s3_bucket.software_installers[0].arn, "${aws_s3_bucket.software_installers[0].arn}/*"]
}
dynamic "statement" {
for_each = local.software_installers_kms_policy
content {
@@ -62,21 +63,6 @@ data "aws_iam_policy_document" "software_installers" {
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
effect = try(statement.value.effect, null)
dynamic "principals" {
for_each = try(statement.value.principals, [])
content {
type = principals.value.type
identifiers = principals.value.identifiers
}
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
}
@@ -122,6 +108,7 @@ data "aws_iam_policy_document" "fleet-execution" {
aws_secretsmanager_secret.fleet_server_private_key.arn
]
}
dynamic "statement" {
for_each = local.execution_kms_policy
content {
+196 -17
View File
@@ -160,6 +160,17 @@ check "deprecated_fleet_config_awslogs_kms_enabled" {
}
}
check "kms_base_policy_requires_module_managed_cmk" {
assert {
condition = var.kms_base_policy == null || (
local.application_logs_create_kms_key == true ||
local.private_key_secret_create_kms_key == true ||
local.software_installers_create_kms_key == true
)
error_message = "kms_base_policy is not used by byo-ecs unless this module is creating at least one CMK. When kms_key_arn is provided, external key policies remain caller-managed."
}
}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
@@ -377,15 +388,57 @@ resource "aws_appautoscaling_policy" "ecs_policy_cpu" {
}
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "application_logs_kms" {
count = local.application_logs_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.fleet_config.awslogs.kms.extra_kms_policies,
[local.kms_service_statements.cloudwatch_logs]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.fleet_config.awslogs.kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -496,16 +549,79 @@ resource "aws_kms_key" "private_key_secret" {
policy = data.aws_iam_policy_document.private_key_secret_kms[0].json
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "private_key_secret_kms" {
count = local.private_key_secret_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.fleet_config.private_key_secret_kms.extra_kms_policies,
[local.kms_service_statements.secretsmanager],
[local.kms_service_statements.execution_role]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.fleet_config.private_key_secret_kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.secretsmanager]
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.execution_role]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -527,16 +643,79 @@ data "aws_iam_policy_document" "private_key_secret_kms" {
}
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "software_installers_kms" {
count = local.software_installers_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.fleet_config.software_installers.extra_kms_policies,
local.software_installers_kms_task_role_statements,
local.software_installers_kms_service_statements
)
for_each = local.kms_base_policy_statements
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.fleet_config.software_installers.extra_kms_policies
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = local.software_installers_kms_task_role_statements
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
actions = try(statement.value.actions, [])
resources = try(statement.value.resources, [])
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = local.software_installers_kms_service_statements
content {
sid = try(statement.value.sid, "")
effect = try(statement.value.effect, null)
+58
View File
@@ -301,6 +301,64 @@ variable "fleet_config" {
condition = var.fleet_config.ephemeral_storage == null ? true : (var.fleet_config.ephemeral_storage.size_in_gib >= 21 && var.fleet_config.ephemeral_storage.size_in_gib <= 200)
error_message = "fleet_config.ephemeral_storage.size_in_gib must be between 21 and 200 GiB when set."
}
validation {
condition = (
var.fleet_config.private_key_secret_kms.kms_key_arn == null ||
coalesce(var.fleet_config.private_key_secret_kms.cmk_enabled, var.fleet_config.private_key_secret_kms.enabled, false)
)
error_message = "fleet_config.private_key_secret_kms.kms_key_arn requires fleet_config.private_key_secret_kms.cmk_enabled = true (or legacy enabled = true)."
}
validation {
condition = (
length(var.fleet_config.private_key_secret_kms.extra_kms_policies) == 0 ||
(
coalesce(var.fleet_config.private_key_secret_kms.cmk_enabled, var.fleet_config.private_key_secret_kms.enabled, false) &&
var.fleet_config.private_key_secret_kms.kms_key_arn == null
)
)
error_message = "fleet_config.private_key_secret_kms.extra_kms_policies can be set only when byo-ecs is creating the private key secret CMK."
}
validation {
condition = (
var.fleet_config.awslogs.kms.kms_key_arn == null ||
(
var.fleet_config.awslogs.create == true &&
coalesce(var.fleet_config.awslogs.kms.cmk_enabled, var.fleet_config.awslogs.kms.enabled, false)
)
)
error_message = "fleet_config.awslogs.kms.kms_key_arn requires fleet_config.awslogs.create = true and fleet_config.awslogs.kms.cmk_enabled = true (or legacy enabled = true)."
}
validation {
condition = (
length(var.fleet_config.awslogs.kms.extra_kms_policies) == 0 ||
(
var.fleet_config.awslogs.create == true &&
coalesce(var.fleet_config.awslogs.kms.cmk_enabled, var.fleet_config.awslogs.kms.enabled, false) &&
var.fleet_config.awslogs.kms.kms_key_arn == null
)
)
error_message = "fleet_config.awslogs.kms.extra_kms_policies can be set only when byo-ecs is creating the application logs CMK."
}
validation {
condition = !(var.fleet_config.software_installers.kms_key_arn != null && var.fleet_config.software_installers.create_kms_key == true)
error_message = "fleet_config.software_installers.kms_key_arn and fleet_config.software_installers.create_kms_key are mutually exclusive; set one or the other, not both."
}
validation {
condition = (
length(var.fleet_config.software_installers.extra_kms_policies) == 0 ||
(
var.fleet_config.software_installers.create_kms_key == true &&
var.fleet_config.software_installers.kms_key_arn == null
)
)
error_message = "fleet_config.software_installers.extra_kms_policies can be set only when byo-ecs is creating the software installers CMK."
}
}
variable "migration_config" {
+107 -13
View File
@@ -206,6 +206,16 @@ check "deprecated_fleet_config_fargate_ephemeral_storage_kms_enabled" {
}
}
check "kms_base_policy_requires_module_managed_cmk" {
assert {
condition = var.kms_base_policy == null || (
local.fargate_ephemeral_storage_create_kms_key == true ||
local.cluster_cloudwatch_log_group_create_kms_key == true
)
error_message = "kms_base_policy is not used by byo-db unless this module is creating at least one CMK. When kms_key_arn is provided, external key policies remain caller-managed."
}
}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
@@ -237,18 +247,60 @@ module "cluster" {
tags = var.ecs_cluster.tags
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "fargate_ephemeral_storage_kms" {
count = local.fargate_ephemeral_storage_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies,
[
local.kms_service_statements.fargate_generate_data_key,
local.kms_service_statements.fargate_create_grant,
]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [
local.kms_service_statements.fargate_generate_data_key,
local.kms_service_statements.fargate_create_grant,
]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -283,15 +335,57 @@ resource "aws_kms_alias" "fargate_ephemeral_storage" {
name = "alias/${var.fleet_config.fargate_ephemeral_storage_kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "cluster_cloudwatch_log_group_kms" {
count = local.cluster_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies,
[local.kms_service_statements.cloudwatch_logs]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
+42
View File
@@ -124,6 +124,29 @@ variable "ecs_cluster" {
}
description = "The config for the terraform-aws-modules/ecs/aws module. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`."
nullable = false
validation {
condition = (
var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn == null ||
(
var.ecs_cluster.cloudwatch_log_group.create == true &&
coalesce(var.ecs_cluster.cloudwatch_log_group.kms.cmk_enabled, var.ecs_cluster.cloudwatch_log_group.kms.enabled, false)
)
)
error_message = "ecs_cluster.cloudwatch_log_group.kms.kms_key_arn requires ecs_cluster.cloudwatch_log_group.create = true and ecs_cluster.cloudwatch_log_group.kms.cmk_enabled = true (or legacy enabled = true)."
}
validation {
condition = (
length(var.ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies) == 0 ||
(
var.ecs_cluster.cloudwatch_log_group.create == true &&
coalesce(var.ecs_cluster.cloudwatch_log_group.kms.cmk_enabled, var.ecs_cluster.cloudwatch_log_group.kms.enabled, false) &&
var.ecs_cluster.cloudwatch_log_group.kms.kms_key_arn == null
)
)
error_message = "ecs_cluster.cloudwatch_log_group.kms.extra_kms_policies can be set only when byo-db is creating the ECS cluster CloudWatch log group CMK."
}
}
@@ -443,6 +466,25 @@ variable "fleet_config" {
}
description = "The configuration object for Fleet itself. Fields that default to null will have their respective resources created if not specified. For published KMS blocks, legacy `enabled` is deprecated and still accepted; prefer `cmk_enabled`."
nullable = false
validation {
condition = (
var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn == null ||
coalesce(var.fleet_config.fargate_ephemeral_storage_kms.cmk_enabled, var.fleet_config.fargate_ephemeral_storage_kms.enabled, false)
)
error_message = "fleet_config.fargate_ephemeral_storage_kms.kms_key_arn requires fleet_config.fargate_ephemeral_storage_kms.cmk_enabled = true (or legacy enabled = true)."
}
validation {
condition = (
length(var.fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies) == 0 ||
(
coalesce(var.fleet_config.fargate_ephemeral_storage_kms.cmk_enabled, var.fleet_config.fargate_ephemeral_storage_kms.enabled, false) &&
var.fleet_config.fargate_ephemeral_storage_kms.kms_key_arn == null
)
)
error_message = "fleet_config.fargate_ephemeral_storage_kms.extra_kms_policies can be set only when byo-db is creating the Fargate ephemeral storage CMK."
}
}
variable "migration_config" {
+318 -31
View File
@@ -194,15 +194,71 @@ data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
check "kms_base_policy_requires_module_managed_cmk" {
assert {
condition = var.kms_base_policy == null || (
local.rds_storage_create_kms_key == true ||
local.rds_password_secret_create_kms_key == true ||
local.rds_observability_create_kms_key == true ||
local.rds_cloudwatch_log_group_create_kms_key == true ||
local.redis_at_rest_create_kms_key == true ||
local.redis_cloudwatch_log_group_create_kms_key == true
)
error_message = "kms_base_policy is not used by byo-vpc unless this module is creating at least one CMK. When kms_key_arn is provided, external key policies remain caller-managed."
}
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_storage_kms" {
count = local.rds_storage_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.rds_config.storage_kms.extra_kms_policies,
[local.kms_service_statements.rds]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.rds_config.storage_kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.rds]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -237,16 +293,79 @@ resource "aws_kms_alias" "rds_storage" {
name = "alias/${var.rds_config.storage_kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_password_secret_kms" {
count = local.rds_password_secret_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.rds_config.password_secret_kms.extra_kms_policies,
[local.kms_service_statements.secretsmanager],
[local.kms_service_statements.execution_role]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.rds_config.password_secret_kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.secretsmanager]
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.execution_role]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -281,15 +400,57 @@ resource "aws_kms_alias" "rds_password_secret" {
name = "alias/${var.rds_config.password_secret_kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_observability_kms" {
count = local.rds_observability_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.rds_config.observability.kms.extra_kms_policies,
[local.kms_service_statements.rds]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.rds_config.observability.kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.rds]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -324,15 +485,57 @@ resource "aws_kms_alias" "rds_observability" {
name = "alias/${var.rds_config.observability.kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "rds_cloudwatch_log_group_kms" {
count = local.rds_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.rds_config.cloudwatch_log_group.kms.extra_kms_policies,
[local.kms_service_statements.cloudwatch_logs]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.rds_config.cloudwatch_log_group.kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -367,15 +570,57 @@ resource "aws_kms_alias" "rds_cloudwatch_log_group" {
name = "alias/${var.rds_config.cloudwatch_log_group.kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "redis_at_rest_kms" {
count = local.redis_at_rest_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.redis_config.at_rest_kms.extra_kms_policies,
[local.kms_service_statements.elasticache]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.redis_config.at_rest_kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.elasticache]
content {
sid = statement.value.sid
effect = statement.value.effect
@@ -410,15 +655,57 @@ resource "aws_kms_alias" "redis_at_rest" {
name = "alias/${var.redis_config.at_rest_kms.kms_alias}"
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "redis_cloudwatch_log_group_kms" {
count = local.redis_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.redis_config.cloudwatch_log_group.kms.extra_kms_policies,
[local.kms_service_statements.cloudwatch_logs]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.redis_config.cloudwatch_log_group.kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
+56 -5
View File
@@ -46,15 +46,66 @@ data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
check "kms_base_policy_requires_module_managed_cmk" {
assert {
condition = var.kms_base_policy == null || (
local.vpc_flow_log_cloudwatch_log_group_create_kms_key == true
)
error_message = "kms_base_policy is not used by the root module unless this module is creating the VPC flow log CloudWatch log group CMK. When kms_key_arn is provided, external key policies remain caller-managed."
}
}
# Each source uses its own dynamic "statement" block to avoid Terraform type
# conflicts when concatenating typed variable values with inline literal tuples.
data "aws_iam_policy_document" "vpc_flow_log_cloudwatch_log_group_kms" {
count = local.vpc_flow_log_cloudwatch_log_group_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = concat(
local.kms_base_policy_statements,
var.vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies,
[local.kms_service_statements.cloudwatch_logs]
)
for_each = local.kms_base_policy_statements
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = var.vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies
content {
sid = statement.value.sid
effect = statement.value.effect
actions = statement.value.actions
resources = statement.value.resources
principals {
type = statement.value.principals.type
identifiers = statement.value.principals.identifiers
}
dynamic "condition" {
for_each = try(statement.value.conditions, [])
content {
test = condition.value.test
variable = condition.value.variable
values = condition.value.values
}
}
}
}
dynamic "statement" {
for_each = [local.kms_service_statements.cloudwatch_logs]
content {
sid = statement.value.sid
effect = statement.value.effect
+23
View File
@@ -70,6 +70,29 @@ variable "vpc" {
}
vpc_flow_log_tags = {}
}
validation {
condition = (
var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn == null ||
(
var.vpc.enable_flow_log == true &&
var.vpc.create_flow_log_cloudwatch_log_group == true &&
var.vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled == true
)
)
error_message = "vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn requires vpc.enable_flow_log = true, vpc.create_flow_log_cloudwatch_log_group = true, and vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled = true."
}
validation {
condition = (
length(var.vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies) == 0 ||
(
var.vpc.enable_flow_log == true &&
var.vpc.create_flow_log_cloudwatch_log_group == true &&
var.vpc.flow_log_cloudwatch_log_group_kms.cmk_enabled == true &&
var.vpc.flow_log_cloudwatch_log_group_kms.kms_key_arn == null
)
)
error_message = "vpc.flow_log_cloudwatch_log_group_kms.extra_kms_policies can be set only when the root module is creating the VPC flow log CloudWatch log group CMK."
}
}
variable "certificate_arn" {