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/19 08:40:28 UTC
[camel-k] 02/32: feat(maven): Honor proxy environment variables
This is an automated email from the ASF dual-hosted git repository.
astefanutti pushed a commit to branch release-1.7.x
in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 1232547966627dc04be1bd24ae7256abac9601c9
Author: Antonin Stefanutti <an...@stefanutti.fr>
AuthorDate: Thu Jan 6 17:57:59 2022 +0100
feat(maven): Honor proxy environment variables
---
pkg/builder/project.go | 5 ++-
pkg/trait/openapi.go | 15 ++++---
pkg/util/camel/catalog.go | 5 ++-
pkg/util/maven/maven_proxies.go | 75 +++++++++++++++++++++++++++++++++++
pkg/util/maven/maven_settings.go | 35 +++++++++-------
pkg/util/maven/maven_settings_test.go | 7 +++-
pkg/util/maven/maven_types.go | 12 ++++++
7 files changed, 131 insertions(+), 23 deletions(-)
diff --git a/pkg/builder/project.go b/pkg/builder/project.go
index 3fc6ba8..d427c14 100644
--- a/pkg/builder/project.go
+++ b/pkg/builder/project.go
@@ -94,7 +94,10 @@ func generateProjectSettings(ctx *builderContext) error {
ctx.Maven.UserSettings = []byte(val)
}
- settings := maven.NewSettings()
+ settings, err := maven.NewSettings(maven.ProxyFromEnvironment)
+ if err != nil {
+ return err
+ }
data, err := settings.MarshalBytes()
if err != nil {
return err
diff --git a/pkg/trait/openapi.go b/pkg/trait/openapi.go
index a69854c..e86253c 100644
--- a/pkg/trait/openapi.go
+++ b/pkg/trait/openapi.go
@@ -33,7 +33,7 @@ import (
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "sigs.k8s.io/controller-runtime/pkg/client"
+ ctrl "sigs.k8s.io/controller-runtime/pkg/client"
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util"
@@ -142,7 +142,7 @@ func (t *openAPITrait) Apply(e *Environment) error {
func (t *openAPITrait) generateOpenAPIConfigMap(e *Environment, resource v1.ResourceSpec, tmpDir, generatedContentName string) error {
cm := corev1.ConfigMap{}
- key := client.ObjectKey{
+ key := ctrl.ObjectKey{
Namespace: e.Integration.Namespace,
Name: generatedContentName,
}
@@ -213,12 +213,15 @@ func (t *openAPITrait) createNewOpenAPIConfigMap(e *Environment, resource v1.Res
mc.UserSettings = []byte(settings)
}
- settings := maven.NewSettings()
- data, err := settings.MarshalBytes()
- if err != nil {
+ if settings, err := maven.NewSettings(maven.ProxyFromEnvironment); err != nil {
return err
+ } else {
+ data, err := settings.MarshalBytes()
+ if err != nil {
+ return err
+ }
+ mc.GlobalSettings = data
}
- mc.GlobalSettings = data
if e.Platform.Status.Build.Maven.CASecret != nil {
certData, err := kubernetes.GetSecretRefData(e.Ctx, e.Client, e.Platform.Namespace, e.Platform.Status.Build.Maven.CASecret)
diff --git a/pkg/util/camel/catalog.go b/pkg/util/camel/catalog.go
index 8419131..081d59b 100644
--- a/pkg/util/camel/catalog.go
+++ b/pkg/util/camel/catalog.go
@@ -74,7 +74,10 @@ func GenerateCatalog(
if err != nil {
return nil, err
}
- settings := maven.NewSettings()
+ settings, err := maven.NewSettings(maven.ProxyFromEnvironment)
+ if err != nil {
+ return nil, err
+ }
globalSettings, err := settings.MarshalBytes()
if err != nil {
return nil, err
diff --git a/pkg/util/maven/maven_proxies.go b/pkg/util/maven/maven_proxies.go
new file mode 100644
index 0000000..4418ea5
--- /dev/null
+++ b/pkg/util/maven/maven_proxies.go
@@ -0,0 +1,75 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package maven
+
+import (
+ "net/url"
+ "os"
+ "strings"
+)
+
+var ProxyFromEnvironment = proxyFromEnvironment{}
+
+type proxyFromEnvironment struct{}
+
+func (_ proxyFromEnvironment) apply(settings *Settings) error {
+ if httpProxy := os.Getenv("HTTP_PROXY"); httpProxy != "" {
+ proxy, err := parseProxyFromEnvVar(httpProxy)
+ if err != nil {
+ return err
+ }
+ settings.Proxies = append(settings.Proxies, proxy)
+ }
+
+ if httpsProxy := os.Getenv("HTTPS_PROXY"); httpsProxy != "" {
+ proxy, err := parseProxyFromEnvVar(httpsProxy)
+ if err != nil {
+ return err
+ }
+ settings.Proxies = append(settings.Proxies, proxy)
+ }
+
+ return nil
+}
+
+func parseProxyFromEnvVar(proxyEnvVar string) (Proxy, error) {
+ u, err := url.Parse(proxyEnvVar)
+ if err != nil {
+ return Proxy{}, err
+ }
+ proxy := Proxy{
+ Active: true,
+ Protocol: u.Scheme,
+ Host: u.Hostname(),
+ Port: u.Port(),
+ }
+ if user := u.User; user != nil {
+ proxy.Username = user.Username()
+ if password, set := user.Password(); set {
+ proxy.Password = password
+ }
+ }
+ if noProxy := os.Getenv("NO_PROXY"); noProxy != "" {
+ nonProxyHosts := strings.ReplaceAll(noProxy, " ", "")
+ nonProxyHosts = strings.ReplaceAll(nonProxyHosts, ",", "|")
+ nonProxyHosts = strings.ReplaceAll(nonProxyHosts, "|.", "|*.")
+ proxy.NonProxyHosts = nonProxyHosts
+ }
+
+ return proxy, nil
+}
diff --git a/pkg/util/maven/maven_settings.go b/pkg/util/maven/maven_settings.go
index cc06088..855c421 100644
--- a/pkg/util/maven/maven_settings.go
+++ b/pkg/util/maven/maven_settings.go
@@ -18,7 +18,6 @@ limitations under the License.
package maven
import (
- "bytes"
"encoding/xml"
"strings"
@@ -34,30 +33,38 @@ import (
var DefaultMavenRepositories = "https://repo.maven.apache.org/maven2@id=central"
func (s Settings) MarshalBytes() ([]byte, error) {
- w := &bytes.Buffer{}
- w.WriteString(xml.Header)
-
- e := xml.NewEncoder(w)
- e.Indent("", " ")
-
- if err := e.Encode(s); err != nil {
- return []byte{}, err
- }
+ return util.EncodeXML(s)
+}
- return w.Bytes(), nil
+type SettingsOption interface {
+ apply(settings *Settings) error
}
-func NewSettings() Settings {
- return Settings{
+func NewSettings(options ...SettingsOption) (Settings, error) {
+ settings := Settings{
XMLName: xml.Name{Local: "settings"},
XMLNs: "http://maven.apache.org/SETTINGS/1.0.0",
XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
XsiSchemaLocation: "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd",
}
+
+ for _, option := range options {
+ err := option.apply(&settings)
+ if err != nil {
+ return Settings{}, err
+ }
+ }
+
+ return settings, nil
}
func NewDefaultSettings(repositories []v1.Repository, mirrors []Mirror) Settings {
- settings := NewSettings()
+ settings := Settings{
+ XMLName: xml.Name{Local: "settings"},
+ XMLNs: "http://maven.apache.org/SETTINGS/1.0.0",
+ XMLNsXsi: "http://www.w3.org/2001/XMLSchema-instance",
+ XsiSchemaLocation: "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd",
+ }
var additionalRepos []v1.Repository
for _, defaultRepo := range defaultMavenRepositories() {
diff --git a/pkg/util/maven/maven_settings_test.go b/pkg/util/maven/maven_settings_test.go
index a6b6a6a..eabe87f 100644
--- a/pkg/util/maven/maven_settings_test.go
+++ b/pkg/util/maven/maven_settings_test.go
@@ -54,6 +54,7 @@ const expectedSettings = `<?xml version="1.0" encoding="UTF-8"?>
<pluginRepositories></pluginRepositories>
</profile>
</profiles>
+ <proxies></proxies>
<mirrors></mirrors>
</settings>`
@@ -97,6 +98,7 @@ const expectedDefaultSettings = `<?xml version="1.0" encoding="UTF-8"?>
</pluginRepositories>
</profile>
</profiles>
+ <proxies></proxies>
<mirrors></mirrors>
</settings>`
@@ -164,6 +166,7 @@ const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-
</pluginRepositories>
</profile>
</profiles>
+ <proxies></proxies>
<mirrors>
<mirror>
<id>foo</id>
@@ -174,7 +177,9 @@ const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-
</settings>`
func TestSettingsGeneration(t *testing.T) {
- settings := NewSettings()
+ settings, err := NewSettings()
+ assert.Nil(t, err)
+
settings.LocalRepository = "/tmp/artifacts/m2"
settings.Profiles = []Profile{
{
diff --git a/pkg/util/maven/maven_types.go b/pkg/util/maven/maven_types.go
index e2ea006..ed9fa8f 100644
--- a/pkg/util/maven/maven_types.go
+++ b/pkg/util/maven/maven_types.go
@@ -60,6 +60,7 @@ type Settings struct {
XsiSchemaLocation string `xml:"xsi:schemaLocation,attr"`
LocalRepository string `xml:"localRepository"`
Profiles []Profile `xml:"profiles>profile,omitempty"`
+ Proxies []Proxy `xml:"proxies>proxy,omitempty"`
Mirrors []Mirror `xml:"mirrors>mirror,omitempty"`
}
@@ -120,3 +121,14 @@ type PropertyActivation struct {
Name string `xml:"name"`
Value string `xml:"value"`
}
+
+type Proxy struct {
+ ID string `xml:"id"`
+ Active bool `xml:"active"`
+ Protocol string `xml:"protocol"`
+ Host string `xml:"host"`
+ Port string `xml:"port,omitempty"`
+ Username string `xml:"username,omitempty"`
+ Password string `xml:"password,omitempty"`
+ NonProxyHosts string `xml:"nonProxyHosts,omitempty"`
+}