Skip to content

Commit

Permalink
Use current time as version in external configuration layer if neithe…
Browse files Browse the repository at this point in the history
…r BP_TOMCAT_EXT_CONF_VERSION nor BP_TOMCAT_EXT_CONF_SHA256 are present

Co-authored-by: Ralf Pannemans <[email protected]>
  • Loading branch information
phil9909 and c0d1ngm0nk3y committed Nov 30, 2022
1 parent 5ef64f0 commit fe7015b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 14 deletions.
10 changes: 8 additions & 2 deletions tomcat/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"github.com/paketo-buildpacks/libpak/effect"
"github.com/paketo-buildpacks/libpak/sbom"
Expand Down Expand Up @@ -123,8 +124,13 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {

var externalConfigurationDependency *libpak.BuildpackDependency
if uri, ok := cr.Resolve("BP_TOMCAT_EXT_CONF_URI"); ok {
v, _ := cr.Resolve("BP_TOMCAT_EXT_CONF_VERSION")
s, _ := cr.Resolve("BP_TOMCAT_EXT_CONF_SHA256")
v, versionExists := cr.Resolve("BP_TOMCAT_EXT_CONF_VERSION")
s, shaExists := cr.Resolve("BP_TOMCAT_EXT_CONF_SHA256")

if !versionExists && !shaExists {
v = time.Now().Format(time.RFC3339)
b.Logger.Infof("No BP_TOMCAT_EXT_CONF_VERSION or BP_TOMCAT_EXT_CONF_SHA256 provided, using version '%s'", v)
}

externalConfigurationDependency = &libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Expand Down
75 changes: 63 additions & 12 deletions tomcat/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/paketo-buildpacks/libpak/sbom/mocks"

Expand Down Expand Up @@ -341,18 +342,6 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {

context("$BP_TOMCAT_EXT_CONF_URI", func() {
it.Before(func() {
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_SHA256", "test-sha256")).To(Succeed())
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_URI", "test-uri")).To(Succeed())
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_VERSION", "test-version")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_URI")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI is set", func() {
Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "WEB-INF"), 0755)).To(Succeed())

ctx.Buildpack.Metadata = map[string]interface{}{
Expand Down Expand Up @@ -381,9 +370,22 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
}
ctx.StackID = "test-stack-id"

Expect(os.Setenv("BP_TOMCAT_EXT_CONF_SHA256", "test-sha256")).To(Succeed())
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_URI", "test-uri")).To(Succeed())
Expect(os.Setenv("BP_TOMCAT_EXT_CONF_VERSION", "test-version")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_URI")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI, $BP_TOMCAT_EXT_CONF_VERSION and $BP_TOMCAT_EXT_CONF_SHA256 are set", func() {
result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
Expect(result.Layers[2].(tomcat.Base).ExternalConfigurationDependency).To(Equal(&libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Name: "Tomcat External Configuration",
Expand All @@ -394,6 +396,55 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
}))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)
})

it("uses time as version if neither $BP_TOMCAT_EXT_CONF_VERSION nor $BP_TOMCAT_EXT_CONF_SHA256 is provided", func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())

result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
version := result.Layers[2].(tomcat.Base).ExternalConfigurationDependency.Version
Expect(time.Parse(time.RFC3339, version)).NotTo(BeNil())
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI and $BP_TOMCAT_EXT_CONF_VERSION are set", func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_SHA256")).To(Succeed())

result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
Expect(result.Layers[2].(tomcat.Base).ExternalConfigurationDependency).To(Equal(&libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Name: "Tomcat External Configuration",
Version: "test-version",
URI: "test-uri",
SHA256: "",
Stacks: []string{ctx.StackID},
}))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)
})

it("contributes external configuration when $BP_TOMCAT_EXT_CONF_URI and $BP_TOMCAT_EXT_CONF_SHA256 are set", func() {
Expect(os.Unsetenv("BP_TOMCAT_EXT_CONF_VERSION")).To(Succeed())

result, err := tomcat.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Layers).To(HaveLen(3))
Expect(result.Layers[2].(tomcat.Base).ExternalConfigurationDependency).To(Equal(&libpak.BuildpackDependency{
ID: "tomcat-external-configuration",
Name: "Tomcat External Configuration",
Version: "",
URI: "test-uri",
SHA256: "test-sha256",
Stacks: []string{ctx.StackID},
}))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)
})

})

it("returns default context path", func() {
Expand Down

0 comments on commit fe7015b

Please sign in to comment.