Skip to content

Commit d9cc321

Browse files
author
Michael Chmielewski
committed
Merge branch 'master' into tagged-resources
2 parents bc972bf + 52ef807 commit d9cc321

File tree

9 files changed

+116
-47
lines changed

9 files changed

+116
-47
lines changed

README.md

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,18 @@ The Threat Stack configuration is defined as follows:
7474
module "threatstack_aws_integration" {
7575
source = "github.com/threatstack/threatstack-terraform?ref=<integration_version>"
7676
77+
#...
78+
7779
# Strings generated from the Threat Stack Add Account page
7880
threatstack = {
7981
8082
account_id = string
8183
external_id = string
8284
8385
}
86+
87+
#...
88+
8489
}
8590
```
8691

@@ -91,24 +96,24 @@ module "threatstack_aws_integration" {
9196

9297
#### AWS configuration
9398

94-
This Terraform input variable is split into 3 sections: required settings, flag settings, and optional settings
99+
This Terraform input variable is split into 4 sections: required settings, flag settings, optional settings, and how to use an existing cloudtrail with this module.
95100

96101
##### Required settings
97102

98103
```hcl
99104
module "threatstack_aws_integration" {
100105
source = "github.com/threatstack/threatstack-terraform?ref=<integration_version>"
101106
102-
aws_account_info = {
103-
104107
# ...
105108
109+
aws_account_info = {
110+
106111
account_id = string
107112
region = string
113+
}
108114
109-
#...
115+
#...
110116
111-
}
112117
}
113118
```
114119

@@ -122,19 +127,19 @@ module "threatstack_aws_integration" {
122127
module "threatstack_aws_integration" {
123128
source = "github.com/threatstack/threatstack-terraform?ref=<integration_version>"
124129
125-
aws_flags = {
130+
# ...
126131
127-
# ...
132+
aws_flags = {
128133
129134
enable_logging = bool # Defaults to `true`
130135
enable_log_file_validation = bool # Defaults to `true`
131136
include_global_service_events = bool # Defaults to `true`
132137
is_multi_region_trail = bool # Defaults to `true`
133138
s3_force_destroy = bool # Defaults to `false`
139+
}
134140
135-
#...
141+
#...
136142
137-
}
138143
}
139144
140145
```
@@ -148,29 +153,32 @@ module "threatstack_aws_integration" {
148153

149154
* ___aws_flags.s3_force_destroy (optional):___ Bucket destroy will fail if the bucket is not empty. Set to `"true"` if you REALLY want to destroy logs on teardown.
150155

151-
152156
##### Optional settings
153157

154158
```hcl
155159
module "threatstack_aws_integration" {
156160
source = "github.com/threatstack/threatstack-terraform?ref=<integration_version>"
157161
158-
aws_optional_conf = {
162+
# ...
159163
160-
# ...
164+
aws_optional_conf = {
161165
162166
cloudtrail_name = string # Defaults to "ThreatStackIntegration"
163167
iam_role_name = string # Defaults to "ThreatStackIntegration"
164168
sns_topic_name = string # Defaults to "ThreatStackIntegration"
165169
sns_topic_display_name = string # Defaults to "Threat Stack integration topic."
166170
sqs_queue_name = string # Defaults to "ThreatStackIntegration"
167171
s3_bucket_name = string # Defaults to "threatstack-integration"
172+
<<<<<<< HEAD
168173
s3_bucket_prefix = string # Defaults to "/"
169174
tags = map # Defaults to {} (empty map)
175+
=======
176+
s3_force_destroy = string # Defaults to "/"
177+
}
178+
>>>>>>> master
170179
171-
#...
180+
#...
172181
173-
}
174182
}
175183
```
176184

@@ -190,6 +198,34 @@ module "threatstack_aws_integration" {
190198

191199
* ___aws_optional_conf.tags(optional):___ Map of tags to apply to all resources.
192200

201+
##### Using existing cloudtrail infrastructure
202+
203+
If you already have your Cloudtrail set up, with its corresponding cloudwatch log group and S3 bucket, you can configure this module to use this infrastructure by setting the following settings. The module will still set up the SQS and SNS resources required, as well as the various IAM resources to allow for the integration to talk to Threat Stack's platform.
204+
205+
> **NOTE:**
206+
> Do not define the ___existing_cloudtrail___ variable at all if you want this module to manage all of the resources for the Threat Stack integration. By default, the ___existing_cloudtrail___ variable is set to `null`
207+
208+
```hcl
209+
module "threatstack_aws_integration" {
210+
source = "github.com/threatstack/threatstack-terraform?ref=<integration_version>"
211+
212+
# ...
213+
214+
existing_cloudtrail = {
215+
216+
cloudtrail_arn = string # The ARN of the existing cloudtrail with which you want to integrate.
217+
s3_bucket_arn = string # The ARN of the existing cloudtrail's S3 bucket
218+
}
219+
220+
# ...
221+
222+
}
223+
```
224+
225+
* ___existing_cloudtrail.cloudtrail_arn (required if using existing cloudtrail):___ Only passed in so that it can be used as an output. Nothing in the integration directly refers to the existing cloudtrail itself.
226+
227+
* ___existing_cloudtrail.s3_bucket_arn (required if using existing cloudtrail):___ Used by the IAM role that links the Threat Stack account to the bucket with that contains the needed data.
228+
193229
## Outputs
194230

195231
### Exposing this module's outputs to the rest of your terraform definitions
@@ -249,6 +285,8 @@ It is recommended that these outputs be rewrapped in outputs defined in your imp
249285
> **NOTE:**
250286
> You can also see this list in this module's `outputs.tf` file.
251287
288+
> If you are defining the ___existing_cloudtrail___ block, many of these outputs will be set to `""` (an empty string).
289+
252290
* ___cloudtrail_arn:___ ARN of CloudTrail.
253291

254292
* ___cloudtrail_home_region:___ Home region for CloudTrail

aws_cloudtrail.tf

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ data "template_file" "aws_iam_cloudtrail_to_cloudwatch_policy" {
1515
}
1616

1717
resource "aws_cloudwatch_log_group" "ct" {
18+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
19+
1820
name = "/aws/cloudtrail/${var.aws_optional_conf.cloudtrail_name}"
1921
tags = var.aws_optional_conf.tags
2022

@@ -25,30 +27,35 @@ resource "aws_cloudwatch_log_group" "ct" {
2527
}
2628

2729
resource "aws_iam_role" "ct" {
30+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
31+
2832
name = "${var.aws_optional_conf.cloudtrail_name}-CloudTrailToCloudWatch"
2933
tags = var.aws_optional_conf.tags
3034

3135
assume_role_policy = data.template_file.aws_iam_cloudtrail_to_cloudwatch_assume_role_policy.rendered
3236
}
3337

3438
resource "aws_iam_role_policy" "ct" {
39+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
40+
3541
name = "CloudTrailToCloudWatch"
36-
role = aws_iam_role.ct.id
42+
role = aws_iam_role.ct[0].id
3743
policy = data.template_file.aws_iam_cloudtrail_to_cloudwatch_policy.rendered
3844
}
3945

4046
resource "aws_cloudtrail" "ct" {
47+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
48+
4149
name = var.aws_optional_conf.cloudtrail_name
4250
tags = var.aws_optional_conf.tags
4351

44-
s3_bucket_name = aws_s3_bucket.bucket.id
52+
s3_bucket_name = aws_s3_bucket.bucket[0].id
4553
enable_logging = var.aws_flags.enable_logging
4654
enable_log_file_validation = var.aws_flags.enable_log_file_validation
4755
include_global_service_events = var.aws_flags.include_global_service_events
4856
is_multi_region_trail = var.aws_flags.is_multi_region_trail
49-
cloud_watch_logs_group_arn = "${replace(aws_cloudwatch_log_group.ct.arn, "/:\\*$/", "")}:*"
50-
cloud_watch_logs_role_arn = aws_iam_role.ct.arn
57+
cloud_watch_logs_group_arn = "${replace(aws_cloudwatch_log_group.ct[0].arn, "/:\\*$/", "")}:*"
58+
cloud_watch_logs_role_arn = aws_iam_role.ct[0].arn
5159
sns_topic_name = aws_sns_topic.sns.arn
5260
depends_on = [aws_s3_bucket_policy.bucket]
5361
}
54-

aws_iam_role.tf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ data "template_file" "aws_iam_role_policy" {
1212
template = file("${path.module}/aws_iam_role_policy.tpl")
1313
vars = {
1414
sqs_queue_arn = aws_sqs_queue.sqs.arn
15-
s3_resource = "${aws_s3_bucket.bucket.arn}/*"
15+
16+
# This checks to see if a new bucket exists (null check)
17+
# If it is null, just give a null so coalesce skips it
18+
# If not null, return the arn of the bucket, which is what we really need
19+
s3_resource = coalesce((length(aws_s3_bucket.bucket) > 0 ? aws_s3_bucket.bucket[0].arn : ""), var.existing_cloudtrail.s3_bucket_arn)
1620
}
1721
}
1822

@@ -21,7 +25,6 @@ resource "aws_iam_role" "role" {
2125
tags = var.aws_optional_conf.tags
2226

2327
assume_role_policy = data.template_file.aws_iam_assume_role_policy.rendered
24-
depends_on = [aws_iam_role_policy.ct]
2528
}
2629

2730
resource "aws_iam_role_policy" "role" {

aws_s3_bucket.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
// AWS CloudTrail S3 Bucket
22
data "template_file" "aws_s3_bucket_policy" {
3+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
4+
35
template = file("${path.module}/aws_s3_bucket_policy.tpl")
46

57
vars = {
68
aws_account_id = var.aws_account_info.account_id
7-
s3_bucket_arn = aws_s3_bucket.bucket.arn
9+
s3_bucket_arn = aws_s3_bucket.bucket[0].arn
810
}
911
}
1012

1113
resource "aws_s3_bucket" "bucket" {
14+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
15+
1216
# This is to keep things consistent and prevent conflicts across
1317
# environments.
1418
bucket = var.aws_optional_conf.s3_bucket_name
@@ -25,7 +29,9 @@ resource "aws_s3_bucket" "bucket" {
2529
}
2630

2731
resource "aws_s3_bucket_policy" "bucket" {
28-
bucket = aws_s3_bucket.bucket.id
29-
policy = data.template_file.aws_s3_bucket_policy.rendered
32+
count = var.existing_cloudtrail != null ? 0 : 1 # Don't create this if using an existing cloudtrail
33+
34+
bucket = aws_s3_bucket.bucket[0].id
35+
policy = data.template_file.aws_s3_bucket_policy[0].rendered
3036
}
3137

aws_sqs_queue.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ resource "aws_sns_topic_subscription" "sqs" {
2525
endpoint = aws_sqs_queue.sqs.arn
2626
depends_on = [aws_sqs_queue_policy.sqs]
2727
}
28-

module_provider_constraints.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
terraform {
22
required_providers {
3-
aws = ">= 2.0"
4-
template = "~> 2.1"
3+
aws = ">= 2.0"
4+
template = "~> 2.1"
55
}
66
}

outputs.tf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,43 @@
1111
// }
1212

1313
output "cloudtrail_id" {
14-
value = aws_cloudtrail.ct.id
14+
value = var.existing_cloudtrail == null ? aws_cloudtrail.ct[0].id : ""
1515
}
1616

17-
output "cloudtrail_home_region" {
18-
value = aws_cloudtrail.ct.home_region
17+
output "cloudtrail_arn" {
18+
value = var.existing_cloudtrail == null ? aws_cloudtrail.ct[0].arn : var.existing_cloudtrail.cloudtrail_arn
1919
}
2020

21-
output "cloudtrail_arn" {
22-
value = aws_cloudtrail.ct.arn
21+
output "cloudtrail_home_region" {
22+
value = var.existing_cloudtrail == null ? aws_cloudtrail.ct[0].home_region : ""
2323
}
2424

2525
output "iam_role_cloudtrail_name" {
26-
value = aws_iam_role.ct.name
26+
value = var.existing_cloudtrail == null ? aws_iam_role.ct[0].name : ""
2727
}
2828

2929
output "iam_role_cloudtrail_arn" {
30-
value = aws_iam_role.ct.arn
30+
value = var.existing_cloudtrail == null ? aws_iam_role.ct[0].arn : ""
3131
}
3232

3333
output "cloudwatch_log_group_arn" {
34-
value = aws_cloudwatch_log_group.ct.arn
34+
value = var.existing_cloudtrail == null ? aws_cloudwatch_log_group.ct[0].arn : ""
3535
}
3636

37-
output "iam_role_name" {
38-
value = aws_iam_role.role.name
37+
output "s3_bucket_id" {
38+
value = var.existing_cloudtrail == null ? aws_s3_bucket.bucket[0].id : ""
3939
}
4040

41-
output "iam_role_arn" {
42-
value = aws_iam_role.role.arn
41+
output "s3_bucket_arn" {
42+
value = coalesce((length(aws_s3_bucket.bucket) > 0 ? aws_s3_bucket.bucket[0].arn : ""), var.existing_cloudtrail.s3_bucket_arn)
4343
}
4444

45-
output "s3_bucket_id" {
46-
value = aws_s3_bucket.bucket.id
45+
output "iam_role_name" {
46+
value = aws_iam_role.role.name
4747
}
4848

49-
output "s3_bucket_arn" {
50-
value = aws_s3_bucket.bucket.arn
49+
output "iam_role_arn" {
50+
value = aws_iam_role.role.arn
5151
}
5252

5353
output "sns_topic_arn" {

temp_provider.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "aws" {
2+
version = "~> 2.19"
3+
region = "us-east-1"
4+
}

variables.tf

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,28 @@ variable "threatstack" {
2424
variable "aws_account_info" {
2525
description = "(REQUIRED) AWS account settings"
2626
type = object({
27-
account_id = string
28-
region = string
27+
account_id = string
28+
region = string
2929
})
3030

3131
default = {
32-
account_id = null
33-
region = null
32+
account_id = null
33+
region = null
3434
}
3535
}
3636

37+
# Setting for integrating with an existing cloudtrail resource
38+
#
39+
variable "existing_cloudtrail" {
40+
description = "(Optional) Uses existing cloudtrail infrastructure instead of creating all new resources"
41+
type = object({
42+
cloudtrail_arn = string
43+
s3_bucket_arn = string
44+
})
45+
46+
default = null
47+
}
48+
3749
# AWS-related configuration flags (Optional)
3850
#
3951
# The flags have defaults, so the module can work without these explicitly set

0 commit comments

Comments
 (0)