Skip to content

Commit

Permalink
Mark the detected script executable
Browse files Browse the repository at this point in the history
When the buildpack runs, it will locate the dist zip script to execute. If that script is found, then we also mark it as `0755`. This happens unconditionally, but we do not fail if there is an error. We just log a warning and proceed. It really shouldn't fail but maybe if the file had weird file permissions/ownership.

The choice to do this unconditionally is that it is slightly simpler. Checking and then setting is two operations. If thereis a preference to doing it that way, making the switch is easy enough. Just let me know

Signed-off-by: Daniel Mikusa <[email protected]>
  • Loading branch information
dmikusa committed Feb 11, 2024
1 parent 804b202 commit f2fc93f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions distzip/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"path/filepath"

"github.com/heroku/color"
"github.com/paketo-buildpacks/libpak/effect"
"github.com/paketo-buildpacks/libpak/sbom"

Expand Down Expand Up @@ -67,6 +68,11 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
return libcnb.BuildResult{}, fmt.Errorf("unable to create configuration resolver\n%w", err)
}

err = os.Chmod(s, 0755)
if err != nil {
b.Logger.Bodyf("%s Unable to make script executable\n%w", color.YellowString("WARNING:"), err.Error())
}

result.Processes = append(result.Processes,
libcnb.Process{Type: "dist-zip", Command: s},
libcnb.Process{Type: "task", Command: s},
Expand Down
26 changes: 26 additions & 0 deletions distzip/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,32 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
})
})

context("DistZip exists but isn't executable", func() {
var scriptPath string

it.Before(func() {
scriptPath = filepath.Join(ctx.Application.Path, "app", "bin", "test-script")
Expect(os.MkdirAll(filepath.Join(ctx.Application.Path, "app", "bin"), 0755)).To(Succeed())
Expect(os.WriteFile(scriptPath, []byte{}, 0644))
})

it("contributes processes and marks the script executable", func() {
result, err := distzip.Build{SBOMScanner: &sbomScanner}.Build(ctx)
Expect(err).NotTo(HaveOccurred())

Expect(result.Processes).To(ContainElements(
libcnb.Process{Type: "dist-zip", Command: scriptPath},
libcnb.Process{Type: "task", Command: scriptPath},
libcnb.Process{Type: "web", Command: scriptPath, Default: true},
))
sbomScanner.AssertCalled(t, "ScanLaunch", ctx.Application.Path, libcnb.SyftJSON, libcnb.CycloneDXJSON)

info, err := os.Stat(scriptPath)
Expect(err).NotTo(HaveOccurred())
Expect(info.Mode().Perm().String()).To(Equal("-rwxr-xr-x"))
})
})

context("DistZip does not exists", func() {
it("passes plan entries to subsequent buildpacks", func() {
result, err := distzip.Build{}.Build(ctx)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/buildpacks/libcnb v1.30.1
github.com/heroku/color v0.0.6
github.com/onsi/gomega v1.31.1
github.com/paketo-buildpacks/libpak v1.68.1
github.com/sclevine/spec v1.4.0
Expand All @@ -15,7 +16,6 @@ require (
github.com/creack/pty v1.1.21 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/heroku/color v0.0.6 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down

0 comments on commit f2fc93f

Please sign in to comment.