remove parent java package from json && add java manifest section parsing

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman
2020-10-28 17:19:06 -04:00
parent 62f6146c37
commit 2675891110
5 changed files with 126 additions and 305 deletions
+8 -4
View File
@@ -70,10 +70,14 @@ func (a archiveFilename) version() string {
}
func (a archiveFilename) name() string {
// there should be only one name, if there is more or less then something is wrong
if len(a.fields) != 1 {
return ""
for _, fieldSet := range a.fields {
if name, ok := fieldSet["name"]; ok {
// return the first name
return name
}
}
return a.fields[0]["name"]
// derive the name from the archive name (no path or extension)
basename := filepath.Base(a.raw)
return strings.TrimSuffix(basename, filepath.Ext(basename))
}
+23 -6
View File
@@ -12,17 +12,29 @@ import (
const manifestPath = "META-INF/MANIFEST.MF"
// nolint:funlen
func parseJavaManifest(reader io.Reader) (*pkg.JavaManifest, error) {
var manifest pkg.JavaManifest
manifestMap := make(map[string]string)
sections := []map[string]string{
make(map[string]string),
}
currentSection := 0
scanner := bufio.NewScanner(reader)
var lastKey string
for scanner.Scan() {
line := scanner.Text()
// ignore empty lines
// empty lines denote section separators
if strings.TrimSpace(line) == "" {
currentSection++
// we don't want to allocate a new section map that wont necessarily be used, do that once there is
// a non-empty line to process
// do not process line continuations after this
lastKey = ""
continue
} else if currentSection >= len(sections) {
sections = append(sections, make(map[string]string))
}
if line[0] == ' ' {
@@ -30,7 +42,7 @@ func parseJavaManifest(reader io.Reader) (*pkg.JavaManifest, error) {
if lastKey == "" {
return nil, fmt.Errorf("found continuation with no previous key (%s)", line)
}
manifestMap[lastKey] += strings.TrimSpace(line)
sections[currentSection][lastKey] += strings.TrimSpace(line)
} else {
// this is a new key-value pair
idx := strings.Index(line, ":")
@@ -40,9 +52,9 @@ func parseJavaManifest(reader io.Reader) (*pkg.JavaManifest, error) {
key := strings.TrimSpace(line[0:idx])
value := strings.TrimSpace(line[idx+1:])
manifestMap[key] = value
sections[currentSection][key] = value
// keep track of key for future continuations
// keep track of key for potential future continuations
lastKey = key
}
}
@@ -51,10 +63,15 @@ func parseJavaManifest(reader io.Reader) (*pkg.JavaManifest, error) {
return nil, fmt.Errorf("unable to read java manifest: %w", err)
}
if err := mapstructure.Decode(manifestMap, &manifest); err != nil {
if err := mapstructure.Decode(sections[0], &manifest); err != nil {
return nil, fmt.Errorf("unable to parse java manifest: %w", err)
}
// append on extra sections
if len(sections) > 1 {
manifest.Sections = sections[1:]
}
// clean select fields
if strings.Trim(manifest.ImplVersion, " ") != "" {
// transform versions with dates attached to just versions (e.g. "1.3 2244 October 5 2008" --> "1.3")
+12 -5
View File
@@ -2,10 +2,11 @@ package java
import (
"encoding/json"
"github.com/anchore/syft/syft/pkg"
"github.com/go-test/deep"
"os"
"testing"
"github.com/anchore/syft/syft/pkg"
"github.com/go-test/deep"
)
func TestParseJavaManifest(t *testing.T) {
@@ -38,10 +39,16 @@ func TestParseJavaManifest(t *testing.T) {
ManifestVersion: "1.0",
Extra: map[string]string{
"Archiver-Version": "Plexus Archiver",
"Build-Jdk": "14.0.1",
"Built-By": "?",
"Created-By": "Apache Maven 3.6.3",
"Main-Class": "hello.HelloWorld",
},
Sections: []map[string]string{
{
"Built-By": "?",
},
{
"Build-Jdk": "14.0.1",
"Main-Class": "hello.HelloWorld",
},
},
},
},
+2 -2
View File
@@ -7,7 +7,7 @@ type JavaMetadata struct {
VirtualPath string `json:"virtualPath"`
Manifest *JavaManifest `mapstructure:"Manifest" json:"manifest,omitempty"`
PomProperties *PomProperties `mapstructure:"PomProperties" json:"pomProperties,omitempty"`
Parent *Package `json:"parentPackage,omitempty"` // TODO: should this be included in the json output?
Parent *Package `json:"-"`
}
// PomProperties represents the fields of interest extracted from a Java archive's pom.xml file.
@@ -31,7 +31,7 @@ type JavaManifest struct {
ImplVersion string `mapstructure:"Implementation-Version" json:"implementationVersion"`
ImplVendor string `mapstructure:"Implementation-Vendor" json:"implementationVendor"`
Extra map[string]string `mapstructure:",remain" json:"extraFields"`
Sections []map[string]string `json:"sections"`
Sections []map[string]string `json:"sections,omitempty"`
}
func (m JavaMetadata) PackageURL() string {