-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.tf
165 lines (155 loc) · 4.27 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* ## Usage
*
* Creates an AWS GuardDuty Detector, KMS Key for encrypting exports to S3, and CloudWatch rule to watch for findings.
*
* ```hcl
* module "guardduty" {
* source = "dod-iac/guardduty/aws"
*
* tags = {
* Application = var.application
* Environment = var.environment
* Automation = "Terraform"
* }
* }
* ```
*
* You can customize the finding publishing frequency.
*
* ```hcl
* module "guardduty" {
* source = "dod-iac/guardduty/aws"
*
* enable = true
* finding_publishing_frequency = "SIX_HOURS"
* tags = {
* Application = var.application
* Environment = var.environment
* Automation = "Terraform"
* }
* }
* ```
*
* You can exports GuardDuty findings to a S3 bucket using the s3_bucket_name variable.
*
* ```hcl
* module "guardduty" {
* source = "dod-iac/guardduty/aws"
*
* enable = true
* s3_bucket_name = module.logs.aws_logs_bucket
* tags = {
* Application = var.application
* Environment = var.environment
* Automation = "Terraform"
* }
* }
* ```
*
* ## Terraform Version
*
* Terraform 0.12. Pin module version to ~> 1.0.0 . Submit pull-requests to master branch.
*
* Terraform 0.11 is not supported.
*
* ## License
*
* This project constitutes a work of the United States Government and is not subject to domestic copyright protection under 17 USC § 105. However, because the project utilizes code licensed from contributors and other third parties, it therefore is licensed under the MIT License. See LICENSE file for more information.
*/
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_iam_policy_document" "key_policy" {
policy_id = "key-consolepolicy"
statement {
sid = "Enable IAM User Permissions"
actions = [
"kms:*"
]
effect = "Allow"
principals {
type = "AWS"
identifiers = [
format(
"arn:%s:iam::%s:root",
data.aws_partition.current.partition,
data.aws_caller_identity.current.account_id
)
]
}
resources = ["*"]
}
statement {
sid = "Allow GuardDuty to use the key"
actions = [
"kms:GenerateDataKey"
]
effect = "Allow"
principals {
type = "Service"
identifiers = [
"guardduty.amazonaws.com"
]
}
resources = ["*"]
}
}
resource "aws_kms_key" "guardduty" {
description = "Key used to encrypt GuardDuty findings."
key_usage = "ENCRYPT_DECRYPT"
deletion_window_in_days = 30
policy = data.aws_iam_policy_document.key_policy.json
enable_key_rotation = true
tags = var.kms_key_tags
}
resource "aws_kms_alias" "guardduty" {
name = var.kms_alias_name
target_key_id = aws_kms_key.guardduty.key_id
}
resource "aws_guardduty_detector" "main" {
enable = var.enable
finding_publishing_frequency = var.finding_publishing_frequency
depends_on = [
aws_kms_key.guardduty,
aws_kms_alias.guardduty
]
}
data "aws_s3_bucket" "main" {
count = length(var.s3_bucket_name) > 0 ? 1 : 0
bucket = var.s3_bucket_name
}
# GuardDuty expects a folder to exist, otherwise it throws an error.
resource "aws_s3_bucket_object" "guardduty" {
count = length(var.s3_bucket_name) > 0 && length(var.s3_bucket_prefix) > 0 ? 1 : 0
bucket = data.aws_s3_bucket.main.0.id
acl = "private"
key = var.s3_bucket_prefix == "/" ? "/" : format("%s/", (
substr(var.s3_bucket_prefix, 0, 1) == "/" ?
substr(var.s3_bucket_prefix, 1, length(var.s3_bucket_prefix)) :
var.s3_bucket_prefix
))
source = "/dev/null"
}
resource "aws_guardduty_publishing_destination" "main" {
count = length(var.s3_bucket_name) > 0 ? 1 : 0
detector_id = aws_guardduty_detector.main.id
destination_arn = format("%s%s", data.aws_s3_bucket.main.0.arn, (length(var.s3_bucket_prefix) > 0 ? var.s3_bucket_prefix : "/"))
kms_key_arn = aws_kms_key.guardduty.arn
depends_on = [
aws_s3_bucket_object.guardduty
]
}
resource "aws_cloudwatch_event_rule" "guardduty_findings" {
name = "guardduty-finding-events"
description = "AWS GuardDuty event findings"
event_pattern = <<EOF
{
"detail-type": [
"GuardDuty Finding"
],
"source": [
"aws.guardduty"
]
}
EOF
}