Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure only one JDK layer is ever added #79

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,26 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {

v, _ := cr.Resolve("BP_JVM_VERSION")

if _, ok, err := pr.Resolve("jdk"); err != nil {
_, jdkRequired, err := pr.Resolve("jdk")
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve jdk plan entry\n%w", err)
} else if ok {
}

jrePlanEntry, jreRequired, err := pr.Resolve("jre")
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve jre plan entry\n%w", err)
}

jreAvailable := jreRequired
if jreRequired {
_, err := dr.Resolve("jre", v)
if libpak.IsNoValidDependencies(err) {
jreAvailable = false
}
}

// we need a JDK and we're not using the JDK as a JRE
if jdkRequired && !(jreRequired && !jreAvailable) {
dep, err := dr.Resolve("jdk", v)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
Expand All @@ -74,16 +91,18 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
result.BOM.Entries = append(result.BOM.Entries, be)
}

if e, ok, err := pr.Resolve("jre"); err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to resolve jre plan entry\n%w", err)
} else if ok {
if jreRequired {
dt := JREType
depJRE, err := dr.Resolve("jre", v)

if libpak.IsNoValidDependencies(err) {
if !jreAvailable {
warn := color.New(color.FgYellow, color.Bold)
b.Logger.Header(warn.Sprint("No valid JRE available, providing matching JDK instead. Using a JDK at runtime has security implications."))

// This forces the contributed layer to be build + cache + launch so it's available everywhere
jrePlanEntry.Metadata["build"] = true
jrePlanEntry.Metadata["cache"] = true

dt = JDKType
depJRE, err = dr.Resolve("jdk", v)
}
Expand All @@ -92,7 +111,7 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
return libcnb.BuildResult{}, fmt.Errorf("unable to find dependency\n%w", err)
}

jre, be, err := NewJRE(context.Application.Path, depJRE, dc, dt, cl, e.Metadata)
jre, be, err := NewJRE(context.Application.Path, depJRE, dc, dt, cl, jrePlanEntry.Metadata)
if err != nil {
return libcnb.BuildResult{}, fmt.Errorf("unable to create jre\n%w", err)
}
Expand All @@ -101,7 +120,7 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
result.Layers = append(result.Layers, jre)
result.BOM.Entries = append(result.BOM.Entries, be)

if IsLaunchContribution(e.Metadata) {
if IsLaunchContribution(jrePlanEntry.Metadata) {
helpers := []string{"active-processor-count", "java-opts", "link-local-dns", "memory-calculator",
"openssl-certificate-loader", "security-providers-configurer"}

Expand Down
39 changes: 37 additions & 2 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
}))
})

it("contributes JDK when no JRE", func() {
it("contributes JDK when no JRE and only a JRE is wanted", func() {
ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "jre", Metadata: LaunchContribution})
ctx.Buildpack.Metadata = map[string]interface{}{
"dependencies": []map[string]interface{}{
Expand All @@ -192,7 +192,42 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
Expect(result.BOM.Entries).To(HaveLen(3))
Expect(result.BOM.Entries[0].Name).To(Equal("jdk"))
Expect(result.BOM.Entries[0].Launch).To(BeTrue())
Expect(result.BOM.Entries[0].Build).To(BeFalse())
Expect(result.BOM.Entries[0].Build).To(BeTrue())
Expect(result.BOM.Entries[1].Name).To(Equal("helper"))
Expect(result.BOM.Entries[1].Launch).To(BeTrue())
Expect(result.BOM.Entries[2].Name).To(Equal("jvmkill"))
Expect(result.BOM.Entries[2].Launch).To(BeTrue())
})

it("contributes JDK when no JRE and both a JDK and JRE are wanted", func() {
ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "jdk", Metadata: LaunchContribution})
ctx.Plan.Entries = append(ctx.Plan.Entries, libcnb.BuildpackPlanEntry{Name: "jre", Metadata: LaunchContribution})
ctx.Buildpack.Metadata = map[string]interface{}{
"dependencies": []map[string]interface{}{
{
"id": "jdk",
"version": "1.1.1",
"stacks": []interface{}{"test-stack-id"},
},
{
"id": "jvmkill",
"version": "1.1.1",
"stacks": []interface{}{"test-stack-id"},
},
},
}
ctx.StackID = "test-stack-id"

result, err := libjvm.Build{}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers[0].Name()).To(Equal("jdk"))
Expect(result.Layers[0].(libjvm.JRE).LayerContributor.Dependency.ID).To(Equal("jdk"))

Expect(result.BOM.Entries).To(HaveLen(3))
Expect(result.BOM.Entries[0].Name).To(Equal("jdk"))
Expect(result.BOM.Entries[0].Launch).To(BeTrue())
Expect(result.BOM.Entries[0].Build).To(BeTrue())
Expect(result.BOM.Entries[1].Name).To(Equal("helper"))
Expect(result.BOM.Entries[1].Launch).To(BeTrue())
Expect(result.BOM.Entries[2].Name).To(Equal("jvmkill"))
Expand Down