This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 3cd69e4babf80f355cb7d19ef1219f549a3254f8
Author: Pasquale Congiusti <[email protected]>
AuthorDate: Thu Jun 3 16:39:49 2021 +0200

    feat(cmd/run): validate destination path
    
    Ref #2003
---
 pkg/cmd/run_help.go      | 23 +++++++++++++++++++++--
 pkg/cmd/run_help_test.go | 15 +++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/pkg/cmd/run_help.go b/pkg/cmd/run_help.go
index a1ab41e..9734dbd 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 abae91d..ebbff23 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())
+}

Reply via email to