diff --git a/internal/handler/insightsParserFilter.go b/internal/handler/insightsParserFilter.go index 8c3de48..8f54c64 100644 --- a/internal/handler/insightsParserFilter.go +++ b/internal/handler/insightsParserFilter.go @@ -4,27 +4,47 @@ import ( "fmt" cdx "github.com/CycloneDX/cyclonedx-go" "github.com/Khan/genqlient/graphql" + "github.com/uselagoon/lagoon/services/insights-handler/internal/lagoonclient" "log/slog" ) -func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, string, error) { +func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiClient graphql.Client, resource ResourceDestination) ([]LagoonFact, []lagoonclient.LagoonProblem, string, error) { source := fmt.Sprintf("insights:sbom:%s", resource.Service) logger := slog.With("ProjectName", resource.Project, "EnvironmentName", resource.Environment, "Source", source) + // ret values + problemSlice := []lagoonclient.LagoonProblem{ + { + Id: 0, + Environment: 0, + Identifier: "test", + Version: "", + FixedVersion: "", + Source: "", + Service: "", + Data: "", + Severity: "", + SeverityScore: 0, + AssociatedPackage: "", + Description: "", + Links: "", + }, + } + if insights.InsightsType != Sbom { - return []LagoonFact{}, "", nil + return []LagoonFact{}, problemSlice, "", nil } bom, err := getBOMfromPayload(v) if err != nil { - return []LagoonFact{}, "", err + return []LagoonFact{}, problemSlice, "", err } // Determine lagoon resource destination _, environment, apiErr := determineResourceFromLagoonAPI(apiClient, resource) if apiErr != nil { - return nil, "", apiErr + return nil, problemSlice, "", apiErr } // we process the SBOM here @@ -32,7 +52,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC if h.ProblemsFromSBOM == true { isAlive, err := IsTrivyServerIsAlive(h.TrivyServerEndpoint) if err != nil { - return nil, "", fmt.Errorf("trivy server not alive: %v", err.Error()) + return nil, problemSlice, "", fmt.Errorf("trivy server not alive: %v", err.Error()) } else { logger.Debug("Trivy is reachable") } @@ -40,7 +60,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC err = SbomToProblems(apiClient, h.TrivyServerEndpoint, "/tmp/", environment.Id, resource.Service, *bom) } if err != nil { - return nil, "", err + return nil, problemSlice, "", err } } @@ -49,11 +69,11 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC facts, err = KeyFactsFilter(facts) if err != nil { - return nil, "", err + return nil, problemSlice, "", err } if len(facts) == 0 { - return nil, "", fmt.Errorf("no facts to process") + return nil, problemSlice, "", 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) @@ -63,7 +83,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC "Length", len(*bom.Components), ) - return facts, source, nil + return facts, problemSlice, source, nil } func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environmentId int, source string) []LagoonFact { diff --git a/internal/handler/main.go b/internal/handler/main.go index 4381f74..ca621a3 100644 --- a/internal/handler/main.go +++ b/internal/handler/main.go @@ -305,6 +305,7 @@ func (h *Messaging) gatherFactsFromInsightData(incoming *InsightsMessage, resour break } lagoonSourceFactMap := LagoonSourceFactMap{} + lagoonSourceProblemMap := LagoonSourceProblemMap{} // since we only have two parser filter types now - let's explicitly call them // First we call the image inspect processor, in case there's anything there @@ -320,14 +321,16 @@ func (h *Messaging) gatherFactsFromInsightData(incoming *InsightsMessage, resour // Then we call the SBOM processor, in case we're dealing with this type if insights.InsightsType == Sbom { - result, source, err := processSbomInsightsData(h, insights, binaryPayload, apiClient, resource) + facts, problems, source, err := processSbomInsightsData(h, insights, binaryPayload, apiClient, resource) if err != nil { slog.Error("Error running filter", "error", err.Error()) } - lagoonSourceFactMap[source] = result + lagoonSourceFactMap[source] = facts + lagoonSourceProblemMap[source] = problems } lagoonSourceFactMapCollection = append(lagoonSourceFactMapCollection, lagoonSourceFactMap) + lagoonSourceProblemMapCollection = append(lagoonSourceProblemMapCollection, lagoonSourceProblemMap) } return lagoonSourceFactMapCollection, lagoonSourceProblemMapCollection, nil