Skip to content

Commit

Permalink
chore: improve dataflow report for component (#151)
Browse files Browse the repository at this point in the history
* chore: improve dataflow report for component

* fix: improve tests
  • Loading branch information
cfabianski committed Nov 22, 2022
1 parent 6a9e591 commit 644b7ff
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 29 deletions.
3 changes: 2 additions & 1 deletion pkg/classification/db/recipes/atlassian_cloud.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"type": "external_service",
"urls": [
"https://api.atlassian.com",
"https://atlassian.net/rest/api/"
"https://atlassian.net/rest/api/",
"https://auth.atlassian.com"
],
"packages": [
{
Expand Down
8 changes: 8 additions & 0 deletions pkg/classification/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ type RecipeURLMatch struct {
var ErrInvalidRecipes = errors.New("invalid interface recipe")
var ErrInvalidInternalDomainRegexp = errors.New("could not parse internal domains as regexp")

func (classification *Classification) Name() string {
if classification.RecipeMatch {
return classification.RecipeName
} else {
return classification.URL
}
}

func New(config Config) (*Classifier, error) {
// prepare regular expressions for recipes
var preparedRecipes []Recipe
Expand Down
28 changes: 23 additions & 5 deletions pkg/report/output/dataflow/components/components.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package components

import (
"strings"

"github.com/bearer/curio/pkg/report/output/dataflow/detectiondecoder"
"github.com/bearer/curio/pkg/report/output/dataflow/types"

Expand All @@ -20,10 +18,12 @@ type component struct {
uuid string
detectors map[string]*detector // group detectors by detectorName
}

type detector struct {
name string
files map[string]*fileHolder // group files by filename
}

type fileHolder struct {
name string
lineNumbers map[int]int //group lines by linenumber
Expand All @@ -47,7 +47,13 @@ func (holder *Holder) AddInterface(detection interface{}) error {
}

if value.Classification.Decision.State == classify.Valid {
holder.addComponent(strings.ToLower(value.Classification.RecipeName), value.Classification.RecipeUUID, string(value.DetectorType), value.Source.Filename, *value.Source.LineNumber)
holder.addComponent(
value.Classification.Name(),
value.Classification.RecipeUUID,
string(value.DetectorType),
value.Source.Filename,
*value.Source.LineNumber,
)
}

return nil
Expand All @@ -64,7 +70,13 @@ func (holder *Holder) AddDependency(detection interface{}) error {
}

if value.Classification.Decision.State == classify.Valid {
holder.addComponent(strings.ToLower(value.Classification.RecipeName), value.Classification.RecipeUUID, string(value.DetectorType), value.Source.Filename, *value.Source.LineNumber)
holder.addComponent(
value.Classification.RecipeName,
value.Classification.RecipeUUID,
string(value.DetectorType),
value.Source.Filename,
*value.Source.LineNumber,
)
}

return nil
Expand All @@ -81,7 +93,13 @@ func (holder *Holder) AddFramework(detection interface{}) error {
}

if value.Classification.Decision.State == classify.Valid {
holder.addComponent(strings.ToLower(value.Classification.RecipeName), value.Classification.RecipeUUID, string(value.DetectorType), value.Source.Filename, *value.Source.LineNumber)
holder.addComponent(
value.Classification.RecipeName,
value.Classification.RecipeUUID,
string(value.DetectorType),
value.Source.Filename,
*value.Source.LineNumber,
)
}

return nil
Expand Down
32 changes: 16 additions & 16 deletions pkg/report/output/dataflow/components/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestDataflowComponents(t *testing.T) {
Want []types.Component
}{
{
Name: "single detection - dependency",
FileContent: `{ "detector_type": "gemfile-lock", "type": "dependency_classified", "source": {"filename": "Gemfile.lock", "line_number": 258}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "stripe"}}`,
Name: "single detection - dependency",
FileContent: `{ "detector_type": "gemfile-lock", "type": "dependency_classified", "source": {"filename": "Gemfile.lock", "line_number": 258}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe", "recipe_match": true}}`,
Want: []types.Component{
{
Name: "stripe",
Name: "Stripe",
Locations: []types.ComponentLocation{
{
Detector: "gemfile-lock",
Expand All @@ -35,16 +35,16 @@ func TestDataflowComponents(t *testing.T) {
},
},
{
Name: "single detection - dependency - no classification",
Name: "single detection - dependency - no classification",
FileContent: `{ "detector_type": "gemfile-lock", "type": "dependency_classified", "source": {"filename": "Gemfile.lock", "line_number": 258}}`,
Want: []types.Component{},
Want: []types.Component{},
},
{
Name: "single detection - interface",
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe"}}`,
Name: "single detection - interface",
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe", "recipe_match": true}}`,
Want: []types.Component{
{
Name: "stripe",
Name: "Stripe",
Locations: []types.ComponentLocation{
{
Detector: "ruby",
Expand All @@ -56,17 +56,17 @@ func TestDataflowComponents(t *testing.T) {
},
},
{
Name: "single detection - interface - no classification",
Name: "single detection - interface - no classification",
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}}`,
Want: []types.Component{},
Want: []types.Component{},
},
{
Name: "single detection - duplicates",
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe"}}
{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe"}}`,
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe", "recipe_match": true}}
{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe", "recipe_match": true}}`,
Want: []types.Component{
{
Name: "stripe",
Name: "Stripe",
Locations: []types.ComponentLocation{
{
Detector: "ruby",
Expand All @@ -79,11 +79,11 @@ func TestDataflowComponents(t *testing.T) {
},
{
Name: "multiple detections - deterministic output",
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe"}}
{"detector_type": "gemfile-lock", "type": "dependency_classified", "source": {"filename": "Gemfile.lock", "line_number": 258}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "stripe"}}`,
FileContent: `{ "detector_type": "ruby", "type": "interface_classified", "source": {"filename": "billing.rb", "line_number": 2}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe", "recipe_match": true}}
{"detector_type": "gemfile-lock", "type": "dependency_classified", "source": {"filename": "Gemfile.lock", "line_number": 258}, "classification": { "Decision": { "state": "valid" }, "recipe_name": "Stripe", "recipe_match": true}}`,
Want: []types.Component{
{
Name: "stripe",
Name: "Stripe",
Locations: []types.ComponentLocation{
{
Detector: "gemfile-lock",
Expand Down
1 change: 0 additions & 1 deletion pkg/report/output/dataflow/dataflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func GetOutput(input []interface{}, config settings.Config, isInternal bool) (*D
return nil, err
}
}

}

dataflow := &DataFlow{
Expand Down
10 changes: 5 additions & 5 deletions pkg/report/output/dataflow/types/components.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package types

type Component struct {
Name string
Name string `json:"name"`
UUID string `json:"uuid,omitempty" yaml:"uuid,omitempty"`
Locations []ComponentLocation
Locations []ComponentLocation `json:"locations"`
}

type ComponentLocation struct {
Detector string
Filename string
LineNumber int
Detector string `json:"detector"`
Filename string `json:"filename"`
LineNumber int `json:"line_number"`
}
2 changes: 1 addition & 1 deletion pkg/util/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (path *Path) Exists() bool {
}

if err != nil {
log.Error().Msgf("error checking if file '%s' exists: %s", path.AbsolutePath, err)
log.Debug().Msgf("file '%s' doesn't exist", path.AbsolutePath)
return false
}

Expand Down

0 comments on commit 644b7ff

Please sign in to comment.