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 8993eca031437c65ef66567ca080cb780dd38852 Author: Antonin Stefanutti <[email protected]> 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 e9cd843..06de5ed 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 1961355..bc262b7 100644 --- a/pkg/trait/openapi.go +++ b/pkg/trait/openapi.go @@ -34,7 +34,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" - "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" @@ -191,7 +191,7 @@ func (t *openAPITrait) generateFromDataSpecs(e *Environment, tmpDir string, spec func (t *openAPITrait) generateOpenAPIConfigMap(e *Environment, resource v1.DataSpec, tmpDir, generatedContentName string) error { cm := corev1.ConfigMap{} - key := client.ObjectKey{ + key := ctrl.ObjectKey{ Namespace: e.Integration.Namespace, Name: generatedContentName, } @@ -262,12 +262,15 @@ func (t *openAPITrait) createNewOpenAPIConfigMap(e *Environment, resource v1.Dat 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 88df86d..ad05456 100644 --- a/pkg/util/camel/catalog.go +++ b/pkg/util/camel/catalog.go @@ -84,7 +84,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 b9da8bf..8d61c49 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 0874d0f..3a078bf 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"` +}
