Skip to content

Commit

Permalink
Testing passing back problems
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaize Kaye committed Apr 15, 2024
1 parent 4910d8b commit 75d65e8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
38 changes: 29 additions & 9 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,63 @@ 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
// 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())
return nil, problemSlice, "", 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
return nil, problemSlice, "", err
}
}

Expand All @@ -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)
Expand All @@ -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 {
Expand Down
7 changes: 5 additions & 2 deletions internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 75d65e8

Please sign in to comment.