-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkms-key-and-policy.tf
80 lines (76 loc) · 1.96 KB
/
kms-key-and-policy.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
data "aws_iam_policy_document" "bucket_kms_policy" {
statement {
sid = "List of principal ARNs to manage bucket key"
effect = "Allow"
principals {
type = "AWS"
identifiers = var.managing_arns
}
actions = [
"kms:*"
]
resources = ["*"]
}
statement {
sid = "List of principal ARNs to use bucket key"
effect = "Allow"
principals {
type = "AWS"
identifiers = var.principal_arns
}
actions = [
"kms:CreateAlias",
"kms:DescribeKey",
"kms:Decrypt",
"kms:DeleteAlias",
"kms:GenerateDataKey",
"kms:GetKeyPolicy",
"kms:GetKeyRotationStatus",
"kms:ListAliases",
"kms:ListResourceTags",
"kms:UpdateAlias"
]
resources = ["*"]
}
statement {
sid = "Deny key usage for everyone else"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"kms:CreateAlias",
"kms:DescribeKey",
"kms:Decrypt",
"kms:DeleteAlias",
"kms:GenerateDataKey",
"kms:GetKeyPolicy",
"kms:GetKeyRotationStatus",
"kms:ListAliases",
"kms:ListResourceTags",
"kms:UpdateAlias"
]
resources = ["*"]
condition {
test = "StringNotEqualsIfExists"
variable = "aws:PrincipalArn"
values = concat(var.managing_arns, var.principal_arns)
}
condition {
test = "StringNotEqualsIfExists"
values = ["dynamodb.amazonaws.com"]
variable = "aws:PrincipalServiceName"
}
}
}
resource "aws_kms_key" "bucket_kms_key" {
description = "This key is used to encrypt state bucket objects"
deletion_window_in_days = 7
enable_key_rotation = true
policy = data.aws_iam_policy_document.bucket_kms_policy.json
}
resource "aws_kms_alias" "bucket_kms_key_alias" {
name = "alias/${var.bucket_name}-kms-key-alias"
target_key_id = aws_kms_key.bucket_kms_key.key_id
}