-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
register.go
executable file
·137 lines (115 loc) · 3.2 KB
/
register.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package rules
import (
"sync"
"gopkg.in/yaml.v3"
"github.com/aquasecurity/trivy-checks/specs"
"github.com/aquasecurity/trivy/pkg/iac/framework"
"github.com/aquasecurity/trivy/pkg/iac/scan"
dftypes "github.com/aquasecurity/trivy/pkg/iac/types"
ruleTypes "github.com/aquasecurity/trivy/pkg/iac/types/rules"
)
type registry struct {
sync.RWMutex
index int
frameworks map[framework.Framework][]ruleTypes.RegisteredRule
}
var coreRegistry = registry{
frameworks: make(map[framework.Framework][]ruleTypes.RegisteredRule),
}
func Reset() {
coreRegistry.Reset()
}
func Register(rule scan.Rule) ruleTypes.RegisteredRule {
return coreRegistry.register(rule)
}
func Deregister(rule ruleTypes.RegisteredRule) {
coreRegistry.deregister(rule)
}
func (r *registry) register(rule scan.Rule) ruleTypes.RegisteredRule {
r.Lock()
defer r.Unlock()
if len(rule.Frameworks) == 0 {
rule.Frameworks = map[framework.Framework][]string{framework.Default: nil}
}
registeredRule := ruleTypes.RegisteredRule{
Number: r.index,
Rule: rule,
}
r.index++
for fw := range rule.Frameworks {
r.frameworks[fw] = append(r.frameworks[fw], registeredRule)
}
r.frameworks[framework.ALL] = append(r.frameworks[framework.ALL], registeredRule)
return registeredRule
}
func (r *registry) deregister(rule ruleTypes.RegisteredRule) {
r.Lock()
defer r.Unlock()
for fw := range r.frameworks {
for i, registered := range r.frameworks[fw] {
if registered.Number == rule.Number {
r.frameworks[fw] = append(r.frameworks[fw][:i], r.frameworks[fw][i+1:]...)
break
}
}
}
}
func (r *registry) getFrameworkRules(fw ...framework.Framework) []ruleTypes.RegisteredRule {
r.RLock()
defer r.RUnlock()
var registered []ruleTypes.RegisteredRule
if len(fw) == 0 {
fw = []framework.Framework{framework.Default}
}
unique := make(map[int]struct{})
for _, f := range fw {
for _, rule := range r.frameworks[f] {
if _, ok := unique[rule.Number]; ok {
continue
}
registered = append(registered, rule)
unique[rule.Number] = struct{}{}
}
}
return registered
}
func (r *registry) getSpecRules(spec string) []ruleTypes.RegisteredRule {
r.RLock()
defer r.RUnlock()
var specRules []ruleTypes.RegisteredRule
var complianceSpec dftypes.ComplianceSpec
specContent := specs.GetSpec(spec)
if err := yaml.Unmarshal([]byte(specContent), &complianceSpec); err != nil {
return nil
}
registered := r.getFrameworkRules(framework.ALL)
for _, rule := range registered {
for _, csRule := range complianceSpec.Spec.Controls {
if len(csRule.Checks) > 0 {
for _, c := range csRule.Checks {
if rule.GetRule().AVDID == c.ID {
specRules = append(specRules, rule)
}
}
}
}
}
return specRules
}
func (r *registry) Reset() {
r.Lock()
defer r.Unlock()
r.frameworks = make(map[framework.Framework][]ruleTypes.RegisteredRule)
}
func GetFrameworkRules(fw ...framework.Framework) []ruleTypes.RegisteredRule {
return coreRegistry.getFrameworkRules(fw...)
}
func GetSpecRules(spec string) []ruleTypes.RegisteredRule {
if spec != "" {
return coreRegistry.getSpecRules(spec)
}
return GetFrameworkRules()
}
func GetRegistered(fw ...framework.Framework) []ruleTypes.RegisteredRule {
return GetFrameworkRules(fw...)
}