Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/cwhub: improve support for k8s config maps with custom items #3154

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions cmd/crowdsec-cli/capi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/types"
)

const (
CAPIBaseURL = "https://api.crowdsec.net/"
CAPIURLPrefix = "v3"
)

type cliCapi struct {
cfg configGetter
}
Expand Down Expand Up @@ -78,7 +73,7 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
Password: password,
UserAgent: cwversion.UserAgent(),
URL: apiurl,
VersionPrefix: CAPIURLPrefix,
VersionPrefix: "v3",
}, nil)
if err != nil {
return fmt.Errorf("api client register ('%s'): %w", types.CAPIBaseURL, err)
Expand Down
6 changes: 2 additions & 4 deletions pkg/cwhub/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"fmt"
)

var (
// ErrNilRemoteHub is returned when trying to download with a local-only configuration.
ErrNilRemoteHub = errors.New("remote hub configuration is not provided. Please report this issue to the developers")
)
// ErrNilRemoteHub is returned when trying to download with a local-only configuration.
var ErrNilRemoteHub = errors.New("remote hub configuration is not provided. Please report this issue to the developers")

// IndexNotFoundError is returned when the remote hub index is not found.
type IndexNotFoundError struct {
Expand Down
28 changes: 28 additions & 0 deletions pkg/cwhub/relativepath.go
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{}
}

Check warning on line 14 in pkg/cwhub/relativepath.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/relativepath.go#L13-L14

Added lines #L13 - L14 were not covered by tests

absBaseDir, err := filepath.Abs(baseDir)
if err != nil {
return []string{}
}

Check warning on line 19 in pkg/cwhub/relativepath.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/relativepath.go#L18-L19

Added lines #L18 - L19 were not covered by tests

// 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))
}
72 changes: 72 additions & 0 deletions pkg/cwhub/relativepath_test.go
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)
})
}
}
Loading