Skip to content

Commit 01072c3

Browse files
afiunepiotrbPengyuanZhao
authored
feat: support providing a cloudtrail from a different account (#147)
* Support for scenario where module is installed into a different account than the cloudtrail itself. SNS option. This is a tiny tweak which allows passing in the arn of the cloudtrail in a different account, and it will allow that cloudtrail to publish into the sns topic. * Added examples * Adding the new doc for the one new input * Add script for running terraform-docs, added terraform docs config and update the README to just have terraform-docs update it in-place * fix(test): run tests and point to local module Signed-off-by: Salim Afiune Maya <afiune@lacework.net> --------- Signed-off-by: Salim Afiune Maya <afiune@lacework.net> Co-authored-by: Piotr Banasik <piotr@jane.app> Co-authored-by: Pengyuan Zhao <PengyuanZhao@users.noreply.github.com>
1 parent 26c5e30 commit 01072c3

File tree

9 files changed

+135
-5
lines changed

9 files changed

+135
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Terraform module for configuring an integration with Lacework and AWS for CloudT
9191
| <a name="input_cloudtrail_name"></a> [cloudtrail\_name](#input\_cloudtrail\_name) | The name of the CloudTrail. Required when setting use\_existing\_cloudtrail to true | `string` | `"lacework-cloudtrail"` | no |
9292
| <a name="input_consolidated_trail"></a> [consolidated\_trail](#input\_consolidated\_trail) | Set this to true to configure a consolidated cloudtrail | `bool` | `false` | no |
9393
| <a name="input_create_lacework_integration"></a> [create\_lacework\_integration](#input\_create\_lacework\_integration) | Set this to `false` if you don't want the module to automatically create a corresponding Lacework integration. | `bool` | `true` | no |
94+
| <a name="input_cross_account_cloudtrail_arn"></a> [cross\_account\_cloudtrail\_arn](#input\_cross\_account\_cloudtrail\_arn) | If using an existing CloudTrail in another account, provide the ARN here | `string` | `null` | no |
9495
| <a name="input_cross_account_policy_name"></a> [cross\_account\_policy\_name](#input\_cross\_account\_policy\_name) | n/a | `string` | `""` | no |
9596
| <a name="input_enable_cloudtrail_s3_management_events"></a> [enable\_cloudtrail\_s3\_management\_events](#input\_enable\_cloudtrail\_s3\_management\_events) | Enable CloudTrail Object-level logging | `bool` | `false` | no |
9697
| <a name="input_enable_log_file_validation"></a> [enable\_log\_file\_validation](#input\_enable\_log\_file\_validation) | Specifies whether cloudtrail log file integrity validation is enabled | `bool` | `true` | no |
@@ -144,4 +145,4 @@ Terraform module for configuring an integration with Lacework and AWS for CloudT
144145
| <a name="output_sqs_arn"></a> [sqs\_arn](#output\_sqs\_arn) | SQS Queue ARN |
145146
| <a name="output_sqs_name"></a> [sqs\_name](#output\_sqs\_name) | SQS Queue name |
146147
| <a name="output_sqs_url"></a> [sqs\_url](#output\_sqs\_url) | SQS Queue URL |
147-
<!-- END_TF_DOCS -->
148+
<!-- END_TF_DOCS -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Consolidated Existing CloudTrail in Another Account + Co-Existing with Datadog
2+
3+
This example handles a bit of a specific case.
4+
5+
1. You're following the AWS best practice to consolidate all CloudTrail logs into a single "security" account.
6+
* The CloudTrail config itself exists on the org's management account
7+
* The Bucket / KMS key exist on the security account
8+
9+
2. You're setting up LW when either transitioning from Datadog cloudtrail integration, or you're using both LW and Datadog.
10+
* The coveat here is that DD's integration expects to attach a lambda hook to the CloudTrail bucket, only one such hook can be defined on the bucket, and the DD interation can be a bit aggressive about re-claiming this hook.
11+
* This necessitates that the LW integration is done via the SNS topic the LW module sets up instead.
12+
13+
# Important bits of code
14+
15+
## Org Management Account
16+
17+
```hcl
18+
resource "aws_cloudtrail" "org-trail" {
19+
...
20+
21+
# Hook the lw cloudtrail sns topic
22+
sns_topic_name = "arn:aws:sns:us-west-2:200000000000:lw-cloudtrail"
23+
}
24+
```
25+
26+
## Security Account
27+
28+
```hcl
29+
module "aws_cloudtrail" {
30+
source = "lacework/cloudtrail/aws"
31+
version = "~> 2.8"
32+
33+
consolidated_trail = true
34+
35+
# Use an existing CloudTrail
36+
use_existing_cloudtrail = true
37+
38+
# this is arn to the existung bucket in the security account
39+
bucket_arn = "arn:aws:s3:::org-ct-bucket"
40+
41+
# this is the arn to the trail in the org management account
42+
cross_account_cloudtrail_arn = "arn:aws:cloudtrail:us-west-2:100000000000:trail/org-trail"
43+
44+
bucket_encryption_enabled = true
45+
bucket_sse_algorithm = "aws:kms"
46+
47+
# this is the arn to the kms key in the security account
48+
bucket_sse_key_arn = "arn:aws:kms:us-west-2:200000000000:key/6e2010aa-27e4-49c6-8887-956abc1caeb9"
49+
50+
# Create new SNS Topic and SQS Queue
51+
# the SNS topic is referenced in the org management account
52+
sns_topic_name = "lw-cloudtrail"
53+
sqs_queue_name = "lw-cloudtrail"
54+
55+
# Name the integration inside LW UI
56+
lacework_integration_name = "AWS Consolidated CloudTrail"
57+
}
58+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# in org management account - ID: 100000000000
2+
3+
resource "aws_cloudtrail" "org-trail" {
4+
name = "org-trail"
5+
s3_bucket_name = "org-ct-bucket"
6+
kms_key_id = "arn:aws:kms:us-west-2:200000000000:key/6e2010aa-27e4-49c6-8887-956abc1caeb9"
7+
is_multi_region_trail = true
8+
is_organization_trail = true
9+
enable_log_file_validation = true
10+
11+
# Hook the lw cloudtrail sns topic
12+
sns_topic_name = "arn:aws:sns:us-west-2:200000000000:lw-cloudtrail"
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# in a "security" account - ID: 200000000000
2+
3+
provider "lacework" {}
4+
5+
provider "aws" {
6+
region = "us-west-2"
7+
}
8+
9+
# the CT logs live in this account
10+
# Note: in order to make this co-exist with Datadog (useful for the transitional period)
11+
# datadog has to be allowed to maintain the lambda hook on the bucket, so we can't have
12+
# LW hook that (it'd be the easiest option)
13+
resource "aws_s3_bucket" "ct_bucket" {
14+
bucket = "org-ct-bucket"
15+
}
16+
17+
# and so does their KMS Key
18+
# let's assume its ARN is: arn:aws:kms:us-west-2:200000000000:key/6e2010aa-27e4-49c6-8887-956abc1caeb9
19+
resource "aws_kms_key" "ct_key" {
20+
}
21+
22+
module "aws_cloudtrail" {
23+
source = "../../"
24+
25+
consolidated_trail = true
26+
27+
# Use an existing CloudTrail
28+
use_existing_cloudtrail = true
29+
bucket_arn = "arn:aws:s3:::org-ct-bucket"
30+
cross_account_cloudtrail_arn = "arn:aws:cloudtrail:us-west-2:100000000000:trail/org-trail"
31+
32+
bucket_encryption_enabled = true
33+
bucket_sse_algorithm = "aws:kms"
34+
bucket_sse_key_arn = "arn:aws:kms:us-west-2:200000000000:key/6e2010aa-27e4-49c6-8887-956abc1caeb9"
35+
36+
# Create new SNS Topic and SQS Queue
37+
sns_topic_name = "lw-cloudtrail"
38+
sqs_queue_name = "lw-cloudtrail"
39+
40+
# Name the integration inside LW UI
41+
lacework_integration_name = "AWS Consolidated CloudTrail"
42+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# required for Terraform 13
2+
terraform {
3+
required_providers {
4+
lacework = {
5+
source = "lacework/lacework"
6+
}
7+
}
8+
}

main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ locals {
3333
bucket_encryption_enabled = var.bucket_encryption_enabled && length(local.bucket_sse_key_arn) > 0
3434
bucket_versioning_enabled = var.bucket_versioning_enabled ? "Enabled" : "Suspended"
3535
bucket_sse_key_arn = var.use_existing_kms_key ? var.bucket_sse_key_arn : ((var.use_existing_cloudtrail || length(var.bucket_sse_key_arn) > 0) ? var.bucket_sse_key_arn : aws_kms_key.lacework_kms_key[0].arn)
36+
cloudtrail_arn = var.consolidated_trail && var.use_existing_cloudtrail && var.cross_account_cloudtrail_arn != null ? var.cross_account_cloudtrail_arn : "arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/${var.cloudtrail_name}"
3637
version_file = "${abspath(path.module)}/VERSION"
3738
module_name = "terraform-aws-cloudtrail"
3839
module_version = fileexists(local.version_file) ? file(local.version_file) : ""
@@ -475,11 +476,11 @@ data "aws_iam_policy_document" "sns_topic_policy" {
475476
effect = "Allow"
476477

477478
dynamic "condition" {
478-
for_each = !var.consolidated_trail ? [1] : []
479+
for_each = (!var.consolidated_trail || var.cross_account_cloudtrail_arn != null) ? [1] : []
479480
content {
480481
test = "StringEquals"
481482
variable = "AWS:SourceArn"
482-
values = ["arn:aws:cloudtrail:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:trail/${var.cloudtrail_name}"]
483+
values = [local.cloudtrail_arn]
483484
}
484485
}
485486

scripts/ci_tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ TEST_CASES=(
1515
examples/complete-cloudtrail
1616
examples/complete-cloudtrail-access-logs-with-object-level-logging
1717
examples/cloudtrail-existing-kms-key
18-
examples/consolidated-cloudtrail-multiple-lacework-tenants
18+
examples/consolidated-existing-cloudtrail-in-another-account-datadog-co-existance
19+
examples/consolidated-cloudtrail-multiple-lacework-tenants
1920
examples/consolidated-cloudtrail
2021
examples/existing-cloudtrail-end-to-end-encryption
2122
examples/existing-cloudtrail-s3-encryption

scripts/terraform-docs.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ else
1010
echo "## terraform-docs not found in PATH, neither was docker"
1111
echo "## please install terraform-docs or docker"
1212
exit 1
13-
fi
13+
fi

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,9 @@ variable "enable_cloudtrail_s3_management_events" {
301301
default = false
302302
description = "Enable CloudTrail Object-level logging"
303303
}
304+
305+
variable "cross_account_cloudtrail_arn" {
306+
type = string
307+
default = null
308+
description = "If using an existing CloudTrail in another account, provide the ARN here"
309+
}

0 commit comments

Comments
 (0)