forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
788 lines (693 loc) · 23.6 KB
/
Copy pathparse.go
File metadata and controls
788 lines (693 loc) · 23.6 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package jar
import (
"archive/zip"
"bufio"
"context"
"encoding/xml"
"errors"
"io"
"os"
"path"
"path/filepath"
"regexp"
"slices"
"strings"
mavenversion "github.com/masahiro331/go-mvn-version"
"golang.org/x/net/html/charset"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/digest"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/licensing"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
xos "github.com/aquasecurity/trivy/pkg/x/os"
)
var (
jarFileRegEx = regexp.MustCompile(`^([a-zA-Z0-9\._-]*[^-*])-(\d\S*(?:-SNAPSHOT)?).jar$`)
)
type Client interface {
Exists(groupID, artifactID string) (bool, error)
SearchBySHA1(sha1 string) (Properties, error)
SearchByArtifactID(artifactID, version string) (string, error)
}
type Parser struct {
logger *log.Logger
rootFilePath string
offline bool
checksum bool
size int64
licenseConfidenceLevel float64
client Client
}
type Option func(*Parser)
func WithFilePath(filePath string) Option {
return func(p *Parser) {
p.rootFilePath = filePath
}
}
func WithOffline(offline bool) Option {
return func(p *Parser) {
p.offline = offline
}
}
// WithChecksum enables calculation of the SHA-1 digest for every archive
// (not only the ones that are looked up by SHA-1) and saving it to Package.Digest.
func WithChecksum(checksum bool) Option {
return func(p *Parser) {
p.checksum = checksum
}
}
func WithSize(size int64) Option {
return func(p *Parser) {
p.size = size
}
}
func WithLicenseClassifierConfidenceLevel(level float64) Option {
return func(p *Parser) {
p.licenseConfidenceLevel = level
}
}
func NewParser(c Client, opts ...Option) *Parser {
p := &Parser{
logger: log.WithPrefix("jar"),
client: c,
}
for _, opt := range opts {
opt(p)
}
return p
}
func (p *Parser) Parse(_ context.Context, r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) {
pkgs, deps, err := p.parseArtifact(p.rootFilePath, p.size, r)
if err != nil {
return nil, nil, xerrors.Errorf("unable to parse %s: %w", p.rootFilePath, err)
}
return removePackageDuplicates(pkgs), deps, nil
}
func (p *Parser) parseArtifact(filePath string, size int64, r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) {
pkgs, deps, err := p.parsePackages(filePath, size, r)
if err != nil {
return nil, nil, err
}
// When a checksum is requested, every package must carry the digest of its
// own file. Packages from nested archives (and the one resolved by
// searchBySHA1) already have it, so fill in this archive's digest only for
// the packages that are still missing one.
if p.checksum {
if err := fillArchiveDigest(pkgs, r); err != nil {
return nil, nil, xerrors.Errorf("unable to set digest for %s: %w", filePath, err)
}
}
return pkgs, deps, nil
}
func (p *Parser) parsePackages(filePath string, size int64, r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) {
p.logger.Debug("Parsing Java artifacts...", log.FilePath(filePath))
// Try to extract artifactId and version from the file name
// e.g. spring-core-5.3.4-SNAPSHOT.jar => sprint-core, 5.3.4-SNAPSHOT
fileProps := parseFileName(filePath)
pkgs, m, foundPomProps, licenseFile, err := p.traverseZip(size, r, fileProps)
if err != nil {
return nil, nil, xerrors.Errorf("zip error: %w", err)
}
// If pom.properties is found, it should be preferred than MANIFEST.MF.
// Otherwise, resolve the artifact of the jar itself from MANIFEST.MF / SHA-1 / file name.
// Such an artifact has no embedded pom.xml (maven-archiver writes pom.xml and
// pom.properties together), so it carries no pom.xml license.
if !foundPomProps {
pkg, found, err := p.resolveArtifact(r, m, fileProps)
if err != nil {
return nil, nil, err
}
if found {
pkgs = append(pkgs, pkg)
}
}
// Classify and attach the LICENSE file now that the jar's own artifact is resolved
// (it may have been added above from MANIFEST.MF / SHA-1 / file name).
attachManifestLicenses(pkgs, fileProps.FilePath, m.licenses)
p.attachFileLicenses(pkgs, fileProps.FilePath, licenseFile)
return pkgs, nil, nil
}
// resolveArtifact determines the artifact of the jar itself when pom.properties is absent,
// trying MANIFEST.MF, then Maven Central by SHA-1, then a heuristic search by file name.
func (p *Parser) resolveArtifact(r xio.ReadSeekerAt, m manifest, fileProps Properties) (ftypes.Package, bool, error) {
fileName := filepath.Base(fileProps.FilePath)
manifestProps := m.properties(fileProps.FilePath)
if p.offline {
// In offline mode, we will not check if the artifact information is correct.
if !manifestProps.Valid() {
p.logger.Debug("Unable to identify POM in offline mode", log.String("file", fileName))
return ftypes.Package{}, false, nil
}
return manifestProps.Package(), true, nil
}
if manifestProps.Valid() {
// Even if MANIFEST.MF is found, the groupId and artifactId might not be valid.
// We have to make sure that the artifact exists actually.
if ok, _ := p.client.Exists(manifestProps.GroupID, manifestProps.ArtifactID); ok {
// If groupId and artifactId are valid, they will be returned.
return manifestProps.Package(), true, nil
}
}
// If groupId and artifactId are not found, call Maven Central's search API with SHA-1 digest.
pkg, err := p.searchBySHA1(r, fileProps.FilePath)
if err == nil {
return pkg, true, nil
} else if !errors.Is(err, ArtifactNotFoundErr) {
return ftypes.Package{}, false, xerrors.Errorf("failed to search by SHA1: %w", err)
}
p.logger.Debug("No such POM in the central repositories", log.String("file", fileName))
// Return when artifactId or version from the file name are empty
if fileProps.ArtifactID == "" || fileProps.Version == "" {
return ftypes.Package{}, false, nil
}
// Try to search groupId by artifactId via sonatype API
// When some artifacts have the same groupIds, it might result in false detection.
fileProps.GroupID, err = p.client.SearchByArtifactID(fileProps.ArtifactID, fileProps.Version)
if err == nil {
p.logger.Debug("POM was determined in a heuristic way", log.String("file", fileName),
log.String("artifact", fileProps.String()))
return fileProps.Package(), true, nil
} else if !errors.Is(err, ArtifactNotFoundErr) {
return ftypes.Package{}, false, xerrors.Errorf("failed to search by artifact id: %w", err)
}
return ftypes.Package{}, false, nil
}
// fillArchiveDigest sets the SHA-1 digest of the archive (r) on every package
// that does not have a digest yet. The digest is calculated lazily, so the
// archive is not read when all packages already carry their own digest.
//
// Packages that have no file of their own — e.g. dependencies flattened into a
// shaded/uber JAR, which only leave a bundled pom.properties behind — all share
// this archive's digest. That is consistent with their FilePath, which is also
// the enclosing archive, so the digest stays aligned with the file it refers to.
func fillArchiveDigest(pkgs []ftypes.Package, r xio.ReadSeekerAt) error {
var d digest.Digest
for i := range pkgs {
if pkgs[i].Digest != "" {
continue
}
// Compute the archive digest at most once and reuse it afterwards.
// An empty d means it has not been calculated yet.
if d == "" {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return xerrors.Errorf("file seek error: %w", err)
}
var err error
if d, err = digest.CalcSHA1(r); err != nil {
return xerrors.Errorf("unable to calculate SHA-1: %w", err)
}
}
pkgs[i].Digest = d
}
return nil
}
func (p *Parser) traverseZip(size int64, r xio.ReadSeekerAt, fileProps Properties) (
[]ftypes.Package, manifest, bool, *zip.File, error) {
var pkgs []ftypes.Package
var m manifest
var foundPomProps bool
var licenseFiles []*zip.File
// Licenses declared in embedded META-INF/maven/<g>/<a>/pom.xml, keyed by "groupID:artifactID".
// The path carries no version, so packages are matched by G:A after the loop
// (file order in the zip is not guaranteed).
pomLicenses := make(map[string][]string)
zr, err := zip.NewReader(r, size)
if err != nil {
return nil, manifest{}, false, nil, xerrors.Errorf("zip error: %w", err)
}
for _, fileInJar := range zr.File {
switch {
case filepath.Base(fileInJar.Name) == "pom.xml":
// Collect licenses declared in the embedded META-INF/maven/<g>/<a>/pom.xml.
groupID, artifactID, ok := embeddedPomGAV(fileInJar.Name)
if !ok {
break
}
names, err := parsePomLicenses(fileInJar)
if err != nil {
p.logger.Debug("Failed to parse licenses", log.String("file", fileInJar.Name), log.Err(err))
break
}
if len(names) > 0 {
pomLicenses[packageName(groupID, artifactID)] = names
}
case isJarLicenseFile(fileInJar.Name):
licenseFiles = append(licenseFiles, fileInJar)
case filepath.Base(fileInJar.Name) == "pom.properties":
props, err := parsePomProperties(fileInJar, fileProps.FilePath)
if err != nil {
return nil, manifest{}, false, nil, xerrors.Errorf("failed to parse %s: %w", fileInJar.Name, err)
}
// Validation of props to avoid getting packages with empty Name/Version
if props.Valid() {
pkgs = append(pkgs, props.Package())
// Check if the pom.properties is for the original JAR/WAR/EAR
if fileProps.ArtifactID == props.ArtifactID && fileProps.Version == props.Version {
foundPomProps = true
}
}
case filepath.Base(fileInJar.Name) == "MANIFEST.MF":
m, err = parseManifest(fileInJar)
if err != nil {
return nil, manifest{}, false, nil, xerrors.Errorf("failed to parse MANIFEST.MF: %w", err)
}
case isArtifact(fileInJar.Name):
innerPkgs, _, err := p.parseInnerJar(fileInJar, fileProps.FilePath) // TODO process inner deps
if err != nil {
p.logger.Debug("Failed to parse", log.String("file", fileInJar.Name), log.Err(err))
continue
}
pkgs = append(pkgs, innerPkgs...)
}
}
// Attach licenses from embedded pom.xml, matched by "groupID:artifactID".
attachPomLicenses(pkgs, pomLicenses)
var licenseFile *zip.File
if len(licenseFiles) == 1 {
licenseFile = licenseFiles[0]
}
return pkgs, m, foundPomProps, licenseFile, nil
}
// attachPomLicenses attaches licenses declared in embedded pom.xml files to packages,
// matched by "groupID:artifactID". Packages that already have a license (e.g. set by a
// nested jar from its own pom.xml) are left untouched.
func attachPomLicenses(pkgs []ftypes.Package, pomLicenses map[string][]string) {
for i := range pkgs {
pkg := &pkgs[i]
if len(pkg.Licenses) > 0 {
continue
}
if names, ok := pomLicenses[pkg.Name]; ok {
pkg.Licenses = names
}
}
}
// attachManifestLicenses attaches Jenkins plugin licenses declared in MANIFEST.MF
// to the jar's own package when a single unambiguous package belongs to this jar.
func attachManifestLicenses(pkgs []ftypes.Package, filePath string, licenses []string) {
if len(licenses) == 0 {
return
}
var pkg *ftypes.Package
for i := range pkgs {
if pkgs[i].FilePath != filePath {
continue
}
if pkg != nil {
return // more than one package belongs to this jar
}
pkg = &pkgs[i]
}
if pkg == nil || len(pkg.Licenses) > 0 {
return
}
pkg.Licenses = licenses
}
// attachFileLicenses classifies the LICENSE file packed in a jar and attaches it to the
// jar's own package, but only when the owner is unambiguous: a single LICENSE file, a
// single package belonging to this jar, and no license from its pom.xml yet.
func (p *Parser) attachFileLicenses(pkgs []ftypes.Package, filePath string, licenseFile *zip.File) {
if licenseFile == nil {
return
}
var pkg *ftypes.Package
for i := range pkgs {
if pkgs[i].FilePath != filePath {
continue
}
if pkg != nil {
return // more than one package belongs to this jar
}
pkg = &pkgs[i]
}
if pkg == nil {
return // no package belongs to this jar
}
if len(pkg.Licenses) > 0 {
return
}
names, err := p.classifyPackedLicense(licenseFile)
if err != nil {
p.logger.Debug("Failed to classify license file", log.FilePath(licenseFile.Name), log.Err(err))
return
}
if len(names) > 0 {
pkg.Licenses = names
}
}
func (p *Parser) parseInnerJar(zf *zip.File, rootPath string) ([]ftypes.Package, []ftypes.Dependency, error) {
fr, err := zf.Open()
if err != nil {
return nil, nil, xerrors.Errorf("unable to open %s: %w", zf.Name, err)
}
f, err := xos.CreateTemp("", "jar-inner-")
if err != nil {
return nil, nil, xerrors.Errorf("unable to create a temp file: %w", err)
}
defer func() {
_ = f.Close()
_ = os.Remove(f.Name())
}()
// Copy the file content to the temp file
if n, err := io.CopyN(f, fr, int64(zf.UncompressedSize64)); err != nil {
return nil, nil, xerrors.Errorf("file copy error: %w", err)
} else if n != int64(zf.UncompressedSize64) {
return nil, nil, xerrors.Errorf("file copy size error: %w", err)
}
// build full path to inner jar
fullPath := path.Join(rootPath, zf.Name) // nolint:gosec
if !strings.HasPrefix(fullPath, path.Clean(rootPath)) {
return nil, nil, nil // zip slip
}
// Parse jar/war/ear recursively
innerPkgs, innerDeps, err := p.parseArtifact(fullPath, int64(zf.UncompressedSize64), f)
if err != nil {
return nil, nil, xerrors.Errorf("failed to parse %s: %w", zf.Name, err)
}
return innerPkgs, innerDeps, nil
}
func (p *Parser) searchBySHA1(r io.ReadSeeker, filePath string) (ftypes.Package, error) {
if _, err := r.Seek(0, io.SeekStart); err != nil {
return ftypes.Package{}, xerrors.Errorf("file seek error: %w", err)
}
d, err := digest.CalcSHA1(r)
if err != nil {
return ftypes.Package{}, xerrors.Errorf("unable to calculate SHA-1: %w", err)
}
prop, err := p.client.SearchBySHA1(d.Encoded())
if err != nil {
return ftypes.Package{}, err
}
prop.FilePath = filePath
pkg := prop.Package()
// searchBySHA1 has already calculated the archive's SHA-1, so stamp it on the
// resolved package to avoid recalculating it in fillArchiveDigest.
if p.checksum {
pkg.Digest = d
}
return pkg, nil
}
func isArtifact(name string) bool {
ext := filepath.Ext(name)
if ext == ".jar" || ext == ".ear" || ext == ".war" {
return true
}
return false
}
func parseFileName(filePath string) Properties {
fileName := filepath.Base(filePath)
packageVersion := jarFileRegEx.FindStringSubmatch(fileName)
if len(packageVersion) != 3 {
return Properties{FilePath: filePath}
}
return Properties{
ArtifactID: packageVersion[1],
Version: packageVersion[2],
FilePath: filePath,
}
}
func parsePomProperties(f *zip.File, filePath string) (Properties, error) {
file, err := f.Open()
if err != nil {
return Properties{}, xerrors.Errorf("unable to open pom.properties: %w", err)
}
defer file.Close()
p := Properties{
FilePath: filePath,
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
switch {
case strings.HasPrefix(line, "groupId="):
p.GroupID = strings.TrimPrefix(line, "groupId=")
case strings.HasPrefix(line, "artifactId="):
p.ArtifactID = strings.TrimPrefix(line, "artifactId=")
case strings.HasPrefix(line, "version="):
p.Version = strings.TrimPrefix(line, "version=")
}
}
if err = scanner.Err(); err != nil {
return Properties{}, xerrors.Errorf("scan error: %w", err)
}
return p, nil
}
// embeddedPom is a minimal view of an embedded pom.xml: only the license names are needed.
type embeddedPom struct {
Licenses struct {
License []struct {
Name string `xml:"name"`
} `xml:"license"`
} `xml:"licenses"`
}
// embeddedPomGAV extracts groupId and artifactId from a path of the form
// META-INF/maven/<groupId>/<artifactId>/pom.xml. The version is not part of the path.
// ok is false when the path is not a Maven descriptor pom.xml.
func embeddedPomGAV(name string) (groupID, artifactID string, ok bool) {
rel, found := strings.CutPrefix(name, "META-INF/maven/")
if !found {
return "", "", false
}
rel, found = strings.CutSuffix(rel, "/pom.xml")
if !found {
return "", "", false
}
groupID, artifactID, found = strings.Cut(rel, "/")
if !found || groupID == "" || artifactID == "" {
return "", "", false
}
return groupID, artifactID, true
}
// parsePomLicenses returns the raw <license><name> values from an embedded pom.xml.
func parsePomLicenses(f *zip.File) ([]string, error) {
file, err := f.Open()
if err != nil {
return nil, xerrors.Errorf("unable to open %s: %w", f.Name, err)
}
defer file.Close()
return decodePomLicenses(file)
}
// decodePomLicenses decodes a pom.xml and returns the raw <license><name> values.
// Names are kept as-is; normalization happens downstream.
func decodePomLicenses(r io.Reader) ([]string, error) {
var pom embeddedPom
decoder := xml.NewDecoder(r)
decoder.CharsetReader = charset.NewReaderLabel
if err := decoder.Decode(&pom); err != nil {
return nil, xerrors.Errorf("xml decode error: %w", err)
}
var names []string
for _, lic := range pom.Licenses.License {
if name := strings.TrimSpace(lic.Name); name != "" {
names = append(names, name)
}
}
return names, nil
}
// isJarLicenseFile reports whether a zip entry is a license file eligible for
// classification: located at the jar root or directly under META-INF/ (not in a subdirectory),
// with a base name whose stem is license/licence/copyright (e.g. LICENSE, LICENSE.txt).
// Vendored licenses use prefixed names (e.g. FastDoubleParser-LICENSE) or nested
// paths, so they are intentionally excluded.
func isJarLicenseFile(name string) bool {
dir := path.Dir(name)
if dir != "." && dir != "META-INF" {
return false
}
base := path.Base(name)
if isArtifact(base) {
return false // e.g. license.jar is a nested archive, not a license file
}
stem := strings.TrimSuffix(base, path.Ext(base))
return licensing.LicenseFileNames.Contains(stem)
}
// classifyPackedLicense classifies a LICENSE file packed in a jar and returns the
// detected license names.
func (p *Parser) classifyPackedLicense(f *zip.File) ([]string, error) {
file, err := f.Open()
if err != nil {
return nil, xerrors.Errorf("unable to open %s: %w", f.Name, err)
}
defer file.Close()
lf, err := licensing.Classify(f.Name, file, p.licenseConfidenceLevel)
if err != nil {
return nil, xerrors.Errorf("license classification error: %w", err)
}
if lf == nil {
return nil, nil
}
return lf.Findings.Names(), nil
}
type manifest struct {
implementationVersion string
implementationTitle string
implementationVendor string
implementationVendorId string
specificationTitle string
specificationVersion string
specificationVendor string
bundleName string
bundleVersion string
bundleSymbolicName string
licenses []string
}
func parseManifest(f *zip.File) (manifest, error) {
file, err := f.Open()
if err != nil {
return manifest{}, xerrors.Errorf("unable to open MANIFEST.MF: %w", err)
}
defer file.Close()
var m manifest
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Skip variables. e.g. Bundle-Name: %bundleName
ss := strings.Fields(line)
if len(ss) <= 1 || (len(ss) > 1 && strings.HasPrefix(ss[1], "%")) {
continue
}
// It is not determined which fields are present in each application.
// In some cases, none of them are included, in which case they cannot be detected.
switch {
case strings.HasPrefix(line, "Implementation-Version:"):
m.implementationVersion = strings.TrimPrefix(line, "Implementation-Version:")
case strings.HasPrefix(line, "Implementation-Title:"):
m.implementationTitle = strings.TrimPrefix(line, "Implementation-Title:")
case strings.HasPrefix(line, "Implementation-Vendor:"):
m.implementationVendor = strings.TrimPrefix(line, "Implementation-Vendor:")
case strings.HasPrefix(line, "Implementation-Vendor-Id:"):
m.implementationVendorId = strings.TrimPrefix(line, "Implementation-Vendor-Id:")
case strings.HasPrefix(line, "Specification-Version:"):
m.specificationVersion = strings.TrimPrefix(line, "Specification-Version:")
case strings.HasPrefix(line, "Specification-Title:"):
m.specificationTitle = strings.TrimPrefix(line, "Specification-Title:")
case strings.HasPrefix(line, "Specification-Vendor:"):
m.specificationVendor = strings.TrimPrefix(line, "Specification-Vendor:")
case strings.HasPrefix(line, "Bundle-Version:"):
m.bundleVersion = strings.TrimPrefix(line, "Bundle-Version:")
case strings.HasPrefix(line, "Bundle-Name:"):
m.bundleName = strings.TrimPrefix(line, "Bundle-Name:")
case strings.HasPrefix(line, "Bundle-SymbolicName:"):
m.bundleSymbolicName = strings.TrimPrefix(line, "Bundle-SymbolicName:")
default:
key, value, ok := strings.Cut(line, ":")
if !ok || !isPluginLicenseNameKey(key) {
continue
}
if name := strings.TrimSpace(value); name != "" {
m.licenses = append(m.licenses, name)
}
}
}
if err = scanner.Err(); err != nil {
return manifest{}, xerrors.Errorf("scan error: %w", err)
}
return m, nil
}
func isPluginLicenseNameKey(key string) bool {
if key == "Plugin-License-Name" {
return true
}
suffix, ok := strings.CutPrefix(key, "Plugin-License-Name-")
if !ok || suffix == "" {
return false
}
for _, r := range suffix {
if r < '0' || r > '9' {
return false
}
}
return true
}
func (m manifest) properties(filePath string) Properties {
groupID, err := m.determineGroupID()
if err != nil {
return Properties{}
}
artifactID, err := m.determineArtifactID()
if err != nil {
return Properties{}
}
version, err := m.determineVersion()
if err != nil {
return Properties{}
}
return Properties{
GroupID: groupID,
ArtifactID: artifactID,
Version: version,
FilePath: filePath,
}
}
func (m manifest) determineGroupID() (string, error) {
var groupID string
switch {
case m.implementationVendorId != "":
groupID = m.implementationVendorId
case m.bundleSymbolicName != "":
groupID = m.bundleSymbolicName
// e.g. "com.fasterxml.jackson.core.jackson-databind" => "com.fasterxml.jackson.core"
idx := strings.LastIndex(m.bundleSymbolicName, ".")
if idx > 0 {
groupID = m.bundleSymbolicName[:idx]
}
case m.implementationVendor != "":
groupID = m.implementationVendor
case m.specificationVendor != "":
groupID = m.specificationVendor
default:
return "", xerrors.New("no groupID found")
}
return strings.TrimSpace(groupID), nil
}
func (m manifest) determineArtifactID() (string, error) {
var artifactID string
switch {
case m.implementationTitle != "":
artifactID = m.implementationTitle
case m.specificationTitle != "":
artifactID = m.specificationTitle
case m.bundleName != "":
artifactID = m.bundleName
default:
return "", xerrors.New("no artifactID found")
}
return strings.TrimSpace(artifactID), nil
}
func (m manifest) determineVersion() (string, error) {
var version string
switch {
case m.implementationVersion != "":
version = m.implementationVersion
case m.specificationVersion != "":
version = m.specificationVersion
case m.bundleVersion != "":
version = m.bundleVersion
default:
return "", xerrors.New("no version found")
}
return strings.TrimSpace(version), nil
}
func removePackageDuplicates(pkgs []ftypes.Package) []ftypes.Package {
// name::filePath => versions
var uniq = make(map[string][]mavenversion.Version)
var uniqPkgs []ftypes.Package
for _, pkg := range pkgs {
uniqID := pkg.Name + "::" + pkg.FilePath
// err is always nil
// cf. https://github.com/masahiro331/go-mvn-version/blob/d3157d602a08806ad94464c443e0cef1370694a1/version.go#L20-L25
pkgVer, _ := mavenversion.NewVersion(pkg.Version)
savedVers, ok := uniq[uniqID]
if !ok || !slices.ContainsFunc(savedVers, func(v mavenversion.Version) bool {
// There are times when patch `0` is omitted.
// So we can't compare versions just as strings
// for example `2.17.0` and `2.17` must be equal
return v.Equal(pkgVer)
}) {
uniq[uniqID] = append(uniq[uniqID], pkgVer)
uniqPkgs = append(uniqPkgs, pkg)
}
}
return uniqPkgs
}