diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go index a1ab41e44b..9734dbdc2b 100644 --- a/pkg/cmd/run_help.go +++ b/pkg/cmd/run_help.go @@ -29,6 +29,8 @@ import ( "github.com/apache/camel-k/pkg/util/kubernetes" ) +var invalidPaths = []string{"/etc/camel", "/deployments/dependencies"} + // RunConfigOption represents a config option type RunConfigOption struct { ConfigType configOptionType @@ -41,6 +43,19 @@ func (runConfigOption *RunConfigOption) DestinationPath() string { return runConfigOption.destinationPath } +// Validate checks if the DestinationPath exists and in case if it's a valid path +func (runConfigOption *RunConfigOption) Validate() error { + if runConfigOption.destinationPath == "" { + return nil + } + for _, invalidPath := range invalidPaths { + if runConfigOption.destinationPath == invalidPath || strings.HasPrefix(runConfigOption.destinationPath, invalidPath+"/") { + return fmt.Errorf("you cannot mount a file under %s path", invalidPath) + } + } + return nil +} + type configOptionType string const ( @@ -79,7 +94,7 @@ func ParseResourceOption(item string) (*RunConfigOption, error) { if err != nil { if strings.HasPrefix(err.Error(), "could not match configuration") { fmt.Printf("Warn: --resource %s has been deprecated. You should use --resource file:%s instead.\n", item, item) - return newRunConfigOption(ConfigOptionTypeFile, item), nil + return parseOption("file:" + item) } return nil, err } @@ -109,7 +124,11 @@ func parseOption(item string) (*RunConfigOption, error) { // Should never reach this return nil, fmt.Errorf("invalid config option type %s", groups[1]) } - return newRunConfigOption(cot, groups[2]), nil + configurationOption := newRunConfigOption(cot, groups[2]) + if err := configurationOption.Validate(); err != nil { + return nil, err + } + return configurationOption, nil } func applyOption(config *RunConfigOption, integrationSpec *v1.IntegrationSpec, diff --git a/pkg/cmd/run_help_test.go b/pkg/cmd/run_help_test.go index abae91d54f..ebbff23bb3 100644 --- a/pkg/cmd/run_help_test.go +++ b/pkg/cmd/run_help_test.go @@ -67,3 +67,18 @@ func TestFilterFileLocation(t *testing.T) { assert.Equal(t, "app.properties", filteredOptions[1]) assert.Equal(t, "/validfile", filteredOptions[2]) } + +func TestValidateFileLocation(t *testing.T) { + validLocation := "file:my-file.txt@/tmp/another-name.xml" + etcCamelLocation := "configmap:my-cm@/etc/camel/configmaps" + deploymentsDepsLocation := "secret:my-sec@/deployments/dependencies" + + _, err := ParseConfigOption(validLocation) + assert.Nil(t, err) + _, err = ParseConfigOption(etcCamelLocation) + assert.NotNil(t, err) + assert.Equal(t, "you cannot mount a file under /etc/camel path", err.Error()) + _, err = ParseConfigOption(deploymentsDepsLocation) + assert.NotNil(t, err) + assert.Equal(t, "you cannot mount a file under /deployments/dependencies path", err.Error()) +}