-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable KMS encryption and capability of using customer-managed key (#10)
* Enable KMS encryption * Add capability of encrypting ECR repo with CMK Co-authored-by: lzrocha <[email protected]>
- Loading branch information
Showing
8 changed files
with
78 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
data "aws_caller_identity" "current" {} | ||
|
||
data "aws_region" "current" {} |
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 |
---|---|---|
|
@@ -41,4 +41,6 @@ resource "aws_ecr_repository_policy" "default" { | |
] | ||
} | ||
EOF | ||
|
||
depends_on = [aws_ecr_repository.default] | ||
} |
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,57 @@ | ||
data "aws_iam_policy_document" "kms_policy_ecr" { | ||
count = var.ecr_cmk_encryption ? 1 : 0 | ||
statement { | ||
sid = "Allow direct access to key metadata to the account" | ||
effect = "Allow" | ||
principals { | ||
type = "AWS" | ||
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"] | ||
} | ||
actions = [ | ||
"kms:*" | ||
] | ||
resources = ["*"] | ||
} | ||
statement { | ||
actions = [ | ||
"kms:Encrypt", | ||
"kms:Decrypt", | ||
"kms:ReEncrypt*", | ||
"kms:GenerateDataKey*", | ||
"kms:DescribeKey", | ||
"kms:CreateGrant", | ||
"kms:RetireGrant" | ||
] | ||
principals { | ||
type = "AWS" | ||
identifiers = ["*"] | ||
} | ||
resources = ["*"] | ||
condition { | ||
test = "StringEquals" | ||
variable = "kms:CallerAccount" | ||
values = [join(",", var.trust_accounts)] | ||
} | ||
condition { | ||
test = "StringEquals" | ||
variable = "kms:ViaService" | ||
values = ["ecr.${data.aws_region.current.name}.amazonaws.com"] | ||
} | ||
sid = "Allow access through Amazon ECR for all principals in the account that are authorized to use Amazon ECR" | ||
} | ||
} | ||
|
||
|
||
resource "aws_kms_key" "ecr" { | ||
count = var.ecr_cmk_encryption ? 1 : 0 | ||
deletion_window_in_days = 30 | ||
description = "Customer-managed key that protects ECR data" | ||
enable_key_rotation = true | ||
policy = data.aws_iam_policy_document.kms_policy_ecr[0].json | ||
} | ||
|
||
resource "aws_kms_alias" "ecr" { | ||
count = var.ecr_cmk_encryption ? 1 : 0 | ||
name = "alias/cmk/ecr" | ||
target_key_id = aws_kms_key.ecr[0].key_id | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
terraform { | ||
required_version = ">= 0.12.0" | ||
required_version = ">= 0.12.31" | ||
} |