Skip to content

Commit 126a5c5

Browse files
committed
fix: enhance PURL conversion to handle additional types and improve error messaging
Signed-off-by: Gagan H R <hrgagan4@gmail.com>
1 parent e51a89c commit 126a5c5

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

pkg/assembler/helpers/purl.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,17 @@ func purlConvert(p purl.PackageURL) (*model.PkgInputSpec, error) {
159159
// so that they can be referenced with higher specificity in GUAC
160160
//
161161
// PURL types not defined in purl library handled generically
162-
case "alpine", "alpm", "apk", "huggingface", "githubactions", "mlflow", "qpkg", "pub", "swid", PurlTypeGuac:
162+
case "alpine", "githubactions", PurlTypeGuac:
163163
fallthrough
164164
// PURL types defined in purl library handled generically
165165
case purl.TypeBitbucket, purl.TypeCocoapods, purl.TypeCargo,
166166
purl.TypeComposer, purl.TypeConan, purl.TypeConda, purl.TypeCran,
167167
purl.TypeDebian, purl.TypeGem, purl.TypeGithub,
168168
purl.TypeGolang, purl.TypeHackage, purl.TypeHex, purl.TypeMaven,
169169
purl.TypeNPM, purl.TypeNuget, purl.TypePyPi, purl.TypeRPM, purl.TypeSwift,
170-
purl.TypeGeneric, purl.TypeYocto, purl.TypeCpan:
171-
// some code
170+
purl.TypeGeneric, purl.TypeYocto, purl.TypeCpan,
171+
purl.TypeAlpm, purl.TypeApk, purl.TypeBitnami, purl.TypeHuggingface,
172+
purl.TypeMLFlow, purl.TypePub, purl.TypeQpkg, purl.TypeSWID:
172173
r := pkg(p.Type, p.Namespace, p.Name, p.Version, p.Subpath, p.Qualifiers.Map())
173174
return r, nil
174175

@@ -223,9 +224,13 @@ func purlConvert(p purl.PackageURL) (*model.PkgInputSpec, error) {
223224
return r, nil
224225

225226
default:
226-
// unhandled types should throw an error so we can make sure to review the
227-
// implementation of newly introduced PURL types.
228-
return nil, fmt.Errorf("unhandled PURL type: %s", p.Type)
227+
if _, ok := purl.KnownTypes[p.Type]; ok {
228+
return nil, fmt.Errorf("unhandled known PURL type: %q. Please report at https://github.com/guacsec/guac/issues/new", p.Type)
229+
}
230+
if _, ok := purl.CandidateTypes[p.Type]; ok {
231+
return nil, fmt.Errorf("unhandled candidate PURL type: %q. Please report at https://github.com/guacsec/guac/issues/new", p.Type)
232+
}
233+
return nil, fmt.Errorf("unsupported PURL type: %q", p.Type)
229234
}
230235
}
231236

pkg/assembler/helpers/purl_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package helpers
1717

1818
import (
1919
"fmt"
20+
"strings"
2021
"testing"
2122

2223
"github.com/google/go-cmp/cmp"
@@ -216,6 +217,9 @@ func TestPurlConvert(t *testing.T) {
216217
}, {
217218
purlUri: "pkg:cpan/Pod-Perldoc@3.20",
218219
expected: pkg("cpan", "", "Pod-Perldoc", "3.20", "", map[string]string{}),
220+
}, {
221+
purlUri: "pkg:bitnami/apache@2.4.57",
222+
expected: pkg("bitnami", "", "apache", "2.4.57", "", map[string]string{}),
219223
},
220224
}
221225

@@ -234,6 +238,38 @@ func TestPurlConvert(t *testing.T) {
234238
}
235239
}
236240

241+
func TestPurlConvertUnhandledTypes(t *testing.T) {
242+
testCases := []struct {
243+
name string
244+
purlUri string
245+
expectedErr string
246+
}{
247+
{
248+
name: "unhandled candidate purl type",
249+
purlUri: "pkg:helm/prometheus@14.0.0",
250+
expectedErr: "unhandled candidate PURL type",
251+
},
252+
{
253+
name: "unsupported unknown purl type",
254+
purlUri: "pkg:customtype/mynamespace/mypackage@1.0.0",
255+
expectedErr: "unsupported PURL type",
256+
},
257+
}
258+
259+
for _, tt := range testCases {
260+
t.Run(tt.name, func(t *testing.T) {
261+
_, err := PurlToPkg(tt.purlUri)
262+
if err == nil {
263+
t.Errorf("expected error for purl %s, got nil", tt.purlUri)
264+
return
265+
}
266+
if !strings.Contains(err.Error(), tt.expectedErr) {
267+
t.Errorf("expected error containing %q, got: %v", tt.expectedErr, err)
268+
}
269+
})
270+
}
271+
}
272+
237273
func TestPkgInputSpecToPurl(t *testing.T) {
238274
testCases := []struct {
239275
expectedPurlUri string

0 commit comments

Comments
 (0)