-
Notifications
You must be signed in to change notification settings - Fork 129
/
process.go
184 lines (152 loc) · 4.86 KB
/
process.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
package posture
import (
"fmt"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/sdk-golang/pb/edge_client_pb"
"github.com/openziti/ziti/common/pb/edge_ctrl_pb"
"github.com/openziti/ziti/controller/db"
)
type ProcessCheck struct {
*edge_ctrl_pb.DataState_PostureCheck
*edge_ctrl_pb.DataState_PostureCheck_ProcessMulti
}
func (p *ProcessCheck) Evaluate(cache *Cache) *CheckError {
switch p.Semantic {
case db.SemanticAllOf:
return p.requireAll(cache)
case db.SemanticAnyOf:
return p.requireOne(cache)
default:
pfxlog.Logger().Panicf("invalid semantic, expected %s or %s got [%s]", db.SemanticAllOf, db.SemanticAnyOf, p.Semantic)
return nil
}
}
func (p *ProcessCheck) requireAll(cache *Cache) *CheckError {
if cache == nil {
return &CheckError{
Id: p.Id,
Name: p.Name,
Cause: NilStateError,
}
}
cacheProcesses := map[string]*edge_client_pb.PostureResponse_Process{}
for _, process := range cache.ProcessList.Processes {
cacheProcesses[process.Path] = process
}
allInListError := &AllInListError[*edge_ctrl_pb.DataState_PostureCheck_Process]{}
for _, process := range p.Processes {
if process.Path == "" {
return &CheckError{
Id: p.Id,
Name: p.Name,
Cause: fmt.Errorf("invalid path in process check, '%s'", process.Path),
}
}
cacheProcess, ok := cacheProcesses[process.Path]
if !ok {
return &CheckError{
Id: p.Id,
Name: p.Name,
Cause: fmt.Errorf("the process path %s was not found, it hasn't been reported or isn't running on the client", process.Path),
}
}
failedValue := p.compareProcesses(cache.Os.Os.Type, cacheProcess, process)
if failedValue != nil {
allInListError.FailedValues = append(allInListError.FailedValues, *failedValue)
}
}
if len(allInListError.FailedValues) > 0 {
for _, process := range cache.ProcessList.Processes {
allInListError.GivenValues = append(allInListError.GivenValues, &edge_ctrl_pb.DataState_PostureCheck_Process{
OsType: cache.Os.Os.Type,
Path: process.Path,
Hashes: []string{process.Hash},
Fingerprints: process.SignerFingerprints,
})
}
return &CheckError{
Id: p.Id,
Name: p.Name,
Cause: allInListError,
}
}
return nil
}
func (p *ProcessCheck) requireOne(cache *Cache) *CheckError {
if cache == nil {
return &CheckError{
Id: p.Id,
Name: p.Name,
Cause: NilStateError,
}
}
cacheProcesses := map[string]*edge_client_pb.PostureResponse_Process{}
for _, process := range cache.ProcessList.Processes {
cacheProcesses[process.Path] = process
}
anyInList := &AnyInListError[*edge_ctrl_pb.DataState_PostureCheck_Process]{}
for _, process := range p.Processes {
cacheProcess := cacheProcesses[process.Path]
failedValue := p.compareProcesses(cache.Os.Os.Type, cacheProcess, process)
if failedValue != nil {
anyInList.FailedValues = append(anyInList.FailedValues, *failedValue)
} else {
return nil
}
}
for _, process := range cache.ProcessList.Processes {
anyInList.GivenValues = append(anyInList.GivenValues, &edge_ctrl_pb.DataState_PostureCheck_Process{
OsType: cache.Os.Os.Type,
Path: process.Path,
Hashes: []string{process.Hash},
Fingerprints: process.SignerFingerprints,
})
}
return &CheckError{
Id: p.Id,
Name: p.Name,
Cause: anyInList,
}
}
func (p *ProcessCheck) compareProcesses(osType string, given *edge_client_pb.PostureResponse_Process, valid *edge_ctrl_pb.DataState_PostureCheck_Process) *FailedValueError[*edge_ctrl_pb.DataState_PostureCheck_Process] {
result := &FailedValueError[*edge_ctrl_pb.DataState_PostureCheck_Process]{
ExpectedValue: valid,
GivenValue: nil,
Reason: nil,
}
if given == nil {
result.Reason = fmt.Errorf("no matching process by path %s, the process hasn't been sumitted or isn't running", valid.Path)
return result
}
if valid.Path != given.Path {
result.Reason = fmt.Errorf("paths do not match, given %s, expected: %s", given.Path, valid.Path)
return result
}
if valid.OsType != osType {
result.Reason = fmt.Errorf("os types do not match, given %s, expected: %s", osType, valid.OsType)
return result
}
if len(valid.Hashes) > 0 && !stringz.Contains(valid.Hashes, given.Hash) {
result.Reason = fmt.Errorf("hash is not valid, given %s, expected one of: %v", given.Hash, valid.Hashes)
return result
}
if len(valid.Fingerprints) > 0 {
validPrints := map[string]struct{}{}
for _, validPrint := range valid.Fingerprints {
validPrints[validPrint] = struct{}{}
}
validPrintFound := false
for _, givenPrint := range given.SignerFingerprints {
if _, ok := validPrints[givenPrint]; ok {
validPrintFound = true
break
}
}
if !validPrintFound {
result.Reason = fmt.Errorf("valid signer not found, given: %v, expected one of: %v", given.SignerFingerprints, valid.Hashes)
return result
}
}
return nil
}