Skip to content

Commit 955df03

Browse files
authored
feat(sbom): Include the distro qualifier as part of the APK PURL (#2078)
* Include the distro qualifier * Update e2e test data * Don't be a double distrogit add . * Don't set distro if unknown * Fix test
1 parent f66cb12 commit 955df03

File tree

5 files changed

+108
-4
lines changed

5 files changed

+108
-4
lines changed

examples/go-install.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# please see go-install.yaml in this directory.
1212
package:
1313
name: hello
14-
version: v0.0.1
14+
version: 0.0.1
1515
epoch: 0
1616
description: "A project that will greet the world infinitely"
1717

@@ -26,4 +26,4 @@ pipeline:
2626
- uses: go/install
2727
with:
2828
package: github.com/puerco/hello
29-
version: ${{package.version}}
29+
version: v${{package.version}}

pkg/build/testdata/goldenfiles/sboms/7zip-two-fetches-2301-r3.spdx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"externalRefs": [
4242
{
4343
"referenceCategory": "PACKAGE-MANAGER",
44-
"referenceLocator": "pkg:apk/wolfi/7zip-two-fetches@2301-r3?arch=x86_64",
44+
"referenceLocator": "pkg:apk/wolfi/7zip-two-fetches@2301-r3?arch=x86_64\u0026distro=wolfi",
4545
"referenceType": "purl"
4646
}
4747
]

pkg/build/testdata/goldenfiles/sboms/crane-0.20.2-r1.spdx.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"externalRefs": [
4242
{
4343
"referenceCategory": "PACKAGE-MANAGER",
44-
"referenceLocator": "pkg:apk/wolfi/[email protected]?arch=x86_64",
44+
"referenceLocator": "pkg:apk/wolfi/[email protected]?arch=x86_64\u0026distro=wolfi",
4545
"referenceType": "purl"
4646
}
4747
]

pkg/config/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ func newAPKPackageURL(distro, name, version, arch string) *purl.PackageURL {
266266
Version: version,
267267
}
268268

269+
if distro != "unknown" {
270+
u.Qualifiers = append(u.Qualifiers, purl.Qualifier{
271+
Key: "distro",
272+
Value: distro,
273+
})
274+
}
275+
269276
if arch != "" {
270277
u.Qualifiers = append(u.Qualifiers, purl.Qualifier{
271278
Key: "arch",

pkg/config/config_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,3 +1289,100 @@ func TestSetCapability(t *testing.T) {
12891289
})
12901290
}
12911291
}
1292+
1293+
func TestPackageURL(t *testing.T) {
1294+
tests := []struct {
1295+
name string
1296+
pkg Package
1297+
distro string
1298+
arch string
1299+
expected string
1300+
}{
1301+
{
1302+
name: "basic package URL",
1303+
pkg: Package{
1304+
Name: "test-package",
1305+
Version: "1.0.0",
1306+
Epoch: 0,
1307+
},
1308+
distro: "alpine",
1309+
arch: "x86_64",
1310+
expected: "pkg:apk/alpine/[email protected]?arch=x86_64&distro=alpine",
1311+
},
1312+
{
1313+
name: "package with epoch",
1314+
pkg: Package{
1315+
Name: "test-package",
1316+
Version: "2.1.3",
1317+
Epoch: 5,
1318+
},
1319+
distro: "wolfi",
1320+
arch: "aarch64",
1321+
expected: "pkg:apk/wolfi/[email protected]?arch=aarch64&distro=wolfi",
1322+
},
1323+
{
1324+
name: "package without architecture",
1325+
pkg: Package{
1326+
Name: "test-package",
1327+
Version: "1.0.0",
1328+
Epoch: 0,
1329+
},
1330+
distro: "alpine",
1331+
arch: "",
1332+
expected: "pkg:apk/alpine/[email protected]?distro=alpine",
1333+
},
1334+
{
1335+
name: "package with complex version",
1336+
pkg: Package{
1337+
Name: "complex-package",
1338+
Version: "1.2.3-alpha.1",
1339+
Epoch: 10,
1340+
},
1341+
distro: "chainguard",
1342+
arch: "x86_64",
1343+
expected: "pkg:apk/chainguard/[email protected]?arch=x86_64&distro=chainguard",
1344+
},
1345+
{
1346+
name: "package with special characters in name",
1347+
pkg: Package{
1348+
Name: "lib-test_package.so",
1349+
Version: "0.1.0",
1350+
Epoch: 1,
1351+
},
1352+
distro: "alpine",
1353+
arch: "arm64",
1354+
expected: "pkg:apk/alpine/[email protected]?arch=arm64&distro=alpine",
1355+
},
1356+
{
1357+
name: "package with unknown distro",
1358+
pkg: Package{
1359+
Name: "lib-test_package.so",
1360+
Version: "0.1.0",
1361+
Epoch: 1,
1362+
},
1363+
distro: "unknown",
1364+
arch: "arm64",
1365+
expected: "pkg:apk/unknown/[email protected]?arch=arm64",
1366+
},
1367+
}
1368+
1369+
for _, tt := range tests {
1370+
t.Run(tt.name, func(t *testing.T) {
1371+
packageURL := tt.pkg.PackageURL(tt.distro, tt.arch)
1372+
require.NotNil(t, packageURL)
1373+
1374+
actualURL := packageURL.String()
1375+
require.Equal(t, tt.expected, actualURL, "PackageURL string representation should match expected format")
1376+
1377+
// Verify the PackageURL can be parsed back
1378+
parsed, err := purl.FromString(actualURL)
1379+
require.NoError(t, err, "Generated PackageURL should be parseable")
1380+
1381+
// Verify components
1382+
require.Equal(t, purlTypeAPK, parsed.Type)
1383+
require.Equal(t, tt.distro, parsed.Namespace)
1384+
require.Equal(t, tt.pkg.Name, parsed.Name)
1385+
require.Equal(t, tt.pkg.FullVersion(), parsed.Version)
1386+
})
1387+
}
1388+
}

0 commit comments

Comments
 (0)