Skip to content

Commit

Permalink
Added the default ignore config for service disable error (#476)
Browse files Browse the repository at this point in the history

Co-authored-by: Patrick Decat <[email protected]>
  • Loading branch information
ParthaI and pdecat authored Feb 5, 2025
1 parent 9efe995 commit 6dfb12e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions config/gcp.spc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ connection "gcp" {
# If neither is specified, billing and quota are tracked against the project associated with the credentials used for authentication.
# quota_project = "YOUR_QUOTA_PROJECT_ID"

# `ignore_error_messages` (optional) - List of additional GCP error message pattern to ignore for all queries.
# ignore_error_messages = ["^.*API has not been used.*$"]

# `ignore_error_codes` (optional) - List of additional GCP error codes to ignore for all queries.
# By default, common not found error codes are ignored and will still be ignored even if this argument is not set.
# Refer https://cloud.google.com/resource-manager/docs/core_errors#Global_Errors for more information on GCP error codes
Expand Down
5 changes: 4 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ connection "gcp" {
# If not set, no impersonation is done.
#impersonate_service_account = "YOUR_SERVICE_ACCOUNT"
# `quota_project` (optional) - The project ID used for billing and quota. When set,
# `quota_project` (optional) - The project ID used for billing and quota. When set,
# this project ID is used to track quota usage and billing for the operations performed with the GCP connection.
# If `quota_project` is not specified directly, the system will look for the `GOOGLE_CLOUD_QUOTA_PROJECT`
# environment variable to determine which project to use for billing and quota.
# If neither is specified, billing and quota are tracked against the project associated with the credentials used for authentication.
# quota_project = "YOUR_QUOTA_PROJECT_ID"
# `ignore_error_messages` (optional) - List of additional GCP error message patterns to ignore for all queries.
#ignore_error_messages = ["^.*API has not been used.*$"]
# `ignore_error_codes` (optional) - List of additional GCP error codes to ignore for all queries.
# By default, the common not found error codes are ignored and will still be ignored even if this argument is not set.
# Refer https://cloud.google.com/resource-manager/docs/core_errors#Global_Errors for more information on GCP error codes
Expand Down
5 changes: 4 additions & 1 deletion gcp/connection_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ type gcpConfig struct {
Credentials *string `hcl:"credentials"`
ImpersonateAccessToken *string `hcl:"impersonate_access_token"`
ImpersonateServiceAccount *string `hcl:"impersonate_service_account"`
QuotaProject *string `hcl:"quota_project,optional"`
IgnoreErrorMessages []string `hcl:"ignore_error_messages,optional"`
IgnoreErrorCodes []string `hcl:"ignore_error_codes,optional"`
QuotaProject *string `hcl:"quota_project,optional"`
}

func ConfigInstance() interface{} {
Expand All @@ -25,3 +26,5 @@ func GetConfig(connection *plugin.Connection) gcpConfig {
config, _ := connection.Config.(gcpConfig)
return config
}


28 changes: 19 additions & 9 deletions gcp/ignore_error_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcp
import (
"context"
"path"
"regexp"

"github.com/turbot/go-kit/helpers"
"github.com/turbot/go-kit/types"
Expand All @@ -20,27 +21,36 @@ func isIgnorableError(notFoundErrors []string) plugin.ErrorPredicate {
}
}

// shouldIgnoreErrorPluginDefault:: Plugin level default function to ignore a set errors for hydrate functions based on "ignore_error_codes" config argument
// shouldIgnoreErrorPluginDefault:: Plugin level default function to ignore a set errors for hydrate functions based on "ignore_error_codes" and "ignore_error_messages" config argument
func shouldIgnoreErrorPluginDefault() plugin.ErrorPredicateWithContext {
return func(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData, err error) bool {
if !hasIgnoredErrorCodes(d.Connection) {
return false
gcpConfig := GetConfig(d.Connection)

logger := plugin.Logger(ctx)

// Add to support regex match as per error message
for _, pattern := range gcpConfig.IgnoreErrorMessages {
// Validate regex pattern
re, er := regexp.Compile(pattern)
if er != nil {
panic(er.Error() + " the regex pattern configured in 'ignore_error_messages' is invalid. Edit your connection configuration file and then restart Steampipe")
}
result := re.MatchString(err.Error())
if result {
logger.Debug("ignore_error_predicate.shouldIgnoreErrorPluginDefault", "ignore_error_message", err.Error())
return true
}
}

gcpConfig := GetConfig(d.Connection)
if gerr, ok := err.(*googleapi.Error); ok {
// Added to support regex in not found errors
for _, pattern := range gcpConfig.IgnoreErrorCodes {
if ok, _ := path.Match(pattern, types.ToString(gerr.Code)); ok {
logger.Debug("ignore_error_predicate.shouldIgnoreErrorPluginDefault", "ignore_error_code", err.Error())
return true
}
}
}
return false
}
}

func hasIgnoredErrorCodes(connection *plugin.Connection) bool {
gcpConfig := GetConfig(connection)
return len(gcpConfig.IgnoreErrorCodes) > 0
}

0 comments on commit 6dfb12e

Please sign in to comment.