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

Commit

Permalink
fix: Handle panic on Waf logging configuration not found (#970)
Browse files Browse the repository at this point in the history
* fix: Handle panic on Waf logging configuration not found

* handle error

* lint
  • Loading branch information
zagronitay authored Jun 1, 2022
1 parent ced8892 commit 8ffb3e6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
35 changes: 21 additions & 14 deletions resources/services/waf/web_acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package waf
import (
"context"
"encoding/json"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/waf"
Expand Down Expand Up @@ -184,17 +183,21 @@ func fetchWafWebAcls(ctx context.Context, meta schema.ClientMeta, _ *schema.Reso
options.Region = c.Region
})
if err != nil {
var exc *types.WAFNonexistentItemException
if errors.As(err, &exc) {
if exc.ErrorCode() != "WAFNonexistentItemException" {
return err
}
if client.IsAWSError(err, "WAFNonexistentItemException") {
c.Logger().Debug("Logging configuration not found for: %s", webAclOutput.WebACL.Name)
} else {
c.Logger().Error("GetLoggingConfiguration failed with error: %s", err.Error())
}
}

var webAclLoggingConfiguration *types.LoggingConfiguration
if loggingConfigurationOutput != nil {
webAclLoggingConfiguration = loggingConfigurationOutput.LoggingConfiguration
}

res <- &WebACLWrapper{
webAclOutput.WebACL,
loggingConfigurationOutput.LoggingConfiguration,
webAclLoggingConfiguration,
}
}

Expand Down Expand Up @@ -249,15 +252,19 @@ func fetchWafWebACLLoggingConfiguration(ctx context.Context, meta schema.ClientM
return nil
}
func resolveWafWebACLLoggingConfigurationRedactedFields(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
conf := resource.Item.(*types.LoggingConfiguration)
out, err := json.Marshal(conf.RedactedFields)
if err != nil {
return diag.WrapError(err)
if conf := resource.Item.(*types.LoggingConfiguration); conf != nil {
out, err := json.Marshal(conf.RedactedFields)
if err != nil {
return diag.WrapError(err)
}
return resource.Set(c.Name, out)
}
return resource.Set(c.Name, out)
return nil
}

func resolveWafWebACLRuleLoggingConfiguration(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rule := resource.Item.(*WebACLWrapper)
return resource.Set(c.Name, rule.LoggingConfiguration.LogDestinationConfigs)
if rule := resource.Item.(*WebACLWrapper); rule.LoggingConfiguration != nil {
return resource.Set(c.Name, rule.LoggingConfiguration.LogDestinationConfigs)
}
return nil
}
47 changes: 28 additions & 19 deletions resources/services/wafv2/web_acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wafv2
import (
"context"
"encoding/json"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudfront"
Expand Down Expand Up @@ -381,17 +380,21 @@ func fetchWafv2WebAcls(ctx context.Context, meta schema.ClientMeta, parent *sche
options.Region = c.Region
})
if err != nil {
var exc *types.WAFNonexistentItemException
if errors.As(err, &exc) {
if exc.ErrorCode() != "WAFNonexistentItemException" {
return err
}
if client.IsAWSError(err, "WAFNonexistentItemException") {
c.Logger().Debug("Logging configuration not found for: %s", webAclOutput.WebACL.Name)
} else {
c.Logger().Error("GetLoggingConfiguration failed with error: %s", err.Error())
}
}

var webAclLoggingConfiguration *types.LoggingConfiguration
if loggingConfigurationOutput != nil {
webAclLoggingConfiguration = loggingConfigurationOutput.LoggingConfiguration
}

res <- &WebACLWrapper{
webAclOutput.WebACL,
loggingConfigurationOutput.LoggingConfiguration,
webAclLoggingConfiguration,
}
}

Expand Down Expand Up @@ -584,22 +587,28 @@ func fetchWafv2WebACLLoggingConfiguration(ctx context.Context, meta schema.Clien
return nil
}
func resolveWafv2WebACLLoggingConfigurationRedactedFields(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
conf := resource.Item.(*types.LoggingConfiguration)
out, err := json.Marshal(conf.RedactedFields)
if err != nil {
return diag.WrapError(err)
if conf := resource.Item.(*types.LoggingConfiguration); conf != nil {
out, err := json.Marshal(conf.RedactedFields)
if err != nil {
return diag.WrapError(err)
}
return resource.Set(c.Name, out)
}
return resource.Set(c.Name, out)
return nil
}
func resolveWafv2WebACLLoggingConfigurationLoggingFilter(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
conf := resource.Item.(*types.LoggingConfiguration)
out, err := json.Marshal(conf.LoggingFilter)
if err != nil {
return diag.WrapError(err)
if conf := resource.Item.(*types.LoggingConfiguration); conf != nil {
out, err := json.Marshal(conf.LoggingFilter)
if err != nil {
return diag.WrapError(err)
}
return resource.Set(c.Name, out)
}
return resource.Set(c.Name, out)
return nil
}
func resolveWafV2WebACLRuleLoggingConfiguration(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, c schema.Column) error {
rule := resource.Item.(*WebACLWrapper)
return resource.Set(c.Name, rule.LoggingConfiguration.LogDestinationConfigs)
if rule := resource.Item.(*WebACLWrapper); rule.LoggingConfiguration != nil {
return resource.Set(c.Name, rule.LoggingConfiguration.LogDestinationConfigs)
}
return nil
}

0 comments on commit 8ffb3e6

Please sign in to comment.