Skip to content

Commit

Permalink
feat: Add new data structures for an abstract representation of proje…
Browse files Browse the repository at this point in the history
…cts/findings
  • Loading branch information
tarkatronic committed Jul 20, 2023
1 parent 7bff49a commit a311841
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions querying/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package querying
20 changes: 20 additions & 0 deletions querying/ecosystems.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package querying

type FindingEcosystemType uint8

const (
FindingEcosystemApt FindingEcosystemType = iota
FindingEcosystemCSharp
FindingEcosystemDart
FindingEcosystemErlang
FindingEcosystemGHA // GitHub Actions
FindingEcosystemGo
FindingEcosystemJava
FindingEcosystemJS
FindingEcosystemPHP
FindingEcosystemPython
FindingEcosystemRPM
FindingEcosystemRuby
FindingEcosystemRust
FindingEcosystemSwift
)
19 changes: 19 additions & 0 deletions querying/finding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package querying

import "sync"

type FindingIdentifierType uint8

const (
FindingIdentifierCVE FindingIdentifierType = iota
FindingIdentifierGHSA
)

type Finding struct {
Identifiers map[FindingIdentifierType]string
Ecosystem FindingEcosystemType
Severity FindingSeverityType
Description string
PackageName string
mu sync.Mutex
}
76 changes: 76 additions & 0 deletions querying/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package querying

import (
"strings"
"sync"

"golang.org/x/exp/maps"
)

type ProjectCollection struct {
Projects []*Project
mu sync.Mutex
}

type Project struct {
Name string
Findings []*Finding
Links map[string]string
mu sync.Mutex
}

func NewProject(name string) *Project {
return &Project{
Name: name,
Findings: []*Finding{},
Links: map[string]string{},
}
}

func NewProjectCollection() *ProjectCollection {
return &ProjectCollection{
Projects: []*Project{},
}
}

func normalizeProjectName(name string) string {
return strings.Replace(strings.ToLower(name), "-", "_", -1)
}

func (c *ProjectCollection) AddProject(name string) *Project {
c.mu.Lock()
defer c.mu.Unlock()
name = normalizeProjectName(name)
for _, proj := range c.Projects {
if normalizeProjectName(proj.Name) == name {
return proj
}
}
// If we make it past the loop, no existing project was found with this name
newProj := NewProject(name)
c.Projects = append(c.Projects, newProj)
return newProj
}

func (p *Project) AddFinding(identifiers map[FindingIdentifierType]string) *Finding {
var result *Finding
p.mu.Lock()
defer p.mu.Unlock()
for _, finding := range p.Findings {
for idType, id := range finding.Identifiers {
val, ok := identifiers[idType]
if ok && val == id {
result = finding
break
}
}
}
if result == nil {
result = &Finding{
Identifiers: identifiers,
}
} else {
maps.Copy(result.Identifiers, identifiers)
}
return result
}
12 changes: 12 additions & 0 deletions querying/severities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package querying

type FindingSeverityType uint8

const (
FindingSeverityCritical FindingSeverityType = iota
FindingSeverityHigh
FindingSeverityModerate
FindingSeverityLow
FindingSeverityInfo
FindingSeverityUndefined
)

0 comments on commit a311841

Please sign in to comment.