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(misconf): Register checks only when needed #7435

Merged
merged 1 commit into from
Sep 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/iac/rego/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/fs"
"path/filepath"
"strings"
"sync"

"github.com/open-policy-agent/opa/ast"

Expand All @@ -14,8 +15,7 @@ import (
"github.com/aquasecurity/trivy/pkg/log"
)

func init() {

var LoadAndRegister = sync.OnceFunc(func() {
modules, err := LoadEmbeddedPolicies()
if err != nil {
// we should panic as the policies were not embedded properly
Expand All @@ -30,7 +30,7 @@ func init() {
}

RegisterRegoRules(modules)
}
})

func RegisterRegoRules(modules map[string]*ast.Module) {
ctx := context.TODO()
Expand Down
1 change: 1 addition & 0 deletions pkg/iac/rego/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

func Test_EmbeddedLoading(t *testing.T) {
LoadAndRegister()

frameworkRules := rules.GetRegistered()
var found bool
Expand Down
2 changes: 2 additions & 0 deletions pkg/iac/rego/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ type DynamicMetadata struct {
}

func NewScanner(source types.Source, opts ...options.ScannerOption) *Scanner {
LoadAndRegister()

schema, ok := schemas.SchemaMap[source]
if !ok {
schema = schemas.Anything
Expand Down