Skip to content

Commit

Permalink
Merge pull request #58 from kubescape/sourceInfoFiltering
Browse files Browse the repository at this point in the history
Source info filtering
  • Loading branch information
dwertent committed Apr 30, 2023
2 parents 9dd48dd + decadf1 commit 548ba57
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func main() {
if err != nil {
logger.L().Fatal("error during validation", helpers.Error(err))
}

context.SetBackgroundContext()
// after this line we can use logger.L().Ctx() to attach events to spans

accumulatorChannelError := make(chan error, 10)
acc := accumulator.GetAccumulator()
Expand Down
29 changes: 27 additions & 2 deletions pkg/sbom/v1/sbom_spdx_storage_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,22 @@ const (
KubescapeNodeAgentName = "KubescapeNodeAgent"
RelationshipContainType = "CONTAINS"
directorySBOM = "SBOM"
sourceInfoDotnet = "acquired package info from dotnet project assets file"
sourceInfoNodeModule = "acquired package info from installed node module manifest file"
sourceInfoPythonPackage = "acquired package info from installed python package manifest file"
sourceInfoJava = "acquired package info from installed java archive"
sourceInfoGemFile = "acquired package info from installed gem metadata file"
sourceInfoGoModule = "acquired package info from go module information"
sourceInfoRustCargo = "acquired package info from rust cargo manifest"
sourceInfoPHPComposer = "acquired package info from PHP composer manifest"
sourceInfoCabal = "acquired package info from cabal or stack manifest files"
sourceInfoRebar = "acquired package info from rebar3 or mix manifest file"
sourceInfoLinuxKernel = "acquired package info from linux kernel archive"
sourceInfoLinuxKernelModule = "acquired package info from linux kernel module files"
)

var spdxDataDirPath string
var sourceInfoRequiredPrefix []string

type SBOMData struct {
spdxDataPath string
Expand Down Expand Up @@ -62,6 +75,8 @@ func createSBOMDir() {

func init() {
createSBOMDir()
sourceInfoPrefixData := []string{sourceInfoDotnet, sourceInfoNodeModule, sourceInfoPythonPackage, sourceInfoJava, sourceInfoGemFile, sourceInfoGoModule, sourceInfoRustCargo, sourceInfoPHPComposer, sourceInfoCabal, sourceInfoRebar, sourceInfoLinuxKernel, sourceInfoLinuxKernelModule}
sourceInfoRequiredPrefix = append(sourceInfoRequiredPrefix,sourceInfoPrefixData...)
}

func CreateSBOMDataSPDXVersionV040(instanceID instanceidhandler.IInstanceID) SBOMFormat {
Expand Down Expand Up @@ -98,8 +113,18 @@ func (sbom *SBOMData) saveSBOM(spdxData *spdxv1beta1.SBOMSPDXv2p3) error {
}

func parsedFilesBySourceInfo(packageSourceInfo string) []string {
fileListInString := utils.After(packageSourceInfo, ": ")
return strings.Split(fileListInString, ", ")
needToMonitor := false
for i := range sourceInfoRequiredPrefix {
if strings.Contains(packageSourceInfo, sourceInfoRequiredPrefix[i]) {
needToMonitor = true
break
}
}
if needToMonitor {
fileListInString := utils.After(packageSourceInfo, ": ")
return strings.Split(fileListInString, ", ")
}
return []string{}
}

func (sbom *SBOMData) StoreSBOM(sbomData any) error {
Expand Down
25 changes: 25 additions & 0 deletions pkg/sbom/v1/sbom_spdx_storage_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,28 @@ func TestParsedFilesBySourceInfo(t *testing.T) {
t.Fatalf("list[2] should be %s, not %s", "/usr/local/lib/python3.10/site-packages/Deprecated-1.2.13.dist-info/top_level.txt", list[2])
}
}

func TestParsedFilesBySourceInfoFiltered(t *testing.T) {
shouldBeSourcesInfo := []string{"acquired package info from dotnet project assets file: 123, 456", "acquired package info from installed node module manifest file: 123, 456", "acquired package info from installed python package manifest file: 123, 456", "acquired package info from installed java archive: 123, 456", "acquired package info from installed gem metadata file: 123, 456", "acquired package info from go module information: 123, 456", "acquired package info from rust cargo manifest: 123, 456", "acquired package info from PHP composer manifest: 123, 456", "acquired package info from cabal or stack manifest files: 123, 456", "acquired package info from rebar3 or mix manifest files: 123, 456", "acquired package info from linux kernel archive: 123, 456", "acquired package info from linux kernel module files: 123, 456"}
for i := range shouldBeSourcesInfo {
list := parsedFilesBySourceInfo(shouldBeSourcesInfo[i])
if len(list) != 2 {
t.Fatalf("source Info %s: parsed source Info list must be equal to 2", shouldBeSourcesInfo[i])
}
if list[0] != "123" {
t.Fatalf("list[0] should be %s, not %s", "/usr/local/lib/python3.10/site-packages/Deprecated-1.2.13.dist-info/METADATA", list[0])
}
if list[1] != "456" {
t.Fatalf("list[1] should be %s, not %s", "/usr/local/lib/python3.10/site-packages/Deprecated-1.2.13.dist-info/RECORD", list[1])
}
}


shouldNotBeSourcesInfo := []string{"acquired package info from ALPM DB: 1234, 456", "acquired package info from RPM DB: 1234, 456", "acquired package info from APK DB: 1234, 456", "acquired package info from DPKG DB: 1234, 456", "acquired package info from installed cocoapods manifest file: 1234, 456", "acquired package info from conan manifest: 1234, 456", "acquired package info from portage DB: 1234, 456", "acquired package info from nix store path: 123, 456"}
for i := range shouldNotBeSourcesInfo {
list := parsedFilesBySourceInfo(shouldNotBeSourcesInfo[i])
if len(list) != 0 {
t.Fatalf("source Info %s: parsed source Info list must be equal to 0", shouldNotBeSourcesInfo[i])
}
}
}

0 comments on commit 548ba57

Please sign in to comment.