Skip to content

Commit

Permalink
fix(cmd/run): windows scheme support
Browse files Browse the repository at this point in the history
Introduced a check to verify if the source matches with a supported scheme.

Closes apache#2475
  • Loading branch information
squakez committed Jul 2, 2021
1 parent 120ed94 commit c28aa13
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 23 deletions.
12 changes: 0 additions & 12 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,18 +737,6 @@ func (o *runCmdOptions) configureTraits(integration *v1.Integration, options []s
return nil
}

func isLocalAndFileExists(fileName string) (bool, error) {
info, err := os.Stat(fileName)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
// If it is a different error (ie, permission denied) we should report it back
return false, errors.Wrap(err, fmt.Sprintf("file system error while looking for %s", fileName))
}
return !info.IsDir(), nil
}

func addIntegrationProperties(props *properties.Properties, spec *v1.IntegrationSpec) error {
for _, k := range props.Keys() {
v, _ := props.Get(k)
Expand Down
7 changes: 0 additions & 7 deletions pkg/cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,10 +527,3 @@ func TestRunTextCompressedResource(t *testing.T) {
assert.Equal(t, "text/plain", textResourceSpec.ContentType)
assert.True(t, textResourceSpec.Compression)
}

func TestIsLocalFileAndExists(t *testing.T) {
value, err := isLocalAndFileExists("/root/test")
// must not panic because a permission error
assert.NotNil(t, err)
assert.False(t, value)
}
35 changes: 35 additions & 0 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"strings"

"github.com/apache/camel-k/pkg/util/gzip"
"github.com/pkg/errors"

"github.com/mitchellh/mapstructure"

Expand All @@ -42,6 +44,12 @@ import (

const (
offlineCommandLabel = "camel.apache.org/cmd.offline"

// Supported source schemes
gistScheme = "gist"
githubScheme = "github"
httpScheme = "http"
httpsScheme = "https"
)

// DeleteIntegration --
Expand Down Expand Up @@ -248,3 +256,30 @@ func compressToString(content []byte) (string, error) {

return string(bytes), nil
}

func isLocalAndFileExists(uri string) (bool, error) {
if hasSupportedScheme(uri) {
// it's not a local file as it matches one of the supporting schemes
return false, nil
}
info, err := os.Stat(uri)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
// If it is a different error (ie, permission denied) we should report it back
return false, errors.Wrap(err, fmt.Sprintf("file system error while looking for %s", uri))
}
return !info.IsDir(), nil
}

func hasSupportedScheme(uri string) bool {
if strings.HasPrefix(strings.ToLower(uri), gistScheme+":") ||
strings.HasPrefix(strings.ToLower(uri), githubScheme+":") ||
strings.HasPrefix(strings.ToLower(uri), httpScheme+":") ||
strings.HasPrefix(strings.ToLower(uri), httpsScheme+":") {
return true
}

return false
}
8 changes: 4 additions & 4 deletions pkg/cmd/util_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
}

switch {
case u.Scheme == "gist" || strings.HasPrefix(location, "https://gist.github.com/"):
case u.Scheme == gistScheme || strings.HasPrefix(location, "https://gist.github.com/"):
var tc *http.Client

if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok {
Expand Down Expand Up @@ -133,7 +133,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
}
sources = append(sources, answer)
}
case u.Scheme == "github":
case u.Scheme == githubScheme:
answer := Source{
Name: path.Base(location),
Origin: location,
Expand All @@ -149,7 +149,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
return sources, err
}
sources = append(sources, answer)
case u.Scheme == "http":
case u.Scheme == httpScheme:
answer := Source{
Name: path.Base(location),
Origin: location,
Expand All @@ -165,7 +165,7 @@ func ResolveSources(ctx context.Context, locations []string, compress bool) ([]S
return sources, err
}
sources = append(sources, answer)
case u.Scheme == "https":
case u.Scheme == httpsScheme:
answer := Source{
Name: path.Base(location),
Origin: location,
Expand Down
65 changes: 65 additions & 0 deletions pkg/cmd/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCorrectFileValuesButNotFound(t *testing.T) {
value1, err1 := isLocalAndFileExists("c:\\test")
value2, err2 := isLocalAndFileExists("path/to/file")

// they are all not found, but it must not panic
assert.Nil(t, err1)
assert.False(t, value1)
assert.Nil(t, err2)
assert.False(t, value2)
}

func TestPermissionDenied(t *testing.T) {
value, err := isLocalAndFileExists("/root/test")
// must not panic because a permission error
assert.NotNil(t, err)
assert.False(t, value)
}

func TestSupportedScheme(t *testing.T) {
gistValue, err1 := isLocalAndFileExists("gist:some/gist/resource")
githubValue, err2 := isLocalAndFileExists("github:some/github/resource")
httpValue, err3 := isLocalAndFileExists("http://some/http/resource")
httpsValue, err4 := isLocalAndFileExists("https://some/https/resource")

assert.Nil(t, err1)
assert.False(t, gistValue)
assert.Nil(t, err2)
assert.False(t, githubValue)
assert.Nil(t, err3)
assert.False(t, httpValue)
assert.Nil(t, err4)
assert.False(t, httpsValue)
}

func TestUnSupportedScheme(t *testing.T) {
value, err := isLocalAndFileExists("bad_scheme:some/bad/resource")
// must not report an error
assert.Nil(t, err)
assert.False(t, value)
}

0 comments on commit c28aa13

Please sign in to comment.