diff --git a/helper/debug_9.go b/helper/debug_9.go index 7b1405d..69060b1 100644 --- a/helper/debug_9.go +++ b/helper/debug_9.go @@ -18,15 +18,19 @@ package helper import ( "fmt" - "strings" - "github.com/paketo-buildpacks/libpak/sherpa" + "io" + "os" + "strings" "github.com/paketo-buildpacks/libpak/bard" ) +const DefaultIPv6CheckPath = "/sys/module/ipv6/parameters/disable" + type Debug9 struct { - Logger bard.Logger + Logger bard.Logger + CustomIPv6CheckPath string } func (d Debug9) Execute() (map[string]string, error) { @@ -43,11 +47,24 @@ func (d Debug9) Execute() (map[string]string, error) { return nil, nil } - port := "*:" + sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000") + port := sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000") + var host = "*" + var iPv6CheckPath string + if d.CustomIPv6CheckPath != "" { + iPv6CheckPath = d.CustomIPv6CheckPath + } else { + iPv6CheckPath = DefaultIPv6CheckPath + } + if !IPv6Enabled(iPv6CheckPath) { + d.Logger.Infof("IPv6 does not seem to be enabled in the container, configuring debug agent with 0.0.0.0\n") + host = "0.0.0.0" + } + + address := host + ":" + port suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND") - s := fmt.Sprintf("Debugging enabled on port %s", port) + s := fmt.Sprintf("Debugging enabled on address %s", address) if suspend { s = fmt.Sprintf("%s, suspended on start", s) } @@ -59,7 +76,27 @@ func (d Debug9) Execute() (map[string]string, error) { s = "n" } - opts = sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", port, s)) + opts = sherpa.AppendToEnvVar("JAVA_TOOL_OPTIONS", " ", fmt.Sprintf("-agentlib:jdwp=transport=dt_socket,server=y,address=%s,suspend=%s", address, s)) return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil } + +func IPv6Enabled(iPv6CheckPath string) bool { + in, err := os.Open(iPv6CheckPath) + + if err != nil { + return false + } + defer func(in *os.File) { + _ = in.Close() + }(in) + + b, err := io.ReadAll(in) + value := string(b[0:1]) + + if err != nil || value == "1" { + return false + } else { + return true + } +} diff --git a/helper/debug_9_test.go b/helper/debug_9_test.go index 08c52fa..852797a 100644 --- a/helper/debug_9_test.go +++ b/helper/debug_9_test.go @@ -17,6 +17,7 @@ package helper_test import ( + "log" "os" "testing" @@ -36,14 +37,23 @@ func testDebug9(t *testing.T, context spec.G, it spec.S) { }) context("$BPL_DEBUG_ENABLED", func() { - + var fakeIPv6File *os.File it.Before(func() { Expect(os.Setenv("BPL_DEBUG_ENABLED", "true")). To(Succeed()) + + var fakeIPv6FileErr error + fakeIPv6File, fakeIPv6FileErr = os.CreateTemp("", "IPv6Test") + if fakeIPv6FileErr != nil { + log.Fatal(fakeIPv6FileErr) + } + fakeIPv6File.WriteString("0\n") + d.CustomIPv6CheckPath = fakeIPv6File.Name() }) it.After(func() { Expect(os.Unsetenv("BPL_DEBUG_ENABLED")).To(Succeed()) + os.Remove(fakeIPv6File.Name()) }) it("contributes configuration", func() { @@ -114,5 +124,39 @@ func testDebug9(t *testing.T, context spec.G, it spec.S) { }) }) + context("IPv6 is not present", func() { + it.Before(func() { + d1 := []byte("1\n") + os.WriteFile(fakeIPv6File.Name(), d1, 0644) + }) + + it.After(func() { + d1 := []byte("0\n") + os.WriteFile(fakeIPv6File.Name(), d1, 0644) + }) + + it("replaces '*' host with IPv4 0.0.0.0", func() { + Expect(d.Execute()).To(Equal(map[string]string{ + "JAVA_TOOL_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,address=0.0.0.0:8000,suspend=n", + })) + }) + }) + + context("IPv6 kernel module file not there", func() { + it.Before(func() { + d.CustomIPv6CheckPath = "/does/not/exist" + }) + + it.After(func() { + d.CustomIPv6CheckPath = fakeIPv6File.Name() + }) + + it("replaces '*' host with IPv4 0.0.0.0", func() { + Expect(d.Execute()).To(Equal(map[string]string{ + "JAVA_TOOL_OPTIONS": "-agentlib:jdwp=transport=dt_socket,server=y,address=0.0.0.0:8000,suspend=n", + })) + }) + }) + }) }