You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by as...@apache.org on 2022/01/12 13:54:03 UTC

[camel-k] 17/31: feat(jvm): Translate HTTP proxy env variables into system properties

This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit da18313671286ee2c39a9d4fef7cf26aba9e708e
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Mon Jan 10 12:51:34 2022 +0100

    feat(jvm): Translate HTTP proxy env variables into system properties
---
 pkg/trait/jvm.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)

diff --git a/pkg/trait/jvm.go b/pkg/trait/jvm.go
index 994a532..306a068 100644
--- a/pkg/trait/jvm.go
+++ b/pkg/trait/jvm.go
@@ -19,6 +19,7 @@ package trait
 
 import (
 	"fmt"
+	"net/url"
 	"path"
 	"sort"
 	"strings"
@@ -38,6 +39,7 @@ import (
 	"github.com/apache/camel-k/pkg/builder"
 	"github.com/apache/camel-k/pkg/util"
 	"github.com/apache/camel-k/pkg/util/camel"
+	"github.com/apache/camel-k/pkg/util/envvar"
 )
 
 // The JVM trait is used to configure the JVM that runs the integration.
@@ -169,6 +171,59 @@ func (t *jvmTrait) Apply(e *Environment) error {
 		args = append(args, t.Options...)
 	}
 
+	// Translate HTTP proxy environment variables, that are set by the environment trait,
+	// into corresponding JVM system properties.
+	if HTTPProxy := envvar.Get(container.Env, "HTTP_PROXY"); HTTPProxy != nil {
+		u, err := url.Parse(HTTPProxy.Value)
+		if err != nil {
+			return err
+		}
+		if !util.StringSliceContainsAnyOf(t.Options, "http.proxyHost") {
+			args = append(args, "-Dhttp.proxyHost="+u.Hostname())
+		}
+		if !util.StringSliceContainsAnyOf(t.Options, "http.proxyPort") {
+			args = append(args, "-Dhttp.proxyPort="+u.Port())
+		}
+		if user := u.User; !util.StringSliceContainsAnyOf(t.Options, "http.proxyUser") && user != nil {
+			args = append(args, "-Dhttp.proxyUser="+user.Username())
+			if password, ok := user.Password(); !util.StringSliceContainsAnyOf(t.Options, "http.proxyUser") && ok {
+				args = append(args, "-Dhttp.proxyPassword="+password)
+			}
+		}
+	}
+
+	if HTTPSProxy := envvar.Get(container.Env, "HTTPS_PROXY"); HTTPSProxy != nil {
+		u, err := url.Parse(HTTPSProxy.Value)
+		if err != nil {
+			return err
+		}
+		if !util.StringSliceContainsAnyOf(t.Options, "https.proxyHost") {
+			args = append(args, "-Dhttps.proxyHost="+u.Hostname())
+		}
+		if !util.StringSliceContainsAnyOf(t.Options, "https.proxyPort") {
+			args = append(args, "-Dhttps.proxyPort="+u.Port())
+		}
+		if user := u.User; !util.StringSliceContainsAnyOf(t.Options, "https.proxyUser") && user != nil {
+			args = append(args, "-Dhttps.proxyUser="+user.Username())
+			if password, ok := user.Password(); !util.StringSliceContainsAnyOf(t.Options, "https.proxyUser") && ok {
+				args = append(args, "-Dhttps.proxyPassword="+password)
+			}
+		}
+	}
+
+	if noProxy := envvar.Get(container.Env, "NO_PROXY"); noProxy != nil {
+		if !util.StringSliceContainsAnyOf(t.Options, "http.nonProxyHosts") {
+			// Convert to the format expected by the JVM http.nonProxyHosts system property
+			hosts := strings.Split(strings.ReplaceAll(noProxy.Value, " ", ""), ",")
+			for i, host := range hosts {
+				if strings.HasPrefix(host, ".") {
+					hosts[i] = strings.Replace(host, ".", "*.", 1)
+				}
+			}
+			args = append(args, "-Dhttps.nonProxyHosts="+strings.Join(hosts, "|"))
+		}
+	}
+
 	// Tune JVM maximum heap size based on the container memory limit, if any.
 	// This is configured off-container, thus is limited to explicit user configuration.
 	// We may want to inject a wrapper script into the container image, so that it can
@@ -191,8 +246,8 @@ func (t *jvmTrait) Apply(e *Environment) error {
 	items := classpath.List()
 	// Keep class path sorted so that it's consistent over reconciliation cycles
 	sort.Strings(items)
-
 	args = append(args, "-cp", strings.Join(items, ":"))
+
 	args = append(args, e.CamelCatalog.Runtime.ApplicationClass)
 
 	if IsNilOrTrue(t.PrintCommand) {