Skip to content

Commit

Permalink
Fix apache#679: make sure we don't use external deps other than kube …
Browse files Browse the repository at this point in the history
…in apis
  • Loading branch information
nicolaferraro committed May 22, 2019
1 parent b26d24b commit ed6b57b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 28 deletions.
20 changes: 0 additions & 20 deletions pkg/apis/camel/v1alpha1/common_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package v1alpha1
import (
"fmt"

"github.com/mitchellh/mapstructure"
yaml2 "gopkg.in/yaml.v2"
)

Expand All @@ -41,22 +40,3 @@ func (flows Flows) Serialize() (string, error) {
return string(res), nil
}

// Decode the trait configuration to a type safe struct
func (in *TraitSpec) Decode(target interface{}) error {
md := mapstructure.Metadata{}

decoder, err := mapstructure.NewDecoder(
&mapstructure.DecoderConfig{
Metadata: &md,
WeaklyTypedInput: true,
TagName: "property",
Result: &target,
},
)

if err != nil {
return err
}

return decoder.Decode(in.Configuration)
}
18 changes: 12 additions & 6 deletions pkg/apis/camel/v1alpha1/integration_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package v1alpha1
import (
"strings"

"github.com/apache/camel-k/pkg/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -82,12 +81,19 @@ func (is *IntegrationSpec) AddConfiguration(confType string, confValue string) {

// AddDependency --
func (is *IntegrationSpec) AddDependency(dependency string) {
switch {
case strings.HasPrefix(dependency, "camel-"):
util.StringSliceUniqueAdd(&is.Dependencies, "camel:"+strings.TrimPrefix(dependency, "camel-"))
default:
util.StringSliceUniqueAdd(&is.Dependencies, dependency)
if is.Dependencies == nil {
is.Dependencies = make([]string, 0)
}
newDep := dependency
if (strings.HasPrefix(newDep, "camel-")) {
newDep = "camel:"+strings.TrimPrefix(dependency, "camel-")
}
for _, d := range is.Dependencies {
if d == newDep {
return
}
}
is.Dependencies = append(is.Dependencies, newDep)
}

// Configurations --
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/trait_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (c *Catalog) configureTraits(traits map[string]v1alpha1.TraitSpec) error {
for id, traitSpec := range traits {
catTrait := c.GetTrait(id)
if catTrait != nil {
if err := traitSpec.Decode(catTrait); err != nil {
if err := decodeTraitSpec(&traitSpec, catTrait); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/trait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestTraitDecode(t *testing.T) {
env.Integration.Spec.Traits["service"] = svcTrait

svc := newServiceTrait()
err := svcTrait.Decode(svc)
err := decodeTraitSpec(&svcTrait, svc)

assert.Nil(t, err)
assert.Equal(t, 7071, svc.Port)
Expand Down
20 changes: 20 additions & 0 deletions pkg/trait/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"regexp"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/scylladb/go-set/strset"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand Down Expand Up @@ -131,3 +132,22 @@ func parseCsvMap(csvMap *string) (map[string]string, error) {

return m, nil
}

func decodeTraitSpec(in *v1alpha1.TraitSpec, target interface{}) error {
md := mapstructure.Metadata{}

decoder, err := mapstructure.NewDecoder(
&mapstructure.DecoderConfig{
Metadata: &md,
WeaklyTypedInput: true,
TagName: "property",
Result: &target,
},
)

if err != nil {
return err
}

return decoder.Decode(in.Configuration)
}

0 comments on commit ed6b57b

Please sign in to comment.