From 69d2b1ba3cad82f53c17a7d01eba5d914c2af7c3 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Mon, 8 Nov 2021 16:28:09 -0500 Subject: [PATCH] add artifact.Identifiable by Identity() method Signed-off-by: Alex Goodman --- internal/formats/syftjson/to_format_model.go | 4 ++-- syft/artifact/id.go | 4 ++++ syft/artifact/relationship.go | 4 ++-- syft/pkg/ownership_by_files_relationship.go | 4 ++-- .../pkg/ownership_by_files_relationship_test.go | 17 +++++++++++++---- syft/pkg/package.go | 5 +++++ 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/formats/syftjson/to_format_model.go b/internal/formats/syftjson/to_format_model.go index 972ce224e..7c148c8ee 100644 --- a/internal/formats/syftjson/to_format_model.go +++ b/internal/formats/syftjson/to_format_model.go @@ -93,8 +93,8 @@ func toRelationshipModel(relationships []artifact.Relationship) []model.Relation result := make([]model.Relationship, len(relationships)) for i, r := range relationships { result[i] = model.Relationship{ - Parent: string(r.From), - Child: string(r.To), + Parent: string(r.From.Identity()), + Child: string(r.To.Identity()), Type: string(r.Type), Metadata: r.Data, } diff --git a/syft/artifact/id.go b/syft/artifact/id.go index 890e4b4ef..82b1251ed 100644 --- a/syft/artifact/id.go +++ b/syft/artifact/id.go @@ -2,3 +2,7 @@ package artifact // ID represents a unique value for each package added to a package catalog. type ID string + +type Identifiable interface { + Identity() ID +} diff --git a/syft/artifact/relationship.go b/syft/artifact/relationship.go index be81a311d..5292c7b04 100644 --- a/syft/artifact/relationship.go +++ b/syft/artifact/relationship.go @@ -8,8 +8,8 @@ const ( type RelationshipType string type Relationship struct { - From ID `json:"from"` - To ID `json:"to"` + From Identifiable `json:"from"` + To Identifiable `json:"to"` Type RelationshipType `json:"type"` Data interface{} `json:"data,omitempty"` } diff --git a/syft/pkg/ownership_by_files_relationship.go b/syft/pkg/ownership_by_files_relationship.go index 90cc31c49..b33c79431 100644 --- a/syft/pkg/ownership_by_files_relationship.go +++ b/syft/pkg/ownership_by_files_relationship.go @@ -28,8 +28,8 @@ func ownershipByFilesRelationships(catalog *Catalog) []artifact.Relationship { for parent, children := range relationships { for child, files := range children { edges = append(edges, artifact.Relationship{ - From: parent, - To: child, + From: catalog.byID[parent], + To: catalog.byID[child], Type: artifact.OwnershipByFileOverlapRelationship, Data: ownershipByFilesMetadata{ Files: files.List(), diff --git a/syft/pkg/ownership_by_files_relationship_test.go b/syft/pkg/ownership_by_files_relationship_test.go index caf01e98b..d8245108b 100644 --- a/syft/pkg/ownership_by_files_relationship_test.go +++ b/syft/pkg/ownership_by_files_relationship_test.go @@ -10,7 +10,16 @@ import ( "github.com/anchore/syft/syft/source" ) +type node struct { + id string +} + +func (n node) Identity() artifact.ID { + return artifact.ID(n.id) +} + func TestOwnershipByFilesRelationship(t *testing.T) { + tests := []struct { name string pkgs []Package @@ -58,8 +67,8 @@ func TestOwnershipByFilesRelationship(t *testing.T) { }, expectedRelations: []artifact.Relationship{ { - From: "parent", - To: "child", + From: node{"parent"}, + To: node{"child"}, Type: artifact.OwnershipByFileOverlapRelationship, Data: ownershipByFilesMetadata{ Files: []string{ @@ -111,8 +120,8 @@ func TestOwnershipByFilesRelationship(t *testing.T) { }, expectedRelations: []artifact.Relationship{ { - From: "parent", - To: "child", + From: node{"parent"}, + To: node{"child"}, Type: artifact.OwnershipByFileOverlapRelationship, Data: ownershipByFilesMetadata{ Files: []string{ diff --git a/syft/pkg/package.go b/syft/pkg/package.go index 937aeb60b..062780b46 100644 --- a/syft/pkg/package.go +++ b/syft/pkg/package.go @@ -30,6 +30,11 @@ type Package struct { Metadata interface{} // additional data found while parsing the package source } +func (p Package) Identity() artifact.ID { + // TODO: tie this into the fingerprint system on rebase + return p.ID +} + // Stringer to represent a package. func (p Package) String() string { return fmt.Sprintf("Pkg(type=%s, name=%s, version=%s)", p.Type, p.Name, p.Version)