/
v1alpha1.go
210 lines (178 loc) · 5.09 KB
/
v1alpha1.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright 2022 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
"sort"
"strings"
"time"
)
// A Profile describing the Value
type ValueProfile interface {
// profileI the data provided in args
profileI(args ...interface{})
}
type Decision struct {
Children map[string]*Decision `json:"children"`
Reasons []string `json:"reasons"`
Result int `json:"result"`
}
// Level is "Session" or "Gate"
type Alert struct {
str string
Decision *Decision `json:"decision"`
Time int64 `json:"time"`
Level string `json:"level"`
Count uint `json:"count"`
}
type SyncMessageReq struct {
Pile *SessionDataPile `json:"pile"`
Alerts []Alert `json:"alerts"`
IamCompromised bool `json:"compromised"`
}
type SyncMessageResp struct {
Guardian *GuardianSpec `json:"guardian"`
}
func AddAlert(alerts []Alert, decision *Decision, level string) []Alert {
str := decision.SortedString(level)
for i, alert := range alerts {
if alert.str == str {
alerts[i].Count++
return alerts
}
}
alert := Alert{
str: str,
Decision: decision,
Time: time.Now().Unix(),
Level: level,
Count: 1,
}
return append(alerts, alert)
}
func DecideInner(current **Decision, result int, format string, a ...any) {
// found a problem
d := *current
if d == nil {
d = new(Decision)
d.Children = make(map[string]*Decision, 8)
*current = d
}
reason := fmt.Sprintf(format, a...)
d.Reasons = append((*current).Reasons, reason)
d.Result += result
}
func DecideChild(current **Decision, childDecision *Decision, format string, a ...any) {
if childDecision == nil {
return
}
// child found a problem
d := *current
if d == nil {
d = new(Decision)
d.Children = make(map[string]*Decision, 8)
*current = d
}
tag := fmt.Sprintf(format, a...)
d.Children[tag] = childDecision
d.Result += childDecision.Result
}
func (parent *Decision) Summary() string {
if parent.Result > 0 {
return fmt.Sprintf("Fail (%d)", parent.Result)
}
return ""
}
func (parent *Decision) SpillOut(sb *strings.Builder) {
// sprintf("[ %s: %s, %s: %s, ... %s, %s, ... ], ", tag1 , child1.SpillOut(), tag1 , child1.SpillOut(), ..., reason1, reason2, ... )
sb.WriteByte('[')
for tag, child := range parent.Children {
sb.WriteString(tag)
sb.WriteByte(':')
child.SpillOut(sb)
sb.WriteByte(',')
}
for _, reason := range parent.Reasons {
sb.WriteString(reason)
sb.WriteByte(',')
}
sb.WriteByte(']')
}
func (parent *Decision) String(tag string) string {
if parent.Result > 0 {
var sb strings.Builder
sb.WriteString(tag)
parent.SpillOut(&sb)
return sb.String()
}
return ""
}
func (parent *Decision) SortedSpillOut(sb *strings.Builder) {
// sprintf("[ %s: %s, %s: %s, ... %s, %s, ... ], ", tag1 , child1.SpillOut(), tag1 , child1.SpillOut(), ..., reason1, reason2, ... )
var tags []string
sb.WriteByte('[')
for tag := range parent.Children {
tags = append(tags, tag)
}
sort.Strings(tags)
sort.Strings(parent.Reasons)
for _, tag := range tags {
child := parent.Children[tag]
sb.WriteString(tag)
sb.WriteByte(':')
child.SortedSpillOut(sb)
sb.WriteByte(',')
}
for _, reason := range parent.Reasons {
sb.WriteString(reason)
sb.WriteByte(',')
}
sb.WriteByte(']')
}
func (parent *Decision) SortedString(tag string) string {
if parent.Result > 0 {
var sb strings.Builder
sb.WriteString(tag)
parent.SortedSpillOut(&sb)
return sb.String()
}
return ""
}
// A Pile accumulating information from zero or more Values
type ValuePile interface {
// addI one more profile to pile
// Profile should not be used after it is added to a pile
// Pile may absorb some or the profile internal structures
addI(profile ValueProfile)
// mergeI otherPile to this pile
// otherPile should not be used after it is merged to a pile
// Pile may absorb some or the otherPile internal structures
mergeI(otherPile ValuePile)
// Clear the pile from all profiles and free any memory held by pile
Clear()
}
// A Config defining what Value should adhere to
type ValueConfig interface {
// learnI from a pile to this config
// pile should not be used after it is Learned by config
// Config may absorb some or the pile internal structures
learnI(pile ValuePile)
// decideI if profile meets config
// returns nil if profile is approved by config
// otherwise, returns *Decision with details about all failures
// All issues will be reported
// Profile is unchanged and unaffected by decideI and can be used again
decideI(profile ValueProfile) *Decision
// Prepare the config during loading of a new config
Prepare()
}