Skip to content

Commit

Permalink
feat(cmd/run): validate destination path
Browse files Browse the repository at this point in the history
  • Loading branch information
squakez committed Jun 16, 2021
1 parent 8033103 commit d56fc16
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
23 changes: 21 additions & 2 deletions pkg/cmd/run_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions pkg/cmd/run_help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit d56fc16

Please sign in to comment.