Skip to content

Commit

Permalink
Fix apache#2834:Allow Servers to be configured in Maven settings file
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpoth authored and bouskaJ committed Apr 7, 2022
1 parent b719993 commit 97c4162
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 68 deletions.
10 changes: 10 additions & 0 deletions pkg/apis/camel/v1/maven_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ type MavenArtifact struct {
// Maven Version
Version string `json:"version,omitempty" yaml:"version,omitempty" xml:"version,omitempty"`
}

type Server struct {
XMLName xml.Name `xml:"server"`
ID string `xml:"id,omitempty" json:"id,omitempty"`
Username string `xml:"username,omitempty" json:"username,omitempty"`
Password string `xml:"password,omitempty" json:"password,omitempty"`
Configuration Properties `xml:"configuration,omitempty" json:"configuration,omitempty"`
}

type Properties map[string]string
32 changes: 2 additions & 30 deletions pkg/apis/camel/v1/maven_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,18 @@ package v1

import "encoding/xml"

// Repository --
type Repository struct {
ID string `xml:"id" json:"id"`
Name string `xml:"name,omitempty" json:"name,omitempty"`
URL string `xml:"url" json:"url"`
Snapshots RepositoryPolicy `xml:"snapshots,omitempty" json:"snapshots,omitempty"`
Releases RepositoryPolicy `xml:"releases,omitempty" json:"releases,omitempty"`
}

// RepositoryPolicy --
type RepositoryPolicy struct {
Enabled bool `xml:"enabled" json:"enabled"`
UpdatePolicy string `xml:"updatePolicy,omitempty" json:"updatePolicy,omitempty"`
ChecksumPolicy string `xml:"checksumPolicy,omitempty" json:"checksumPolicy,omitempty"`
}

type Server struct {
XMLName xml.Name `xml:"server"`
ID string `xml:"id,omitempty" json:"id,omitempty"`
Username string `xml:"username,omitempty" json:"username,omitempty"`
Password string `xml:"password,omitempty" json:"password,omitempty"`
Configuration Properties `xml:"configuration,omitempty" json:"configuration,omitempty"`
}

type Properties map[string]string



type propertiesEntry struct {
XMLName xml.Name
Value string `xml:",chardata"`
}

func (m v1.Properties) AddAll(properties map[string]string) {
func (m Properties) AddAll(properties map[string]string) {
for k, v := range properties {
m[k] = v
}
}

func (m v1.Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
func (m Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {
return nil
}
Expand Down
224 changes: 224 additions & 0 deletions pkg/builder/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package builder

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,10 +27,169 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/maven"
"github.com/apache/camel-k/pkg/util/test"
)

const customSettings = `<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ` +
`xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<profiles>
<profile>
<id>maven-settings</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
<repository>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</pluginRepository>
<pluginRepository>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<mirrors>
<mirror>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>`

const expectedCustomSettingsWithExtraServers = `<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ` +
`xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<servers>
<server>
<id>image-repository</id>
<username>jpoth</username>
<password>changeit</password>
<configuration>
<allowInsecureRegistries>false</allowInsecureRegistries>
</configuration>
</server>
</servers>
<profiles>
<profile>
<id>maven-settings</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
<repository>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</pluginRepository>
<pluginRepository>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<checksumPolicy>fail</checksumPolicy>
</releases>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<mirrors>
<mirror>
<id>foo</id>
<url>https://foo.bar.org/repo</url>
<mirrorOf>*</mirrorOf>
</mirror>
</mirrors>
</settings>`

func TestMavenSettingsFromConfigMap(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)
Expand Down Expand Up @@ -127,3 +287,67 @@ func TestMavenSettingsFromSecret(t *testing.T) {

assert.Equal(t, []byte("setting-data"), ctx.Maven.UserSettings)
}

func TestInjectEmptyServersIntoDefaultMavenSettings(t *testing.T) {

settings := maven.NewDefaultSettings([]v1.Repository{}, []maven.Mirror{})

content, err := util.EncodeXML(settings)
assert.Nil(t, err)

contentStr := string(content)
newSettings := injectServersIntoMavenSettings(contentStr, nil)

assert.Equal(t, contentStr, newSettings)
}

func TestInjectServersIntoDefaultMavenSettings(t *testing.T) {

settings := maven.NewDefaultSettings([]v1.Repository{}, []maven.Mirror{})

servers := []v1.Server{
{
ID: "image-repository",
Username: "jpoth",
Password: "changeit",
Configuration: map[string]string{
"allowInsecureRegistries": "false",
},
},
}

content, err := util.EncodeXML(settings)
assert.Nil(t, err)

contentStr := string(content)
newSettings := injectServersIntoMavenSettings(contentStr, servers)

settings.Servers = servers
expectedNewSettings, err := util.EncodeXML(settings)
assert.Nil(t, err)

expectedNewSettingsStr := string(expectedNewSettings)
assert.Equal(t, expectedNewSettingsStr, newSettings)
}

func TestInjectServersIntoCustomMavenSettings(t *testing.T) {
servers := []v1.Server{
{
ID: "image-repository",
Username: "jpoth",
Password: "changeit",
Configuration: map[string]string{
"allowInsecureRegistries": "false",
},
},
}

newSettings := injectServersIntoMavenSettings(customSettings, servers)

assert.Equal(t, removeWhitespaces(expectedCustomSettingsWithExtraServers), removeWhitespaces(newSettings))
}

func removeWhitespaces(s string) string {
re := regexp.MustCompile(`\s`)
return re.ReplaceAllString(s, "")
}
30 changes: 0 additions & 30 deletions pkg/util/maven/maven_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,36 +151,6 @@ func (p *Project) AddEncodedDependencyExclusion(gav string, exclusion Exclusion)
}
}

type propertiesEntry struct {
XMLName xml.Name
Value string `xml:",chardata"`
}

func (m Properties) AddAll(properties map[string]string) {
for k, v := range properties {
m[k] = v
}
}

func (m Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {
return nil
}

err := e.EncodeToken(start)
if err != nil {
return err
}

for k, v := range m {
if err := e.Encode(propertiesEntry{XMLName: xml.Name{Local: k}, Value: v}); err != nil {
return err
}
}

return e.EncodeToken(start.End())
}

// NewDependency creates an new dependency from the given GAV.
func NewDependency(groupID string, artifactID string, version string) Dependency {
return Dependency{
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/maven/maven_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const expectedSettings = `<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ` +
`xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/tmp/artifacts/m2</localRepository>
<servers></servers>
<profiles>
<profile>
<id>my-profile</id>
Expand Down Expand Up @@ -62,6 +63,7 @@ const expectedDefaultSettings = `<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ` +
`xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository></localRepository>
<servers></servers>
<profiles>
<profile>
<id>camel-k</id>
Expand Down Expand Up @@ -106,6 +108,7 @@ const expectedDefaultSettingsWithExtraRepo = `<?xml version="1.0" encoding="UTF-
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ` +
`xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository></localRepository>
<servers></servers>
<profiles>
<profile>
<id>camel-k</id>
Expand Down

0 comments on commit 97c4162

Please sign in to comment.