Skip to content

Commit

Permalink
Moves bom decoder to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Feb 8, 2024
1 parent d6731ad commit bafef36
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 47 deletions.
50 changes: 50 additions & 0 deletions internal/handler/bomutils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package handler

import (
"bytes"
"encoding/base64"
"encoding/json"
"github.com/CycloneDX/cyclonedx-go"
"io/ioutil"
"net/http"
"strings"
)

// getBOMfromPayload is used to extract a *cdx.BOM from an incoming payload
func getBOMfromPayload(v string) (*cyclonedx.BOM, error) {
bom := new(cyclonedx.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 := cyclonedx.NewBOMDecoder(bytes.NewReader(res), cyclonedx.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 := cyclonedx.NewBOMDecoder(bytes.NewReader(b), cyclonedx.BOMFileFormatJSON)
if err = decoder.Decode(bom); err != nil {
return nil, err
}
}
return bom, nil
}
48 changes: 1 addition & 47 deletions internal/handler/insightsParserFilter.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package handler

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log/slog"
"net/http"
"strings"

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) {
Expand Down Expand Up @@ -73,45 +66,6 @@ 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 bafef36

Please sign in to comment.