Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/aws-securityhub: Add aws_securityhub_account resource #6839

Merged
merged 2 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import (
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/securityhub"
"github.com/aws/aws-sdk-go/service/servicecatalog"
"github.com/aws/aws-sdk-go/service/servicediscovery"
"github.com/aws/aws-sdk-go/service/ses"
Expand Down Expand Up @@ -197,6 +198,7 @@ type AWSClient struct {
autoscalingconn *autoscaling.AutoScaling
s3conn *s3.S3
secretsmanagerconn *secretsmanager.SecretsManager
securityhubconn *securityhub.SecurityHub
scconn *servicecatalog.ServiceCatalog
sesConn *ses.SES
simpledbconn *simpledb.SimpleDB
Expand Down Expand Up @@ -567,6 +569,7 @@ func (c *Config) Client() (interface{}, error) {
client.sdconn = servicediscovery.New(sess)
client.sesConn = ses.New(sess)
client.secretsmanagerconn = secretsmanager.New(sess)
client.securityhubconn = securityhub.New(sess)
client.sfnconn = sfn.New(sess)
client.snsconn = sns.New(awsSnsSess)
client.sqsconn = sqs.New(awsSqsSess)
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ func Provider() terraform.ResourceProvider {
"aws_network_interface_sg_attachment": resourceAwsNetworkInterfaceSGAttachment(),
"aws_default_security_group": resourceAwsDefaultSecurityGroup(),
"aws_security_group_rule": resourceAwsSecurityGroupRule(),
"aws_securityhub_account": resourceAwsSecurityHubAccount(),
"aws_servicecatalog_portfolio": resourceAwsServiceCatalogPortfolio(),
"aws_service_discovery_private_dns_namespace": resourceAwsServiceDiscoveryPrivateDnsNamespace(),
"aws_service_discovery_public_dns_namespace": resourceAwsServiceDiscoveryPublicDnsNamespace(),
Expand Down
68 changes: 68 additions & 0 deletions aws/resource_aws_securityhub_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/service/securityhub"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsSecurityHubAccount() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSecurityHubAccountCreate,
Read: resourceAwsSecurityHubAccountRead,
Delete: resourceAwsSecurityHubAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{},
}
}

func resourceAwsSecurityHubAccountCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).securityhubconn
log.Print("[DEBUG] Enabling Security Hub for account")

_, err := conn.EnableSecurityHub(&securityhub.EnableSecurityHubInput{})

if err != nil {
return fmt.Errorf("Error enabling Security Hub for account: %s", err)
}

d.SetId("securityhub-account")
gazoakley marked this conversation as resolved.
Show resolved Hide resolved

return resourceAwsSecurityHubAccountRead(d, meta)
}

func resourceAwsSecurityHubAccountRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).securityhubconn

log.Printf("[DEBUG] Checking if Security Hub is enabled")
_, err := conn.GetEnabledStandards(&securityhub.GetEnabledStandardsInput{})

if err != nil {
// Can only read enabled standards if Security Hub is enabled
if isAWSErr(err, "InvalidAccessException", "not subscribed to AWS Security Hub") {
d.SetId("")
return nil
}
return fmt.Errorf("Error checking if Security Hub is enabled: %s", err)
}

return nil
}

func resourceAwsSecurityHubAccountDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).securityhubconn
log.Print("[DEBUG] Disabling Security Hub for account")

_, err := conn.DisableSecurityHub(&securityhub.DisableSecurityHubInput{})

if err != nil {
return fmt.Errorf("Error disabling Security Hub for account: %s", err)
}

return nil
}
84 changes: 84 additions & 0 deletions aws/resource_aws_securityhub_account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/securityhub"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSSecurityHubAccount_basic(t *testing.T) {
gazoakley marked this conversation as resolved.
Show resolved Hide resolved
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSecurityHubAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSecurityHubAccountConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSecurityHubAccountExists("aws_securityhub_account.example"),
),
},
{
ResourceName: "aws_securityhub_account.example",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckAWSSecurityHubAccountExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
_, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

conn := testAccProvider.Meta().(*AWSClient).securityhubconn

_, err := conn.GetEnabledStandards(&securityhub.GetEnabledStandardsInput{})

if err != nil {
// Can only read enabled standards if Security Hub is enabled
if isAWSErr(err, "InvalidAccessException", "not subscribed to AWS Security Hub") {
return fmt.Errorf("Security Hub account not found")
}
return err
}

return nil
}
}

func testAccCheckAWSSecurityHubAccountDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).securityhubconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_securityhub_account" {
continue
}

_, err := conn.GetEnabledStandards(&securityhub.GetEnabledStandardsInput{})

if err != nil {
// Can only read enabled standards if Security Hub is enabled
if isAWSErr(err, "InvalidAccessException", "not subscribed to AWS Security Hub") {
return nil
}
return err
}

return fmt.Errorf("Security Hub account still exists")
}

return nil
}

func testAccAWSSecurityHubAccountConfig() string {
return `
resource "aws_securityhub_account" "example" {}
`
}
11 changes: 11 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2176,6 +2176,17 @@
</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-securityhub") %>>
<a href="#">Security Hub Resources</a>
<ul class="nav nav-visible">

<li<%= sidebar_current("docs-aws-resource-securityhub-account") %>>
<a href="/docs/providers/aws/r/securityhub_account.html">aws_securityhub_account</a>
</li>

</ul>
</li>

<li<%= sidebar_current("docs-aws-resource-ses") %>>
<a href="#">SES Resources</a>
<ul class="nav nav-visible">
Expand Down
37 changes: 37 additions & 0 deletions website/docs/r/securityhub_account.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: "aws"
page_title: "AWS: aws_securityhub_account"
sidebar_current: "docs-aws-resource-securityhub-account"
description: |-
Enables Security Hub.
---

# aws_securityhub_account

-> **Note:** Destroying this resource will disable Security Hub.
gazoakley marked this conversation as resolved.
Show resolved Hide resolved

Enables Security Hub.
gazoakley marked this conversation as resolved.
Show resolved Hide resolved

## Example Usage

```hcl
resource "aws_securityhub_member" "example" {}
gazoakley marked this conversation as resolved.
Show resolved Hide resolved
```

## Argument Reference

The resource does not support any arguments.

## Attributes Reference

The following attributes are exported in addition to the arguments listed above:

* `id` - Returns `securityhub-account`.
gazoakley marked this conversation as resolved.
Show resolved Hide resolved

## Import

Security Hub account enablemenet can be imported using the word `securityhub-account`, e.g.
gazoakley marked this conversation as resolved.
Show resolved Hide resolved

```
$ terraform import aws_securityhub_account.example securityhub-account
gazoakley marked this conversation as resolved.
Show resolved Hide resolved
```