-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Amazon GuardDuty supports exporting findings to an Amazon S3 bucket #10920 #13894
Merged
bflad
merged 18 commits into
hashicorp:master
from
bendehaan:f-guardduty-publishing-destination
Aug 25, 2020
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
789114a
Implementation of resource new resource type for GuardDuty S3 Export …
shaepe-nc 9014a40
Added tests and documentation
shaepe-nc c88e753
Fixed test namings
shaepe-nc a3d8238
Fixed linter issues and removed explicit import test case
shaepe-nc a8bb772
Fixed HCL formatting in documentation
shaepe-nc 3826b50
Fixed some namings and sidebar link
shaepe-nc 35f730c
Update/refactor publishing destination (#1)
bendehaan dd8331b
Merged from Upstream master and squashed commits
shaepe-nc 8aa3366
Merged SDK files again and cleaned aws.erb from merge conflict strings
shaepe-nc 5e5fc08
Delete defaults.go
shaepe-nc 2f72640
Removed changes from vendor subdirectory and synched with master.
shaepe-nc be61565
Refactor GuardDuty to waiter pattern
bendehaan da35b62
Remove unused constant
bendehaan d3093d8
Small refactor based on PR review
bendehaan 1e8b99e
Add disappears test and refactor based on PR review
bendehaan 0be2414
Refactor based on PR review
bendehaan 83720fb
Refactor according to PR review
bendehaan b751615
Additional fix wrt tf 0.12
bendehaan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,181 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/guardduty" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/guardduty/waiter" | ||
) | ||
|
||
func resourceAwsGuardDutyPublishingDestination() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsGuardDutyPublishingDestinationCreate, | ||
Read: resourceAwsGuardDutyPublishingDestinationRead, | ||
Update: resourceAwsGuardDutyPublishingDestinationUpdate, | ||
Delete: resourceAwsGuardDutyPublishingDestinationDelete, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"detector_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"destination_type": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: guardduty.DestinationTypeS3, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
guardduty.DestinationTypeS3, | ||
}, false), | ||
}, | ||
"destination_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateArn, | ||
}, | ||
"kms_key_arn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateArn, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsGuardDutyPublishingDestinationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).guarddutyconn | ||
|
||
detectorID := d.Get("detector_id").(string) | ||
input := guardduty.CreatePublishingDestinationInput{ | ||
DetectorId: aws.String(detectorID), | ||
DestinationProperties: &guardduty.DestinationProperties{ | ||
DestinationArn: aws.String(d.Get("destination_arn").(string)), | ||
KmsKeyArn: aws.String(d.Get("kms_key_arn").(string)), | ||
}, | ||
DestinationType: aws.String(d.Get("destination_type").(string)), | ||
} | ||
|
||
log.Printf("[DEBUG] Creating GuardDuty publishing destination: %s", input) | ||
output, err := conn.CreatePublishingDestination(&input) | ||
if err != nil { | ||
return fmt.Errorf("Creating GuardDuty publishing destination failed: %w", err) | ||
} | ||
|
||
_, err = waiter.PublishingDestinationCreated(conn, *output.DestinationId, detectorID) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error waiting for GuardDuty PublishingDestination status to be \"%s\": %w", | ||
guardduty.PublishingStatusPublishing, err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s:%s", d.Get("detector_id"), aws.StringValue(output.DestinationId))) | ||
|
||
return resourceAwsGuardDutyPublishingDestinationRead(d, meta) | ||
} | ||
|
||
func resourceAwsGuardDutyPublishingDestinationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).guarddutyconn | ||
|
||
destinationId, detectorId, errStateRead := decodeGuardDutyPublishDestinationID(d.Id()) | ||
|
||
if errStateRead != nil { | ||
return errStateRead | ||
} | ||
|
||
input := &guardduty.DescribePublishingDestinationInput{ | ||
DetectorId: aws.String(detectorId), | ||
DestinationId: aws.String(destinationId), | ||
} | ||
|
||
log.Printf("[DEBUG] Reading GuardDuty publishing destination: %s", input) | ||
gdo, err := conn.DescribePublishingDestination(input) | ||
if err != nil { | ||
if isAWSErr(err, guardduty.ErrCodeBadRequestException, "The request is rejected because the one or more input parameters have invalid values.") { | ||
log.Printf("[WARN] GuardDuty publishing destination: %q not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Reading GuardDuty publishing destination: '%s' failed: %w", d.Id(), err) | ||
} | ||
|
||
d.Set("detector_id", detectorId) | ||
d.Set("destination_type", gdo.DestinationType) | ||
d.Set("kms_key_arn", gdo.DestinationProperties.KmsKeyArn) | ||
d.Set("destination_arn", gdo.DestinationProperties.DestinationArn) | ||
return nil | ||
} | ||
|
||
func resourceAwsGuardDutyPublishingDestinationUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).guarddutyconn | ||
|
||
destinationId, detectorId, errStateRead := decodeGuardDutyPublishDestinationID(d.Id()) | ||
|
||
if errStateRead != nil { | ||
return errStateRead | ||
} | ||
|
||
input := guardduty.UpdatePublishingDestinationInput{ | ||
DestinationId: aws.String(destinationId), | ||
DetectorId: aws.String(detectorId), | ||
DestinationProperties: &guardduty.DestinationProperties{ | ||
DestinationArn: aws.String(d.Get("destination_arn").(string)), | ||
KmsKeyArn: aws.String(d.Get("kms_key_arn").(string)), | ||
}, | ||
} | ||
|
||
log.Printf("[DEBUG] Update GuardDuty publishing destination: %s", input) | ||
_, err := conn.UpdatePublishingDestination(&input) | ||
if err != nil { | ||
return fmt.Errorf("Updating GuardDuty publishing destination '%s' failed: %w", d.Id(), err) | ||
} | ||
|
||
return resourceAwsGuardDutyPublishingDestinationRead(d, meta) | ||
} | ||
|
||
func resourceAwsGuardDutyPublishingDestinationDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).guarddutyconn | ||
|
||
destinationId, detectorId, errStateRead := decodeGuardDutyPublishDestinationID(d.Id()) | ||
|
||
if errStateRead != nil { | ||
return errStateRead | ||
} | ||
|
||
input := guardduty.DeletePublishingDestinationInput{ | ||
DestinationId: aws.String(destinationId), | ||
DetectorId: aws.String(detectorId), | ||
} | ||
|
||
log.Printf("[DEBUG] Delete GuardDuty publishing destination: %s", input) | ||
_, err := conn.DeletePublishingDestination(&input) | ||
|
||
if isAWSErr(err, guardduty.ErrCodeBadRequestException, "") { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("Deleting GuardDuty publishing destination '%s' failed: %w", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func decodeGuardDutyPublishDestinationID(id string) (destinationID, detectorID string, err error) { | ||
parts := strings.Split(id, ":") | ||
if len(parts) != 2 { | ||
err = fmt.Errorf("GuardDuty Publishing Destination ID must be of the form <Detector ID>:<Publishing Destination ID>, was provided: %s", id) | ||
return | ||
} | ||
destinationID = parts[1] | ||
detectorID = parts[0] | ||
return | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: If this happens to be rebased with the main branch, we can take advantage of the new AWS Go SDK enumeration functionality here:
Not a big deal though as we haven't begin the refactoring in #14601 yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't rebased recently, given the effort required to do so I'd prefer to get this merged first.