-
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 #17501 from hashicorp/f-security-hub-org-admin-acc…
…ount resource/securityhub_organization_admin_account: new resource
- Loading branch information
Showing
11 changed files
with
453 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_securityhub_organization_admin_account | ||
``` |
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,32 @@ | ||
package finder | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
) | ||
|
||
func AdminAccount(conn *securityhub.SecurityHub, adminAccountID string) (*securityhub.AdminAccount, error) { | ||
input := &securityhub.ListOrganizationAdminAccountsInput{} | ||
var result *securityhub.AdminAccount | ||
|
||
err := conn.ListOrganizationAdminAccountsPages(input, func(page *securityhub.ListOrganizationAdminAccountsOutput, lastPage bool) bool { | ||
if page == nil { | ||
return !lastPage | ||
} | ||
|
||
for _, adminAccount := range page.AdminAccounts { | ||
if adminAccount == nil { | ||
continue | ||
} | ||
|
||
if aws.StringValue(adminAccount.AccountId) == adminAccountID { | ||
result = adminAccount | ||
return false | ||
} | ||
} | ||
|
||
return !lastPage | ||
}) | ||
|
||
return result, err | ||
} |
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,33 @@ | ||
package waiter | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub/finder" | ||
) | ||
|
||
const ( | ||
// AdminStatus NotFound | ||
AdminStatusNotFound = "NotFound" | ||
|
||
// AdminStatus Unknown | ||
AdminStatusUnknown = "Unknown" | ||
) | ||
|
||
// AdminAccountAdminStatus fetches the AdminAccount and its AdminStatus | ||
func AdminAccountAdminStatus(conn *securityhub.SecurityHub, adminAccountID string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
adminAccount, err := finder.AdminAccount(conn, adminAccountID) | ||
|
||
if err != nil { | ||
return nil, AdminStatusUnknown, err | ||
} | ||
|
||
if adminAccount == nil { | ||
return adminAccount, AdminStatusNotFound, nil | ||
} | ||
|
||
return adminAccount, aws.StringValue(adminAccount.Status), 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,52 @@ | ||
package waiter | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
const ( | ||
// Maximum amount of time to wait for an AdminAccount to return Enabled | ||
AdminAccountEnabledTimeout = 5 * time.Minute | ||
|
||
// Maximum amount of time to wait for an AdminAccount to return NotFound | ||
AdminAccountNotFoundTimeout = 5 * time.Minute | ||
) | ||
|
||
// AdminAccountEnabled waits for an AdminAccount to return Enabled | ||
func AdminAccountEnabled(conn *securityhub.SecurityHub, adminAccountID string) (*securityhub.AdminAccount, error) { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{AdminStatusNotFound}, | ||
Target: []string{securityhub.AdminStatusEnabled}, | ||
Refresh: AdminAccountAdminStatus(conn, adminAccountID), | ||
Timeout: AdminAccountEnabledTimeout, | ||
} | ||
|
||
outputRaw, err := stateConf.WaitForState() | ||
|
||
if output, ok := outputRaw.(*securityhub.AdminAccount); ok { | ||
return output, err | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
// AdminAccountNotFound waits for an AdminAccount to return NotFound | ||
func AdminAccountNotFound(conn *securityhub.SecurityHub, adminAccountID string) (*securityhub.AdminAccount, error) { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{securityhub.AdminStatusDisableInProgress}, | ||
Target: []string{AdminStatusNotFound}, | ||
Refresh: AdminAccountAdminStatus(conn, adminAccountID), | ||
Timeout: AdminAccountNotFoundTimeout, | ||
} | ||
|
||
outputRaw, err := stateConf.WaitForState() | ||
|
||
if output, ok := outputRaw.(*securityhub.AdminAccount); ok { | ||
return output, err | ||
} | ||
|
||
return nil, err | ||
} |
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
112 changes: 112 additions & 0 deletions
112
aws/resource_aws_securityhub_organization_admin_account.go
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,112 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/securityhub" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub/finder" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/securityhub/waiter" | ||
) | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsSecurityHubOrganizationAdminAccountCreate, | ||
Read: resourceAwsSecurityHubOrganizationAdminAccountRead, | ||
Delete: resourceAwsSecurityHubOrganizationAdminAccountDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"admin_account_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAwsAccountId, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccountCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).securityhubconn | ||
|
||
adminAccountID := d.Get("admin_account_id").(string) | ||
|
||
input := &securityhub.EnableOrganizationAdminAccountInput{ | ||
AdminAccountId: aws.String(adminAccountID), | ||
} | ||
|
||
_, err := conn.EnableOrganizationAdminAccount(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error enabling Security Hub Organization Admin Account (%s): %w", adminAccountID, err) | ||
} | ||
|
||
d.SetId(adminAccountID) | ||
|
||
if _, err := waiter.AdminAccountEnabled(conn, d.Id()); err != nil { | ||
return fmt.Errorf("error waiting for Security Hub Organization Admin Account (%s) to enable: %w", d.Id(), err) | ||
} | ||
|
||
return resourceAwsSecurityHubOrganizationAdminAccountRead(d, meta) | ||
} | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).securityhubconn | ||
|
||
adminAccount, err := finder.AdminAccount(conn, d.Id()) | ||
|
||
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, securityhub.ErrCodeResourceNotFoundException) { | ||
log.Printf("[WARN] Security Hub Organization Admin Account (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading Security Hub Organization Admin Account (%s): %w", d.Id(), err) | ||
} | ||
|
||
if adminAccount == nil { | ||
if d.IsNewResource() { | ||
return fmt.Errorf("error reading Security Hub Organization Admin Account (%s): %w", d.Id(), err) | ||
} | ||
|
||
log.Printf("[WARN] Security Hub Organization Admin Account (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("admin_account_id", adminAccount.AccountId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsSecurityHubOrganizationAdminAccountDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).securityhubconn | ||
|
||
input := &securityhub.DisableOrganizationAdminAccountInput{ | ||
AdminAccountId: aws.String(d.Id()), | ||
} | ||
|
||
_, err := conn.DisableOrganizationAdminAccount(input) | ||
|
||
if tfawserr.ErrCodeEquals(err, securityhub.ErrCodeResourceNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error disabling Security Hub Organization Admin Account (%s): %w", d.Id(), err) | ||
} | ||
|
||
if _, err := waiter.AdminAccountNotFound(conn, d.Id()); err != nil { | ||
return fmt.Errorf("error waiting for Security Hub Organization Admin Account (%s) to disable: %w", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.