Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Nov 12, 2021
1 parent 595385d commit 95f2d5f
Show file tree
Hide file tree
Showing 15 changed files with 1,037 additions and 0 deletions.
100 changes: 100 additions & 0 deletions cloudtrail_orig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# aws-inf-setup :: cloudtrail

This set up the needed components for cloudtrail in a region: S3, KMS key, SNS, SQS, cloudtrail,
cloudwatch log groups, and associated permissions. It also generates a splunk configuration to be used
for pulling cloudtrail events.

* S3 bucket
* S3 bucket policy

# Usage
Here is a simple example, the one most commonly expected to be used.

```hcl
module "cloudtrail" {
source = "git@github.e.it.census.gov:terraform-modules/aws-inf-setup.git//cloudtrail"
# account_alias = "do2-govcloud"
name = "mycloudtrail"
access_log_bucket = "myaccesslogbucket"
kms_key_management_identifiers = [ "arn:aws:iam::079788916859:role/r-inf-cloud-admin" ]
}
```

This one can be used if you need to customize stuff, though really, the defaults are all built
for a reason, and deployment code (i.e., Ansible) will expect these defaults to be used in
variable file generation.

```hcl
module "cloudtrail_full" {
# logs is generally not needed and not recommended
component_tags = {
"s3" = {
"SpecialTag1" = "something"
"SpecialTag2" = "somethingElse"
}
}
}
```

## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_cloudtrail.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudtrail) | resource |
| [aws_kms_alias.key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
| [aws_kms_key.key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
| [aws_s3_bucket.cloudtrail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_policy.cloudtrail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
| [aws_s3_bucket_policy.policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
| [aws_s3_bucket_public_access_block.cloudtrail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_public_access_block.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [null_resource.policy_delay](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [aws_arn.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/arn) | data source |
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
| [aws_iam_policy_document.cloudtrail_s3](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.key](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_iam_policy_document.policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
| [aws_region.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_access_log_bucket"></a> [access\_log\_bucket](#input\_access\_log\_bucket) | Server Access Logging Bucket ID | `string` | n/a | yes |
| <a name="input_access_log_bucket_prefix"></a> [access\_log\_bucket\_prefix](#input\_access\_log\_bucket\_prefix) | Server Access Log bucket prefix, to which the Object Logging bucket name will be appended to make the target\_prefix | `string` | `"s3"` | no |
| <a name="input_account_alias"></a> [account\_alias](#input\_account\_alias) | AWS Account Alias | `string` | `""` | no |
| <a name="input_account_id"></a> [account\_id](#input\_account\_id) | AWS Account ID (default will pull from current user) | `string` | `""` | no |
| <a name="input_cloudtrail_bucket_prefix"></a> [cloudtrail\_bucket\_prefix](#input\_cloudtrail\_bucket\_prefix) | Access log bucket prefix, to which the bucket name will be appended to make the target\_prefix | `string` | `"cloudtrail"` | no |
| <a name="input_component_tags"></a> [component\_tags](#input\_component\_tags) | Additional tags for Components (s3, kms, ddb) | `map(map(string))` | <pre>{<br> "ddb": {},<br> "kms": {},<br> "s3": {}<br>}</pre> | no |
| <a name="input_enable_sns"></a> [enable\_sns](#input\_enable\_sns) | Flag to enable or disable the creation of SNS for Cloudtrail (TBD) | `bool` | `false` | no |
| <a name="input_enable_sqs"></a> [enable\_sqs](#input\_enable\_sqs) | Flag to enable or disable the creation of SQS attached to SNS for Cloudtrail, used for Splunk ingestion (TBD) | `bool` | `false` | no |
| <a name="input_kms_key_management_identifiers"></a> [kms\_key\_management\_identifiers](#input\_kms\_key\_management\_identifiers) | AWS IAM ARNs (roles, groups, users) for full access to the created KMS Key for this bucket | `list(string)` | `[]` | no |
| <a name="input_name"></a> [name](#input\_name) | Name to apply to Cloudtrail, S3, SNS and SQS | `string` | `null` | no |
| <a name="input_object_log_bucket_prefix"></a> [object\_log\_bucket\_prefix](#input\_object\_log\_bucket\_prefix) | Access log bucket prefix, to which the bucket name will be appended to make the target\_prefix | `string` | `"s3_object_logs"` | no |
| <a name="input_override_prefixes"></a> [override\_prefixes](#input\_override\_prefixes) | Override built-in prefixes by component (efs, s3, ebs, kms, role, policy, security-group). This should be used primarily for common infrastructure things | `map(string)` | `{}` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags to apply to all resources | `map(string)` | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_kms_key_arn"></a> [kms\_key\_arn](#output\_kms\_key\_arn) | Cloudtrail Key ARN |
| <a name="output_kms_key_id"></a> [kms\_key\_id](#output\_kms\_key\_id) | Cloudtrail Key ID |
31 changes: 31 additions & 0 deletions cloudtrail_orig/data.policies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
data "aws_iam_policy_document" "cloudtrail_s3" {
statement {
sid = "AWSCloudTrailWrite"
effect = "Allow"
resources = ["${aws_s3_bucket.cloudtrail.arn}/*"]
actions = ["s3:PutObject"]

principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}

condition {
test = "StringLike"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}

statement {
sid = "AWSCloudTrailAclCheck"
effect = "Allow"
resources = [aws_s3_bucket.cloudtrail.arn]
actions = ["s3:GetBucketAcl"]

principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
}
1 change: 1 addition & 0 deletions cloudtrail_orig/data.tf
1 change: 1 addition & 0 deletions cloudtrail_orig/defaults.tf
166 changes: 166 additions & 0 deletions cloudtrail_orig/kms.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
#---
# kms key
#---
resource "aws_kms_key" "key" {
description = "Encrypt CloudTrail objects and streams"
enable_key_rotation = true
policy = data.aws_iam_policy_document.key.json

tags = merge(
local.common_tags,
map("boc:aws:region", local.region),
map("Name", var.kms_key),
)
lifecycle {
ignore_changes = [tags["boc:tf_module_version"]]
}
}

resource "aws_kms_alias" "key" {
name = "alias/${var.kms_key}"
target_key_id = aws_kms_key.key.key_id
}

output "kms_key_id" {
description = "Cloudtrail Key ID"
value = aws_kms_key.key.id
}
output "kms_key_arn" {
description = "Cloudtrail Key ARN"
value = aws_kms_key.key.arn
}

data "aws_iam_policy_document" "key" {
policy_id = "inf-cloudtrail KMS access"
statement {
sid = "EnableIAMUserPermissions"
effect = "Allow"
actions = ["kms:*"]
resources = ["*"]
principals {
type = "AWS"

identifiers = [
# data.aws_caller_identity.current.arn,
"arn:${data.aws_arn.current.partition}:iam::${var.account_id}:root",
# "arn:${data.aws_arn.current.partition}:sts::${var.account_id}:assumed-role/r-inf-cloud-admin/${var.tag_creator}",
]
}
}

statement {
sid = "AllowCloudTrailEncryptLogs"
effect = "Allow"
actions = ["kms:GenerateDataKey*"]
resources = ["*"]

principals {
type = "Service"
# identifiers = ["cloudtrail.amazonaws.com"]
identifiers = ["cloudtrail.amazonaws.com", "logs.amazonaws.com", "logs.${local.region}.amazonaws.com"]
}

condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_arn.current.partition}:cloudtrail:*:${var.account_id}:trail/*"]
}
}

statement {
sid = "AllowCloudTrailKeyActivities"
effect = "Allow"
actions = [
"kms:Describe*",
"log:AssociateKmsKey",
"log:DisassociateKmsKey"
]
resources = ["*"]

principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com", "logs.amazonaws.com", "logs.${local.region}.amazonaws.com"]
}
}

statement {
sid = "AllowPrincipalsDecryptLogFiles"
effect = "Allow"

principals {
type = "AWS"
identifiers = ["*"]
}
actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncryptFrom"
]
resources = ["*"]

condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [var.account_id]
}

condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_arn.current.partition}:cloudtrail:*:${var.account_id}:trail/*"]
}
}

statement {
sid = "EnableCrossAccountDecryptLogFiles"
effect = "Allow"

principals {
type = "AWS"
identifiers = ["*"]
}

actions = [
"kms:Encrypt",
"kms:Decrypt",
"kms:ReEncryptFrom"
]
resources = ["*"]

condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [var.account_id]
}

condition {
test = "StringLike"
variable = "kms:EncryptionContext:aws:cloudtrail:arn"
values = ["arn:${data.aws_arn.current.partition}:cloudtrail:*:${var.account_id}:trail/*"]
}
}

statement {
sid = "AllowAliasCreationDuringSetup"
effect = "Allow"
actions = ["kms:CreateAlias"]
resources = ["*"]

principals {
type = "AWS"
identifiers = ["*"]
}

condition {
test = "StringEquals"
variable = "kms:CallerAccount"
values = [var.account_id]
}

condition {
test = "StringEquals"
variable = "kms:ViaService"
values = ["ec2.${local.region}.amazonaws.com}"]
}
}
}
6 changes: 6 additions & 0 deletions cloudtrail_orig/m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
*
* cloudtrail
* sqs, dead letter
* sns
*
Loading

0 comments on commit 95f2d5f

Please sign in to comment.