-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6839 from gazoakley/f-security-hub-account
r/aws-securityhub: Add aws_securityhub_account resource
- Loading branch information
Showing
7 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(meta.(*AWSClient).accountid) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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" {} | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAccAWSSecurityHub(t *testing.T) { | ||
testCases := map[string]map[string]func(t *testing.T){ | ||
"Account": { | ||
"basic": testAccAWSSecurityHubAccount_basic, | ||
}, | ||
} | ||
|
||
for group, m := range testCases { | ||
m := m | ||
t.Run(group, func(t *testing.T) { | ||
for name, tc := range m { | ||
tc := tc | ||
t.Run(name, func(t *testing.T) { | ||
tc(t) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 for an AWS account. | ||
--- | ||
|
||
# aws_securityhub_account | ||
|
||
-> **Note:** Destroying this resource will disable Security Hub for this AWS account. | ||
|
||
Enables Security Hub for this AWS account. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "aws_securityhub_account" "example" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The resource does not support any arguments. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported in addition to the arguments listed above: | ||
|
||
* `id` - AWS Account ID. | ||
|
||
## Import | ||
|
||
An existing Security Hub enabled account can be imported using the AWS account ID, e.g. | ||
|
||
``` | ||
$ terraform import aws_securityhub_account.example 123456789012 | ||
``` |