-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsightsParserFilter.go
More file actions
108 lines (93 loc) · 3.01 KB
/
insightsParserFilter.go
File metadata and controls
108 lines (93 loc) · 3.01 KB
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
package handler
import (
"fmt"
cdx "github.com/CycloneDX/cyclonedx-go"
"github.com/Khan/genqlient/graphql"
"log/slog"
)
func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, string, error) {
source := fmt.Sprintf("insights:sbom:%s", resource.Service)
logger := slog.With("ProjectName", resource.Project, "EnvironmentName", resource.Environment, "Source", source)
service := resource.Service
if insights.InsightsType != Sbom {
return []LagoonFact{}, "", nil
}
bom, err := getBOMfromPayload(v)
if err != nil {
return []LagoonFact{}, "", err
}
// Determine lagoon resource destination
_, environment, apiErr := determineResourceFromLagoonAPI(apiClient, resource)
if apiErr != nil {
return nil, "", apiErr
}
// we process the SBOM here
// TODO: This should actually live in its own function somewhere else.
if h.ProblemsFromSBOM == true {
isAlive, err := IsTrivyServerIsAlive(h.TrivyServerEndpoint)
if err != nil {
return nil, "", fmt.Errorf("trivy server not alive: %v", err.Error())
} else {
logger.Debug("Trivy is reachable")
}
if isAlive {
err = SbomToProblems(apiClient, h.TrivyServerEndpoint, "/tmp/", environment.Id, resource.Service, *bom)
}
if err != nil {
return nil, "", err
}
}
// Process SBOM into facts
facts := processFactsFromSBOM(logger, bom.Components, environment.Id, source, service)
facts, err = KeyFactsFilter(facts)
if err != nil {
return nil, "", err
}
if len(facts) == 0 {
return nil, "", fmt.Errorf("no facts to process")
}
//log.Printf("Successfully decoded SBOM of image %s with %s, found %d for '%s:%s'", bom.Metadata.Component.Name, (*bom.Metadata.Tools)[0].Name, len(*bom.Components), resource.Project, resource.Environment)
logger.Info("Successfully decoded SBOM",
"image", bom.Metadata.Component.Name,
"fieldName", (*bom.Metadata.Tools.Components)[0].Name,
"Length", len(*bom.Components),
)
return facts, source, nil
}
func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environmentId int, source string, service string) []LagoonFact {
var factsInput []LagoonFact
if facts == nil || len(*facts) == 0 {
return factsInput
}
var filteredFacts []cdx.Component
keyFactsExistMap := make(map[string]bool)
// Filter key facts
for _, v := range *facts {
if _, ok := keyFactsExistMap[v.Name]; !ok {
keyFactsExistMap[v.Name] = true
filteredFacts = append(filteredFacts, v)
}
}
for _, f := range filteredFacts {
fact := LagoonFact{
Environment: environmentId,
Name: f.Name,
Value: f.Version,
Source: source,
Description: f.PackageURL,
KeyFact: false,
Type: FactTypeText,
Service: service,
}
//if EnableDebug {
// log.Println("[DEBUG] processing fact name " + f.Name)
//}
logger.Debug("Processing fact",
"Name", f.Name,
"Value", f.Version,
)
fact, _ = ProcessLagoonFactAgainstRegisteredFilters(fact, f)
factsInput = append(factsInput, fact)
}
return factsInput
}