-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg/cwhub: improve support for k8s config maps with custom items (#3154)
* pkg/cwhub: improve support for k8s config maps as custom items - allow links to links - ignore hidden ..data directories, but allow links to their content * allow any number of subdirectories in /etc/crowdsec/{hubtype} * item name as subdir/file.yaml * improve func test * lint
- Loading branch information
Showing
7 changed files
with
322 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cwhub | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// relativePathComponents returns the list of path components after baseDir. | ||
// If path is not inside baseDir, it returns an empty slice. | ||
func relativePathComponents(path string, baseDir string) []string { | ||
absPath, err := filepath.Abs(path) | ||
if err != nil { | ||
return []string{} | ||
} | ||
|
||
absBaseDir, err := filepath.Abs(baseDir) | ||
if err != nil { | ||
return []string{} | ||
} | ||
|
||
// is path inside baseDir? | ||
relPath, err := filepath.Rel(absBaseDir, absPath) | ||
if err != nil || strings.HasPrefix(relPath, "..") || relPath == "." { | ||
return []string{} | ||
} | ||
|
||
return strings.Split(relPath, string(filepath.Separator)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cwhub | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRelativePathComponents(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
baseDir string | ||
expected []string | ||
}{ | ||
{ | ||
name: "Path within baseDir", | ||
path: "/home/user/project/src/file.go", | ||
baseDir: "/home/user/project", | ||
expected: []string{"src", "file.go"}, | ||
}, | ||
{ | ||
name: "Path is baseDir", | ||
path: "/home/user/project", | ||
baseDir: "/home/user/project", | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Path outside baseDir", | ||
path: "/home/user/otherproject/src/file.go", | ||
baseDir: "/home/user/project", | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Path is subdirectory of baseDir", | ||
path: "/home/user/project/src/", | ||
baseDir: "/home/user/project", | ||
expected: []string{"src"}, | ||
}, | ||
{ | ||
name: "Relative paths", | ||
path: "project/src/file.go", | ||
baseDir: "project", | ||
expected: []string{"src", "file.go"}, | ||
}, | ||
{ | ||
name: "BaseDir with trailing slash", | ||
path: "/home/user/project/src/file.go", | ||
baseDir: "/home/user/project/", | ||
expected: []string{"src", "file.go"}, | ||
}, | ||
{ | ||
name: "Empty baseDir", | ||
path: "/home/user/project/src/file.go", | ||
baseDir: "", | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "Empty path", | ||
path: "", | ||
baseDir: "/home/user/project", | ||
expected: []string{}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := relativePathComponents(tt.path, tt.baseDir) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
Oops, something went wrong.