-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nikita Pivkin <[email protected]>
- Loading branch information
Showing
10 changed files
with
231 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package checks | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/framework" | ||
"github.com/aquasecurity/trivy/pkg/iac/rego" | ||
"github.com/aquasecurity/trivy/pkg/iac/rules" | ||
"github.com/aquasecurity/trivy/pkg/iac/scan" | ||
) | ||
|
||
func LoadRegoChecks() []scan.Rule { | ||
// Clean up all Go checks | ||
rules.Reset() | ||
|
||
// Load Rego checks | ||
rego.LoadAndRegister() | ||
|
||
var res []scan.Rule | ||
|
||
for _, metadata := range rules.GetRegistered(framework.ALL) { | ||
res = append(res, metadata.Rule) | ||
} | ||
|
||
sort.Slice(res, func(i, j int) bool { | ||
return res[i].AVDID < res[j].AVDID | ||
}) | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package checks | ||
|
||
import ( | ||
goast "go/ast" | ||
"go/parser" | ||
"go/token" | ||
"strings" | ||
|
||
trivy_checks "github.com/aquasecurity/trivy-checks" | ||
"github.com/aquasecurity/trivy/pkg/iac/scan" | ||
) | ||
|
||
type Provider string | ||
|
||
const ( | ||
TerraformProvider Provider = "Terraform" | ||
CloudFormationProvider Provider = "CloudFormation" | ||
) | ||
|
||
func providerByFileName(n string) Provider { | ||
switch { | ||
case strings.HasSuffix(n, "tf.go"): | ||
return TerraformProvider | ||
case strings.HasSuffix(n, "cf.go"): | ||
return CloudFormationProvider | ||
} | ||
|
||
panic("unreachable") | ||
} | ||
|
||
type Example struct { | ||
Path string | ||
Provider Provider | ||
GoodExample bool // bad example if false | ||
Content string | ||
} | ||
|
||
func GetCheckExamples(check scan.Rule) ([]*Example, error) { | ||
var files []string | ||
if check.Terraform != nil { | ||
files = append(files, check.Terraform.BadExamples...) | ||
// files = append(files, check.Terraform.GoodExamples...) | ||
} | ||
|
||
if check.CloudFormation != nil { | ||
files = append(files, check.CloudFormation.BadExamples...) | ||
// files = append(files, check.CloudFormation.GoodExamples...) | ||
} | ||
|
||
var res []*Example | ||
|
||
if check.RegoPackage != "" { | ||
for _, path := range files { | ||
exmpls, err := parseExamplesFromFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res = append(res, exmpls...) | ||
} | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func parseExamplesFromFile(filename string) ([]*Example, error) { | ||
r, err := trivy_checks.EmbeddedPolicyFileSystem.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
fset := token.NewFileSet() | ||
f, err := parser.ParseFile(fset, filename, r, parser.AllErrors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return extractExamples(f, filename), nil | ||
} | ||
|
||
func extractExamples(f *goast.File, filename string) (res []*Example) { | ||
goast.Inspect(f, func(n goast.Node) bool { | ||
valueSpec, ok := n.(*goast.ValueSpec) | ||
if !ok { | ||
return true | ||
} | ||
|
||
for _, id := range valueSpec.Names { | ||
if !isExampleName(id.Name) { | ||
continue | ||
} | ||
|
||
if compositeLit, ok := valueSpec.Values[0].(*goast.CompositeLit); ok { | ||
for _, e := range compositeLit.Elts { | ||
if basicLit, ok := e.(*goast.BasicLit); ok { | ||
res = append(res, &Example{ | ||
Path: filename, | ||
GoodExample: strings.HasSuffix(id.Name, "GoodExamples"), | ||
Provider: providerByFileName(filename), | ||
Content: cleanupExample(basicLit.Value), | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
return res | ||
} | ||
|
||
func isExampleName(name string) bool { | ||
return strings.HasSuffix(name, "GoodExamples") || strings.HasSuffix(name, "BadExamples") | ||
} | ||
|
||
func cleanupExample(s string) string { | ||
return strings.ReplaceAll(s, "`", "") | ||
} |
Oops, something went wrong.