@@ -2,6 +2,7 @@ package rego
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"io/fs"
6
7
"path/filepath"
7
8
"strings"
@@ -10,6 +11,7 @@ import (
10
11
11
12
checks "github.com/aquasecurity/trivy-checks"
12
13
"github.com/aquasecurity/trivy/pkg/iac/rules"
14
+ "github.com/aquasecurity/trivy/pkg/log"
13
15
)
14
16
15
17
func init () {
@@ -47,17 +49,34 @@ func RegisterRegoRules(modules map[string]*ast.Module) {
47
49
}
48
50
49
51
retriever := NewMetadataRetriever (compiler )
52
+ regoCheckIDs := make (map [string ]struct {})
53
+
50
54
for _ , module := range modules {
51
55
metadata , err := retriever .RetrieveMetadata (ctx , module )
52
56
if err != nil {
57
+ log .Warn ("Failed to retrieve metadata" , log .String ("avdid" , metadata .AVDID ), log .Err (err ))
53
58
continue
54
59
}
60
+
55
61
if metadata .AVDID == "" {
62
+ log .Warn ("Check ID is empty" , log .FilePath (module .Package .Location .File ))
56
63
continue
57
64
}
58
- rules .Register (
59
- metadata .ToRule (),
60
- )
65
+
66
+ if ! metadata .Deprecated {
67
+ regoCheckIDs [metadata .AVDID ] = struct {}{}
68
+ }
69
+
70
+ rules .Register (metadata .ToRule ())
71
+ }
72
+
73
+ for _ , check := range rules .GetRegistered () {
74
+ if ! check .Deprecated && check .CanCheck () {
75
+ if _ , exists := regoCheckIDs [check .AVDID ]; exists {
76
+ log .Warn ("Ignore duplicate Go check" , log .String ("avdid" , check .AVDID ))
77
+ rules .Deregister (check )
78
+ }
79
+ }
61
80
}
62
81
}
63
82
@@ -95,8 +114,7 @@ func LoadPoliciesFromDirs(target fs.FS, paths ...string) (map[string]*ast.Module
95
114
ProcessAnnotation : true ,
96
115
})
97
116
if err != nil {
98
- // s.debug.Log("Failed to load module: %s, err: %s", filepath.ToSlash(path), err.Error())
99
- return err
117
+ return fmt .Errorf ("failed to parse Rego module: %w" , err )
100
118
}
101
119
modules [path ] = module
102
120
return nil
0 commit comments