-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathprocessor.go
346 lines (322 loc) · 10.9 KB
/
processor.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Kodex (Community Edition - CE) - Privacy & Security Engineering Platform
// Copyright (C) 2019-2024 KIProtect GmbH (HRB 208395B) - Germany
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package kodex
import (
"encoding/hex"
"github.com/kiprotect/go-helpers/errors"
)
type Processor struct {
errorPolicy ErrorPolicy
parameterSet *ParameterSet
channelWriter ChannelWriter
config Config
key, salt []byte
id string
}
func (p *Processor) ParameterSet() *ParameterSet {
return p.parameterSet
}
func (p *Processor) updateParams(item *Item, undo bool) error {
for _, action := range p.parameterSet.Actions() {
// we get the parameter group for the specific item
parameterGroup, err := action.ParameterGroup(item)
if err != nil {
return err
}
i := 0
for {
i += 1
var spec *Parameters
var loaded bool
var err error
// if a key is specified, we generate all parameters from it and
// do not persist anything to the parameter store
if p.key == nil {
spec, loaded, err = p.parameterSet.ParametersFor(action, parameterGroup)
if err != nil {
// this might be a race condition with another processor
if i > 2 {
return errors.MakeExternalError("(1) error getting action params", "GET-ACTION-PARAMS", nil, err)
}
continue
}
}
if spec == nil {
if undo && p.key == nil {
return errors.MakeExternalError("error getting parameters", "GET-ACTION-PARAMS", nil, nil)
}
// there are no action parameters, we generate some and try
// to save them
if err = action.GenerateParams(p.key, p.salt); err != nil {
return errors.MakeExternalError("error generating params", "GEN-PARAMS", nil, err)
}
if p.key == nil && action.HasParams() {
// we update the action parameters
if err := p.parameterSet.UpdateParameters(action, action.Params(), parameterGroup); err != nil {
// this might be a race condition with another processor
if i > 2 {
return errors.MakeExternalError("error setting params", "GEN-PARAMS", nil, err)
}
// we try again, maybe another processor was simply faster
continue
}
if !p.parameterSet.Empty() {
if err := p.parameterSet.Save(); err != nil {
return errors.MakeExternalError("error saving parameter set", "SAVE-PARAMS", nil, err)
}
}
}
} else if action.HasParams() {
// if the parameters were loaded we need to potentially save the parameter set
if loaded {
if err := p.parameterSet.Save(); err != nil {
return errors.MakeExternalError("error saving parameter set", "SAVE-PARAMS", nil, err)
}
}
// the action might have changed
if err = spec.Action().SetParams(spec.Parameters()); err != nil {
return errors.MakeExternalError("error setting params", "SET-PARAMS", nil, err)
}
}
break
}
}
return nil
}
func MakeProcessor(parameterSet *ParameterSet, channelWriter ChannelWriter, config Config) (*Processor, error) {
processor := Processor{
parameterSet: parameterSet,
channelWriter: channelWriter,
errorPolicy: ReportErrors,
config: config,
id: hex.EncodeToString(RandomID()),
}
return &processor, nil
}
func (p *Processor) SetWriter(channelWriter ChannelWriter) {
p.channelWriter = channelWriter
}
func (p *Processor) Writer() ChannelWriter {
return p.channelWriter
}
func (p *Processor) SetSalt(salt []byte) {
p.salt = salt
}
func (p *Processor) SetKey(key []byte) {
p.key = key
}
func (p *Processor) SetErrorPolicy(policy ErrorPolicy) {
p.errorPolicy = policy
}
func (p *Processor) ErrorPolicy() ErrorPolicy {
return p.errorPolicy
}
func (p *Processor) Setup() error {
for i, action := range p.parameterSet.Actions() {
if setupAction, ok := action.(SetupAction); ok {
controller := p.config.Stream().Project().Controller()
if err := setupAction.Setup(controller.Settings()); err != nil {
// we tear down the actions that were already set up
for j, otherAction := range p.parameterSet.Actions() {
if j >= i {
break
}
if teardownAction, ok := otherAction.(TeardownAction); ok {
if err := teardownAction.Teardown(); err != nil {
Log.Error(err)
}
}
}
return err
}
}
}
return nil
}
func (p *Processor) Teardown() error {
// to do: make a proper error list here
var lastErr error
for _, action := range p.parameterSet.Actions() {
if teardownAction, ok := action.(TeardownAction); ok {
if err := teardownAction.Teardown(); err != nil {
Log.Error(err)
lastErr = err
}
}
}
return lastErr
}
func (p *Processor) Reset() error {
for _, action := range p.parameterSet.Actions() {
if statefulAction, ok := action.(StatefulAction); ok {
if err := statefulAction.Reset(); err != nil {
switch p.errorPolicy {
case ReportErrors:
actionError := errors.MakeExternalError("error resetting action", "RESET-ACTION", nil, err)
Log.Error(actionError)
if err := p.channelWriter.Error(nil, actionError); err != nil {
return err
}
continue
case AbortOnError:
return errors.MakeExternalError("error resetting action", "RESET-ACTION", map[string]interface{}{"action": action.Name()}, err)
}
}
}
}
return nil
}
func (p *Processor) Finalize() ([]*Item, error) {
finalizedItems := make([]*Item, 0)
for _, action := range p.parameterSet.Actions() {
if statefulAction, ok := action.(StatefulAction); ok {
if newItems, err := statefulAction.Finalize(p.channelWriter); err != nil {
switch p.errorPolicy {
case ReportErrors:
actionError := errors.MakeExternalError("error finalization action", "FINALIZE-ACTION", nil, err)
// if we encounter an error during error reporting (i.e.
// too many errors received) we abort the processing.
Log.Error(actionError)
if err := p.channelWriter.Error(nil, actionError); err != nil {
return newItems, err
}
continue
case AbortOnError:
return finalizedItems, errors.MakeExternalError("error finalizing action", "FINALIZE-ACTION", map[string]interface{}{"action": action.Name()}, err)
}
} else {
finalizedItems = append(finalizedItems, newItems...)
}
}
}
return finalizedItems, nil
}
func (p *Processor) processItem(item *Item, paramsMap map[string]interface{}, undo bool) (*Item, error) {
var err error
if err = p.updateParams(item, undo); err != nil {
return nil, errors.MakeExternalError("error setting action params", "SET-ACTION-PARAMS", nil, err)
}
newItem := item
for _, action := range p.parameterSet.Actions() {
err = nil
if undo {
if undoableAction, ok := action.(UndoableAction); ok {
// not all actions that have an Undo function are always
// undoable (e.g. some pseudonymization methods are one-way)
if undoableAction.Undoable(newItem) {
newItem, err = undoableAction.Undo(newItem, p.channelWriter)
}
}
} else {
if configurableAction, ok := action.(ConfigurableAction); p.config != nil && ok {
newItem, err = configurableAction.DoWithConfig(newItem, p.channelWriter, p.config)
} else if doableAction, ok := action.(DoableAction); ok {
newItem, err = doableAction.Do(newItem, p.channelWriter)
}
}
if err != nil {
itemError := errors.MakeExternalError("error processing action", "PROCESS-ACTION", action.Name(), err)
return nil, itemError
}
if newItem == nil {
break
}
}
if !undo && p.key == nil && !p.parameterSet.Empty() {
hashStr := hex.EncodeToString(p.parameterSet.Hash())
if paramsMap != nil {
if _, ok := paramsMap[hashStr]; !ok {
paramsMap[hashStr] = p.parameterSet
}
}
if newItem != nil {
newItem.Set("_kip", hashStr)
}
}
if undo {
newItem.Delete("_kip")
}
return newItem, nil
}
func (p *Processor) Undo(items []*Item, paramsMap map[string]interface{}) ([]*Item, error) {
return p.process(items, paramsMap, true)
}
func (p *Processor) Process(items []*Item, paramsMap map[string]interface{}) ([]*Item, error) {
return p.process(items, paramsMap, false)
}
func (p *Processor) Advance() ([]*Item, error) {
newItems := make([]*Item, 0)
for _, action := range p.parameterSet.Actions() {
if statefulAction, ok := action.(StatefulAction); ok {
if advanceItems, err := statefulAction.Advance(p.channelWriter); err != nil {
switch p.errorPolicy {
case ReportErrors:
advanceErr := errors.MakeExternalError("error advancing action", "ADVANCE-ACTION", action.Name(), err)
Log.Error(advanceErr)
if err := p.channelWriter.Error(nil, advanceErr); err != nil {
return newItems, err
}
continue
case AbortOnError:
return newItems, errors.MakeExternalError("error advancing action", "ADVANCE-ACTION", action.Name(), err)
}
} else {
newItems = append(newItems, advanceItems...)
}
}
}
return newItems, nil
}
func (p *Processor) process(items []*Item, paramsMap map[string]interface{}, undo bool) ([]*Item, error) {
Log.Tracef("Processing %d items with error policy '%s'", len(items), p.errorPolicy)
newItems := make([]*Item, 0)
// we first perform the Advance() method (for stateful actions)
if advanceItems, err := p.Advance(); err != nil {
advanceErr := errors.MakeExternalError("error advancing actions", "ADVANCE-ACTIONS", nil, err)
Log.Error(advanceErr)
if err := p.channelWriter.Error(nil, advanceErr); err != nil {
return newItems, err
}
} else {
newItems = append(newItems, advanceItems...)
}
for _, item := range items {
newItem, err := p.processItem(item, paramsMap, undo)
if err != nil {
switch p.errorPolicy {
case ReportErrors:
itemError := errors.MakeExternalError("error processing item", "PROCESS-ITEM", nil, err)
// if we encounter an error during error reporting (i.e.
// too many errors received) we abort the processing.
if p.config != nil {
Log.Errorf("Error processing item in config %s (project %s): %v", hex.EncodeToString(p.config.ID()), hex.EncodeToString(p.config.Stream().Project().ID()), itemError)
} else {
Log.Errorf("Error processing item: %v", itemError)
}
if err := p.channelWriter.Error(item, itemError); err != nil {
return newItems, err
}
continue
case AbortOnError:
return newItems, errors.MakeExternalError("error processing item", "PROCESS-ITEM", map[string]interface{}{"item": item.All()}, err)
}
}
if newItem != nil {
newItems = append(newItems, newItem)
}
}
return newItems, nil
}