Skip to content

Commit 2481572

Browse files
committed
CCM-6250: init modules
1 parent 4c7d9e5 commit 2481572

55 files changed

Lines changed: 1314 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#tfsec:ignore:aws-iam-no-policy-wildcards
2+
data "aws_iam_policy_document" "admin" {
3+
policy_id = "${local.csi}-admin"
4+
5+
statement {
6+
sid = "AllowKeyAdmin"
7+
effect = "Allow"
8+
9+
actions = [
10+
"kms:Create*",
11+
"kms:Describe*",
12+
"kms:Enable*",
13+
"kms:List*",
14+
"kms:Put*",
15+
"kms:Update*",
16+
"kms:Revoke*",
17+
"kms:Disable*",
18+
"kms:Get*",
19+
"kms:Delete*",
20+
"kms:TagResource",
21+
"kms:UntagResource",
22+
"kms:ScheduleKeyDeletion",
23+
"kms:CancelKeyDeletion",
24+
]
25+
26+
resources = [
27+
aws_kms_key.main.arn,
28+
aws_kms_alias.main.arn,
29+
]
30+
}
31+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#tfsec:ignore:aws-iam-no-policy-wildcards
2+
data "aws_iam_policy_document" "user" {
3+
policy_id = "${local.csi}-user"
4+
5+
statement {
6+
sid = "AllowUseOfTheKmskey"
7+
effect = "Allow"
8+
9+
actions = [
10+
"kms:Encrypt",
11+
"kms:Decrypt",
12+
"kms:ReEncrypt*",
13+
"kms:GenerateDataKey*",
14+
"kms:DescribeKey",
15+
]
16+
17+
resources = [
18+
aws_kms_key.main.arn,
19+
]
20+
}
21+
22+
statement {
23+
sid = "AllowDelegationToAwsServiceViaGrant"
24+
effect = "Allow"
25+
26+
actions = [
27+
"kms:CreateGrant",
28+
]
29+
30+
resources = [
31+
aws_kms_key.main.arn,
32+
]
33+
34+
condition {
35+
test = "Bool"
36+
variable = "kms:GrantIsForAWSResource"
37+
38+
values = [
39+
"true",
40+
]
41+
}
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
data "aws_iam_policy_document" "key" {
2+
source_policy_documents = var.key_policy_documents
3+
4+
dynamic "statement" {
5+
for_each = var.iam_delegation ? [1] : []
6+
content {
7+
sid = "AllowFullLocalAdministration"
8+
effect = "Allow"
9+
10+
principals {
11+
type = "AWS"
12+
13+
identifiers = [
14+
"arn:aws:iam::${var.aws_account_id}:root",
15+
]
16+
}
17+
18+
actions = [
19+
"kms:*",
20+
]
21+
22+
resources = [
23+
"*",
24+
]
25+
}
26+
}
27+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Create the Key Policy for the AWS KMS Key
2+
resource "aws_iam_policy" "admin" {
3+
name = "${local.csi_account}-admin"
4+
path = "/"
5+
policy = data.aws_iam_policy_document.admin.json
6+
7+
tags = merge(
8+
local.default_tags,
9+
{
10+
Name = "${local.csi_account}-admin",
11+
},
12+
)
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Create the Key Policy for the AWS KMS Key
2+
resource "aws_iam_policy" "user" {
3+
name = "${local.csi_account}-user"
4+
path = "/"
5+
policy = data.aws_iam_policy_document.user.json
6+
7+
tags = merge(
8+
local.default_tags,
9+
{
10+
Name = "${local.csi_account}-user",
11+
},
12+
)
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resource "aws_kms_key" "main" {
2+
bypass_policy_lockout_safety_check = false
3+
deletion_window_in_days = var.deletion_window
4+
description = local.csi
5+
enable_key_rotation = true
6+
policy = data.aws_iam_policy_document.key.json
7+
tags = local.default_tags
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "aws_kms_alias" "main" {
2+
name = var.alias
3+
target_key_id = aws_kms_key.main.key_id
4+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
locals {
2+
csi = format(
3+
"%s-%s-%s-%s-%s",
4+
var.project,
5+
var.environment,
6+
var.component,
7+
var.module,
8+
var.name,
9+
)
10+
11+
# CSI for use in resources with an account namespace, eg IAM roles
12+
csi_account = replace(
13+
format(
14+
"%s-%s-%s-%s-%s-%s",
15+
var.project,
16+
var.region,
17+
var.environment,
18+
var.component,
19+
var.module,
20+
var.name,
21+
),
22+
"_",
23+
"",
24+
)
25+
26+
default_tags = merge(
27+
var.default_tags,
28+
{
29+
Module = var.module
30+
Name = local.csi
31+
},
32+
)
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
output "key_arn" {
2+
value = aws_kms_key.main.arn
3+
}
4+
5+
output "key_id" {
6+
value = aws_kms_key.main.key_id
7+
}
8+
9+
output "admin_policy_arn" {
10+
value = aws_iam_policy.admin.arn
11+
}
12+
13+
output "user_policy_arn" {
14+
value = aws_iam_policy.user.arn
15+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
##
2+
# Basic inherited variables for terraformscaffold modules
3+
##
4+
5+
variable "project" {
6+
type = string
7+
description = "The name of the terraformscaffold project calling the module"
8+
}
9+
10+
variable "environment" {
11+
type = string
12+
description = "The name of the terraformscaffold environment the module is called for"
13+
}
14+
15+
variable "component" {
16+
type = string
17+
description = "The name of the terraformscaffold component calling this module"
18+
}
19+
20+
variable "aws_account_id" {
21+
type = string
22+
description = "The AWS Account ID (numeric)"
23+
}
24+
25+
##
26+
# Module self-identification
27+
##
28+
29+
variable "module" {
30+
type = string
31+
description = "The name of this module. This is a special variable, it should be set only here and never overridden."
32+
default = "kms"
33+
}
34+
35+
##
36+
# Variable specific to the module
37+
##
38+
39+
# We presume this will always be specified. The default of {} will cause an error if a valid map is not specified.
40+
# If we ever want to define this but allow it to not be specified, then we must provide a default tag keypair will be applied
41+
# as the true default. In any other case default_tags should be removed from the module.
42+
variable "default_tags" {
43+
type = map(string)
44+
description = "Default tag map for application to all taggable resources in the module"
45+
default = {}
46+
}
47+
48+
variable "region" {
49+
type = string
50+
description = "The AWS Region"
51+
}
52+
53+
variable "name" {
54+
type = string
55+
description = "A unique name to distinguish this module invocation from others within the same CSI scope"
56+
}
57+
58+
variable "deletion_window" {
59+
type = string
60+
description = "KMS key deletion window"
61+
}
62+
63+
variable "alias" {
64+
type = string
65+
description = "Alias name for the hieradata KMS key"
66+
}
67+
68+
variable "key_policy_documents" {
69+
type = list(string)
70+
description = "List of KMS key policy JSON documents"
71+
default = []
72+
}
73+
74+
variable "iam_delegation" {
75+
type = bool
76+
description = "Whether to delegate administration of the key to the local account. Defaults to true"
77+
default = true
78+
}

0 commit comments

Comments
 (0)