-
Notifications
You must be signed in to change notification settings - Fork 129
/
errors.go
164 lines (129 loc) · 4.27 KB
/
errors.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
package posture
import (
"fmt"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"github.com/pkg/errors"
)
type CheckError struct {
Id string
Name string
Cause error
}
func (p *CheckError) Error() string {
return fmt.Sprintf("posture check %s [%s] failed due to error(s): %s", p.Name, p.Id, p.Cause.Error())
}
type NoPoliciesError struct {
}
func (e *NoPoliciesError) Error() string {
return "no policies provide access"
}
type PolicyAccessError struct {
Id string
Name string
Errors []error
}
func (p *PolicyAccessError) Error() string {
if len(p.Errors) == 0 {
return fmt.Sprintf("policy %s [%s] failed", p.Id, p.Name)
}
subErrStr := ""
for i, err := range p.Errors {
if i == 0 {
subErrStr = err.Error()
} else {
subErrStr = subErrStr + ", " + err.Error()
}
}
return fmt.Sprintf("policy %s [%s] failed due to %d error(s): %s", p.Name, p.Id, len(p.Errors), subErrStr)
}
type PolicyAccessErrors []*PolicyAccessError
func (pae *PolicyAccessErrors) Error() string {
if pae == nil || len(*pae) == 0 {
return "unknown policy failure"
}
subErr := ""
for i, err := range *pae {
if i == 0 {
subErr = err.Error()
} else {
subErr = subErr + ", " + err.Error()
}
}
return fmt.Sprintf("%d policies failed: %s", len(*pae), subErr)
}
func EvaluatePostureCheck(postureCheck *edge_ctrl_pb.DataState_PostureCheck, cache *Cache) *CheckError {
check := CtrlCheckToLogic(postureCheck)
return check.Evaluate(cache)
}
// FailedValueError represents a complex object comparison that failed. If a simple comparison failure is needed
// (i.e. bool != bool, string != string) use an `error` instead.
type FailedValueError[V fmt.Stringer] struct {
ExpectedValue V
GivenValue V
Reason error
}
func (v *FailedValueError[V]) String() string {
return fmt.Sprintf("the state did not match because %v, expected: %s, given: %s", v.Reason, v.ExpectedValue, v.GivenValue)
}
func (v *FailedValueError[V]) Error() string {
return v.String()
}
// AllInListError indicates that a given array of expected values had one or more values that did not match/pass.
// GivenValues represents all values supplied to match the expected values. FailedValues represents all the expected
// values that did not pass.
type AllInListError[V fmt.Stringer] struct {
FailedValues []FailedValueError[V]
GivenValues []V
}
func (e *AllInListError[V]) Error() string {
var failureStrings []string
for _, failedValue := range e.FailedValues {
failureStrings = append(failureStrings, failedValue.String())
}
valueStr := ""
for i, v := range e.GivenValues {
if i == 0 {
valueStr = fmt.Sprintf("%v", v)
} else {
valueStr = valueStr + ", " + fmt.Sprintf("%v", v)
}
}
return fmt.Sprintf("all values must be valid have at least one failure, have: %s, failed for: %v", valueStr, failureStrings)
}
// AnyInListError represents the fact that zero expected values did not match/pass where at least one was required.
// GivenValues represents all values supplied to match the expected values. FailedValues represents all the expected
// values that did not pass.
type AnyInListError[V fmt.Stringer] struct {
FailedValues []FailedValueError[V]
GivenValues []V
}
func (e *AnyInListError[V]) Error() string {
var failureStrings []string
for _, failedValue := range e.FailedValues {
failureStrings = append(failureStrings, failedValue.String())
}
valueStr := ""
for i, v := range e.GivenValues {
if i == 0 {
valueStr = fmt.Sprintf("%v", v)
} else {
valueStr = valueStr + ", " + fmt.Sprintf("%v", v)
}
}
return fmt.Sprintf("one valid values is required, got 0, have: %s, failed for: %v", valueStr, failureStrings)
}
// OneInListError represents two arrays of values where one of the supplied GivenValues must be in the ValidValues.
// Used when a large cross join of values (i.e. mac address approve/deny lists) would be reported for every comparison.
type OneInListError[V fmt.Stringer] struct {
ValidValues []V
GivenValues []V
}
func (e *OneInListError[V]) Error() string {
return fmt.Sprintf("none of the given values were in the valid values, given: %v, valid: %v", e.GivenValues, e.ValidValues)
}
type Str string
func (s Str) String() string {
return string(s)
}
var NilStateError = errors.New("posture state was nil")
var NotEqualError = errors.New("the values were not equal")