Skip to content

Commit

Permalink
Merge pull request #115 from dmikusa-pivotal/lifecycle_0_13_3
Browse files Browse the repository at this point in the history
  • Loading branch information
sambhav authored Feb 2, 2022
2 parents d3e6e18 + f496f6e commit 6a941db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
15 changes: 2 additions & 13 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ type Builder interface {
func Build(builder Builder, options ...Option) {
config := Config{
arguments: os.Args,
bomLabel: false,
environmentWriter: internal.EnvironmentWriter{},
exitHandler: internal.NewExitHandler(),
tomlWriter: internal.TOMLWriter{},
Expand Down Expand Up @@ -315,7 +316,7 @@ func Build(builder Builder, options ...Option) {

// Deprecated: as of Buildpack API 0.7, to be removed in a future version
var launchBOM, buildBOM []BOMEntry
if result.BOM != nil {
if result.BOM != nil && config.bomLabel {
for _, entry := range result.BOM.Entries {
if entry.Launch {
launchBOM = append(launchBOM, entry)
Expand Down Expand Up @@ -345,12 +346,6 @@ func Build(builder Builder, options ...Option) {
}
}

// even if there is data, do not write a BOM if we have buildpack API 0.7, that will cause a lifecycle error
if API == "0.7" && len(launch.BOM) > 0 {
logger.Info("Warning: this buildpack is including both old and new format SBOM information, which is an invalid state. To prevent the lifecycle from failing, libcnb is discarding the old SBOM information.")
launch.BOM = nil
}

if err = config.tomlWriter.Write(file, launch); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write application metadata %s\n%w", file, err))
return
Expand All @@ -366,12 +361,6 @@ func Build(builder Builder, options ...Option) {
file = filepath.Join(ctx.Layers.Path, "build.toml")
logger.Debugf("Writing build metadata: %s <= %+v", file, build)

// even if there is data, do not write a BOM if we have buildpack API 0.7, that will cause a lifecycle error
if API == "0.7" && len(build.BOM) > 0 {
logger.Info("Warning: this buildpack is including both old and new format SBOM information, which is an invalid state. To prevent the lifecycle from failing, libcnb is discarding the old SBOM information.")
build.BOM = nil
}

if err = config.tomlWriter.Write(file, build); err != nil {
config.exitHandler.Error(fmt.Errorf("unable to write build metadata %s\n%w", file, err))
return
Expand Down
32 changes: 29 additions & 3 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ version = "1.1.1"

it("fails", func() {
libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -212,6 +213,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -224,6 +226,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -235,6 +238,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
)

Expand Down Expand Up @@ -297,6 +301,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{filepath.Join(buildpackPath, commandPath), layersPath, platformPath, buildpackPlanPath}),
)

Expand All @@ -309,6 +314,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), fmt.Errorf("test-error"))

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -323,6 +329,7 @@ version = "1.1.1"
Return(libcnb.BuildResult{Layers: []libcnb.LayerContributor{layerContributor}}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand All @@ -339,6 +346,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(result, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithEnvironmentWriter(environmentWriter),
)
Expand All @@ -356,6 +364,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(result, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithEnvironmentWriter(environmentWriter),
)
Expand All @@ -373,6 +382,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(result, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),

libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithEnvironmentWriter(environmentWriter),
Expand All @@ -391,6 +401,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(result, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithEnvironmentWriter(environmentWriter),
)
Expand Down Expand Up @@ -422,6 +433,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(result, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand Down Expand Up @@ -453,6 +465,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(result, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand Down Expand Up @@ -501,6 +514,7 @@ version = "1.1.1"
}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand Down Expand Up @@ -541,6 +555,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.BuildResult{PersistentMetadata: m}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand All @@ -553,6 +568,7 @@ version = "1.1.1"
builder.On("Build", mock.Anything).Return(libcnb.NewBuildResult(), nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand All @@ -573,6 +589,7 @@ version = "1.1.1"
Return(libcnb.BuildResult{Layers: []libcnb.LayerContributor{layerContributor}}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand Down Expand Up @@ -605,6 +622,7 @@ version = "1.1.1"
}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand All @@ -626,7 +644,7 @@ version = "1.1.1"
}))
})

context("API 0.7", func() {
context("Config bomLabel is false", func() {
it.Before(func() {
var err error

Expand All @@ -640,7 +658,7 @@ version = "1.1.1"
Expect(ioutil.WriteFile(filepath.Join(buildpackPath, "buildpack.toml"), b.Bytes(), 0600)).To(Succeed())
})

it("writes launch.toml with BOM entries which are removed", func() {
it("writes launch.toml without BOM entries", func() {
builder.On("Build", mock.Anything).Return(libcnb.BuildResult{
BOM: &libcnb.BOM{Entries: []libcnb.BOMEntry{
{
Expand All @@ -663,6 +681,7 @@ version = "1.1.1"
}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(false),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand All @@ -680,7 +699,7 @@ version = "1.1.1"
}))
})

it("writes build.toml with BOM entries which are removed", func() {
it("writes build.toml without BOM entries", func() {
builder.On("Build", mock.Anything).Return(libcnb.BuildResult{
BOM: &libcnb.BOM{Entries: []libcnb.BOMEntry{
{
Expand All @@ -702,6 +721,7 @@ version = "1.1.1"
}, nil)

libcnb.Build(builder,
libcnb.WithBOMLabel(false),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithTOMLWriter(tomlWriter),
)
Expand Down Expand Up @@ -738,6 +758,7 @@ sbom-formats = ["application/vnd.cyclonedx+json"]

it("has no SBOM files", func() {
libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -762,6 +783,7 @@ sbom-formats = []
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -786,6 +808,7 @@ sbom-formats = []
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -797,6 +820,7 @@ sbom-formats = []
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.spdx.json"), []byte{}, 0600)).To(Succeed())

libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -808,6 +832,7 @@ sbom-formats = []
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "layer.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand All @@ -819,6 +844,7 @@ sbom-formats = []
Expect(ioutil.WriteFile(filepath.Join(layersPath, "launch.sbom.random.json"), []byte{}, 0600)).To(Succeed())
Expect(ioutil.WriteFile(filepath.Join(layersPath, "layer.sbom.cdx.json"), []byte{}, 0600)).To(Succeed())
libcnb.Build(builder,
libcnb.WithBOMLabel(true),
libcnb.WithArguments([]string{commandPath, layersPath, platformPath, buildpackPlanPath}),
libcnb.WithExitHandler(exitHandler),
)
Expand Down
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type ExecDWriter interface {
// Config is an object that contains configurable properties for execution.
type Config struct {
arguments []string
bomLabel bool
environmentWriter EnvironmentWriter
exitHandler ExitHandler
tomlWriter TOMLWriter
Expand Down Expand Up @@ -113,3 +114,12 @@ func WithExecDWriter(execdWriter ExecDWriter) Option {
return config
}
}

// WithBOMLabel creates an Option that enables/disables writing the BOM Label
// Deprecated: as of Buildpack API 0.7, to be removed in a future version
func WithBOMLabel(bomLabel bool) Option {
return func(config Config) Config {
config.bomLabel = bomLabel
return config
}
}

0 comments on commit 6a941db

Please sign in to comment.