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

[WIP] r/aws_security_hub: New resources #6744

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
e4c5804
resource/aws_securityhub: New resources for AWS Security Hub
gazoakley Dec 5, 2018
b7ab456
resource/aws_securityhub: Ability to enable AWS Security Hub
gazoakley Dec 6, 2018
950a81d
r/aws_securityhub: Add product subscription/standards subscription
gazoakley Dec 7, 2018
4791ab7
r/aws_securityhub: Fix linter errors
gazoakley Dec 7, 2018
0170a3d
r/aws_securityhub: Tests for aws_securityhub_account/aws_securityhub_…
gazoakley Dec 8, 2018
6d51a21
r/aws_securityhub: Rename aws_securityhub_standard to aws_securityhub…
gazoakley Dec 8, 2018
49675ac
r/aws_securityhub: Remove import behaviour for aws_securityhub_produc…
gazoakley Dec 8, 2018
33c5837
r/aws_securityhub: Update docs
gazoakley Dec 8, 2018
79cc7dd
r/aws_securityhub: Rename aws_securityhub_invitation to aws_securityh…
gazoakley Dec 8, 2018
955f42a
r/aws_securityhub: Remove aws_securityhub_invitation
gazoakley Dec 9, 2018
827b89c
r/aws_securityhub: Move invite functionality to aws_securityhub_member
gazoakley Dec 9, 2018
96eefbf
r/aws_securityhub: Rename aws_standard_subscription to aws_standards_…
gazoakley Dec 9, 2018
28047cb
r/aws_securityhub: Add validate for product_arn
gazoakley Dec 9, 2018
8877447
r/aws_securityhub: Initial aws_securityhub_insight resource (WIP)
gazoakley Dec 11, 2018
5c0e731
r/aws_securityhub: Further work on aws_securityhub_insight
gazoakley Dec 12, 2018
3a4f63a
Merge branch 'master' into f-security-hub
gazoakley Dec 13, 2018
16eff9f
r/aws-securityhub: Remove workaround for serialization error, fix lin…
gazoakley Dec 13, 2018
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
6 changes: 6 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@ 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_securityhub_insight": resourceAwsSecurityHubInsight(),
"aws_securityhub_invite_accepter": resourceAwsSecurityHubInviteAccepter(),
"aws_securityhub_member": resourceAwsSecurityHubMember(),
"aws_securityhub_product_subscription": resourceAwsSecurityHubProductSubscription(),
"aws_securityhub_standards_subscription": resourceAwsSecurityHubStandardsSubscription(),
"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")

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) {
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" {}
`
}
Loading