-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathpurl.go
475 lines (425 loc) · 13.3 KB
/
purl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package purl
import (
"fmt"
"strconv"
"strings"
cn "github.com/google/go-containerregistry/pkg/name"
version "github.com/knqyf263/go-rpm-version"
packageurl "github.com/package-url/packageurl-go"
"golang.org/x/xerrors"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/scanner/utils"
"github.com/aquasecurity/trivy/pkg/types"
)
const (
TypeOCI = "oci"
TypeDart = "dart"
// TypeK8s is a custom type for Kubernetes components in PURL.
// - namespace: The service provider such as EKS or GKE. It is not case sensitive and must be lowercased.
// Known namespaces:
// - empty (upstream)
// - eks (AWS)
// - aks (GCP)
// - gke (Azure)
// - rke (Rancher)
// - name: The k8s component name and is case sensitive.
// - version: The combined version and release of a component.
//
// Examples:
// - pkg:k8s/upstream/k8s.io%[email protected]
// - pkg:k8s/eks/k8s.io%[email protected]
TypeK8s = "k8s"
NamespaceEKS = "eks"
NamespaceAKS = "aks"
NamespaceGKE = "gke"
NamespaceRKE = "rke"
NamespaceOCP = "ocp"
TypeUnknown = "unknown"
)
type PackageURL struct {
packageurl.PackageURL
FilePath string
}
func FromString(purl string) (*PackageURL, error) {
p, err := packageurl.FromString(purl)
if err != nil {
return nil, xerrors.Errorf("failed to parse purl(%s): %w", purl, err)
}
return &PackageURL{
PackageURL: p,
}, nil
}
func (p *PackageURL) Package() *ftypes.Package {
pkg := &ftypes.Package{
Name: p.Name,
Version: p.Version,
}
for _, q := range p.Qualifiers {
switch q.Key {
case "arch":
pkg.Arch = q.Value
case "modularitylabel":
pkg.Modularitylabel = q.Value
case "epoch":
epoch, err := strconv.Atoi(q.Value)
if err == nil {
pkg.Epoch = epoch
}
}
}
// CocoaPods purl has no namespace, but has subpath
// https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#cocoapods
if p.Type == packageurl.TypeCocoapods && p.Subpath != "" {
// CocoaPods uses <moduleName>/<submoduleName> format for package name
// e.g. `pkg:cocoapods/[email protected]#NSData+zlib` => `GoogleUtilities/NSData+zlib`
pkg.Name = p.Name + "/" + p.Subpath
}
if p.Type == packageurl.TypeRPM {
rpmVer := version.NewVersion(p.Version)
pkg.Release = rpmVer.Release()
pkg.Version = rpmVer.Version()
}
// Return packages without namespace.
// OS packages are not supposed to have namespace.
if p.Namespace == "" || p.Class() == types.ClassOSPkg {
return pkg
}
// TODO: replace with packageurl.TypeGradle once they add it.
if p.Type == packageurl.TypeMaven || p.Type == string(ftypes.Gradle) {
// Maven and Gradle packages separate ":"
// e.g. org.springframework:spring-core
pkg.Name = p.Namespace + ":" + p.Name
} else {
pkg.Name = p.Namespace + "/" + p.Name
}
return pkg
}
// LangType returns an application type in Trivy
// nolint: gocyclo
func (p *PackageURL) LangType() ftypes.LangType {
switch p.Type {
case packageurl.TypeComposer:
return ftypes.Composer
case packageurl.TypeMaven:
return ftypes.Jar
case packageurl.TypeGem:
return ftypes.GemSpec
case packageurl.TypeConda:
return ftypes.CondaPkg
case packageurl.TypePyPi:
return ftypes.PythonPkg
case packageurl.TypeGolang:
return ftypes.GoBinary
case packageurl.TypeNPM:
return ftypes.NodePkg
case packageurl.TypeCargo:
return ftypes.Cargo
case packageurl.TypeNuget:
return ftypes.NuGet
case packageurl.TypeSwift:
return ftypes.Swift
case packageurl.TypeCocoapods:
return ftypes.Cocoapods
case packageurl.TypeHex:
return ftypes.Hex
case packageurl.TypeConan:
return ftypes.Conan
case TypeDart: // TODO: replace with packageurl.TypeDart once they add it.
return ftypes.Pub
case packageurl.TypeBitnami:
return ftypes.Bitnami
case TypeK8s:
switch p.Namespace {
case NamespaceEKS:
return ftypes.EKS
case NamespaceGKE:
return ftypes.GKE
case NamespaceAKS:
return ftypes.AKS
case NamespaceRKE:
return ftypes.RKE
case NamespaceOCP:
return ftypes.OCP
case "":
return ftypes.K8sUpstream
}
return TypeUnknown
default:
return TypeUnknown
}
}
func (p *PackageURL) Class() types.ResultClass {
switch p.Type {
case packageurl.TypeApk, packageurl.TypeDebian, packageurl.TypeRPM:
// OS packages
return types.ClassOSPkg
default:
if p.LangType() == TypeUnknown {
return types.ClassUnknown
}
// Language-specific packages
return types.ClassLangPkg
}
}
func (p *PackageURL) BOMRef() string {
// 'bom-ref' must be unique within BOM, but PURLs may conflict
// when the same packages are installed in an artifact.
// In that case, we prefer to make PURLs unique by adding file paths,
// rather than using UUIDs, even if it is not PURL technically.
// ref. https://cyclonedx.org/use-cases/#dependency-graph
purl := p.PackageURL // so that it will not override the qualifiers below
if p.FilePath != "" {
purl.Qualifiers = append(purl.Qualifiers,
packageurl.Qualifier{
Key: "file_path",
Value: p.FilePath,
},
)
}
return purl.String()
}
// nolint: gocyclo
func NewPackageURL(t ftypes.TargetType, metadata types.Metadata, pkg ftypes.Package) (PackageURL, error) {
var qualifiers packageurl.Qualifiers
if metadata.OS != nil {
qualifiers = parseQualifier(pkg)
pkg.Epoch = 0 // we moved Epoch to qualifiers so we don't need it in version
}
ptype := purlType(t)
name := pkg.Name
ver := utils.FormatVersion(pkg)
namespace := ""
subpath := ""
switch ptype {
case packageurl.TypeRPM:
ns, qs := parseRPM(metadata.OS, pkg.Modularitylabel)
namespace = string(ns)
qualifiers = append(qualifiers, qs...)
case packageurl.TypeDebian:
qualifiers = append(qualifiers, parseDeb(metadata.OS)...)
if metadata.OS != nil {
namespace = string(metadata.OS.Family)
}
case packageurl.TypeApk:
var qs packageurl.Qualifiers
name, namespace, qs = parseApk(name, metadata.OS)
qualifiers = append(qualifiers, qs...)
case packageurl.TypeMaven, string(ftypes.Gradle): // TODO: replace with packageurl.TypeGradle once they add it.
namespace, name = parseMaven(name)
case packageurl.TypePyPi:
name = parsePyPI(name)
case packageurl.TypeComposer:
namespace, name = parseComposer(name)
case packageurl.TypeGolang:
namespace, name = parseGolang(name)
if name == "" {
return PackageURL{PackageURL: *packageurl.NewPackageURL("", "", "", "", nil, "")}, nil
}
case packageurl.TypeNPM:
namespace, name = parseNpm(name)
case packageurl.TypeSwift:
namespace, name = parseSwift(name)
case packageurl.TypeCocoapods:
name, subpath = parseCocoapods(name)
case packageurl.TypeOCI:
purl, err := parseOCI(metadata)
if err != nil {
return PackageURL{}, err
}
return PackageURL{PackageURL: purl}, nil
}
return PackageURL{
PackageURL: *packageurl.NewPackageURL(ptype, namespace, name, ver, qualifiers, subpath),
FilePath: pkg.FilePath,
}, nil
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#oci
func parseOCI(metadata types.Metadata) (packageurl.PackageURL, error) {
if len(metadata.RepoDigests) == 0 {
return *packageurl.NewPackageURL("", "", "", "", nil, ""), nil
}
digest, err := cn.NewDigest(metadata.RepoDigests[0])
if err != nil {
return packageurl.PackageURL{}, xerrors.Errorf("failed to parse digest: %w", err)
}
name := strings.ToLower(digest.RepositoryStr())
index := strings.LastIndex(name, "/")
if index != -1 {
name = name[index+1:]
}
var qualifiers packageurl.Qualifiers
if repoURL := digest.Repository.Name(); repoURL != "" {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "repository_url",
Value: repoURL,
})
}
if arch := metadata.ImageConfig.Architecture; arch != "" {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "arch",
Value: metadata.ImageConfig.Architecture,
})
}
return *packageurl.NewPackageURL(packageurl.TypeOCI, "", name, digest.DigestStr(), qualifiers, ""), nil
}
// ref. https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#apk
func parseApk(pkgName string, fos *ftypes.OS) (string, string, packageurl.Qualifiers) {
// the name must be lowercase
pkgName = strings.ToLower(pkgName)
if fos == nil {
return pkgName, "", nil
}
// the namespace must be lowercase
ns := strings.ToLower(string(fos.Family))
qs := packageurl.Qualifiers{
{
Key: "distro",
Value: fos.Name,
},
}
return pkgName, ns, qs
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#deb
func parseDeb(fos *ftypes.OS) packageurl.Qualifiers {
if fos == nil {
return packageurl.Qualifiers{}
}
distro := fmt.Sprintf("%s-%s", fos.Family, fos.Name)
return packageurl.Qualifiers{
{
Key: "distro",
Value: distro,
},
}
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#rpm
func parseRPM(fos *ftypes.OS, modularityLabel string) (ftypes.OSType, packageurl.Qualifiers) {
if fos == nil {
return "", packageurl.Qualifiers{}
}
// SLES string has whitespace
family := fos.Family
if fos.Family == ftypes.SLES {
family = "sles"
}
qualifiers := packageurl.Qualifiers{
{
Key: "distro",
Value: fmt.Sprintf("%s-%s", family, fos.Name),
},
}
if modularityLabel != "" {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "modularitylabel",
Value: modularityLabel,
})
}
return family, qualifiers
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#maven
func parseMaven(pkgName string) (string, string) {
// The group id is the "namespace" and the artifact id is the "name".
name := strings.ReplaceAll(pkgName, ":", "/")
return parsePkgName(name)
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#golang
func parseGolang(pkgName string) (string, string) {
// The PURL will be skipped when the package name is a local path, since it can't identify a software package.
if strings.HasPrefix(pkgName, "./") || strings.HasPrefix(pkgName, "../") {
return "", ""
}
name := strings.ToLower(pkgName)
return parsePkgName(name)
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#pypi
func parsePyPI(pkgName string) string {
// PyPi treats - and _ as the same character and is not case-sensitive.
// Therefore a Pypi package name must be lowercased and underscore "_" replaced with a dash "-".
return strings.ToLower(strings.ReplaceAll(pkgName, "_", "-"))
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#composer
func parseComposer(pkgName string) (string, string) {
return parsePkgName(pkgName)
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#swift
func parseSwift(pkgName string) (string, string) {
return parsePkgName(pkgName)
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#cocoapods
func parseCocoapods(pkgName string) (string, string) {
var subpath string
pkgName, subpath, _ = strings.Cut(pkgName, "/")
return pkgName, subpath
}
// ref. https://github.com/package-url/purl-spec/blob/a748c36ad415c8aeffe2b8a4a5d8a50d16d6d85f/PURL-TYPES.rst#npm
func parseNpm(pkgName string) (string, string) {
// the name must be lowercased
name := strings.ToLower(pkgName)
return parsePkgName(name)
}
func purlType(t ftypes.TargetType) string {
switch t {
case ftypes.Jar, ftypes.Pom, ftypes.Gradle:
return packageurl.TypeMaven
case ftypes.Bundler, ftypes.GemSpec:
return packageurl.TypeGem
case ftypes.NuGet, ftypes.DotNetCore:
return packageurl.TypeNuget
case ftypes.CondaPkg:
return packageurl.TypeConda
case ftypes.PythonPkg, ftypes.Pip, ftypes.Pipenv, ftypes.Poetry:
return packageurl.TypePyPi
case ftypes.GoBinary, ftypes.GoModule:
return packageurl.TypeGolang
case ftypes.Npm, ftypes.NodePkg, ftypes.Yarn, ftypes.Pnpm:
return packageurl.TypeNPM
case ftypes.Cocoapods:
return packageurl.TypeCocoapods
case ftypes.Swift:
return packageurl.TypeSwift
case ftypes.Hex:
return packageurl.TypeHex
case ftypes.Conan:
return packageurl.TypeConan
case ftypes.Pub:
return TypeDart // TODO: replace with packageurl.TypeDart once they add it.
case ftypes.RustBinary, ftypes.Cargo:
return packageurl.TypeCargo
case ftypes.Alpine:
return packageurl.TypeApk
case ftypes.Debian, ftypes.Ubuntu:
return packageurl.TypeDebian
case ftypes.RedHat, ftypes.CentOS, ftypes.Rocky, ftypes.Alma,
ftypes.Amazon, ftypes.Fedora, ftypes.Oracle, ftypes.OpenSUSE,
ftypes.OpenSUSELeap, ftypes.OpenSUSETumbleweed, ftypes.SLES, ftypes.Photon:
return packageurl.TypeRPM
case TypeOCI:
return packageurl.TypeOCI
}
return string(t)
}
func parseQualifier(pkg ftypes.Package) packageurl.Qualifiers {
qualifiers := packageurl.Qualifiers{}
if pkg.Arch != "" {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "arch",
Value: pkg.Arch,
})
}
if pkg.Epoch != 0 {
qualifiers = append(qualifiers, packageurl.Qualifier{
Key: "epoch",
Value: strconv.Itoa(pkg.Epoch),
})
}
return qualifiers
}
func parsePkgName(name string) (string, string) {
var namespace string
index := strings.LastIndex(name, "/")
if index != -1 {
namespace = name[:index]
name = name[index+1:]
}
return namespace, name
}