-
Notifications
You must be signed in to change notification settings - Fork 2
/
kms.tf
124 lines (123 loc) · 3.69 KB
/
kms.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
112
113
114
115
116
117
118
119
120
121
122
123
124
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key
resource "aws_kms_key" "encryption_secret" {
enable_key_rotation = true
description = "Key to encrypt secret"
deletion_window_in_days = 7
tags = {
Name = "${var.name}-encryption-secret"
}
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias
resource "aws_kms_alias" "encryption_secret" {
name = "alias/${var.name}-encryption-secret"
target_key_id = aws_kms_key.encryption_secret.key_id
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy
resource "aws_kms_key_policy" "encryption_secret_policy" {
key_id = aws_kms_key.encryption_secret.id
policy = jsonencode({
Id = "${var.name}-encryption-secret"
Version = "2012-10-17"
Statement = [
{
Sid = "Enable IAM User Permissions"
Effect = "Allow"
Principal = {
AWS = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
}
Action = "kms:*"
Resource = "*"
},
{
Sid = "Allow access through AWS Secrets Manager for all principals in the account that are authorized to use AWS Secrets Manager"
Effect = "Allow"
Principal = {
AWS = ["*"]
}
Action = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncrypt*",
"kms:CreateGrant",
"kms:DescribeKey",
"kms:GenerateDataKey*"
]
Resource = "*"
Condition = {
StringEquals = {
"kms:CallerAccount" = "${data.aws_caller_identity.current.account_id}"
"kms:ViaService" = "secretsmanager.${var.region}.amazonaws.com"
}
}
}
]
})
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key
resource "aws_kms_key" "encryption_rest" {
enable_key_rotation = true
description = "Key to encrypt cache at rest."
deletion_window_in_days = 7
tags = {
Name = "${var.name}-encryption-rest"
}
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias
resource "aws_kms_alias" "encryption_rest" {
name = "alias/${var.name}-encryption-rest"
target_key_id = aws_kms_key.encryption_rest.key_id
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy
resource "aws_kms_key_policy" "encryption_rest_policy" {
key_id = aws_kms_key.encryption_rest.id
policy = jsonencode({
Id = "${var.name}-encryption-rest"
Statement = [
{
Action = ["kms:*"]
Effect = "Allow"
Principal = {
AWS = "${local.principal_root_arn}"
}
Resource = "*"
Sid = "Enable IAM User Permissions"
},
{
Sid = "Allow ElastiCache to use the key"
Effect = "Allow"
Principal = {
Service = "elasticache.amazonaws.com"
}
Action = [
"kms:Decrypt",
"kms:Encrypt",
"kms:GenerateDataKey",
"kms:ReEncrypt*",
"kms:CreateGrant",
"kms:DescribeKey"
]
Resource = "*"
},
{
Effect : "Allow",
Principal : {
Service : "${local.principal_logs_arn}"
},
Action : [
"kms:Encrypt*",
"kms:Decrypt*",
"kms:ReEncrypt*",
"kms:GenerateDataKey*",
"kms:Describe*"
],
Resource : "*",
Condition : {
ArnEquals : {
"kms:EncryptionContext:aws:logs:arn" : [local.slow_log_arn, local.engine_log_arn]
}
}
}
]
Version = "2012-10-17"
})
}