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

Issue 360 debug ipv4 cat #366

Merged
merged 1 commit into from
Feb 12, 2024
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
49 changes: 43 additions & 6 deletions helper/debug_9.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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
}
}
46 changes: 45 additions & 1 deletion helper/debug_9_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package helper_test

import (
"log"
"os"
"testing"

Expand All @@ -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() {
Expand Down Expand Up @@ -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",
}))
})
})

})
}
Loading