-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
111 lines (95 loc) · 2.75 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
resource "aws_kms_key" "this" {
description = var.description
enable_key_rotation = true
deletion_window_in_days = var.deletion_window
policy = data.aws_iam_policy_document.kms_key_policy.json
tags = merge(
{
Name = local.alias_name
Alias = local.alias_name
},
local.tags
)
}
resource "aws_kms_alias" "this" {
name = "alias/${local.alias_name}"
target_key_id = aws_kms_key.this.key_id
}
data "aws_iam_policy_document" "admin_policy" {
statement {
sid = "Allow Admin" # Root user will have permissions to manage the CMK, but do not have permissions to use the CMK in cryptographic operations. - https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#cryptographic-operations
actions = [
"kms:Create*",
"kms:Describe*",
"kms:Enable*",
"kms:List*",
"kms:Put*",
"kms:Update*",
"kms:Revoke*",
"kms:Disable*",
"kms:Get*",
"kms:Delete*",
"kms:TagResource",
"kms:UntagResource",
"kms:ScheduleKeyDeletion",
"kms:CancelKeyDeletion"
]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"]
}
}
}
data "aws_iam_policy_document" "service_cryptography" {
statement {
sid = "Allow Cryptography"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:CreateGrant",
"kms:DescribeKey",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
test = "StringEquals"
variable = "kms:ViaService"
values = var.service_key_info.aws_service_names
}
condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = var.service_key_info.caller_account_ids
}
}
count = local.service_key_count
}
data "aws_iam_policy_document" "direct_cryptography" {
statement {
sid = "Allow Cryptography"
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:CreateGrant",
"kms:DescribeKey",
]
resources = ["*"]
principals {
type = "AWS"
identifiers = var.direct_key_info.allow_access_from_principals
}
}
count = 1 - local.service_key_count
}
data "aws_iam_policy_document" "kms_key_policy" {
source_policy_documents = local.service_key_count == 1 ? [data.aws_iam_policy_document.admin_policy.json, data.aws_iam_policy_document.service_cryptography[0].json] : [data.aws_iam_policy_document.admin_policy.json, data.aws_iam_policy_document.direct_cryptography[0].json]
override_policy_documents = var.additional_policies
}