Skip to content

Commit f768d3a

Browse files
authored
feat(misconf): Register checks only when needed (#7435)
1 parent e6f45cd commit f768d3a

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

pkg/iac/rego/embed.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/fs"
77
"path/filepath"
88
"strings"
9+
"sync"
910

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

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

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

3232
RegisterRegoRules(modules)
33-
}
33+
})
3434

3535
func RegisterRegoRules(modules map[string]*ast.Module) {
3636
ctx := context.TODO()

pkg/iac/rego/embed_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
)
1616

1717
func Test_EmbeddedLoading(t *testing.T) {
18+
LoadAndRegister()
1819

1920
frameworkRules := rules.GetRegistered()
2021
var found bool

pkg/iac/rego/scanner.go

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ type DynamicMetadata struct {
152152
}
153153

154154
func NewScanner(source types.Source, opts ...options.ScannerOption) *Scanner {
155+
LoadAndRegister()
156+
155157
schema, ok := schemas.SchemaMap[source]
156158
if !ok {
157159
schema = schemas.Anything

0 commit comments

Comments
 (0)