KMS-CMK for some lambdas and the mdm secret (#200)

This commit is contained in:
Robert Fairburn authored and GitHub committed 2026-03-26 09:35:12 -05:00
1 parent f9421c10a1
commit bde5e8a341
11 files changed
+712 -31

No files matched your search

+26 -1
View File
@@ -1,7 +1,7 @@
# MDM addon
Notice: Previous versions of this module referred to `dep`, but to reduce confusion that has been replaces with `abm`
to mach the change to the newer Apple Busines Manager. For each key/value pair below, the key names have been changed
to match the change to the newer Apple Business Manager. For each key/value pair below, the key names have been changed
from previous version to match the name of the env var for easier usability. Older unused env vars were also removed
for simplification. This includes removing the need for `extra_environment_variables` completely.
@@ -11,6 +11,10 @@ The following secrets are created:
- scep
- apn
To encrypt those secrets with a CMK, use `secrets_kms`. Supply `kms_key_arn` to use an existing key, or set `cmk_enabled = true` and leave `kms_key_arn = null` to have this module create a key using `kms_alias`.
If you are creating the MDM secrets CMK and this module's `extra_execution_iam_policies` output is fed back into the Fleet ECS execution role in the same stack, pass `secrets_kms.fleet_execution_role_name` as the stable execution role name string instead of trying to read it from a Fleet module output. That avoids introducing a cycle while still letting this module add the execution role to the CMK policy.
Note: ABM is optional. If Apple Business Manager (ABM) is not used, set the abm variable to `null` and it will be omitted.
Since this module cannot determine the value for the secrets at apply time, this module must be applied in 2 phases:
@@ -18,6 +22,27 @@ Since this module cannot determine the value for the secrets at apply time, this
1. In the first phase, just add the module without passing additional config to the main Fleet module
1. In the second phase, after the secret values have been populated, apply while also passing the additional config to the main Fleet module.
Example KMS configuration:
```
module "mdm" {
source = "github.com/fleetdm/fleet-terraform/addons/mdm?depth=1&ref=tf-mod-addon-mdm-v2.0.0"
apn_secret_name = null
scep_secret_name = "fleet-scep"
abm_secret_name = null
enable_apple_mdm = false
enable_windows_mdm = true
secrets_kms = {
cmk_enabled = true
kms_alias = "fleet-mdm-secrets"
kms_base_policy = local.kms_base_policy_statements
fleet_execution_role_name = "fleet-execution-role"
}
}
```
The secrets should have the following layouts, note that all values are strings. If a value is a JSON object, string escape it.:
## ABM
```
+35 -3
View File
@@ -1,7 +1,7 @@
# MDM addon
Notice: Previous versions of this module referred to `dep`, but to reduce confusion that has been replaces with `abm`
to mach the change to the newer Apple Business Manager. For each key/value pair below, the key names have been changed
to match the change to the newer Apple Business Manager. For each key/value pair below, the key names have been changed
from previous version to match the name of the env var for easier usability. Older unused env vars were also removed
for simplification. This includes removing the need for `extra_environment_variables` completely.
@@ -11,6 +11,10 @@ The following secrets are created:
- scep
- apn
To encrypt those secrets with a CMK, use `secrets_kms`. Supply `kms_key_arn` to use an existing key, or set `cmk_enabled = true` and leave `kms_key_arn = null` to have this module create a key using `kms_alias`.
If you are creating the MDM secrets CMK and this module's `extra_execution_iam_policies` output is fed back into the Fleet ECS execution role in the same stack, pass `secrets_kms.fleet_execution_role_name` as the stable execution role name string instead of trying to read it from a Fleet module output. That avoids introducing a cycle while still letting this module add the execution role to the CMK policy.
Note: ABM is optional. If Apple Business Manager (ABM) is not used, set the abm variable to `null` and it will be omitted.
Since this module cannot determine the value for the secrets at apply time, this module must be applied in 2 phases:
@@ -18,6 +22,27 @@ Since this module cannot determine the value for the secrets at apply time, this
1. In the first phase, just add the module without passing additional config to the main Fleet module
1. In the second phase, after the secret values have been populated, apply while also passing the additional config to the main Fleet module.
Example KMS configuration:
```
module "mdm" {
source = "github.com/fleetdm/fleet-terraform/addons/mdm?depth=1&ref=tf-mod-addon-mdm-v2.0.0"
apn_secret_name = null
scep_secret_name = "fleet-scep"
abm_secret_name = null
enable_apple_mdm = false
enable_windows_mdm = true
secrets_kms = {
cmk_enabled = true
kms_alias = "fleet-mdm-secrets"
kms_base_policy = local.kms_base_policy_statements
fleet_execution_role_name = "fleet-execution-role"
}
}
```
The secrets should have the following layouts, note that all values are strings. If a value is a JSON object, string escape it.:
## ABM
```
@@ -58,7 +83,7 @@ No requirements.
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.53.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
## Modules
@@ -69,10 +94,17 @@ No modules.
| Name | Type |
|------|------|
| [aws_iam_policy.main](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
| [aws_kms_alias.secrets](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_key.secrets](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
| [aws_secretsmanager_secret.abm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
| [aws_secretsmanager_secret.apn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | resource |
| [aws_secretsmanager_secret.scep](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/secretsmanager_secret) | 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.secrets_kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_role.fleet_execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_role) | data source |
| [aws_kms_key.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 |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |
## Inputs
@@ -83,8 +115,8 @@ No modules.
| <a name="input_apn_secret_name"></a> [apn\_secret\_name](#input\_apn\_secret\_name) | n/a | `string` | `"fleet-apn"` | no |
| <a name="input_enable_apple_mdm"></a> [enable\_apple\_mdm](#input\_enable\_apple\_mdm) | n/a | `bool` | `true` | no |
| <a name="input_enable_windows_mdm"></a> [enable\_windows\_mdm](#input\_enable\_windows\_mdm) | n/a | `bool` | `false` | no |
| <a name="input_public_domain_name"></a> [public\_domain\_name](#input\_public\_domain\_name) | n/a | `string` | n/a | yes |
| <a name="input_scep_secret_name"></a> [scep\_secret\_name](#input\_scep\_secret\_name) | n/a | `string` | `"fleet-scep"` | no |
| <a name="input_secrets_kms"></a> [secrets\_kms](#input\_secrets\_kms) | Configuration for optional customer-managed KMS encryption of the MDM Secrets Manager secrets. | <pre>object({<br/> cmk_enabled = optional(bool, false)<br/> kms_key_arn = optional(string, null)<br/> kms_alias = optional(string, "fleet-mdm-secrets")<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_execution_role_name = optional(string, null)<br/> })</pre> | <pre>{<br/> "cmk_enabled": false,<br/> "extra_kms_policies": [],<br/> "fleet_execution_role_name": null,<br/> "kms_alias": "fleet-mdm-secrets",<br/> "kms_base_policy": null,<br/> "kms_key_arn": null<br/>}</pre> | no |
## Outputs
+195 -5
View File
@@ -1,8 +1,187 @@
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
locals {
secrets_cmk_enabled = var.secrets_kms.cmk_enabled
secrets_provided_kms_key_id = var.secrets_kms.kms_key_arn
secrets_create_kms_key = local.secrets_cmk_enabled == true && local.secrets_provided_kms_key_id == null
secrets_kms_key_arn = local.secrets_cmk_enabled == true ? (
local.secrets_provided_kms_key_id != null ? data.aws_kms_key.provided[0].arn : aws_kms_key.secrets[0].arn
) : null
fleet_execution_role_name = (
var.secrets_kms.fleet_execution_role_name != null &&
trimspace(var.secrets_kms.fleet_execution_role_name) != ""
) ? trimspace(var.secrets_kms.fleet_execution_role_name) : null
secrets_kms_base_policy_statements = var.secrets_kms.kms_base_policy != null ? var.secrets_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 = []
}
]
secrets_kms_service_statements = [
{
sid = "AllowSecretsManagerUseOfTheKey"
effect = "Allow"
actions = ["kms:Encrypt", "kms:Decrypt", "kms:ReEncrypt*", "kms:GenerateDataKey*", "kms:CreateGrant", "kms:DescribeKey"]
resources = ["*"]
principals = {
type = "Service"
identifiers = ["secretsmanager.amazonaws.com"]
}
conditions = []
}
]
secrets_kms_execution_role_statements = local.fleet_execution_role_name != null ? [
{
sid = "AllowFleetExecutionRoleDecrypt"
effect = "Allow"
actions = ["kms:Decrypt", "kms:DescribeKey"]
resources = ["*"]
principals = {
type = "AWS"
identifiers = [data.aws_iam_role.fleet_execution[0].arn]
}
conditions = []
}
] : []
}
check "kms_base_policy_requires_module_managed_cmk" {
assert {
condition = var.secrets_kms.kms_base_policy == null || (
local.secrets_create_kms_key == true
)
error_message = "secrets_kms.kms_base_policy is not used by mdm unless this module is creating the secrets CMK. When kms_key_arn is provided, external key policies remain caller-managed."
}
}
data "aws_kms_key" "provided" {
count = local.secrets_provided_kms_key_id != null ? 1 : 0
key_id = local.secrets_provided_kms_key_id
}
data "aws_iam_role" "fleet_execution" {
count = local.secrets_create_kms_key == true && local.fleet_execution_role_name != null ? 1 : 0
name = local.fleet_execution_role_name
}
data "aws_iam_policy_document" "secrets_kms" {
count = local.secrets_create_kms_key == true ? 1 : 0
dynamic "statement" {
for_each = local.secrets_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.secrets_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.secrets_kms_service_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.secrets_kms_execution_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" "secrets" {
count = local.secrets_create_kms_key == true ? 1 : 0
description = "CMK for Fleet MDM Secrets Manager secret encryption."
enable_key_rotation = true
policy = data.aws_iam_policy_document.secrets_kms[0].json
}
resource "aws_kms_alias" "secrets" {
count = local.secrets_create_kms_key == true ? 1 : 0
target_key_id = aws_kms_key.secrets[0].id
name = "alias/${var.secrets_kms.kms_alias}"
}
resource "aws_secretsmanager_secret" "apn" {
count = var.apn_secret_name == null ? 0 : 1
name = var.apn_secret_name
count = var.apn_secret_name == null ? 0 : 1
name = var.apn_secret_name
kms_key_id = local.secrets_kms_key_arn
recovery_window_in_days = "0"
lifecycle {
@@ -11,7 +190,8 @@ resource "aws_secretsmanager_secret" "apn" {
}
resource "aws_secretsmanager_secret" "scep" {
name = var.scep_secret_name
name = var.scep_secret_name
kms_key_id = local.secrets_kms_key_arn
recovery_window_in_days = "0"
lifecycle {
@@ -20,8 +200,9 @@ resource "aws_secretsmanager_secret" "scep" {
}
resource "aws_secretsmanager_secret" "abm" {
count = var.abm_secret_name == null ? 0 : 1
name = var.abm_secret_name
count = var.abm_secret_name == null ? 0 : 1
name = var.abm_secret_name
kms_key_id = local.secrets_kms_key_arn
recovery_window_in_days = "0"
lifecycle {
@@ -36,6 +217,15 @@ data "aws_iam_policy_document" "main" {
[aws_secretsmanager_secret.scep.arn],
var.abm_secret_name == null ? [] : [aws_secretsmanager_secret.abm[0].arn])
}
dynamic "statement" {
for_each = local.secrets_kms_key_arn != null ? [local.secrets_kms_key_arn] : []
content {
sid = "UseMDMSecretsKMSKey"
actions = ["kms:Decrypt", "kms:DescribeKey"]
resources = [statement.value]
}
}
}
resource "aws_iam_policy" "main" {
+76
View File
@@ -27,3 +27,79 @@ variable "enable_apple_mdm" {
nullable = false
type = bool
}
variable "secrets_kms" {
description = "Configuration for optional customer-managed KMS encryption of the MDM Secrets Manager secrets."
type = object({
cmk_enabled = optional(bool, false)
kms_key_arn = optional(string, null)
kms_alias = optional(string, "fleet-mdm-secrets")
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_execution_role_name = optional(string, null)
})
default = {
cmk_enabled = false
kms_key_arn = null
kms_alias = "fleet-mdm-secrets"
kms_base_policy = null
extra_kms_policies = []
fleet_execution_role_name = null
}
validation {
condition = (
var.secrets_kms.kms_key_arn == null ||
var.secrets_kms.cmk_enabled == true
)
error_message = "secrets_kms.kms_key_arn requires secrets_kms.cmk_enabled = true."
}
validation {
condition = (
length(var.secrets_kms.extra_kms_policies) == 0 ||
(
var.secrets_kms.cmk_enabled == true &&
var.secrets_kms.kms_key_arn == null
)
)
error_message = "secrets_kms.extra_kms_policies can be set only when the mdm module is creating the secrets CMK."
}
validation {
condition = (
var.secrets_kms.kms_base_policy == null ||
(
var.secrets_kms.cmk_enabled == true &&
var.secrets_kms.kms_key_arn == null
)
)
error_message = "secrets_kms.kms_base_policy can be set only when the mdm module is creating the secrets CMK."
}
validation {
condition = (
var.secrets_kms.fleet_execution_role_name == null ||
trimspace(var.secrets_kms.fleet_execution_role_name) == "" ||
(
var.secrets_kms.cmk_enabled == true &&
var.secrets_kms.kms_key_arn == null
)
)
error_message = "secrets_kms.fleet_execution_role_name can be set only when the mdm module is creating the secrets CMK."
}
}