Merge pull request #314 from anchore/issue-291-java-parent-ref

Ensure java parent pkg ref isn't nil when looking for parent matches
This commit is contained in:
Dan Luhring 2021-01-27 11:51:40 -05:00 committed by GitHub
commit 407769e88c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 29 deletions

View file

@ -178,36 +178,40 @@ func (j *archiveParser) discoverPkgsFromPomProperties(parentPkg *pkg.Package) ([
return nil, fmt.Errorf("failed to parse pom.properties (%s): %w", j.virtualPath, err) return nil, fmt.Errorf("failed to parse pom.properties (%s): %w", j.virtualPath, err)
} }
if propsObj != nil { if propsObj == nil {
if propsObj.Version != "" && propsObj.ArtifactID != "" { continue
// TODO: if there is no parentPkg (no java manifest) one of these poms could be the parent. We should discover the right parent and attach the correct info accordingly to each discovered package }
// keep the artifact name within the virtual path if this package does not match the parent package if propsObj.Version != "" && propsObj.ArtifactID != "" {
vPathSuffix := "" // TODO: if there is no parentPkg (no java manifest) one of these poms could be the parent. We should discover the right parent and attach the correct info accordingly to each discovered package
if !strings.HasPrefix(propsObj.ArtifactID, parentPkg.Name) {
vPathSuffix += ":" + propsObj.ArtifactID
}
virtualPath := j.virtualPath + vPathSuffix
// discovered props = new package // keep the artifact name within the virtual path if this package does not match the parent package
p := pkg.Package{ vPathSuffix := ""
Name: propsObj.ArtifactID, if parentPkg != nil && !strings.HasPrefix(propsObj.ArtifactID, parentPkg.Name) {
Version: propsObj.Version, vPathSuffix += ":" + propsObj.ArtifactID
Language: pkg.Java, }
Type: pkg.JavaPkg, virtualPath := j.virtualPath + vPathSuffix
MetadataType: pkg.JavaMetadataType,
Metadata: pkg.JavaMetadata{
VirtualPath: virtualPath,
PomProperties: propsObj,
Parent: parentPkg,
},
}
pkgKey := uniquePkgKey(&p) // discovered props = new package
p := pkg.Package{
Name: propsObj.ArtifactID,
Version: propsObj.Version,
Language: pkg.Java,
Type: pkg.JavaPkg,
MetadataType: pkg.JavaMetadataType,
Metadata: pkg.JavaMetadata{
VirtualPath: virtualPath,
PomProperties: propsObj,
Parent: parentPkg,
},
}
// the name/version pair matches... pkgKey := uniquePkgKey(&p)
matchesParentPkg := pkgKey == parentKey
// the name/version pair matches...
matchesParentPkg := pkgKey == parentKey
if parentPkg != nil {
// the virtual path matches... // the virtual path matches...
matchesParentPkg = matchesParentPkg || parentPkg.Metadata.(pkg.JavaMetadata).VirtualPath == virtualPath matchesParentPkg = matchesParentPkg || parentPkg.Metadata.(pkg.JavaMetadata).VirtualPath == virtualPath
@ -231,11 +235,13 @@ func (j *archiveParser) discoverPkgsFromPomProperties(parentPkg *pkg.Package) ([
parentMetadata.PomProperties = propsObj parentMetadata.PomProperties = propsObj
parentPkg.Metadata = parentMetadata parentPkg.Metadata = parentMetadata
} }
} else if !j.discoveredPkgs.Contains(pkgKey) {
// only keep packages we haven't seen yet (and are not related to the parent package)
pkgs = append(pkgs, p)
} }
} }
if !matchesParentPkg && !j.discoveredPkgs.Contains(pkgKey) {
// only keep packages we haven't seen yet (and are not related to the parent package)
pkgs = append(pkgs, p)
}
} }
} }
return pkgs, nil return pkgs, nil

View file

@ -0,0 +1,20 @@
package integration
import (
"github.com/anchore/stereoscope/pkg/imagetest"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/source"
"testing"
)
func TestJavaNoMainPackage(t *testing.T) { // Regression: https://github.com/anchore/syft/issues/252
fixtureImageName := "image-java-no-main-package"
_, cleanup := imagetest.GetFixtureImage(t, "docker-archive", fixtureImageName)
tarPath := imagetest.GetFixtureImageTarPath(t, fixtureImageName)
defer cleanup()
_, _, _, err := syft.Catalog("docker-archive:"+tarPath, source.SquashedScope)
if err != nil {
t.Fatalf("failed to catalog image: %+v", err)
}
}

View file

@ -0,0 +1,23 @@
FROM jenkins:2.60.3
USER root
WORKDIR /usr/share/jenkins
RUN mkdir tmp
WORKDIR /usr/share/jenkins/tmp
RUN apt-get update 2>&1 > /dev/null && apt-get install -y less zip 2>&1 > /dev/null
RUN unzip ../jenkins.war 2>&1 > /dev/null
RUN rm -f ./META-INF/MANIFEST.MF
WORKDIR /usr/share/jenkins
RUN rm -rf jenkins.war
RUN cd ./tmp && zip -r ../jenkins.war . && cd ..
RUN rm -rf ./tmp

View file

@ -1,5 +1,5 @@
FROM alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378 FROM alpine@sha256:d9a7354e3845ea8466bb00b22224d9116b183e594527fb5b6c3d30bc01a20378
RUN apk add --no-cache \ RUN apk add --no-cache \
tzdata=2020f-r0 \ tzdata=2021a-r0 \
vim=8.2.2320-r0 \ vim=8.2.2320-r0 \
alpine-sdk=1.0-r0 alpine-sdk=1.0-r0