-
Notifications
You must be signed in to change notification settings - Fork 329
/
policy.go
132 lines (102 loc) · 2.64 KB
/
policy.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
package actionlog
import "github.com/cortezaproject/corteza/server/pkg/slice"
type (
policyMatcher interface {
Match(*Action) bool
}
logPolicyAny struct {
mm []policyMatcher
}
logPolicyAll struct {
mm []policyMatcher
}
logPolicyNegate struct {
m policyMatcher
}
logPolicyMatchRequestOrigin struct {
origin map[string]bool
}
logPolicyMatchResource struct {
resources map[string]bool
}
logPolicyMatchAction struct {
actions map[string]bool
}
logPolicyMatchSeverity struct {
severities map[Severity]bool
}
logPolicyNoop struct {
v bool
}
)
// NewPolicyNone ignores all action logs
func NewPolicyNone() policyMatcher {
return &logPolicyNoop{v: false}
}
// NewPolicyAny returns policy where at least one of the sub-policies should match
func NewPolicyAny(mm ...policyMatcher) policyMatcher {
return &logPolicyAny{mm: mm}
}
func (p logPolicyAny) Match(a *Action) bool {
for _, m := range p.mm {
if m.Match(a) {
return true
}
}
return false
}
// NewPolicyAll returns policy where all sub-policies should match
func NewPolicyAll(mm ...policyMatcher) policyMatcher {
return &logPolicyAll{mm: mm}
}
func (p logPolicyAll) Match(a *Action) bool {
for _, m := range p.mm {
if !m.Match(a) {
return false
}
}
return true
}
// NewPolicyNegate negates passed policy
func NewPolicyNegate(m policyMatcher) policyMatcher {
return &logPolicyNegate{m: m}
}
func (p logPolicyNegate) Match(a *Action) bool {
return !p.m.Match(a)
}
// NewPolicyMatchResource matches resources
func NewPolicyMatchResource(rr ...string) policyMatcher {
return &logPolicyMatchResource{resources: slice.ToStringBoolMap(rr)}
}
func (p logPolicyMatchResource) Match(a *Action) bool {
return p.resources[a.Resource]
}
// NewPolicyMatchAction matches action
func NewPolicyMatchAction(aa ...string) policyMatcher {
return &logPolicyMatchAction{actions: slice.ToStringBoolMap(aa)}
}
func (p logPolicyMatchAction) Match(a *Action) bool {
return p.actions[a.Action]
}
// NewPolicyMatchSeverity matches severity
func NewPolicyMatchSeverity(ss ...Severity) policyMatcher {
var p = &logPolicyMatchSeverity{severities: make(map[Severity]bool)}
for _, s := range ss {
p.severities[s] = true
}
return p
}
func (p logPolicyMatchSeverity) Match(a *Action) bool {
return p.severities[a.Severity]
}
// NewPolicyMatchRequestOrigin matches resources
func NewPolicyMatchRequestOrigin(rr ...string) policyMatcher {
return &logPolicyMatchRequestOrigin{origin: slice.ToStringBoolMap(rr)}
}
func (p logPolicyMatchRequestOrigin) Match(a *Action) bool {
return p.origin[a.RequestOrigin]
}
// Match Internal policy
func (p logPolicyNoop) Match(*Action) bool {
return p.v
}