Skip to content

Commit 7b85239

Browse files
fix: Modify key_service_principals to be generic to support conditions for confused deputy problem (#7)
Co-authored-by: Anton Babenko <[email protected]>
1 parent fd5f053 commit 7b85239

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ No modules.
195195
| <a name="input_key_hmac_users"></a> [key\_hmac\_users](#input\_key\_hmac\_users) | A list of IAM ARNs for [key HMAC users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto) | `list(string)` | `[]` | no |
196196
| <a name="input_key_material_base64"></a> [key\_material\_base64](#input\_key\_material\_base64) | Base64 encoded 256-bit symmetric encryption key material to import. The CMK is permanently associated with this key material. External key only | `string` | `null` | no |
197197
| <a name="input_key_owners"></a> [key\_owners](#input\_key\_owners) | A list of IAM ARNs for those who will have full key permissions (`kms:*`) | `list(string)` | `[]` | no |
198-
| <a name="input_key_service_principals"></a> [key\_service\_principals](#input\_key\_service\_principals) | A map of IAM Services for [key principals](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-services.html) | `map(any)` | `{}` | no |
199198
| <a name="input_key_service_users"></a> [key\_service\_users](#input\_key\_service\_users) | A list of IAM ARNs for [key service users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-service-integration) | `list(string)` | `[]` | no |
199+
| <a name="input_key_statements"></a> [key\_statements](#input\_key\_statements) | A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage | `any` | `{}` | no |
200200
| <a name="input_key_symmetric_encryption_users"></a> [key\_symmetric\_encryption\_users](#input\_key\_symmetric\_encryption\_users) | A list of IAM ARNs for [key symmetric encryption users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-users-crypto) | `list(string)` | `[]` | no |
201201
| <a name="input_key_usage"></a> [key\_usage](#input\_key\_usage) | Specifies the intended use of the key. Valid values: `ENCRYPT_DECRYPT` or `SIGN_VERIFY`. Defaults to `ENCRYPT_DECRYPT` | `string` | `null` | no |
202202
| <a name="input_key_users"></a> [key\_users](#input\_key\_users) | A list of IAM ARNs for [key users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-default-allow-users) | `list(string)` | `[]` | no |

examples/complete/main.tf

+22-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ locals {
1515
}
1616

1717
data "aws_caller_identity" "current" {}
18-
1918
data "aws_region" "current" {}
2019

2120
################################################################################
@@ -42,20 +41,36 @@ module "kms_complete" {
4241
key_hmac_users = [local.current_identity]
4342
key_asymmetric_public_encryption_users = [local.current_identity]
4443
key_asymmetric_sign_verify_users = [local.current_identity]
45-
key_service_principals = {
46-
"aws-logs" = {
47-
sid = "aws-logs"
44+
key_statements = [
45+
{
46+
sid = "CloudWatchLogs"
4847
actions = [
4948
"kms:Encrypt*",
5049
"kms:Decrypt*",
5150
"kms:ReEncrypt*",
5251
"kms:GenerateDataKey*",
5352
"kms:Describe*"
5453
]
55-
resources = ["*"]
56-
principals = ["logs.${data.aws_region.current.name}.amazonaws.com"]
54+
resources = ["*"]
55+
56+
principals = [
57+
{
58+
type = "Service"
59+
identifiers = ["logs.${data.aws_region.current.name}.amazonaws.com"]
60+
}
61+
]
62+
63+
conditions = [
64+
{
65+
test = "ArnLike"
66+
variable = "kms:EncryptionContext:aws:logs:arn"
67+
values = [
68+
"arn:aws:logs:${local.region}:${data.aws_caller_identity.current.account_id}:log-group:*",
69+
]
70+
}
71+
]
5772
}
58-
}
73+
]
5974

6075
# Aliases
6176
aliases = ["one", "foo/bar"]

main.tf

+33-7
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,42 @@ data "aws_iam_policy_document" "this" {
244244
}
245245

246246
dynamic "statement" {
247-
for_each = var.key_service_principals
247+
for_each = var.key_statements
248248

249249
content {
250-
sid = statement.value.sid
251-
actions = statement.value.actions
252-
resources = statement.value.resources
250+
sid = try(statement.value.sid, null)
251+
actions = try(statement.value.actions, null)
252+
not_actions = try(statement.value.not_actions, null)
253+
effect = try(statement.value.effect, null)
254+
resources = try(statement.value.resources, null)
255+
not_resources = try(statement.value.not_resources, null)
256+
257+
dynamic "principals" {
258+
for_each = try(statement.value.principals, [])
259+
260+
content {
261+
type = principals.value.type
262+
identifiers = principals.value.identifiers
263+
}
264+
}
253265

254-
principals {
255-
type = "Service"
256-
identifiers = statement.value.principals
266+
dynamic "not_principals" {
267+
for_each = try(statement.value.not_principals, [])
268+
269+
content {
270+
type = not_principals.value.type
271+
identifiers = not_principals.value.identifiers
272+
}
273+
}
274+
275+
dynamic "condition" {
276+
for_each = try(statement.value.conditions, [])
277+
278+
content {
279+
test = condition.value.test
280+
values = condition.value.values
281+
variable = condition.value.variable
282+
}
257283
}
258284
}
259285
}

variables.tf

+7-6
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,6 @@ variable "key_users" {
110110
default = []
111111
}
112112

113-
variable "key_service_principals" {
114-
description = "A map of IAM Services for [key principals](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-services.html)"
115-
type = map(any)
116-
default = {}
117-
}
118-
119113
variable "key_service_users" {
120114
description = "A list of IAM ARNs for [key service users](https://docs.aws.amazon.com/kms/latest/developerguide/key-policy-default.html#key-policy-service-integration)"
121115
type = list(string)
@@ -146,6 +140,12 @@ variable "key_asymmetric_sign_verify_users" {
146140
default = []
147141
}
148142

143+
variable "key_statements" {
144+
description = "A map of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) for custom permission usage"
145+
type = any
146+
default = {}
147+
}
148+
149149
variable "source_policy_documents" {
150150
description = "List of IAM policy documents that are merged together into the exported document. Statements must have unique `sid`s"
151151
type = list(string)
@@ -158,6 +158,7 @@ variable "override_policy_documents" {
158158
default = []
159159
}
160160

161+
161162
################################################################################
162163
# Alias
163164
################################################################################

0 commit comments

Comments
 (0)