From 7188912dcc42af1de3acd0e9490d0559aeb84380 Mon Sep 17 00:00:00 2001 From: Ralf Pannemans Date: Wed, 19 Apr 2023 11:08:03 +0200 Subject: [PATCH] Do not add debug options if already available in JAVA_TOOL_OPTIONS Co-authored-by: Johannes Dillmann Co-authored-by: Ralf Pannemans --- helper/debug_8.go | 11 ++++++++++- helper/debug_8_test.go | 14 ++++++++++++++ helper/debug_9.go | 11 ++++++++++- helper/debug_9_test.go | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/helper/debug_8.go b/helper/debug_8.go index 6f73a44..8d08244 100644 --- a/helper/debug_8.go +++ b/helper/debug_8.go @@ -18,6 +18,7 @@ package helper import ( "fmt" + "strings" "github.com/paketo-buildpacks/libpak/bard" "github.com/paketo-buildpacks/libpak/sherpa" @@ -33,6 +34,14 @@ func (d Debug8) Execute() (map[string]string, error) { return nil, nil } + opts := sherpa.GetEnvWithDefault("JAVA_TOOL_OPTIONS", "") + debugAlreadyExists := strings.Contains(opts, "-agentlib:jdwp=") + + if debugAlreadyExists { + d.Logger.Info("Java agent 'jdwp' already configured") + return nil, nil + } + port := sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000") suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND") @@ -49,7 +58,7 @@ func (d Debug8) 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", port, s)) return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil } diff --git a/helper/debug_8_test.go b/helper/debug_8_test.go index 27ebace..d5d7430 100644 --- a/helper/debug_8_test.go +++ b/helper/debug_8_test.go @@ -53,6 +53,20 @@ func testDebug8(t *testing.T, context spec.G, it spec.S) { })) }) + context("jdwp agent already configured", func() { + it.Before(func() { + Expect(os.Setenv("JAVA_TOOL_OPTIONS", "-agentlib:jdwp=something")).To(Succeed()) + }) + + it.After(func() { + Expect(os.Unsetenv("JAVA_TOOL_OPTIONS")).To(Succeed()) + }) + + it("does not update JAVA_TOOL_OPTIONS", func() { + Expect(d.Execute()).To(BeEmpty()) + }) + }) + context("$BPL_DEBUG_PORT", func() { it.Before(func() { Expect(os.Setenv("BPL_DEBUG_PORT", "8001")).To(Succeed()) diff --git a/helper/debug_9.go b/helper/debug_9.go index 6ddffd8..7b1405d 100644 --- a/helper/debug_9.go +++ b/helper/debug_9.go @@ -18,6 +18,7 @@ package helper import ( "fmt" + "strings" "github.com/paketo-buildpacks/libpak/sherpa" @@ -34,6 +35,14 @@ func (d Debug9) Execute() (map[string]string, error) { return nil, nil } + opts := sherpa.GetEnvWithDefault("JAVA_TOOL_OPTIONS", "") + debugAlreadyExists := strings.Contains(opts, "-agentlib:jdwp=") + + if debugAlreadyExists { + d.Logger.Info("Java agent 'jdwp' already configured") + return nil, nil + } + port := "*:" + sherpa.GetEnvWithDefault("BPL_DEBUG_PORT", "8000") suspend := sherpa.ResolveBool("BPL_DEBUG_SUSPEND") @@ -50,7 +59,7 @@ 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", port, s)) return map[string]string{"JAVA_TOOL_OPTIONS": opts}, nil } diff --git a/helper/debug_9_test.go b/helper/debug_9_test.go index aed5658..08c52fa 100644 --- a/helper/debug_9_test.go +++ b/helper/debug_9_test.go @@ -52,6 +52,20 @@ func testDebug9(t *testing.T, context spec.G, it spec.S) { })) }) + context("jdwp agent already configured", func() { + it.Before(func() { + Expect(os.Setenv("JAVA_TOOL_OPTIONS", "-agentlib:jdwp=something")).To(Succeed()) + }) + + it.After(func() { + Expect(os.Unsetenv("JAVA_TOOL_OPTIONS")).To(Succeed()) + }) + + it("does not update JAVA_TOOL_OPTIONS", func() { + Expect(d.Execute()).To(BeEmpty()) + }) + }) + context("$BPL_DEBUG_PORT", func() { it.Before(func() { Expect(os.Setenv("BPL_DEBUG_PORT", "8001")).To(Succeed())