2020-11-23 19:43:53 +00:00
|
|
|
package findings
|
|
|
|
|
|
|
|
import (
|
2022-10-03 05:52:06 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/securityhub"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/securityhub/types"
|
2020-11-23 19:43:53 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Publisher represents an object that publishes finds to AWS Security Hub.
|
|
|
|
type Publisher struct {
|
2022-10-03 05:52:06 +00:00
|
|
|
client securityhub.Client // AWS Security Hub Service Client
|
2020-11-23 19:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A PublisherOutput represents an object that contains information about the service call.
|
|
|
|
type PublisherOutput struct {
|
|
|
|
// The number of findings that failed to import.
|
|
|
|
//
|
|
|
|
// FailedCount is a required field
|
2022-10-03 05:52:06 +00:00
|
|
|
FailedCount int32
|
2020-11-23 19:43:53 +00:00
|
|
|
|
|
|
|
// The list of findings that failed to import.
|
2022-10-03 05:52:06 +00:00
|
|
|
FailedFindings []types.ImportFindingsError
|
2020-11-23 19:43:53 +00:00
|
|
|
|
|
|
|
// The number of findings that were successfully imported.
|
|
|
|
//
|
|
|
|
// SuccessCount is a required field
|
2022-10-03 05:52:06 +00:00
|
|
|
SuccessCount int32
|
2020-11-23 19:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new Publisher.
|
2022-10-03 05:52:06 +00:00
|
|
|
func New(client securityhub.Client) *Publisher {
|
2020-11-23 19:43:53 +00:00
|
|
|
return &Publisher{
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublishFinding publishes findings to AWS Security Hub Service
|
2022-10-03 05:52:06 +00:00
|
|
|
func (p *Publisher) PublishFinding(finding []types.AwsSecurityFinding) (*PublisherOutput, error) {
|
2020-11-23 19:43:53 +00:00
|
|
|
o := PublisherOutput{}
|
|
|
|
i := securityhub.BatchImportFindingsInput{}
|
|
|
|
i.Findings = finding
|
|
|
|
var errs error
|
|
|
|
|
|
|
|
// Split the slice into batches of 100 finding.
|
|
|
|
batch := 100
|
|
|
|
|
|
|
|
for i := 0; i < len(finding); i += batch {
|
|
|
|
i := securityhub.BatchImportFindingsInput{}
|
|
|
|
i.Findings = finding
|
2022-10-03 05:52:06 +00:00
|
|
|
r, err := p.client.BatchImportFindings(context.Background(), &i) // Process the batch.
|
2020-11-23 19:43:53 +00:00
|
|
|
if err != nil {
|
|
|
|
errs = errors.Wrap(err, "finding publish failed")
|
|
|
|
}
|
2022-10-03 05:52:06 +00:00
|
|
|
if r != nil {
|
|
|
|
if r.FailedCount != 0 {
|
|
|
|
o.FailedCount += r.FailedCount
|
|
|
|
}
|
|
|
|
if r.SuccessCount != 0 {
|
|
|
|
o.SuccessCount += r.SuccessCount
|
|
|
|
}
|
|
|
|
o.FailedFindings = append(o.FailedFindings, r.FailedFindings...)
|
2020-11-23 19:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return &o, errs
|
|
|
|
}
|