Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate maven artifacts #774

Merged
merged 1 commit into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 56 additions & 43 deletions pkg/cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,58 +212,62 @@ func (o *installCmdOptions) install(_ *cobra.Command, _ []string) error {
platform.Spec.Build.Timeout.Duration = d
}

if len(o.mavenRepositories) > 0 {
o.mavenSettings = fmt.Sprintf("configmap:%s-maven-settings/settings.xml", platform.Name)
o.mavenSettings = fmt.Sprintf("configmap:%s-maven-settings/settings.xml", platform.Name)

settings := maven.NewSettings()
repositories := make([]maven.Repository, 0, len(o.mavenRepositories))
settings := maven.NewSettings()
repositories := make([]maven.Repository, 0, len(o.mavenRepositories))

for i, r := range o.mavenRepositories {
repository := maven.NewRepository(r)
if repository.ID == "" {
repository.ID = fmt.Sprintf("repository-%03d", i)
}

repositories = append(repositories, repository)
for i, r := range o.mavenRepositories {
repository := maven.NewRepository(r)
if repository.ID == "" {
repository.ID = fmt.Sprintf("repository-%03d", i)
}

settings.Profiles = []maven.Profile{
{
ID: "maven-settings",
Activation: maven.Activation{
ActiveByDefault: true,
},
Repositories: repositories,
PluginRepositories: repositories,
},
}
repositories = append(repositories, repository)
}

data, err := util.EncodeXML(settings)
if err != nil {
return err
}
// Enables strict checksums for Maven central if not already configured
if !containsMvnCentral(repositories) {
repository := maven.NewRepository("https://repo.maven.apache.org/maven2@id=central")
repositories = append([]maven.Repository{repository}, repositories...)
}

cm := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: platform.Name + "-maven-settings",
Namespace: namespace,
Labels: map[string]string{
"app": "camel-k",
},
settings.Profiles = []maven.Profile{
{
ID: "maven-settings",
Activation: maven.Activation{
ActiveByDefault: true,
},
Data: map[string]string{
"settings.xml": string(data),
Repositories: repositories,
PluginRepositories: repositories,
},
}

data, err := util.EncodeXML(settings)
if err != nil {
return err
}

cm := corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: platform.Name + "-maven-settings",
Namespace: namespace,
Labels: map[string]string{
"app": "camel-k",
},
}
},
Data: map[string]string{
"settings.xml": string(data),
},
}

err = install.RuntimeObjectOrCollect(o.Context, c, namespace, collection, &cm)
if err != nil {
return err
}
err = install.RuntimeObjectOrCollect(o.Context, c, namespace, collection, &cm)
if err != nil {
return err
}

if o.mavenSettings != "" {
Expand Down Expand Up @@ -434,3 +438,12 @@ func decodeMavenSettings(mavenSettings string) (v1alpha1.ValueSource, error) {

return v1alpha1.ValueSource{}, fmt.Errorf("illegal maven setting definition, syntax: configmap|secret:resource-name[/settings path]")
}

func containsMvnCentral(repositories []maven.Repository) bool {
for _, r := range repositories {
if r.ID == "central" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if we should check by ip ? as the id can be set by the user, then it can setanother non maven central repo as central

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I did ponder whether to make the check more complicated. Trouble is, there's a bunch of different host name combinations that you could potentially use for central, so checking against them isn't very reliable. Even trying to compare IPs is maybe not 100% reliable either because of load balancers.

So I figured if someone explicitly configures central as the repo ID, then assume it's the defacto config for Maven central.

return true
}
}
return false
}
9 changes: 7 additions & 2 deletions pkg/util/maven/maven_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ func NewRepository(repo string) Repository {
r := Repository{
URL: repo,
Releases: RepositoryPolicy{
Enabled: true,
Enabled: true,
ChecksumPolicy: "fail",
},
Snapshots: RepositoryPolicy{
Enabled: false,
Enabled: false,
ChecksumPolicy: "fail",
},
}

Expand All @@ -184,6 +186,9 @@ func NewRepository(repo string) Repository {
r.Releases.Enabled = false
case strings.HasPrefix(attribute, "id="):
r.ID = attribute[3:]
case strings.HasPrefix(attribute, "checksumpolicy="):
r.Snapshots.ChecksumPolicy = attribute[15:]
r.Releases.ChecksumPolicy = attribute[15:]
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/util/maven/maven_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func TestNewRepository(t *testing.T) {
assert.Equal(t, "http://nexus/public", r.URL)
assert.True(t, r.Releases.Enabled)
assert.False(t, r.Snapshots.Enabled)
assert.Equal(t, "fail", r.Releases.ChecksumPolicy)
assert.Equal(t, "fail", r.Snapshots.ChecksumPolicy)
}

func TestNewRepositoryWithSnapshots(t *testing.T) {
Expand All @@ -195,6 +197,8 @@ func TestNewRepositoryWithSnapshots(t *testing.T) {
assert.Equal(t, "http://nexus/public", r.URL)
assert.True(t, r.Releases.Enabled)
assert.True(t, r.Snapshots.Enabled)
assert.Equal(t, "fail", r.Releases.ChecksumPolicy)
assert.Equal(t, "fail", r.Snapshots.ChecksumPolicy)
}

func TestNewRepositoryWithSnapshotsAndID(t *testing.T) {
Expand All @@ -203,6 +207,8 @@ func TestNewRepositoryWithSnapshotsAndID(t *testing.T) {
assert.Equal(t, "http://nexus/public", r.URL)
assert.True(t, r.Releases.Enabled)
assert.True(t, r.Snapshots.Enabled)
assert.Equal(t, "fail", r.Releases.ChecksumPolicy)
assert.Equal(t, "fail", r.Snapshots.ChecksumPolicy)
}

func TestNewRepositoryWithID(t *testing.T) {
Expand All @@ -211,4 +217,16 @@ func TestNewRepositoryWithID(t *testing.T) {
assert.Equal(t, "http://nexus/public", r.URL)
assert.True(t, r.Releases.Enabled)
assert.False(t, r.Snapshots.Enabled)
assert.Equal(t, "fail", r.Releases.ChecksumPolicy)
assert.Equal(t, "fail", r.Snapshots.ChecksumPolicy)
}

func TestNewRepositoryWithChecksumPolicy(t *testing.T) {
r := NewRepository("http://nexus/public@checksumpolicy=warn")
assert.Equal(t, "", r.ID)
assert.Equal(t, "http://nexus/public", r.URL)
assert.True(t, r.Releases.Enabled)
assert.False(t, r.Snapshots.Enabled)
assert.Equal(t, "warn", r.Releases.ChecksumPolicy)
assert.Equal(t, "warn", r.Snapshots.ChecksumPolicy)
}
10 changes: 7 additions & 3 deletions pkg/util/maven/maven_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ const expectedSettings = `<?xml version="1.0" encoding="UTF-8"?>
<url>https://repo.maven.apache.org/maven2</url>
<snapshots>
<enabled>false</enabled>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
</repository>
</repositories>
Expand All @@ -66,11 +68,13 @@ func TestSettingsGeneration(t *testing.T) {
ID: "central",
URL: "https://repo.maven.apache.org/maven2",
Snapshots: RepositoryPolicy{
Enabled: false,
Enabled: false,
ChecksumPolicy: "warn",
},
Releases: RepositoryPolicy{
Enabled: true,
UpdatePolicy: "never",
Enabled: true,
UpdatePolicy: "never",
ChecksumPolicy: "fail",
},
},
},
Expand Down
5 changes: 3 additions & 2 deletions pkg/util/maven/maven_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ type Repository struct {

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

// Build --
Expand Down