Skip to content

Commit

Permalink
Merge pull request #3 from cloudposse/feature/initial-module-implemen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
mcalhoun committed Nov 13, 2020
1 parent de690fc commit e6e6240
Show file tree
Hide file tree
Showing 18 changed files with 351 additions and 176 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/auto-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ name: auto-release
on:
push:
branches:
- master
- master

jobs:
semver:
runs-on: ubuntu-latest
steps:
# Drafts your next Release notes as Pull Requests are merged into "master"
- uses: release-drafter/release-drafter@v5
with:
publish: true
prerelease: false
config-name: auto-release.yml
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Drafts your next Release notes as Pull Requests are merged into "master"
- uses: release-drafter/release-drafter@v5
with:
publish: true
prerelease: false
config-name: auto-release.yml
env:
GITHUB_TOKEN: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }}
117 changes: 68 additions & 49 deletions README.md

Large diffs are not rendered by default.

43 changes: 28 additions & 15 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#

# Name of this project
name: terraform-example-module
name: terraform-aws-security-hub

# Logo for this project
#logo: docs/logo.png
Expand All @@ -20,13 +20,13 @@ copyrights:
year: "2020"

# Canonical GitHub repo
github_repo: cloudposse/terraform-example-module
github_repo: cloudposse/terraform-aws-security-hub

# Badges to display
badges:
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-example-module.svg"
url: "https://github.com/cloudposse/terraform-example-module/releases/latest"
image: "https://img.shields.io/github/release/cloudposse/terraform-aws-security-hub.svg"
url: "https://github.com/cloudposse/terraform-aws-security-hub/releases/latest"
- name: "Slack Community"
image: "https://slack.cloudposse.com/badge.svg"
url: "https://slack.cloudposse.com"
Expand Down Expand Up @@ -57,28 +57,41 @@ references:

# Short description of this project
description: |-
This is `terraform-example-module` project provides all the scaffolding for a typical well-built Cloud Posse module. It's a template repository you can
use when creating new repositories.
Terraform module to deploy [AWS Security Hub](https://aws.amazon.com/security-hub/).
# Introduction to the project
#introduction: |-
# This is an introduction.
introduction: |-
This module enables AWS Security Hub in one region of one account and optionally sets up an SNS topic to receive
notifications of its findings.
# How to use this module. Should be an easy example to copy and paste.
usage: |-
Here's how to invoke this example module in your projects
For a complete example, see [examples/complete](examples/complete).
For automated tests of the complete example using [bats](https://github.com/bats-core/bats-core) and [Terratest](https://github.com/gruntwork-io/terratest) (which tests and deploys the example on AWS), see [test](test).
Here's how to invoke this module in your projects:
```hcl
module "example" {
source = "https://github.com/cloudposse/terraform-example-module.git?ref=master"
example = "Hello world!"
module "securityhub" {
source = "cloudposse/security-hub/aws"
create_sns_topic = true
subscribers = {
opsgenie = {
protocol = "https"
endpoint = "https://api.example.com/v1/"
endpoint_auto_confirms = true
}
}
}
```
# Example usage
examples: |-
Here is an example of using this module:
- [`examples/complete`](https://github.com/cloudposse/terraform-example-module/) - complete example of using this module
- [`examples/complete`](https://github.com/cloudposse/terraform-aws-security-hub/) - complete example of using this module
# How to get started quickly
#quickstart: |-
Expand All @@ -91,5 +104,5 @@ include:

# Contributors to this project
contributors:
- name: "Erik Osterman"
github: "osterman"
- name: "Matt Calhoun"
github: "mcalhoun"
3 changes: 2 additions & 1 deletion context.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#

module "this" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
source = "cloudposse/label/null"
version = "0.21.0"

enabled = var.enabled
namespace = var.namespace
Expand Down
19 changes: 11 additions & 8 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,44 @@

| Name | Version |
|------|---------|
| terraform | >= 0.12.0, < 0.14.0 |
| local | ~> 1.2 |
| random | ~> 2.2 |
| terraform | >= 0.13.0 |
| aws | >= 2 |

## Providers

| Name | Version |
|------|---------|
| random | ~> 2.2 |
| aws | >= 2 |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| additional\_tag\_map | Additional tags for appending to tags\_as\_list\_of\_maps. Not added to `tags`. | `map(string)` | `{}` | no |
| attributes | Additional attributes (e.g. `1`) | `list(string)` | `[]` | no |
| cloudwatch\_event\_rule\_pattern\_detail\_type | The detail-type pattern used to match events that will be sent to SNS. <br><br>For more information, see:<br>https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/CloudWatchEventsandEventPatterns.html | `string` | `"Security Hub Findings - Imported"` | no |
| context | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | <pre>object({<br> enabled = bool<br> namespace = string<br> environment = string<br> stage = string<br> name = string<br> delimiter = string<br> attributes = list(string)<br> tags = map(string)<br> additional_tag_map = map(string)<br> regex_replace_chars = string<br> label_order = list(string)<br> id_length_limit = number<br> })</pre> | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_order": [],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {}<br>}</pre> | no |
| create\_sns\_topic | Flag to indicate whether an SNS topic should be created for notifications<br><br>If you want to send findings to a new SNS topic, set this to true and provide a valid configuration for subscribers | `bool` | `false` | no |
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
| enabled | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
| enabled\_standards | A list of standards/rulesets to enable<br><br>See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/securityhub_standards_subscription#argument-reference<br><br>The possible values are:<br><br> - standards/aws-foundational-security-best-practices/v/1.0.0<br> - ruleset/cis-aws-foundations-benchmark/v/1.2.0<br> - standards/pci-dss/v/3.2.1 | `list` | `[]` | no |
| environment | Environment, e.g. 'uw2', 'us-west-2', OR 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
| example | Example variable | `string` | `"hello world"` | no |
| id\_length\_limit | Limit `id` to this many characters.<br>Set to `0` for unlimited length.<br>Set to `null` for default, which is `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
| imported\_findings\_notification\_arn | The ARN for an SNS topic to send findings notifications to. This is only used if create\_sns\_topic is false.<br><br>If you want to send findings to an existing SNS topic, set the value of this to the ARN of the existing topic and set <br>create\_sns\_topic to false. | `string` | `null` | no |
| label\_order | The naming order of the id output and Name tag.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 5 elements, but at least one must be present. | `list(string)` | `null` | no |
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `null` | no |
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `null` | no |
| regex\_replace\_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| subscribers | A map of subscription configurations for SNS topics<br><br>For more information, see:<br>https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription#argument-reference<br> <br>protocol: <br> The protocol to use. The possible values for this are: sqs, sms, lambda, application. (http or https are partially <br> supported, see link) (email is an option but is unsupported in terraform, see link).<br>endpoint: <br> The endpoint to send data to, the contents will vary with the protocol. (see link for more information)<br>endpoint\_auto\_confirms:<br> Boolean indicating whether the end point is capable of auto confirming subscription e.g., PagerDuty. Default is <br> false | <pre>map(object({<br> protocol = string<br> endpoint = string<br> endpoint_auto_confirms = bool<br> }))</pre> | `{}` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | `map(string)` | `{}` | no |

## Outputs

| Name | Description |
|------|-------------|
| example | Example output |
| id | ID of the created example |
| random | Stable random number for this example |
| enabled\_subscriptions | A list of subscriptions that have been enabled |
| sns\_topic | The SNS topic that was created |
| sns\_topic\_subscriptions | The SNS topic that was created |

<!-- markdownlint-restore -->
3 changes: 2 additions & 1 deletion examples/complete/context.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#

module "this" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.19.2"
source = "cloudposse/label/null"
version = "0.21.0"

enabled = var.enabled
namespace = var.namespace
Expand Down
13 changes: 5 additions & 8 deletions examples/complete/fixtures.us-east-2.tfvars
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
region = "us-east-2"

namespace = "eg"

region = "us-east-2"
namespace = "eg"
environment = "ue2"
stage = "test"

stage = "test"

name = "example"

create_sns_topic = true
enabled_standards = ["standards/aws-foundational-security-best-practices/v/1.0.0", "ruleset/cis-aws-foundations-benchmark/v/1.2.0"]
7 changes: 6 additions & 1 deletion examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
provider "aws" {
region = var.region
}

module "example" {
source = "../.."

example = var.example
enabled_standards = var.enabled_standards
create_sns_topic = var.create_sns_topic

context = module.this.context
}
16 changes: 5 additions & 11 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
output "id" {
description = "ID of the created example"
value = module.example.id
output "enabled_subscriptions" {
description = "Enabled subscriptions"
value = module.example.enabled_subscriptions
}

output "example" {
description = "Output \"example\" from example module"
value = module.example.example
}

output "random" {
description = "Output \"random\" from example module"
value = module.example.random
output "sns_topic" {
value = module.example.sns_topic
}
20 changes: 18 additions & 2 deletions examples/complete/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
variable "example" {
#----------------------------------------------------------------------------------------------------------------------
# REQUIRED VARIABLES
# The module expects values to be supplied for these paramaters.
#----------------------------------------------------------------------------------------------------------------------
variable "region" {
type = string
description = "The value which will be passed to the example module"
description = "AWS region"
}

variable "enabled_standards" {
description = "A list of standards to enable in the account"
type = list(string)
default = []
}

variable "create_sns_topic" {
description = "Flag to indicate whether an SNS topic should be created for notifications."
type = bool
default = false
}
6 changes: 5 additions & 1 deletion examples/complete/versions.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
terraform {
required_version = ">= 0.12.0, < 0.14"
required_version = ">= 0.13.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 2"
}
local = "~> 1.2"
}
}
81 changes: 74 additions & 7 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,80 @@
resource "random_integer" "example" {
#-----------------------------------------------------------------------------------------------------------------------
# Subscribe the Acccount to Security Hub
#-----------------------------------------------------------------------------------------------------------------------
resource "aws_securityhub_account" "this" {
count = module.this.enabled ? 1 : 0
}

#-----------------------------------------------------------------------------------------------------------------------
# Optionally subscribe to Security Hub Standards
# https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-standards.html
#-----------------------------------------------------------------------------------------------------------------------
resource "aws_securityhub_standards_subscription" "this" {
for_each = local.enabled_standards_arns
depends_on = [aws_securityhub_account.this]
standards_arn = each.key
}

#-----------------------------------------------------------------------------------------------------------------------
# Optionally configure Event Bridge Rules and SNS subscriptions
# https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-cwe-integration-types.html
#-----------------------------------------------------------------------------------------------------------------------
module "sns_topic" {
source = "cloudposse/sns-topic/aws"
version = "0.9.0"
count = local.create_sns_topic ? 1 : 0

attributes = ["securityhub"]
subscribers = {}
sqs_dlq_enabled = false

context = module.this.context
}

min = 1
max = 50000
keepers = {
example = var.example
}
module "imported_findings_label" {
source = "cloudposse/label/null"
version = "0.21.0"

attributes = ["securityhub-imported-findings"]
context = module.this.context
}

resource "aws_cloudwatch_event_rule" "imported_findings" {
count = local.enable_notifications == true ? 1 : 0
name = module.imported_findings_label.id
description = "SecurityHubEvent - Imported Findings"
tags = module.this.tags

event_pattern = jsonencode(
{
"source" : [
"aws.securityhub"
],
"detail-type" : [
var.cloudwatch_event_rule_pattern_detail_type
]
}
)
}

resource "aws_cloudwatch_event_target" "imported_findings" {
count = local.enable_notifications == true ? 1 : 0
rule = aws_cloudwatch_event_rule.imported_findings[0].name
arn = local.imported_findings_notification_arn
}

#-----------------------------------------------------------------------------------------------------------------------
# Locals and Data References
#-----------------------------------------------------------------------------------------------------------------------
locals {
example = format("%v %v", var.example, join("", random_integer.example[*].result))
enable_notifications = module.this.enabled && (var.create_sns_topic || var.imported_findings_notification_arn != null)
create_sns_topic = module.this.enabled && var.create_sns_topic
imported_findings_notification_arn = local.enable_notifications ? (var.imported_findings_notification_arn != null ? var.imported_findings_notification_arn : module.sns_topic[0].sns_topic.arn) : null
enabled_standards_arns = toset([
for standard in var.enabled_standards :
format("arn:%s:securityhub:%s::%s", data.aws_partition.this.partition, length(regexall("ruleset", standard)) == 0 ? data.aws_region.this.name : "", standard)
])
}

data "aws_partition" "this" {}
data "aws_region" "this" {}
21 changes: 12 additions & 9 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
output "id" {
description = "ID of the created example"
value = module.this.enabled ? module.this.id : null
output "enabled_subscriptions" {
description = "A list of subscriptions that have been enabled"
value = [
for standard in aws_securityhub_standards_subscription.this :
standard.id
]
}

output "example" {
description = "Example output"
value = module.this.enabled ? local.example : null
output "sns_topic" {
description = "The SNS topic that was created"
value = local.create_sns_topic ? module.sns_topic[0].sns_topic : null
}

output "random" {
description = "Stable random number for this example"
value = module.this.enabled ? join("", random_integer.example[*].result) : null
output "sns_topic_subscriptions" {
description = "The SNS topic that was created"
value = local.create_sns_topic ? module.sns_topic[0].aws_sns_topic_subscriptions : null
}
4 changes: 3 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ clean:
all: module examples/complete

## Run basic sanity checks against the module itself
module: export TESTS ?= installed lint get-modules module-pinning get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
##
## Remove module-pinning test from TESTS until it supports terraform registry syntax
module: export TESTS ?= installed lint get-modules get-plugins provider-pinning validate terraform-docs input-descriptions output-descriptions
module: deps
$(call RUN_TESTS, ../)

Expand Down
Loading

0 comments on commit e6e6240

Please sign in to comment.