-
-
Couldn't load subscription status.
- Fork 225
feat: updates kms comp with embedded policy creation #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5549544
d5bf21f
b77fbe0
0409e70
25ee464
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,80 @@ | ||||||||||
| locals { | ||||||||||
| account_id = data.aws_caller_identity.current.account_id | ||||||||||
| account_principal = "arn:aws:iam::${local.account_id}:root" | ||||||||||
|
|
||||||||||
| administration_principals = [ | ||||||||||
| local.account_principal | ||||||||||
| ] | ||||||||||
|
|
||||||||||
|
Comment on lines
+5
to
+8
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This local doesn't seem very useful
Suggested change
|
||||||||||
| principals = sort(distinct(concat( | ||||||||||
| var.allowed_principal_arns, | ||||||||||
| module.allowed_role_map.principals, | ||||||||||
| ))) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| data "aws_caller_identity" "current" {} | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
|
||||||||||
| module "allowed_role_map" { | ||||||||||
| source = "../account-map/modules/roles-to-principals" | ||||||||||
|
|
||||||||||
| privileged = false | ||||||||||
| role_map = var.allowed_roles | ||||||||||
|
|
||||||||||
| context = module.this.context | ||||||||||
| } | ||||||||||
|
|
||||||||||
| module "kms_key" { | ||||||||||
| source = "cloudposse/kms-key/aws" | ||||||||||
| version = "0.12.1" | ||||||||||
|
|
||||||||||
| description = var.description | ||||||||||
| alias = var.alias == null ? "alias/${module.this.id}" : var.alias | ||||||||||
| description = var.description == null ? "${module.this.id} KMS Key. Managed by Terraform." : var.description | ||||||||||
| deletion_window_in_days = var.deletion_window_in_days | ||||||||||
| enable_key_rotation = var.enable_key_rotation | ||||||||||
| alias = var.alias | ||||||||||
| policy = var.policy | ||||||||||
| key_usage = var.key_usage | ||||||||||
| customer_master_key_spec = var.customer_master_key_spec | ||||||||||
| multi_region = var.multi_region | ||||||||||
|
|
||||||||||
| policy = var.policy != "" ? var.policy : data.aws_iam_policy_document.key_policy.json | ||||||||||
|
|
||||||||||
| context = module.this.context | ||||||||||
| } | ||||||||||
|
|
||||||||||
| data "aws_iam_policy_document" "key_policy" { | ||||||||||
goruha marked this conversation as resolved.
Show resolved
Hide resolved
goruha marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
|
|
||||||||||
| statement { | ||||||||||
| sid = "KeyAdministration" | ||||||||||
| effect = "Allow" | ||||||||||
|
|
||||||||||
| actions = [ | ||||||||||
| "kms:*", | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| resources = ["*"] | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to allow overriding the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
|
|
||||||||||
| principals { | ||||||||||
| type = "AWS" | ||||||||||
| identifiers = local.administration_principals | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| statement { | ||||||||||
| sid = "KeyUsage" | ||||||||||
| effect = "Allow" | ||||||||||
|
|
||||||||||
| actions = [ | ||||||||||
| "kms:Encrypt", | ||||||||||
| "kms:Decrypt", | ||||||||||
| "kms:ReEncrypt*", | ||||||||||
| "kms:GenerateDataKey*", | ||||||||||
| "kms:DescribeKey", | ||||||||||
| ] | ||||||||||
|
|
||||||||||
| resources = ["*"] | ||||||||||
|
|
||||||||||
| principals { | ||||||||||
| type = "AWS" | ||||||||||
| identifiers = local.principals | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of hard coding this policy here, can we utilize this module and expose the inputs as variables so we can be as flexible as possible from the YAML? https://github.com/cloudposse/terraform-aws-iam-policy e.g. components:
terraform:
kms:
vars:
policy:
iam_policy_statements:
KeyAdministration:
effect: "Allow"
actions:
- "kms:*"
resources:
- "*"
principals:
type: "AWS"
# This may be something we can contribute to the upstream module to dynamically fill these %s in
# or it can be added by the HCL for this component
identifiers:
- "arn:%s:iam::%s:root"
KeyUsage:
effect: "Allow"
actions:
- "kms:Encrypt"
- "kms:Decrypt"
- "kms:ReEncrypt*"
- "kms:GenerateDataKey*"
- "kms:DescribeKey"
resources:
- "*"
principals:
type: "AWS"
# Same here, these can be provided by the HCL unless this `identifiers` key is provided
identifiers:
- "..."There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this is totally valid, it's non-trivial and others haven't done this work / needed this yet, so to move this PR along I'm going to consider this out of scope. If we decide we need this to merge, I can make the changes necessary 👍 |
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,18 @@ variable "region" { | |
| description = "AWS Region" | ||
| } | ||
|
|
||
| variable "alias" { | ||
| type = string | ||
| description = "The display name of the alias. The name must start with the word alias followed by a forward slash. If not specified, the alias name will be auto-generated." | ||
| default = null | ||
| } | ||
|
|
||
| variable "description" { | ||
| type = string | ||
| description = "The description for the KMS Key." | ||
| default = null | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the description isn't set to the original value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure honestly as it has been a long time, but updated regardless 😅 |
||
| } | ||
|
|
||
| variable "deletion_window_in_days" { | ||
| type = number | ||
| default = 10 | ||
|
|
@@ -15,16 +27,22 @@ variable "enable_key_rotation" { | |
| description = "Specifies whether key rotation is enabled" | ||
| } | ||
|
|
||
| variable "description" { | ||
| variable "key_usage" { | ||
| type = string | ||
| default = "Parameter Store KMS master key" | ||
| description = "The description of the key as viewed in AWS console" | ||
| default = "ENCRYPT_DECRYPT" | ||
| description = "Specifies the intended use of the key. Valid values: `ENCRYPT_DECRYPT` or `SIGN_VERIFY`." | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a validation rule here or is this already done by the upstream module? |
||
| } | ||
|
|
||
| variable "alias" { | ||
| variable "multi_region" { | ||
| type = bool | ||
| default = false | ||
| description = "Indicates whether the KMS key is a multi-Region (true) or regional (false) key." | ||
| } | ||
|
|
||
| variable "customer_master_key_spec" { | ||
| type = string | ||
| default = "" | ||
| description = "The display name of the alias. The name must start with the word `alias` followed by a forward slash. If not specified, the alias name will be auto-generated." | ||
| default = "SYMMETRIC_DEFAULT" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a breaking change or is this the normal default and we're just being explicit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the downstream child module default. I believe all is good on this one, not a breaking change. I've added validation. |
||
| description = "Specifies whether the key contains a symmetric key or an asymmetric key pair and the encryption algorithms or signing algorithms that the key supports. Valid values: `SYMMETRIC_DEFAULT`, `RSA_2048`, `RSA_3072`, `RSA_4096`, `ECC_NIST_P256`, `ECC_NIST_P384`, `ECC_NIST_P521`, or `ECC_SECG_P256K1`." | ||
| } | ||
|
|
||
| variable "policy" { | ||
|
|
@@ -33,20 +51,17 @@ variable "policy" { | |
| description = "A valid KMS policy JSON document. Note that if the policy document is not specific enough (but still valid), Terraform may view the policy as constantly changing in a terraform plan. In this case, please make sure you use the verbose/specific version of the policy." | ||
| } | ||
|
|
||
| variable "key_usage" { | ||
| type = string | ||
| default = "ENCRYPT_DECRYPT" | ||
| description = "Specifies the intended use of the key. Valid values: `ENCRYPT_DECRYPT` or `SIGN_VERIFY`." | ||
| } | ||
|
|
||
| variable "customer_master_key_spec" { | ||
| type = string | ||
| default = "SYMMETRIC_DEFAULT" | ||
| description = "Specifies whether the key contains a symmetric key or an asymmetric key pair and the encryption algorithms or signing algorithms that the key supports. Valid values: `SYMMETRIC_DEFAULT`, `RSA_2048`, `RSA_3072`, `RSA_4096`, `ECC_NIST_P256`, `ECC_NIST_P384`, `ECC_NIST_P521`, or `ECC_SECG_P256K1`." | ||
| variable "allowed_roles" { | ||
| type = map(list(string)) | ||
| description = <<-EOT | ||
| Map of account:[role, role...] specifying roles allowed to assume the role. | ||
| Roles are symbolic names like `ops` or `terraform`. Use `*` as role for entire account. | ||
| EOT | ||
| default = {} | ||
| } | ||
|
|
||
| variable "multi_region" { | ||
| type = bool | ||
| default = false | ||
| description = "Indicates whether the KMS key is a multi-Region (true) or regional (false) key." | ||
| variable "allowed_principal_arns" { | ||
| type = list(string) | ||
| description = "List of AWS principal ARNs allowed to assume the role." | ||
| default = [] | ||
| } | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the data source here for govcloud
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition