Skip to content
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

feat: support exclude expiry in config #1897

Merged
merged 2 commits into from Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/guides/configuration/config.md
Expand Up @@ -106,6 +106,24 @@ exclude:
- aws-s3-enable-versioning
```

Excludes can include an expiry date, after which the check will be re-enabled.

```json
{
"exclude": ["CUS002:2022-12-31", "aws-s3-enable-versioning"]
}
```

or in yaml


```yaml
---
exclude:
- CUS002:2022-12-31
- aws-s3-enable-versioning
```

### Minimum required version

For your CI you might want to pull a config file into all of your build processes with a centrally managed config file. If this is the case, you might also want to require a minimum tfsec version to be used.
Expand Down
4 changes: 2 additions & 2 deletions internal/app/tfsec/cmd/flags.go
Expand Up @@ -288,8 +288,8 @@ func applyConfigFiles(options []options.ScannerOption, dir string) ([]options.Sc
if len(conf.IncludedChecks) > 0 {
options = append(options, scanner.ScannerWithIncludedRules(conf.IncludedChecks))
}
if len(conf.ExcludedChecks) > 0 {
options = append(options, scanner.ScannerWithExcludedRules(append(conf.ExcludedChecks, excludedRuleIDs)))
if len(conf.GetValidExcludedChecks()) > 0 {
options = append(options, scanner.ScannerWithExcludedRules(append(conf.GetValidExcludedChecks(), excludedRuleIDs)))
}
if len(conf.ExcludeIgnores) > 0 {
options = append(options, scanner.ScannerWithExcludeIgnores(append(conf.ExcludeIgnores, excludeIgnoresIDs)))
Expand Down
20 changes: 20 additions & 0 deletions internal/pkg/config/config.go
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/aquasecurity/defsec/pkg/severity"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -54,6 +55,25 @@ func LoadConfig(configFilePath string) (*Config, error) {
return config, nil
}

func (c *Config) GetValidExcludedChecks() (excludedChecks []string) {
for _, check := range c.ExcludedChecks {
if strings.Contains(check, ":") {
parts := strings.Split(check, ":")
if len(parts) == 2 {
if expiry, err := time.Parse("2006-01-02", parts[1]); err == nil {
if expiry.Before(time.Now()) {
continue
}
}
}
excludedChecks = append(excludedChecks, parts[0])
} else {
excludedChecks = append(excludedChecks, check)
}
}
return excludedChecks
}

func rewriteSeverityOverrides(config *Config) {
for k, s := range config.SeverityOverrides {
config.SeverityOverrides[k] = string(severity.StringToSeverity(s))
Expand Down
43 changes: 40 additions & 3 deletions internal/pkg/config/config_test.go
Expand Up @@ -44,7 +44,44 @@ exclude_ignores:
c := load(t, "config.yaml", content)

assert.Contains(t, c.SeverityOverrides, "AWS018")
assert.Contains(t, c.ExcludedChecks, "DP001")
assert.Contains(t, c.GetValidExcludedChecks(), "DP001")
assert.Contains(t, c.ExcludeIgnores, "DP002")
}

func TestExpiredExcludesElementsFromYAMLNotPresent(t *testing.T) {
content := `
severity_overrides:
AWS018: LOW

exclude:
- DP001:2021-01-01

exclude_ignores:
- DP002
`
c := load(t, "config.yaml", content)

assert.Contains(t, c.SeverityOverrides, "AWS018")
assert.Len(t, c.GetValidExcludedChecks(), 0)
assert.Contains(t, c.ExcludeIgnores, "DP002")
}

func TestExcludesElementsFromYAMLWithFutureExpiryIsPresent(t *testing.T) {
content := `
severity_overrides:
AWS018: LOW

exclude:
- DP001:3099-01-01

exclude_ignores:
- DP002
`
c := load(t, "config.yaml", content)

assert.Contains(t, c.SeverityOverrides, "AWS018")
assert.Len(t, c.GetValidExcludedChecks(), 1)
assert.Contains(t, c.GetValidExcludedChecks(), "DP001")
assert.Contains(t, c.ExcludeIgnores, "DP002")
}

Expand All @@ -62,7 +99,7 @@ exclude_ignores:
c := load(t, "config.yml", content)

assert.Contains(t, c.SeverityOverrides, "AWS018")
assert.Contains(t, c.ExcludedChecks, "DP001")
assert.Contains(t, c.GetValidExcludedChecks(), "DP001")
assert.Contains(t, c.ExcludeIgnores, "DP002")
}

Expand All @@ -82,7 +119,7 @@ func TestExcludesElementsFromJSON(t *testing.T) {
c := load(t, "config.json", content)

assert.Contains(t, c.SeverityOverrides, "AWS018")
assert.Contains(t, c.ExcludedChecks, "DP001")
assert.Contains(t, c.GetValidExcludedChecks(), "DP001")
assert.Contains(t, c.ExcludeIgnores, "DP002")
}

Expand Down