From 1fb25108680f7652ff1704de1466fbfa43829fb7 Mon Sep 17 00:00:00 2001 From: Marco Franssen Date: Wed, 23 Mar 2022 16:04:04 +0100 Subject: [PATCH] Add test for resolver and fix implementation --- pkg/attestation/resolvers/txt/resolver.go | 18 ++++++++- .../resolvers/txt/resolver_test.go | 38 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 pkg/attestation/resolvers/txt/resolver_test.go diff --git a/pkg/attestation/resolvers/txt/resolver.go b/pkg/attestation/resolvers/txt/resolver.go index 5621e6b..e9da584 100644 --- a/pkg/attestation/resolvers/txt/resolver.go +++ b/pkg/attestation/resolvers/txt/resolver.go @@ -3,6 +3,7 @@ package txt import ( "bufio" "io" + "strings" "github.com/package-url/packageurl-go" @@ -26,9 +27,24 @@ func (r *Resolver) Resolve(rc io.Reader) ([]attestation.Attestation, error) { } atts = append(atts, attestation.Attestation{ PURL: purl, - Type: attestation.SBOM, + Type: getType(purl), }) } return atts, nil } + +func getType(p packageurl.PackageURL) attestation.Type { + if attType, ok := p.Qualifiers.Map()["attestation_type"]; ok { + switch strings.ToLower(attType) { + case "provenance": + return attestation.Provenance + case "sbom": + return attestation.SBOM + default: + return attestation.Unknown + } + } + + return attestation.Unknown +} diff --git a/pkg/attestation/resolvers/txt/resolver_test.go b/pkg/attestation/resolvers/txt/resolver_test.go new file mode 100644 index 0000000..774ea22 --- /dev/null +++ b/pkg/attestation/resolvers/txt/resolver_test.go @@ -0,0 +1,38 @@ +package txt_test + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/philips-labs/fatt/pkg/attestation/resolvers/txt" +) + +func TestResolve(t *testing.T) { + assert := assert.New(t) + + purlsFile := `pkg:docker/philips-labs/fatt@sha256:6cc65b2c82c2baa3391890abb8ab741efbcbc87baff3b06d5797afacb314ddd9?repository_url=ghcr.io&attestation_type=sbom +pkg:docker/philips-labs/fatt@sha256:6cc65b2c82c2baa3391890abb8ab741efbcbc87baff3b06d5797afacb314ddd9?repository_url=ghcr.io&attestation_type=provenance +pkg:docker/philips-labs/fatt@sha256:6cc65b2c82c2baa3391890abb8ab741efbcbc87baff3b06d5797afacb314ddd9?repository_url=ghcr.io +pkg:nuget/philips-labs/fatt@sha256:6cc65b2c82c2baa3391890abb8ab741efbcbc87baff3b06d5797afacb314ddd9?repository_url=nuget.org&attestation_type=provenance +pkg:nuget/philips-labs/fatt@sha256:823413cc65b2c82c2baa3391890abb8ab741e87baff3b06d5797afacb314ddd9?repository_url=nuget.org&attestation_type=sbom` + + r := &txt.Resolver{} + atts, err := r.Resolve(strings.NewReader(purlsFile)) + assert.NoError(err) + assert.Len(atts, 5) + + assert.Equal("SBOM", atts[0].Type.String()) + assert.Equal("Provenance", atts[1].Type.String()) + assert.Equal("Unknown", atts[2].Type.String()) + assert.Equal("Provenance", atts[3].Type.String()) + assert.Equal("SBOM", atts[4].Type.String()) + + purlsFile = `pkg:docker/philips-labs/fatt@sha256:6cc65b2c82c2baa3391890abb8ab741efbcbc87baff3b06d5797afacb314ddd9?repository_url=ghcr.io&attestation_type=sbom + ghcr.io/philips-labs/fatt@sha256:6cc65b2c82c2baa3391890abb8ab741efbcbc87baff3b06d5797afacb314ddd9` + atts, err = r.Resolve(strings.NewReader(purlsFile)) + assert.Error(err) + assert.EqualError(err, "scheme is missing") + assert.Len(atts, 0) +}