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."
}
}