-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
314 additions
and
0 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| locals { | ||
| name = var.name == null ? format("%v-%v", lookup(local._defaults["org_logging"], "name"), local.region) : var.name | ||
| kms_key_name = format("%v%v", local._prefixes["kms"], local.name) | ||
| kms_admin_root = format("arn:%v:iam::%v:root", local.partition, local.account_id) | ||
| # kms_admin_roles = compact(concat([local.kms_admin_root], var.kms_admin_roles)) | ||
| kms_admin_roles = var.kms_admin_roles | ||
| kms_policy_document = var.kms_policy_document != null ? var.kms_policy_document : data.aws_iam_policy_document.empty.json | ||
|
|
||
| } | ||
|
|
||
| resource "aws_kms_key" "key" { | ||
| description = "KMS CMK for Organization Logging" | ||
| enable_key_rotation = true | ||
| policy = data.aws_iam_policy_document.key_policy_combined.json | ||
|
|
||
| tags = merge( | ||
| local.base_tags, | ||
| var.tags, | ||
| { | ||
| "boc:aws:region" = local.region | ||
| Name = local.name | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| resource "aws_kms_alias" "key" { | ||
| name = "alias/${local.kms_key_name}" | ||
| target_key_id = aws_kms_key.key.key_id | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "key_policy_combined" { | ||
| source_policy_documents = [ | ||
| data.aws_iam_policy_document.key.json, | ||
| data.aws_iam_policy_document.key_admin.json, | ||
| local.kms_policy_document | ||
| ] | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "key_orig" { | ||
| policy_id = "Logging KMS Access" | ||
| statement { | ||
| sid = "EnableIAMUserPermissions" | ||
| effect = "Allow" | ||
| actions = ["kms:*"] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = [local.kms_admin_root] | ||
| } | ||
| } | ||
| statement { | ||
| sid = "AllowOrgLoggingEncryptLogs" | ||
| effect = "Allow" | ||
| actions = ["kms:GenerateDataKey*"] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["cloudtrail.amazonaws.com", "logs.amazonaws.com", "logs.${local.region}.amazonaws.com"] | ||
| } | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
| # values = [format("arn:%v:cloudtrail:%v:%v:trail/*",local.partition,local.region,local.account_id] | ||
| values = [format("arn:%v:cloudtrail:*:%v:trail/*", local.partition, local.account_id)] | ||
| } | ||
| } | ||
| statement { | ||
| sid = "AllowOrgLoggingKeyActivities" | ||
| effect = "Allow" | ||
| actions = [ | ||
| "kms:Describe*", | ||
| "log:AssociateKmsKey", | ||
| "log:DisassociateKmsKey" | ||
| ] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["cloudtrail.amazonaws.com", "logs.amazonaws.com", "logs.${local.region}.amazonaws.com"] | ||
| } | ||
| } | ||
| statement { | ||
| sid = "AllowPrincipalsDecryptLogFiles" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = ["*"] | ||
| } | ||
| actions = [ | ||
| "kms:Encrypt", | ||
| "kms:Decrypt", | ||
| "kms:ReEncryptFrom" | ||
| ] | ||
| resources = ["*"] | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:CallerAccount" | ||
| values = [var.account_id] | ||
| } | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
| # values = [format("arn:%v:cloudtrail:%v:%v:trail/*",local.partition,local.region,local.account_id] | ||
| values = [format("arn:%v:cloudtrail:*:%v:trail/*", local.partition, local.account_id)] | ||
| } | ||
| } | ||
| statement { | ||
| sid = "EnableCrossAccountDecryptLogFiles" | ||
| effect = "Allow" | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = ["*"] | ||
| } | ||
| actions = [ | ||
| "kms:Encrypt", | ||
| "kms:Decrypt", | ||
| "kms:ReEncryptFrom" | ||
| ] | ||
| resources = ["*"] | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:CallerAccount" | ||
| values = [var.account_id] | ||
| } | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
| # values = [format("arn:%v:cloudtrail:%v:%v:trail/*",local.partition,local.region,local.account_id] | ||
| values = [format("arn:%v:cloudtrail:*:%v:trail/*", local.partition, local.account_id)] | ||
| } | ||
| } | ||
| statement { | ||
| sid = "AllowAliasCreationDuringSetup" | ||
| effect = "Allow" | ||
| actions = ["kms:CreateAlias"] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = ["*"] | ||
| } | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:CallerAccount" | ||
| values = [var.account_id] | ||
| } | ||
| condition { | ||
| test = "StringEquals" | ||
| variable = "kms:ViaService" | ||
| values = [format("ec2.%v.amazonaws.com", local.region)] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "key_admin" { | ||
| dynamic "statement" { | ||
| for_each = length(local.kms_admin_roles) > 0 ? [1] : [] | ||
| content { | ||
| sid = "BuiltinKMSAdminRoles" | ||
| effect = "Allow" | ||
| actions = ["kms:*"] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = local.kms_admin_roles | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| data "aws_iam_policy_document" "empty" {} | ||
|
|
||
|
|
||
| #--- | ||
| # key policy for clodutrail | ||
| # https://docs.aws.amazon.com/awscloudtrail/latest/userguide/create-kms-key-policy-for-cloudtrail.html | ||
| # can't use aws_cloudtrail.this.arn as it makes for a circular reference | ||
| # | ||
| # from aws-setup-s3-object-logging | ||
| #--- | ||
| data "aws_iam_policy_document" "key" { | ||
| policy_id = "object-logging-cloud-trail" | ||
| # manage key by root and other principals | ||
| statement { | ||
| sid = "IAMPermissionsAccessKMSManagement" | ||
| effect = "Allow" | ||
| actions = ["kms:*"] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "AWS" | ||
| identifiers = [local.kms_admin_root] | ||
| } | ||
| } | ||
| # let cloudtrial, logs, sns, and sqs find key | ||
| statement { | ||
| sid = "KMSDescribeKeyFromServices" | ||
| effect = "Allow" | ||
| actions = ["kms:DescribeKey"] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["cloudtrail.amazonaws.com", "sns.amazonaws.com", "sqs.amazonaws.com"] | ||
| } | ||
| } | ||
| statement { | ||
| sid = "OrgLoggingKMSEncryptAccess" | ||
| effect = "Allow" | ||
| actions = [ | ||
| "kms:Decrypt*", | ||
| "kms:Encrypt*", | ||
| "kms:ReEncrypt*", | ||
| "kms:GenerateDataKey", | ||
| ] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["cloudtrail.amazonaws.com"] | ||
| } | ||
| # condition { | ||
| # test = "StringLike" | ||
| # variable = "kms:EncryptionContext:aws:cloudtrail:arn" | ||
| # values = [format("arn:%v:cloudtrail:*:%v:trail/*", local.partition, local.account_id)] | ||
| # } | ||
| } | ||
| # https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/encrypt-log-data-kms.html | ||
| statement { | ||
| sid = "Cloudwatch" | ||
| effect = "Allow" | ||
| actions = [ | ||
| "kms:Decrypt*", | ||
| "kms:Encrypt*", | ||
| "kms:ReEncrypt*", | ||
| "kms:GenerateDataKey*", | ||
| "kms:Describe*" | ||
| ] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["logs.amazonaws.com", "logs.${local.region}.amazonaws.com"] | ||
| } | ||
| condition { | ||
| test = "StringLike" | ||
| variable = "kms:EncryptionContext:aws:logs:arn" | ||
| values = [format("arn:%v:logs:%v:%v:log-group:*", local.partition, local.region, local.account_id)] | ||
| } | ||
| } | ||
| # https://aws.amazon.com/blogs/compute/encrypting-messages-published-to-amazon-sns-with-aws-kms/ | ||
| # https://docs.aws.amazon.com/sns/latest/dg/sns-key-management.html#sns-what-permissions-for-sse | ||
| # https://docs.aws.amazon.com/sns/latest/dg/sns-enable-encryption-for-topic-sqs-queue-subscriptions.html | ||
| statement { | ||
| sid = "ServiceMSAccess" | ||
| effect = "Allow" | ||
| actions = [ | ||
| "kms:Decrypt*", | ||
| "kms:GenerateDataKey*", | ||
| "kms:Describe*" | ||
| ] | ||
| resources = ["*"] | ||
| principals { | ||
| type = "Service" | ||
| identifiers = ["sns.amazonaws.com", "sqs.amazonaws.com"] | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # producers and consumers | ||
| # https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-key-management.html#compatibility-with-aws-services |
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,14 @@ | ||
| output "kms_key_id" { | ||
| description = "Org Logging Key ID" | ||
| value = aws_kms_key.key.id | ||
| } | ||
|
|
||
| output "kms_key_arn" { | ||
| description = "Org Logging Key ARN" | ||
| value = aws_kms_key.key.arn | ||
| } | ||
|
|
||
| output "kms_alias_name" { | ||
| description = "Org Logging Key Alias name" | ||
| value = aws_kms_alias.key.arn | ||
| } |
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,23 @@ | ||
| variable "name" { | ||
| description = "Name to apply to Org Logging KMS Key (default: k-inf-org-logging)" | ||
| type = string | ||
| default = null | ||
| } | ||
|
|
||
| variable "kms_policy_document" { | ||
| description = "AWS KMS Key Policy Document JSON, merged with admin policy document" | ||
| type = string | ||
| default = null | ||
| } | ||
|
|
||
| variable "kms_admin_roles" { | ||
| description = "AWS KMS Key administrative role(s) which have full access to the key. The root user is included by default." | ||
| type = list(string) | ||
| default = [] | ||
| } | ||
|
|
||
| variable "component_tags" { | ||
| description = "Additional tags for Components (s3, kms, ddb)" | ||
| type = map(map(string)) | ||
| default = { "s3" = {}, "kms" = {}, "ddb" = {} } | ||
| } |