/
handlers_evalstatus.go
412 lines (354 loc) · 13.7 KB
/
handlers_evalstatus.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
403
404
405
406
407
408
409
410
411
412
//
// 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 controlplane
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)
// ListEvaluationResults lists the evaluation results for entities filtered b
// entity type, labels, profiles, and rule types.
func (s *Server) ListEvaluationResults(
ctx context.Context,
in *minderv1.ListEvaluationResultsRequest,
) (*minderv1.ListEvaluationResultsResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
if _, err := uuid.Parse(in.GetProfile()); err != nil && in.GetProfile() != "" {
return nil, status.Error(codes.InvalidArgument, "Error invalid profile ID")
}
// Build indexes of the request parameters
rtIndex, entIdIndex, entTypeIndex := indexRequestParams(in)
// Build a list of all profiles
profileList, err := buildProjectsProfileList(ctx, s.store, []uuid.UUID{projectID}, in.GetLabelFilter())
if err != nil {
return nil, err
}
// Build a list of the status of the profiles
profileStatusList, err := buildProjectStatusList(ctx, s.store, []uuid.UUID{projectID})
if err != nil {
return nil, err
}
// Filter the profile and status lists to those in the reques params
profileList, profileStatusList = filterProfileLists(in, profileList, profileStatusList)
// Do the final sort of all the data
entities, profileStatuses, statusByEntity, err := sortEntitiesEvaluationStatus(
ctx, s.store, profileList, profileStatusList, rtIndex, entIdIndex, entTypeIndex,
)
if err != nil {
return nil, fmt.Errorf("sorting rule evaluations: %w", err)
}
return buildListEvaluationResponse(entities, profileStatuses, statusByEntity), nil
}
// sortEntitiesEvaluationStatus queries the database from the filtered lists
// and compiles the unified entities map, the map of profile statuses and the
// status by entity map that will be assembled into the evaluation status
// response.
func sortEntitiesEvaluationStatus(
ctx context.Context, store db.Store,
profileList []db.ListProfilesByProjectIDAndLabelRow,
profileStatusList map[uuid.UUID]db.GetProfileStatusByProjectRow,
rtIndex, entIdIndex, entTypeIndex map[string]struct{},
) (
entities map[string]*minderv1.EntityTypedId,
profileStatuses map[uuid.UUID]*minderv1.ProfileStatus,
statusByEntity map[string]map[uuid.UUID][]*minderv1.RuleEvaluationStatus, err error,
) {
entities = map[string]*minderv1.EntityTypedId{}
profileStatuses = map[uuid.UUID]*minderv1.ProfileStatus{}
statusByEntity = map[string]map[uuid.UUID][]*minderv1.RuleEvaluationStatus{}
for _, p := range profileList {
p := p
evals, err := store.ListRuleEvaluationsByProfileId(
ctx, db.ListRuleEvaluationsByProfileIdParams{ProfileID: p.Profile.ID},
)
if err != nil {
return nil, nil, nil,
status.Errorf(codes.Internal,
"error reading evaluations from profile %q: %v", p.Profile.ID.String(), err)
}
for _, e := range evals {
// Filter by rule type name
if _, ok := rtIndex[e.RuleTypeName]; !ok && len(rtIndex) > 0 {
continue
}
ent := buildEntityFromEvaluation(e)
entString := fmt.Sprintf("%s/%s", ent.Type, ent.Id)
/// If we're constrained to a single entity type, ignore others
if _, ok := entTypeIndex[ent.Type.String()]; !ok && len(entTypeIndex) > 0 {
continue
}
// Filter other entities if we have a list
if _, ok := entIdIndex[entString]; !ok && len(entIdIndex) > 0 {
continue
}
entities[entString] = ent
if _, ok := profileStatuses[p.Profile.ID]; !ok {
profileStatuses[p.Profile.ID] = buildProfileStatus(&p, profileStatusList)
}
stat, err := buildRuleEvaluationStatusFromDBEvaluation(ctx, &p, e)
if err != nil {
// A failure parsing the PR metadata points to a corrupt record. Log but don't err.
zerolog.Ctx(ctx).Error().Err(err).Msg("error building rule evaluation status")
}
if _, ok := statusByEntity[entString]; !ok {
statusByEntity[entString] = make(map[uuid.UUID][]*minderv1.RuleEvaluationStatus)
}
statusByEntity[entString][p.Profile.ID] = append(statusByEntity[entString][p.Profile.ID], stat)
}
}
return entities, profileStatuses, statusByEntity, err
}
// buildListEvaluationResponse builds the final response from the sorted and
// filetered list of evaluations, statuses and profiles
func buildListEvaluationResponse(
entities map[string]*minderv1.EntityTypedId,
profileStatuses map[uuid.UUID]*minderv1.ProfileStatus,
statusByEntity map[string]map[uuid.UUID][]*minderv1.RuleEvaluationStatus,
) *minderv1.ListEvaluationResultsResponse {
res := &minderv1.ListEvaluationResultsResponse{
Entities: []*minderv1.ListEvaluationResultsResponse_EntityEvaluationResults{},
}
for entString, ent := range entities {
if _, ok := statusByEntity[entString]; !ok {
continue
}
returnProfileEvals := &minderv1.ListEvaluationResultsResponse_EntityEvaluationResults{
Entity: ent,
Profiles: []*minderv1.ListEvaluationResultsResponse_EntityProfileEvaluationResults{},
}
for profileId, results := range statusByEntity[entString] {
res := &minderv1.ListEvaluationResultsResponse_EntityProfileEvaluationResults{
ProfileStatus: profileStatuses[profileId],
Results: results,
}
returnProfileEvals.Profiles = append(returnProfileEvals.Profiles, res)
}
res.Entities = append(res.Entities, returnProfileEvals)
}
return res
}
// indexRequestParams returns indexes of the request params to look them up.
// The functions returns indexes of the ruletype, entity ids and full entity.
func indexRequestParams(
in *minderv1.ListEvaluationResultsRequest,
) (rtIndex, entIdIndex, entTypeIndex map[string]struct{}) {
// These are the indexes
rtIndex = map[string]struct{}{}
entIdIndex = map[string]struct{}{}
entTypeIndex = map[string]struct{}{}
for _, rt := range in.GetRuleName() {
if rt == "" {
continue
}
rtIndex[rt] = struct{}{}
}
for _, ent := range in.GetEntity() {
if ent.Id == "" {
entTypeIndex[ent.Type.String()] = struct{}{}
} else {
entIdIndex[fmt.Sprintf("%s/%s", ent.Type, ent.Id)] = struct{}{}
}
}
return rtIndex, entIdIndex, entTypeIndex
}
// buildProjectStatusList returns a list if project statuses keyed by project ID
func buildProjectStatusList(
ctx context.Context, store db.Store, projects []uuid.UUID,
) (map[uuid.UUID]db.GetProfileStatusByProjectRow, error) {
profileStatusList := map[uuid.UUID]db.GetProfileStatusByProjectRow{}
for _, projectID := range projects {
sl, err := store.GetProfileStatusByProject(ctx, projectID)
if err != nil {
return nil, status.Errorf(codes.Internal, "error reading profile status: %v", err)
}
for _, srow := range sl {
profileStatusList[srow.ID] = srow
}
}
return profileStatusList, nil
}
// buildProjectsProfileList takes a list of projects and returns a list if profiles
func buildProjectsProfileList(
ctx context.Context, store db.Store, projects []uuid.UUID, filter string,
) ([]db.ListProfilesByProjectIDAndLabelRow, error) {
profileList := []db.ListProfilesByProjectIDAndLabelRow{}
listParams := db.ListProfilesByProjectIDAndLabelParams{}
listParams.LabelsFromFilter(filter)
for _, projectID := range projects {
listParams.ProjectID = projectID
zerolog.Ctx(ctx).Debug().Interface("listParams", listParams).Msg("profile list parameters")
profiles, err := store.ListProfilesByProjectIDAndLabel(ctx, listParams)
if err != nil {
return nil, status.Errorf(codes.Internal, "error listing profiles")
}
profileList = append(profileList, profiles...)
}
return profileList, nil
}
func filterProfileLists(
in *minderv1.ListEvaluationResultsRequest,
inProfileList []db.ListProfilesByProjectIDAndLabelRow,
inProfileStatus map[uuid.UUID]db.GetProfileStatusByProjectRow,
) ([]db.ListProfilesByProjectIDAndLabelRow, map[uuid.UUID]db.GetProfileStatusByProjectRow) {
if in.GetProfile() == "" {
return inProfileList, inProfileStatus
}
outProfileList := []db.ListProfilesByProjectIDAndLabelRow{}
outProfileStatus := map[uuid.UUID]db.GetProfileStatusByProjectRow{}
for _, p := range inProfileList {
if p.Profile.ID.String() == in.GetProfile() {
outProfileList = append(outProfileList, p)
}
}
if v, ok := inProfileStatus[uuid.MustParse(in.GetProfile())]; ok {
outProfileStatus[uuid.MustParse(in.GetProfile())] = v
}
return outProfileList, outProfileStatus
}
// buildRuleEvaluationStatusFromDBEvaluation converts from an evaluation and a
// profile from the database to a minder RuleEvaluationStatus
func buildRuleEvaluationStatusFromDBEvaluation(
ctx context.Context,
profile *db.ListProfilesByProjectIDAndLabelRow, eval db.ListRuleEvaluationsByProfileIdRow,
) (*minderv1.RuleEvaluationStatus, error) {
guidance := ""
// Only return the rule type guidance text when there is a problem
if eval.EvalStatus.EvalStatusTypes == db.EvalStatusTypesFailure ||
eval.EvalStatus.EvalStatusTypes == db.EvalStatusTypesError {
guidance = eval.RuleTypeGuidance
}
sev := &minderv1.Severity{}
sev.EnsureDefault()
if err := sev.Value.FromString(string(eval.RuleTypeSeverityValue)); err != nil {
zerolog.Ctx(ctx).
Err(err).
Str("value", string(eval.RuleTypeSeverityValue)).
Msg("error converting severity will use defaults")
}
entityInfo := map[string]string{}
remediationURL := ""
var err error
if eval.Entity == db.EntitiesRepository {
entityInfo["provider"] = eval.Provider
entityInfo["repo_owner"] = eval.RepoOwner
entityInfo["repo_name"] = eval.RepoName
entityInfo["repository_id"] = eval.RepositoryID.UUID.String()
remediationURL, err = getRemediationURLFromMetadata(
eval.RemMetadata.RawMessage, fmt.Sprintf("%s/%s", eval.RepoOwner, eval.RepoName),
)
if err != nil {
return nil, fmt.Errorf("parsing remediation pull request data: %w", err)
}
}
// Default to the rule type name if no display_name is set
nString := eval.RuleTypeDisplayName
if nString == "" {
nString = eval.RuleTypeName
}
return &minderv1.RuleEvaluationStatus{
RuleEvaluationId: eval.RuleEvaluationID.String(),
RuleId: eval.RuleTypeID.String(),
ProfileId: profile.Profile.ID.String(),
RuleName: eval.RuleName,
Entity: string(eval.Entity),
Status: string(eval.EvalStatus.EvalStatusTypes),
LastUpdated: timestamppb.New(eval.EvalLastUpdated.Time),
EntityInfo: entityInfo,
Details: eval.EvalDetails.String,
Guidance: guidance,
RemediationStatus: string(eval.RemStatus.RemediationStatusTypes),
RemediationLastUpdated: timestamppb.New(eval.RemLastUpdated.Time),
RemediationDetails: eval.RemDetails.String,
RemediationUrl: remediationURL,
RuleDisplayName: nString,
RuleTypeName: eval.RuleTypeName,
Alert: buildEvalResultAlertFromLRERow(&eval),
Severity: sev,
}, nil
}
func buildEntityFromEvaluation(eval db.ListRuleEvaluationsByProfileIdRow) *minderv1.EntityTypedId {
ent := &minderv1.EntityTypedId{
Type: dbEntityToEntity(eval.Entity),
}
if ent.Type == minderv1.Entity_ENTITY_REPOSITORIES && eval.RepoOwner != "" && eval.RepoName != "" {
ent.Id = eval.RepositoryID.UUID.String()
}
return ent
}
// buildProfileStatus build a minderv1.ProfileStatus struct from a lookup row
func buildProfileStatus(
row *db.ListProfilesByProjectIDAndLabelRow,
profileStatusList map[uuid.UUID]db.GetProfileStatusByProjectRow,
) *minderv1.ProfileStatus {
pfStatus := ""
if _, ok := profileStatusList[row.Profile.ID]; ok {
pfStatus = string(profileStatusList[row.Profile.ID].ProfileStatus)
}
displayName := row.Profile.DisplayName
if displayName == "" {
displayName = row.Profile.Name
}
return &minderv1.ProfileStatus{
ProfileId: row.Profile.ID.String(),
ProfileName: row.Profile.Name,
ProfileDisplayName: displayName,
ProfileStatus: pfStatus,
LastUpdated: timestamppb.New(row.Profile.UpdatedAt),
}
}
// buildEvalResultAlertFromLRERow build the evaluation result alert from a
// database row.
func buildEvalResultAlertFromLRERow(eval *db.ListRuleEvaluationsByProfileIdRow) *minderv1.EvalResultAlert {
era := &minderv1.EvalResultAlert{
Status: string(eval.AlertStatus.AlertStatusTypes),
LastUpdated: timestamppb.New(eval.AlertLastUpdated.Time),
Details: eval.AlertDetails.String,
}
if eval.AlertMetadata.Valid && eval.AlertStatus.AlertStatusTypes == db.AlertStatusTypesOn {
repoSlug := ""
if eval.RepoOwner != "" && eval.RepoName != "" {
repoSlug = fmt.Sprintf("%s/%s", eval.RepoOwner, eval.RepoName)
}
urlString, err := getAlertURLFromMetadata(
eval.AlertMetadata.RawMessage, repoSlug,
)
if err == nil {
era.Url = urlString
}
}
return era
}
func dbEntityToEntity(dbEnt db.Entities) minderv1.Entity {
switch dbEnt {
case db.EntitiesPullRequest:
return minderv1.Entity_ENTITY_PULL_REQUESTS
case db.EntitiesArtifact:
return minderv1.Entity_ENTITY_ARTIFACTS
case db.EntitiesRepository:
return minderv1.Entity_ENTITY_REPOSITORIES
case db.EntitiesBuildEnvironment:
return minderv1.Entity_ENTITY_BUILD_ENVIRONMENTS
default:
return minderv1.Entity_ENTITY_UNSPECIFIED
}
}