This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/master by this push: new 46317b5 fix: Enable maven strict checksum policy on platform initialization new f889687 Merge pull request #836 from jamesnetherton/776-platform-strict-checksum 46317b5 is described below commit 46317b528f8e375d84a6c970baa97b34eda3b2c4 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Thu Jul 18 08:11:04 2019 +0100 fix: Enable maven strict checksum policy on platform initialization fixes #776 --- .../v1alpha1/integrationplatform_types_support.go | 8 ++ pkg/cmd/install.go | 71 +--------- pkg/controller/integrationplatform/initialize.go | 53 +++++++- .../integrationplatform/initialize_test.go | 22 +++ pkg/util/maven/maven_settings.go | 68 +++++++++- pkg/util/maven/maven_settings_test.go | 149 +++++++++++++++++++++ 6 files changed, 300 insertions(+), 71 deletions(-) diff --git a/pkg/apis/camel/v1alpha1/integrationplatform_types_support.go b/pkg/apis/camel/v1alpha1/integrationplatform_types_support.go index ff35966..3a5def0 100644 --- a/pkg/apis/camel/v1alpha1/integrationplatform_types_support.go +++ b/pkg/apis/camel/v1alpha1/integrationplatform_types_support.go @@ -76,6 +76,14 @@ func (in *IntegrationPlatform) Configurations() []ConfigurationSpec { return in.Spec.Configuration } +// AddConfiguration -- +func (in *IntegrationPlatform) AddConfiguration(confType string, confValue string) { + in.Spec.Configuration = append(in.Spec.Configuration, ConfigurationSpec{ + Type: confType, + Value: confValue, + }) +} + // GetCondition returns the condition with the provided type. func (in *IntegrationPlatformStatus) GetCondition(condType IntegrationPlatformConditionType) *IntegrationPlatformCondition { for i := range in.Conditions { diff --git a/pkg/cmd/install.go b/pkg/cmd/install.go index 885fb0a..9f13bb6 100644 --- a/pkg/cmd/install.go +++ b/pkg/cmd/install.go @@ -31,9 +31,7 @@ import ( "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" "github.com/apache/camel-k/pkg/install" - "github.com/apache/camel-k/pkg/util" "github.com/apache/camel-k/pkg/util/kubernetes" - "github.com/apache/camel-k/pkg/util/maven" "github.com/apache/camel-k/pkg/util/watch" "github.com/pkg/errors" @@ -41,7 +39,6 @@ import ( corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func newCmdInstall(rootCmdOptions *RootCmdOptions) *cobra.Command { @@ -212,62 +209,10 @@ func (o *installCmdOptions) install(_ *cobra.Command, _ []string) error { platform.Spec.Build.Timeout.Duration = d } - o.mavenSettings = fmt.Sprintf("configmap:%s-maven-settings/settings.xml", platform.Name) - - 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) + if len(o.mavenRepositories) > 0 { + for _, r := range o.mavenRepositories { + platform.AddConfiguration("repository", r) } - - repositories = append(repositories, repository) - } - - // 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...) - } - - settings.Profiles = []maven.Profile{ - { - ID: "maven-settings", - Activation: maven.Activation{ - ActiveByDefault: true, - }, - 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 } if o.mavenSettings != "" { @@ -275,7 +220,6 @@ func (o *installCmdOptions) install(_ *cobra.Command, _ []string) error { if err != nil { return err } - platform.Spec.Build.Maven.Settings = mavenSettings } @@ -438,12 +382,3 @@ 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" { - return true - } - } - return false -} diff --git a/pkg/controller/integrationplatform/initialize.go b/pkg/controller/integrationplatform/initialize.go index e387941..fc2ac59 100644 --- a/pkg/controller/integrationplatform/initialize.go +++ b/pkg/controller/integrationplatform/initialize.go @@ -19,8 +19,11 @@ package integrationplatform import ( "context" + "fmt" "time" + "github.com/apache/camel-k/pkg/util/maven" + corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" @@ -110,7 +113,10 @@ func (action *initializeAction) Handle(ctx context.Context, platform *v1alpha1.I action.L.Info("No registry specified for publishing images") } - action.setDefaults(platform) + err = action.setDefaults(ctx, platform) + if err != nil { + return nil, err + } if platform.Spec.Build.Maven.Timeout.Duration != 0 { action.L.Infof("Maven Timeout set to %s", platform.Spec.Build.Maven.Timeout.Duration) @@ -168,7 +174,7 @@ func (action *initializeAction) isDuplicate(ctx context.Context, thisPlatform *v return false, nil } -func (action *initializeAction) setDefaults(platform *v1alpha1.IntegrationPlatform) { +func (action *initializeAction) setDefaults(ctx context.Context, platform *v1alpha1.IntegrationPlatform) error { if platform.Spec.Profile == "" { platform.Spec.Profile = platformutil.GetProfile(platform) } @@ -215,12 +221,41 @@ func (action *initializeAction) setDefaults(platform *v1alpha1.IntegrationPlatfo platform.Spec.Build.Maven.Timeout.Duration = (time.Duration(n) * time.Second).Truncate(time.Second) } + if platform.Spec.Build.Maven.Settings.ConfigMapKeyRef == nil && platform.Spec.Build.Maven.Settings.SecretKeyRef == nil { + var repositories []maven.Repository + for i, c := range platform.Spec.Configuration { + if c.Type == "repository" { + repository := maven.NewRepository(c.Value) + if repository.ID == "" { + repository.ID = fmt.Sprintf("repository-%03d", i) + } + repositories = append(repositories, repository) + } + } + + settings := maven.NewDefaultSettings(repositories) + + err := createMavenSettingsConfigMap(ctx, action.client, platform, settings) + if err != nil { + return err + } + + platform.Spec.Build.Maven.Settings.ConfigMapKeyRef = &corev1.ConfigMapKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: platform.Name + "-maven-settings", + }, + Key: "settings.xml", + } + } + action.L.Infof("CamelVersion set to %s", platform.Spec.Build.CamelVersion) action.L.Infof("RuntimeVersion set to %s", platform.Spec.Build.RuntimeVersion) action.L.Infof("BaseImage set to %s", platform.Spec.Build.BaseImage) action.L.Infof("LocalRepository set to %s", platform.Spec.Build.LocalRepository) action.L.Infof("Timeout set to %s", platform.Spec.Build.Timeout) action.L.Infof("Maven Timeout set to %s", platform.Spec.Build.Maven.Timeout.Duration) + + return nil } func createPersistentVolumeClaim(ctx context.Context, client client.Client, platform *v1alpha1.IntegrationPlatform) error { @@ -261,3 +296,17 @@ func createPersistentVolumeClaim(ctx context.Context, client client.Client, plat return nil } + +func createMavenSettingsConfigMap(ctx context.Context, client client.Client, platform *v1alpha1.IntegrationPlatform, settings maven.Settings) error { + cm, err := maven.CreateSettingsConfigMap(platform.Namespace, platform.Name, settings) + if err != nil { + return err + } + + err = client.Create(ctx, cm) + if err != nil && !k8serrors.IsAlreadyExists(err) { + return err + } + + return nil +} diff --git a/pkg/controller/integrationplatform/initialize_test.go b/pkg/controller/integrationplatform/initialize_test.go index bd181a7..e744a9d 100644 --- a/pkg/controller/integrationplatform/initialize_test.go +++ b/pkg/controller/integrationplatform/initialize_test.go @@ -122,3 +122,25 @@ func TestTimeouts_Truncated(t *testing.T) { assert.Equal(t, 2*time.Minute, answer.Spec.Build.Maven.Timeout.Duration) assert.Equal(t, 5*time.Minute, answer.Spec.Build.Timeout.Duration) } + +func TestDefaultMavenSettingsApplied(t *testing.T) { + ip := v1alpha1.IntegrationPlatform{} + ip.Namespace = "ns" + ip.Name = "test-platform" + ip.Spec.Cluster = v1alpha1.IntegrationPlatformClusterOpenShift + + c, err := test.NewFakeClient(&ip) + assert.Nil(t, err) + + h := NewInitializeAction() + h.InjectLogger(log.Log) + h.InjectClient(c) + + answer, err := h.Handle(context.TODO(), &ip) + assert.Nil(t, err) + assert.NotNil(t, answer) + + assert.NotNil(t, answer.Spec.Build.Maven.Settings.ConfigMapKeyRef) + assert.Equal(t, "test-platform-maven-settings", answer.Spec.Build.Maven.Settings.ConfigMapKeyRef.Name) + assert.Equal(t, "settings.xml", answer.Spec.Build.Maven.Settings.ConfigMapKeyRef.Key) +} diff --git a/pkg/util/maven/maven_settings.go b/pkg/util/maven/maven_settings.go index 16f96b7..1111e26 100644 --- a/pkg/util/maven/maven_settings.go +++ b/pkg/util/maven/maven_settings.go @@ -17,7 +17,14 @@ limitations under the License. package maven -import "encoding/xml" +import ( + "encoding/xml" + + "github.com/apache/camel-k/pkg/util" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) // NewSettings -- func NewSettings() Settings { @@ -28,3 +35,62 @@ func NewSettings() Settings { XsiSchemaLocation: "http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd", } } + +// NewDefaultSettings -- +func NewDefaultSettings(repositories []Repository) Settings { + settings := NewSettings() + + if !containsMvnCentral(repositories) { + repository := NewRepository("https://repo.maven.apache.org/maven2@id=central") + repositories = append([]Repository{repository}, repositories...) + } + + settings.Profiles = []Profile{ + { + ID: "maven-settings", + Activation: Activation{ + ActiveByDefault: true, + }, + Repositories: repositories, + PluginRepositories: repositories, + }, + } + + return settings +} + +// CreateSettingsConfigMap -- +func CreateSettingsConfigMap(namespace string, name string, settings Settings) (*corev1.ConfigMap, error) { + data, err := util.EncodeXML(settings) + if err != nil { + return nil, err + } + + cm := &corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + Kind: "ConfigMap", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: name + "-maven-settings", + Namespace: namespace, + Labels: map[string]string{ + "app": "camel-k", + }, + }, + Data: map[string]string{ + "settings.xml": string(data), + }, + } + + return cm, nil +} + +func containsMvnCentral(repositories []Repository) bool { + for _, r := range repositories { + if r.ID == "central" { + return true + } + } + return false +} diff --git a/pkg/util/maven/maven_settings_test.go b/pkg/util/maven/maven_settings_test.go index 75d2a20..a4a1776 100644 --- a/pkg/util/maven/maven_settings_test.go +++ b/pkg/util/maven/maven_settings_test.go @@ -54,6 +54,114 @@ const expectedSettings = `<?xml version="1.0" encoding="UTF-8"?> </profiles> </settings>` +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> + <profiles> + <profile> + <id>maven-settings</id> + <activation> + <activeByDefault>true</activeByDefault> + </activation> + <repositories> + <repository> + <id>central</id> + <url>https://repo.maven.apache.org/maven2</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://repo.maven.apache.org/maven2</url> + <snapshots> + <enabled>false</enabled> + <checksumPolicy>fail</checksumPolicy> + </snapshots> + <releases> + <enabled>true</enabled> + <checksumPolicy>fail</checksumPolicy> + </releases> + </pluginRepository> + </pluginRepositories> + </profile> + </profiles> +</settings>` + +const expectedDefaultSettingsWithExtraRepo = `<?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> + <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> +</settings>` + func TestSettingsGeneration(t *testing.T) { settings := NewSettings() settings.LocalRepository = "/tmp/artifacts/m2" @@ -88,3 +196,44 @@ func TestSettingsGeneration(t *testing.T) { assert.Equal(t, expectedSettings, string(content)) } + +func TestDefaultSettingsGeneration(t *testing.T) { + settings := NewDefaultSettings([]Repository{}) + + content, err := util.EncodeXML(settings) + + assert.Nil(t, err) + assert.NotNil(t, settings) + + assert.Equal(t, expectedDefaultSettings, string(content)) +} + +func TestDefaultSettingsGenerationWithAdditionalRepo(t *testing.T) { + repositories := []Repository{ + NewRepository("https://repo1.maven.org/maven2@id=central"), + NewRepository("https://foo.bar.org/repo@id=foo"), + } + settings := NewDefaultSettings(repositories) + + content, err := util.EncodeXML(settings) + + assert.Nil(t, err) + assert.NotNil(t, settings) + + assert.Equal(t, expectedDefaultSettingsWithExtraRepo, string(content)) +} + +func TestCreateSettingsConfigMap(t *testing.T) { + settings := NewDefaultSettings([]Repository{}) + + configMap, err := CreateSettingsConfigMap("foo", "bar", settings) + assert.Nil(t, err) + assert.NotNil(t, configMap) + + content, err := util.EncodeXML(settings) + + assert.Nil(t, err) + assert.NotNil(t, settings) + + assert.Equal(t, string(content), configMap.Data["settings.xml"]) +}