Skip to content

Commit

Permalink
Factoring out further filter core functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Feb 8, 2024
1 parent 2c80dde commit d6731ad
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 56 deletions.
47 changes: 24 additions & 23 deletions internal/handler/insightsFactsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
75 changes: 42 additions & 33 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d6731ad

Please sign in to comment.