-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
226 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
## Requirements | ||
|
||
No requirements. | ||
|
||
## Providers | ||
|
||
| Name | Version | | ||
|------|---------| | ||
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a | | ||
|
||
## Modules | ||
|
||
No modules. | ||
|
||
## Resources | ||
|
||
| Name | Type | | ||
|------|------| | ||
| [aws_kms_alias.a](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource | | ||
| [aws_kms_key.kms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource | | ||
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source | | ||
| [aws_iam_policy_document.kms](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 | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| <a name="input_cloudtrail_name"></a> [cloudtrail\_name](#input\_cloudtrail\_name) | Cloudtrail/trail for attaching currently kms | `string` | `"cloudtrail-default"` | no | | ||
| <a name="input_kms_deletion_window_in_days"></a> [kms\_deletion\_window\_in\_days](#input\_kms\_deletion\_window\_in\_days) | The waiting period, specified in number of days. After the waiting period ends, AWS KMS deletes the KMS key. | `number` | `30` | no | | ||
| <a name="input_kms_enable_key_rotation"></a> [kms\_enable\_key\_rotation](#input\_kms\_enable\_key\_rotation) | Specifies whether key rotation is enabled. Defaults to false. | `bool` | `true` | no | | ||
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `map(any)` | n/a | yes | | ||
|
||
## Outputs | ||
|
||
| Name | Description | | ||
|------|-------------| | ||
| <a name="output_kms_arn"></a> [kms\_arn](#output\_kms\_arn) | Arn of kms created | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# This is a standard kms that frees any cloudtrail/trails from vulnerabilities. | ||
# Docs: https://dev.to/aws-builders/encrypt-cloudtrail-logs-with-multi-region-key-with-terraform-1hln | ||
|
||
locals { | ||
arn_format = "arn:${data.aws_partition.current.partition}" | ||
} | ||
|
||
data "aws_partition" "current" {} | ||
data "aws_caller_identity" "current" {} | ||
data "aws_region" "current" {} | ||
|
||
data "aws_iam_policy_document" "kms" { | ||
statement { | ||
sid = "Enable Root User Permissions" | ||
effect = "Allow" | ||
|
||
actions = [ | ||
"kms:Create*", | ||
"kms:Describe*", | ||
"kms:Enable*", | ||
"kms:List*", | ||
"kms:Put*", | ||
"kms:Update*", | ||
"kms:Revoke*", | ||
"kms:Disable*", | ||
"kms:Get*", | ||
"kms:Delete*", | ||
"kms:Tag*", | ||
"kms:Untag*", | ||
"kms:ScheduleKeyDeletion", | ||
"kms:CancelKeyDeletion" | ||
] | ||
|
||
#bridgecrew:skip=CKV_AWS_109:This policy applies only to the key it is attached to | ||
#bridgecrew:skip=CKV_AWS_111:This policy applies only to the key it is attached to | ||
resources = [ | ||
"*" | ||
] | ||
|
||
principals { | ||
type = "AWS" | ||
|
||
identifiers = [ | ||
"${local.arn_format}:iam::${data.aws_caller_identity.current.account_id}:root" | ||
] | ||
} | ||
} | ||
statement { | ||
sid = "Allow CloudTrail to encrypt - ${var.cloudtrail_name}" | ||
effect = "Allow" | ||
actions = ["kms:GenerateDataKey*"] | ||
resources = ["*"] | ||
principals { | ||
type = "Service" | ||
identifiers = ["cloudtrail.amazonaws.com"] | ||
} | ||
condition { | ||
test = "StringLike" | ||
variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
values = ["arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/${var.cloudtrail_name}"] | ||
} | ||
condition { | ||
test = "StringEquals" | ||
variable = "aws:SourceArn" | ||
values = ["arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/${var.cloudtrail_name}"] | ||
} | ||
} | ||
} | ||
|
||
resource "aws_kms_alias" "a" { | ||
name = "alias/${var.cloudtrail_name}" | ||
target_key_id = aws_kms_key.kms.key_id | ||
} | ||
|
||
resource "aws_kms_key" "kms" { | ||
description = "KMS key for cloudtrail: ${var.cloudtrail_name}" | ||
deletion_window_in_days = var.kms_deletion_window_in_days | ||
enable_key_rotation = var.kms_enable_key_rotation | ||
policy = join("", data.aws_iam_policy_document.kms.*.json) | ||
tags = var.tags | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
output "kms_arn" { | ||
description = "Arn of kms created" | ||
value = aws_kms_key.kms.arn | ||
} |
Oops, something went wrong.