forked from hashicorp/terraform-provider-aws
/
resource_aws_securityhub_account.go
68 lines (51 loc) · 1.83 KB
/
resource_aws_securityhub_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
}