Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pull-request-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ jobs:
contents: read
issues: write
pull-requests: write
uses: launchbynttdata/launch-workflows/.github/workflows/reusable-pr-label-by-branch.yml@0.8.0
uses: launchbynttdata/launch-workflows/.github/workflows/reusable-pr-label-by-branch.yml@0.11.0
secrets: inherit # pragma: allowlist secret
2 changes: 1 addition & 1 deletion .github/workflows/pull-request-terraform-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
permissions:
contents: read
id-token: write
uses: launchbynttdata/launch-workflows/.github/workflows/reusable-terraform-check-aws.yml@0.8.0
uses: launchbynttdata/launch-workflows/.github/workflows/reusable-terraform-check-aws.yml@0.11.0
with:
assume_role_arn: ${{ vars.TERRAFORM_CHECK_AWS_ASSUME_ROLE_ARN }}
region: ${{ vars.TERRAFORM_CHECK_AWS_REGION }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ jobs:
permissions:
contents: write
pull-requests: write
uses: launchbynttdata/launch-workflows/.github/workflows/reusable-release-on-merge.yml@0.8.0
uses: launchbynttdata/launch-workflows/.github/workflows/reusable-release-on-merge.yml@0.11.0
secrets: inherit # pragma: allowlist secret
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ terraform.*
.repo/
components/
.semverbot.toml
.tflint.hcl
.golangci.yaml

.idea
!examples/*.tfvars
Expand Down Expand Up @@ -59,10 +57,7 @@ terraform.rc
# Files from common modules
azure_env.sh
.releaserc.json
.tflint.hcl

# Pre-commit hook
.pre-commit-config.yaml

# VS Code
.vscode/
Expand All @@ -75,3 +70,7 @@ azure_env.sh
**/*.egg-info

vendor/

.envrc
.env.local
.env
5 changes: 5 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "2"
run:
# Timeout for analysis, e.g. 30s, 5m.
timeout: 5m
allow-parallel-runners: true
61 changes: 61 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: forbidden-files
name: forbidden files
entry:
found Copier update rejection files; review and remove them before
merging.
language: fail
files: "\\.rej$"
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: check-json
- id: check-merge-conflict
args: [--assume-in-merge]
- id: check-shebang-scripts-are-executable
- id: check-yaml
args:
- --allow-multiple-documents
- id: end-of-file-fixer
- id: mixed-line-ending
args:
- --fix=auto
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.99.0
hooks:
- id: terraform_fmt
- id: terraform_validate
- id: terraform_docs
args:
- --hook-config=--path-to-file=README.md
- --hook-config=--add-to-existing-file=true
- --hook-config=--create-file-if-not-exist=true
- --args=--sort=false
- repo: https://github.com/golangci/golangci-lint
rev: v2.6.2
hooks:
- id: golangci-lint
name: golangci-lint
description: Fast linters runner for Go.
entry: golangci-lint run --fix
types: [go]
language: golang
pass_filenames: false
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
rev: v9.22.0
hooks:
- id: commitlint
stages: [commit-msg]
additional_dependencies: ["@commitlint/config-conventional"]
- repo: https://github.com/Yelp/detect-secrets
rev: v1.5.0
hooks:
- id: detect-secrets
args: ["--baseline", ".secrets.baseline"]
exclude: package.lock.json
4 changes: 4 additions & 0 deletions .tflint.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
plugin "terraform" {
enabled = true
preset = "recommended"
}
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
conftest 0.56.0
golang 1.24.2
golang 1.24.10
golangci-lint 2.2.1
pre-commit 4.2.0
regula 3.2.1 # https://github.com/launchbynttdata/asdf-regula
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_key_id"></a> [key\_id](#input\_key\_id) | (Required) The ID of the KMS Key to attach the policy. | `string` | n/a | yes |
| <a name="input_policy"></a> [policy](#input\_policy) | A JSON-formatted string that represents the key policy to attach to the KMS key. | <pre>map(object({<br/> sid = string<br/> effect = string<br/> principals = map(list(string))<br/> actions = list(string)<br/> resources = list(string)<br/> }))</pre> | `null` | no |
| <a name="input_policy"></a> [policy](#input\_policy) | A JSON-formatted string that represents the key policy to attach to the KMS key. | <pre>map(object({<br/> sid = string<br/> effect = string<br/> principals = map(list(string))<br/> actions = list(string)<br/> resources = list(string)<br/> condition = optional(list(object({<br/> test = string<br/> variable = string<br/> values = list(string)<br/> })))<br/> }))</pre> | n/a | yes |
| <a name="input_bypass_policy_lockout_safety_check"></a> [bypass\_policy\_lockout\_safety\_check](#input\_bypass\_policy\_lockout\_safety\_check) | (Optional) A boolean flag to indicate whether to bypass the KMS key policy lockout safety check. Defaults to false. | `bool` | `false` | no |

## Outputs
Expand Down
7 changes: 6 additions & 1 deletion examples/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@

| Name | Source | Version |
|------|--------|---------|
| <a name="module_kms_key"></a> [kms\_key](#module\_kms\_key) | terraform.registry.launch.nttdata.com/module_primitive/kms_key/aws | ~> 0.1 |
| <a name="module_kms_key_policy"></a> [kms\_key\_policy](#module\_kms\_key\_policy) | ../../ | n/a |

## Resources

| Name | Type |
|------|------|
| [aws_kms_key.example](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_roles.administrator_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_roles) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

Expand All @@ -38,4 +40,7 @@
| Name | Description |
|------|-------------|
| <a name="output_policy_id"></a> [policy\_id](#output\_policy\_id) | The ID of the KMS Key Policy resource. |
| <a name="output_kms_key_region"></a> [kms\_key\_region](#output\_kms\_key\_region) | Region where the KMS key and policy are managed. |
| <a name="output_key_id"></a> [key\_id](#output\_key\_id) | The ID of the KMS Key associated with the policy. |
| <a name="output_key_arn"></a> [key\_arn](#output\_key\_arn) | The ARN of the KMS Key associated with the policy. |
<!-- END_TF_DOCS -->
30 changes: 27 additions & 3 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
data "aws_caller_identity" "current" {}

data "aws_region" "current" {}

data "aws_iam_roles" "administrator_access" {
path_prefix = "/aws-reserved/sso.amazonaws.com/"
name_regex = "^AWSReservedSSO_AdministratorAccess_.*$"
}

locals {
## Construct the policy to include the current account root as a principal. Useful for testing. Not useful in production.
policy = {
Expand All @@ -14,16 +21,33 @@ locals {
"AWS" = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
}
"AllowAccountKeyManagement" = {
sid = "AllowAccountKeyManagement"
effect = "Allow"
actions = [
"kms:*"
]
resources = ["arn:aws:kms:*:${data.aws_caller_identity.current.account_id}:key/*"]
principals = {
"AWS" = concat(
tolist(data.aws_iam_roles.administrator_access.arns),
["arn:aws:iam::020127659860:role/github-actions-deploy-role-terraform"],
)
}
}
}
}

resource "aws_kms_key" "example" {
module "kms_key" {
# checkov:skip=CKV_TF_1: trusted registry source
source = "terraform.registry.launch.nttdata.com/module_primitive/kms_key/aws"
version = "~> 0.1"
description = "Terratest KMS Key"
key_usage = "ENCRYPT_DECRYPT"
policy = ""
enable_key_rotation = true
rotation_period_in_days = 365
multi_region = false
deletion_window_in_days = 7
tags = {
"Environment" = "Test"
}
Expand All @@ -32,7 +56,7 @@ resource "aws_kms_key" "example" {
module "kms_key_policy" {
source = "../../"

key_id = aws_kms_key.example.key_id
key_id = module.kms_key.key_id
policy = local.policy
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check

Expand Down
15 changes: 15 additions & 0 deletions examples/simple/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@ output "policy_id" {
description = "The ID of the KMS Key Policy resource."
value = module.kms_key_policy.id
}

output "kms_key_region" {
description = "Region where the KMS key and policy are managed."
value = data.aws_region.current.name
}

output "key_id" {
description = "The ID of the KMS Key associated with the policy."
value = module.kms_key.key_id
}

output "key_arn" {
description = "The ARN of the KMS Key associated with the policy."
value = module.kms_key.arn
}
46 changes: 46 additions & 0 deletions examples/with_condition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# simple
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# simple
# with_condition


<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.100.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_kms_key"></a> [kms\_key](#module\_kms\_key) | terraform.registry.launch.nttdata.com/module_primitive/kms_key/aws | ~> 0.1 |
| <a name="module_kms_key_policy"></a> [kms\_key\_policy](#module\_kms\_key\_policy) | ../../ | n/a |

## Resources

| Name | Type |
|------|------|
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_roles.administrator_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_roles) | 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_bypass_policy_lockout_safety_check"></a> [bypass\_policy\_lockout\_safety\_check](#input\_bypass\_policy\_lockout\_safety\_check) | (Optional) A boolean flag to indicate whether to bypass the KMS key policy lockout safety check. Defaults to false. | `bool` | `false` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_policy_id"></a> [policy\_id](#output\_policy\_id) | The ID of the KMS Key Policy resource. |
| <a name="output_kms_key_region"></a> [kms\_key\_region](#output\_kms\_key\_region) | Region where the KMS key and policy are managed. |
| <a name="output_key_id"></a> [key\_id](#output\_key\_id) | The ID of the KMS Key associated with the policy. |
| <a name="output_key_arn"></a> [key\_arn](#output\_key\_arn) | The ARN of the KMS Key associated with the policy. |
<!-- END_TF_DOCS -->
85 changes: 85 additions & 0 deletions examples/with_condition/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
data "aws_caller_identity" "current" {}

data "aws_region" "current" {}

data "aws_iam_roles" "administrator_access" {
path_prefix = "/aws-reserved/sso.amazonaws.com/"
name_regex = "^AWSReservedSSO_AdministratorAccess_.*$"
}

locals {
## Construct the policy to include the current account root as a principal. Useful for testing. Not useful in production.
policy = {
"EnableIAMUserPermissions" = {
sid = "EnableIAMUserPermissions"
effect = "Allow"
actions = [
"kms:*"
]
resources = ["*"]
principals = {
"AWS" = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
condition = [
{
test = "ArnEquals"
variable = "aws:PrincipalArn"
values = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
]
}
"AllowAccountKeyManagement" = {
sid = "AllowAccountKeyManagement"
effect = "Allow"
actions = [
"kms:*"
]
resources = ["arn:aws:kms:*:${data.aws_caller_identity.current.account_id}:key/*"]
principals = {
"AWS" = concat(
tolist(data.aws_iam_roles.administrator_access.arns),
["arn:aws:iam::020127659860:role/github-actions-deploy-role-terraform"],
)
}
condition = [
{
test = "StringEquals"
variable = "aws:PrincipalAccount"
values = [data.aws_caller_identity.current.account_id]
},
{
test = "ArnEquals"
variable = "aws:PrincipalArn"
values = concat(
tolist(data.aws_iam_roles.administrator_access.arns),
["arn:aws:iam::020127659860:role/github-actions-deploy-role-terraform"],
)
}
]
}
}
}

module "kms_key" {
# checkov:skip=CKV_TF_1: trusted registry source
source = "terraform.registry.launch.nttdata.com/module_primitive/kms_key/aws"
version = "~> 0.1"
description = "Terratest KMS Key"
key_usage = "ENCRYPT_DECRYPT"
enable_key_rotation = true
rotation_period_in_days = 365
multi_region = true
deletion_window_in_days = 7
tags = {
"Environment" = "Test"
}
}

module "kms_key_policy" {
source = "../../"

key_id = module.kms_key.key_id
policy = local.policy
bypass_policy_lockout_safety_check = var.bypass_policy_lockout_safety_check

}
19 changes: 19 additions & 0 deletions examples/with_condition/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
output "policy_id" {
description = "The ID of the KMS Key Policy resource."
value = module.kms_key_policy.id
}

output "kms_key_region" {
description = "Region where the KMS key and policy are managed."
value = data.aws_region.current.name
}

output "key_id" {
description = "The ID of the KMS Key associated with the policy."
value = module.kms_key.key_id
}

output "key_arn" {
description = "The ARN of the KMS Key associated with the policy."
value = module.kms_key.arn
}
1 change: 1 addition & 0 deletions examples/with_condition/test.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bypass_policy_lockout_safety_check = false
Loading
Loading