Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions syft/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/bmatcuk/doublestar/v4"
"github.com/mholt/archiver/v3"
digest "github.com/opencontainers/go-digest"
"github.com/spf13/afero"

"github.com/anchore/stereoscope"
Expand All @@ -25,6 +26,7 @@ import (
// Source is an object that captures the data source to be cataloged, configuration, and a specific resolver used
// in cataloging (based on the data source and configuration)
type Source struct {
id string
Image *image.Image // the image object to be cataloged (image only)
Metadata Metadata
directoryResolver *directoryResolver
Expand Down Expand Up @@ -304,6 +306,50 @@ func NewFromImage(img *image.Image, userImageStr string) (Source, error) {
}, nil
}

func (s *Source) ID() string {
return s.id
}

func (s *Source) SetID() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this PR include a call to this function?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Second PR is going to do that so we can link it into relationships - Just wanted to keep this PR small, but I can add the call so we're setting it on construction

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking again, there also isn't ONE great place to put the single call since we have functions like New, NewFromImage NewFromDirectory NewFromFile.

Let me see all the places it needs to be included so we have full coverage of all the source creation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could just have it get set on the first call to ID if s.id = ""

if s.Metadata.Scheme != ImageScheme {
// How do we generate ID for non-image sources?
s.id = digest.FromString(s.Metadata.Path).String()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No path digest here: I think we want what's seen here
https://github.com/anchore/syft/pull/1218/files#diff-b79b344d258161c0ca7b6347af27d5ed643fdb01f4fbe654a69a688efe6a97cf

Getting a sum from the file itself or the metadata from the dir

return
}

// calcuate chain ID for image sources
// https://github.com/opencontainers/image-spec/blob/main/config.md#layer-chainid
s.id = calculateChainID(s.Image)

if s.id == "" {
// TODO what happens here if image has no layers?
s.id = digest.FromString(s.Metadata.ImageMetadata.UserInput).String()
}
return
}

func calculateChainID(img *image.Image) string {
Comment thread
spiffcs marked this conversation as resolved.
if len(img.Layers) < 1 {
return ""
}

// DiffID(L0) = digest of layer 0
// https://github.com/anchore/stereoscope/blob/1b1b744a919964f38d14e1416fb3f25221b761ce/pkg/image/layer_metadata.go#L19-L32
chainID := img.Layers[0].Metadata.Digest
id := chain(chainID, img.Layers[1:])

return id
}

func chain(chainID string, layers []*image.Layer) string {
if len(layers) < 1 {
return chainID
}

chainID = digest.FromString(layers[0].Metadata.Digest + " " + chainID).String()
return chain(chainID, layers[1:])
}

func (s *Source) FileResolver(scope Scope) (FileResolver, error) {
switch s.Metadata.Scheme {
case DirectoryScheme, FileScheme:
Expand Down
38 changes: 38 additions & 0 deletions syft/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,44 @@ func TestNewFromImageFails(t *testing.T) {
})
}

func TestSetID(t *testing.T) {
img := imagetest.GetFixtureImage(t, "oci-archive", "image-simple")
tests := []struct {
name string
input *Source
expected string
}{
{
name: "source.SetID sets the ID for non image sources",
input: &Source{
Metadata: Metadata{
Scheme: FileScheme,
Path: "test-fixtures/image-simple/file-1.txt",
},
},
expected: "sha256:fbfb0730f4306b27c118715998ba58f1ad350f0451513c36c267dc4b9d3b688d",
},
{
name: "source.SetID sets the ID for image sources",
input: &Source{
Image: img,
Metadata: Metadata{
Scheme: ImageScheme,
ImageMetadata: NewImageMetadata(img, "image-simple"),
},
},
expected: "sha256:e6d9f87981af1a1007a42be43b21ba6abe7c1608b1541e877c69052af5356669",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.input.SetID()
assert.Equal(t, test.expected, test.input.ID())
})
}
}

func TestNewFromImage(t *testing.T) {
layer := image.NewLayer(nil)
img := image.Image{
Expand Down