diff --git a/internal/handler/insightsFactsParserFilter.go b/internal/handler/insightsFactsParserFilter.go index b2946a1..0343188 100644 --- a/internal/handler/insightsFactsParserFilter.go +++ b/internal/handler/insightsFactsParserFilter.go @@ -19,35 +19,36 @@ func processFactsInsightsData(h *Messaging, insights InsightsData, v string, api source := fmt.Sprintf("insights:facts:%s", resource.Service) logger := slog.With("ProjectName", resource.Project, "EnvironmentName", resource.Environment, "Source", source) if insights.LagoonType == Facts && insights.InsightsType == Raw { - r := strings.NewReader(v) + return ProcessFactsData(v, logger, source) + } + return nil, "", nil +} - // Decode base64 - //dec := base64.NewDecoder(base64.StdEncoding, r) - res, err := ioutil.ReadAll(r) - if err != nil { - slog.Error("Error reading insights data", "Error", err) - } +func ProcessFactsData(v string, logger *slog.Logger, source string) ([]LagoonFact, string, error) { + r := strings.NewReader(v) + // Decode base64 + res, err := ioutil.ReadAll(r) + if err != nil { + slog.Error("Error reading insights data", "Error", err) + } - facts, err := processFactsFromJSON(logger, res, source) - if err != nil { - return nil, "", err - } + facts, err := processFactsFromJSON(logger, res, source) + if err != nil { + return nil, "", err + } - facts, err = KeyFactsFilter(facts) - if err != nil { - return nil, "", err - } + facts, err = KeyFactsFilter(facts) + if err != nil { + return nil, "", err + } - if len(facts) == 0 { - return nil, "", fmt.Errorf("no facts to process") - } + if len(facts) == 0 { + return nil, "", fmt.Errorf("no facts to process") + } - //log.Printf("Successfully processed %d fact(s), for '%s:%s', from source '%s'", len(facts), resource.Project, resource.Environment, source) - logger.Info("Successfully processed facts", "number", len(facts)) + logger.Info("Successfully processed facts", "number", len(facts)) - return facts, source, nil - } - return nil, "", nil + return facts, source, nil } func processFactsFromJSON(logger *slog.Logger, facts []byte, source string) ([]LagoonFact, error) { diff --git a/internal/handler/insightsParserFilter.go b/internal/handler/insightsParserFilter.go index 5db42a7..bf4aab1 100644 --- a/internal/handler/insightsParserFilter.go +++ b/internal/handler/insightsParserFilter.go @@ -23,39 +23,9 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC return []LagoonFact{}, "", nil } - bom := new(cdx.BOM) - - // Decode base64 - r := strings.NewReader(v) - dec := base64.NewDecoder(base64.StdEncoding, r) - - res, err := ioutil.ReadAll(dec) + bom, err := getBOMfromPayload(v) if err != nil { - return nil, "", err - } - - fileType := http.DetectContentType(res) - - if fileType != "application/zip" && fileType != "application/x-gzip" && fileType != "application/gzip" { - decoder := cdx.NewBOMDecoder(bytes.NewReader(res), cdx.BOMFileFormatJSON) - if err = decoder.Decode(bom); err != nil { - return nil, "", err - } - } else { - // Compressed cyclonedx sbom - result, decErr := decodeGzipString(v) - if decErr != nil { - return nil, "", decErr - } - b, mErr := json.MarshalIndent(result, "", " ") - if mErr != nil { - return nil, "", mErr - } - - decoder := cdx.NewBOMDecoder(bytes.NewReader(b), cdx.BOMFileFormatJSON) - if err = decoder.Decode(bom); err != nil { - return nil, "", err - } + return []LagoonFact{}, "", err } // Determine lagoon resource destination @@ -65,7 +35,7 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC } // 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 { @@ -103,6 +73,45 @@ func processSbomInsightsData(h *Messaging, insights InsightsData, v string, apiC return facts, source, nil } +// getBOMfromPayload is used to extract a *cdx.BOM from an incoming payload +func getBOMfromPayload(v string) (*cdx.BOM, error) { + bom := new(cdx.BOM) + + // Decode base64 + r := strings.NewReader(v) + dec := base64.NewDecoder(base64.StdEncoding, r) + + res, err := ioutil.ReadAll(dec) + if err != nil { + return nil, err + } + + fileType := http.DetectContentType(res) + + if fileType != "application/zip" && fileType != "application/x-gzip" && fileType != "application/gzip" { + decoder := cdx.NewBOMDecoder(bytes.NewReader(res), cdx.BOMFileFormatJSON) + if err = decoder.Decode(bom); err != nil { + return nil, err + } + } else { + // Compressed cyclonedx sbom + result, decErr := decodeGzipString(v) + if decErr != nil { + return nil, decErr + } + b, mErr := json.MarshalIndent(result, "", " ") + if mErr != nil { + return nil, mErr + } + + decoder := cdx.NewBOMDecoder(bytes.NewReader(b), cdx.BOMFileFormatJSON) + if err = decoder.Decode(bom); err != nil { + return nil, err + } + } + return bom, nil +} + func processFactsFromSBOM(logger *slog.Logger, facts *[]cdx.Component, environmentId int, source string) []LagoonFact { var factsInput []LagoonFact if len(*facts) == 0 {