Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: Ignore some not founds in autoscaling groups (#1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
irmatov authored Jul 8, 2022
1 parent 2fd061b commit 33c2f11
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
14 changes: 8 additions & 6 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ var (
`\bdial\s(tcp|udp)\s` + // "dial tcp "
fmt.Sprintf(`(?:%s|%s):\d+`, ipv4Regex, bracketedIpv6Regex), // "192.168.1.2:123" or "[::1]:123"
)
ec2ImageIdRegex = regexp.MustCompile(`The image ID '[^']+'`)
encAuthRegex = regexp.MustCompile(`(\s)(Encoded authorization failure message:)\s[A-Za-z0-9_-]+`)
userRegex = regexp.MustCompile(`(\s)(is not authorized to perform: .+ on resource:\s)(user)\s.+`)
s3Regex = regexp.MustCompile(`(\s)(S3(Key|Bucket))=(.+?)([,;\s])`)
resourceNotExistsRegex = regexp.MustCompile(`(\sThe )([A-Za-z0-9 -]+ )'([A-Za-z0-9-]+?)'( does not exist)`)
resourceNotFoundRegex = regexp.MustCompile(`([A-Za-z0-9 -]+)( name not found - Could not find )([A-Za-z0-9 -]+)( named )'([A-Za-z0-9-]+?)'`)
ec2ImageIdRegex = regexp.MustCompile(`The image ID '[^']+'`)
encAuthRegex = regexp.MustCompile(`(\s)(Encoded authorization failure message:)\s[A-Za-z0-9_-]+`)
userRegex = regexp.MustCompile(`(\s)(is not authorized to perform: .+ on resource:\s)(user)\s.+`)
s3Regex = regexp.MustCompile(`(\s)(S3(Key|Bucket))=(.+?)([,;\s])`)
resourceNotExistsRegex = regexp.MustCompile(`(\sThe )([A-Za-z0-9 -]+ )'([A-Za-z0-9-]+?)'( does not exist)`)
resourceNotFoundRegex = regexp.MustCompile(`([A-Za-z0-9 -]+)( name not found - Could not find )([A-Za-z0-9 -]+)( named )'([A-Za-z0-9-]+?)'`)
autoscalingGroupNotFound = regexp.MustCompile(`(ValidationError: Group ).+( not found)`)
)

var errorCodeDescriptions = map[string]string{
Expand Down Expand Up @@ -272,6 +273,7 @@ func removePII(aa []string, msg string) string {
msg = resourceNotExistsRegex.ReplaceAllString(msg, "${1}${2}'xxxx'${4}")
msg = resourceNotFoundRegex.ReplaceAllString(msg, "${1}${2}${3}${4}'xxxx'")
msg = ec2ImageIdRegex.ReplaceAllString(msg, "The image ID 'xxxx'")
msg = autoscalingGroupNotFound.ReplaceAllString(msg, "${1}xxxx${2}")
msg = accountObfusactor(aa, msg)

return msg
Expand Down
4 changes: 4 additions & 0 deletions client/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func TestRemovePII(t *testing.T) {
`operation error EC2: DescribeImageAttribute, https response error StatusCode: 400, RequestID: 3PQRRTJ1BAB82DWH, api error InvalidAMIID.Unavailable: The image ID 'ami-01964cde3b8020132' is no longer available`,
`operation error EC2: DescribeImageAttribute, https response error StatusCode: 400, RequestID: xxxx, api error InvalidAMIID.Unavailable: The image ID 'xxxx' is no longer available`,
},
{
`operation error Auto Scaling: DescribePolicies, https response error StatusCode: 400, RequestID: 3PQRRTJ1BAB82DWH, api error ValidationError: Group group-name not found`,
`operation error Auto Scaling: DescribePolicies, https response error StatusCode: 400, RequestID: xxxx, api error ValidationError: Group xxxx not found`,
},
}
for i, tc := range cases {
res := removePII([]string{"123456789"}, tc.Input)
Expand Down
12 changes: 10 additions & 2 deletions resources/services/autoscaling/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"strings"
"regexp"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
Expand All @@ -15,6 +15,8 @@ import (
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

var groupNotFoundRegex = regexp.MustCompile(`AutoScalingGroup name not found|Group .* not found`)

type autoscalingGroupWrapper struct {
types.AutoScalingGroup
NotificationConfigurations []types.NotificationConfiguration
Expand Down Expand Up @@ -703,6 +705,9 @@ func fetchAutoscalingGroupScalingPolicies(ctx context.Context, meta schema.Clien
o.Region = cl.Region
})
if err != nil {
if isAutoScalingGroupNotExistsError(err) {
return nil
}
return diag.WrapError(err)
}
res <- output.ScalingPolicies
Expand Down Expand Up @@ -751,6 +756,9 @@ func fetchAutoscalingGroupLifecycleHooks(ctx context.Context, meta schema.Client
o.Region = cl.Region
})
if err != nil {
if isAutoScalingGroupNotExistsError(err) {
return nil
}
return diag.WrapError(err)
}
res <- output.LifecycleHooks
Expand All @@ -774,7 +782,7 @@ func getNotificationConfigurationByGroupName(name string, set []types.Notificati
func isAutoScalingGroupNotExistsError(err error) bool {
var ae smithy.APIError
if errors.As(err, &ae) {
if ae.ErrorCode() == "ValidationError" && strings.Contains(ae.ErrorMessage(), "AutoScalingGroup name not found") {
if ae.ErrorCode() == "ValidationError" && groupNotFoundRegex.MatchString(ae.ErrorMessage()) {
return true
}
}
Expand Down

0 comments on commit 33c2f11

Please sign in to comment.