-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathexecutor.go
402 lines (344 loc) · 12.3 KB
/
executor.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2023 Stacklok, Inc.
//
// 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 engine
import (
"context"
"fmt"
"sync"
"time"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/google/uuid"
"github.com/rs/zerolog"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine/actions/alert"
"github.com/stacklok/minder/internal/engine/actions/remediate"
"github.com/stacklok/minder/internal/engine/entities"
evalerrors "github.com/stacklok/minder/internal/engine/errors"
"github.com/stacklok/minder/internal/engine/ingestcache"
engif "github.com/stacklok/minder/internal/engine/interfaces"
"github.com/stacklok/minder/internal/events"
minderlogger "github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/providers/manager"
pb "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
provinfv1 "github.com/stacklok/minder/pkg/providers/v1"
)
const (
// DefaultExecutionTimeout is the timeout for execution of a set
// of profiles on an entity.
DefaultExecutionTimeout = 5 * time.Minute
// ArtifactSignatureWaitPeriod is the waiting period for potential artifact signature to be available
// before proceeding with evaluation.
ArtifactSignatureWaitPeriod = 10 * time.Second
)
// Executor is the engine that executes the rules for a given event
type Executor struct {
querier db.Store
evt events.Publisher
handlerMiddleware []message.HandlerMiddleware
wgEntityEventExecution *sync.WaitGroup
// terminationcontext is used to terminate the executor
// when the server is shutting down.
terminationcontext context.Context
providerManager manager.ProviderManager
}
// NewExecutor creates a new executor
func NewExecutor(
ctx context.Context,
querier db.Store,
evt events.Publisher,
providerManager manager.ProviderManager,
handlerMiddleware []message.HandlerMiddleware,
) *Executor {
return &Executor{
querier: querier,
evt: evt,
wgEntityEventExecution: &sync.WaitGroup{},
terminationcontext: ctx,
handlerMiddleware: handlerMiddleware,
providerManager: providerManager,
}
}
// Register implements the Consumer interface.
func (e *Executor) Register(r events.Registrar) {
r.Register(events.TopicQueueEntityEvaluate, e.HandleEntityEvent, e.handlerMiddleware...)
}
// Wait waits for all the entity executions to finish.
func (e *Executor) Wait() {
e.wgEntityEventExecution.Wait()
}
// TODO: We should consider decoupling the event processing from the business
// logic - if there is a failure in the business logic, it can cause the tests
// to hang instead of failing.
// HandleEntityEvent handles events coming from webhooks/signals
// as well as the init event.
func (e *Executor) HandleEntityEvent(msg *message.Message) error {
// Grab the context before making a copy of the message
msgCtx := msg.Context()
// Let's not share memory with the caller
msg = msg.Copy()
inf, err := entities.ParseEntityEvent(msg)
if err != nil {
return fmt.Errorf("error unmarshalling payload: %w", err)
}
e.wgEntityEventExecution.Add(1)
go func() {
defer e.wgEntityEventExecution.Done()
if inf.Type == pb.Entity_ENTITY_ARTIFACTS {
time.Sleep(ArtifactSignatureWaitPeriod)
}
// TODO: Make this timeout configurable
ctx, cancel := context.WithTimeout(e.terminationcontext, DefaultExecutionTimeout)
defer cancel()
ts := minderlogger.BusinessRecord(msgCtx)
ctx = ts.WithTelemetry(ctx)
if err := inf.WithExecutionIDFromMessage(msg); err != nil {
logger := zerolog.Ctx(ctx)
logger.Info().
Str("message_id", msg.UUID).
Msg("message does not contain execution ID, skipping")
return
}
err := e.evalEntityEvent(ctx, inf)
// record telemetry regardless of error. We explicitly record telemetry
// here even though we also record it in the middleware because the evaluation
// is done in a separate goroutine which usually still runs after the middleware
// had already recorded the telemetry.
logMsg := zerolog.Ctx(ctx).Info()
if err != nil {
logMsg = zerolog.Ctx(ctx).Error()
}
ts.Record(logMsg).Send()
if err != nil {
zerolog.Ctx(ctx).Info().
Str("project", inf.ProjectID.String()).
Str("provider_id", inf.ProviderID.String()).
Str("entity", inf.Type.String()).
Err(err).Msg("got error while evaluating entity event")
}
}()
return nil
}
func (e *Executor) evalEntityEvent(ctx context.Context, inf *entities.EntityInfoWrapper) error {
logger := zerolog.Ctx(ctx).Info().
Str("entity_type", inf.Type.ToString()).
Str("execution_id", inf.ExecutionID.String()).
Str("provider_id", inf.ProviderID.String()).
Str("project_id", inf.ProjectID.String())
logger.Msg("entity evaluation - started")
provider, err := e.providerManager.InstantiateFromID(ctx, inf.ProviderID)
if err != nil {
return fmt.Errorf("could not instantiate provider: %w", err)
}
// This is a cache, so we can avoid querying the ingester upstream
// for every rule. We use a sync.Map because it's safe for concurrent
// access.
var ingestCache ingestcache.Cache
if inf.Type == pb.Entity_ENTITY_ARTIFACTS {
// We use a noop cache for artifacts because we don't want to cache
// anything for them. The signature information is essentially another artifact version,
// and so we don't want to cache that.
ingestCache = ingestcache.NewNoopCache()
} else {
ingestCache = ingestcache.NewCache()
}
defer e.releaseLockAndFlush(ctx, inf)
err = e.forProjectsInHierarchy(
ctx, inf, func(ctx context.Context, profile *pb.Profile, hierarchy []uuid.UUID) error {
// Get only these rules that are relevant for this entity type
relevant, err := GetRulesForEntity(profile, inf.Type)
if err != nil {
return fmt.Errorf("error getting rules for entity: %w", err)
}
// Let's evaluate all the rules for this profile
err = TraverseRules(relevant, func(rule *pb.Profile_Rule) error {
// Get the engine evaluator for this rule type
evalParams, rte, err := e.getEvaluator(
ctx, inf, provider, profile, rule, hierarchy, ingestCache)
if err != nil {
return err
}
// Update the lock lease at the end of the evaluation
defer e.updateLockLease(ctx, *inf.ExecutionID, evalParams)
// Evaluate the rule
evalParams.SetEvalErr(rte.Eval(ctx, inf, evalParams))
// Perform actions, if any
evalParams.SetActionsErr(ctx, rte.Actions(ctx, inf, evalParams))
// Log the evaluation
logEval(ctx, inf, evalParams)
// Create or update the evaluation status
return e.createOrUpdateEvalStatus(ctx, evalParams)
})
if err != nil {
p := profile.Name
if profile.Id != nil {
p = *profile.Id
}
return fmt.Errorf("error traversing rules for profile %s: %w", p, err)
}
return nil
})
if err != nil {
return fmt.Errorf("error evaluating entity event: %w", err)
}
return nil
}
func (e *Executor) forProjectsInHierarchy(
ctx context.Context,
inf *entities.EntityInfoWrapper,
f func(context.Context, *pb.Profile, []uuid.UUID) error,
) error {
projList, err := e.querier.GetParentProjects(ctx, inf.ProjectID)
if err != nil {
return fmt.Errorf("error getting parent projects: %w", err)
}
for idx, projID := range projList {
projectHierarchy := projList[idx:]
// Get profiles relevant to project
dbpols, err := e.querier.ListProfilesByProjectID(ctx, projID)
if err != nil {
return fmt.Errorf("error getting profiles: %w", err)
}
for _, profile := range MergeDatabaseListIntoProfiles(dbpols) {
if err := f(ctx, profile, projectHierarchy); err != nil {
return err
}
}
}
return nil
}
func (e *Executor) getEvaluator(
ctx context.Context,
inf *entities.EntityInfoWrapper,
provider provinfv1.Provider,
profile *pb.Profile,
rule *pb.Profile_Rule,
hierarchy []uuid.UUID,
ingestCache ingestcache.Cache,
) (*engif.EvalStatusParams, *RuleTypeEngine, error) {
// Create eval status params
params, err := e.createEvalStatusParams(ctx, inf, profile, rule)
if err != nil {
return nil, nil, fmt.Errorf("error creating eval status params: %w", err)
}
// NOTE: We're only using the first project in the hierarchy for now.
// This means that a rule type must exist in the same project as the profile.
// This will be revisited in the future.
projID := hierarchy[0]
// Load Rule Class from database
// TODO(jaosorior): Rule types should be cached in memory so
// we don't have to query the database for each rule.
dbrt, err := e.querier.GetRuleTypeByName(ctx, db.GetRuleTypeByNameParams{
ProjectID: projID,
Name: rule.Type,
})
if err != nil {
return nil, nil, fmt.Errorf("error getting rule type when traversing profile %s: %w", params.ProfileID, err)
}
// Parse the rule type
rt, err := RuleTypePBFromDB(&dbrt)
if err != nil {
return nil, nil, fmt.Errorf("error parsing rule type when traversing profile %s: %w", params.ProfileID, err)
}
// Save the rule type uuid
ruleTypeID, err := uuid.Parse(*rt.Id)
if err != nil {
return nil, nil, fmt.Errorf("error parsing rule type ID: %w", err)
}
params.RuleTypeID = ruleTypeID
params.RuleType = rt
// Create the rule type engine
rte, err := NewRuleTypeEngine(ctx, profile, rt, provider)
if err != nil {
return nil, nil, fmt.Errorf("error creating rule type engine: %w", err)
}
rte = rte.WithIngesterCache(ingestCache)
// All okay
params.SetActionsOnOff(rte.GetActionsOnOff())
return params, rte, nil
}
func (e *Executor) updateLockLease(
ctx context.Context,
executionID uuid.UUID,
params *engif.EvalStatusParams,
) {
logger := params.DecorateLogger(
zerolog.Ctx(ctx).With().Str("execution_id", executionID.String()).Logger())
if err := e.querier.UpdateLease(ctx, db.UpdateLeaseParams{
Entity: params.EntityType,
RepositoryID: params.RepoID,
ArtifactID: params.ArtifactID,
PullRequestID: params.PullRequestID,
LockedBy: executionID,
}); err != nil {
logger.Err(err).Msg("error updating lock lease")
return
}
logger.Info().Msg("lock lease updated")
}
func (e *Executor) releaseLockAndFlush(
ctx context.Context,
inf *entities.EntityInfoWrapper,
) {
repoID, artID, prID := inf.GetEntityDBIDs()
logger := zerolog.Ctx(ctx).Info().
Str("entity_type", inf.Type.ToString()).
Str("execution_id", inf.ExecutionID.String())
if repoID.Valid {
logger = logger.Str("repo_id", repoID.UUID.String())
}
if artID.Valid {
logger = logger.Str("artifact_id", artID.UUID.String())
}
if prID.Valid {
logger = logger.Str("pull_request_id", prID.UUID.String())
}
if err := e.querier.ReleaseLock(ctx, db.ReleaseLockParams{
Entity: entities.EntityTypeToDB(inf.Type),
RepositoryID: repoID,
ArtifactID: artID,
PullRequestID: prID,
LockedBy: *inf.ExecutionID,
}); err != nil {
logger.Err(err).Msg("error updating lock lease")
}
// We don't need to unset the execution ID because the event is going to be
// deleted from the database anyway. The aggregator will take care of that.
msg, err := inf.BuildMessage()
if err != nil {
logger.Err(err).Msg("error building message")
return
}
if err := e.evt.Publish(events.TopicQueueEntityFlush, msg); err != nil {
logger.Err(err).Msg("error publishing flush event")
}
}
func logEval(
ctx context.Context,
inf *entities.EntityInfoWrapper,
params *engif.EvalStatusParams) {
evalLog := params.DecorateLogger(
zerolog.Ctx(ctx).With().
Str("eval_status", string(evalerrors.ErrorAsEvalStatus(params.GetEvalErr()))).
Str("project_id", inf.ProjectID.String()).
Logger())
// log evaluation result and actions status
evalLog.Info().
Str("action", string(remediate.ActionType)).
Str("action_status", string(evalerrors.ErrorAsRemediationStatus(params.GetActionsErr().RemediateErr))).
Str("action", string(alert.ActionType)).
Str("action_status", string(evalerrors.ErrorAsAlertStatus(params.GetActionsErr().AlertErr))).
Msg("entity evaluation - completed")
// log business logic
minderlogger.BusinessRecord(ctx).AddRuleEval(params)
}