|
@@ -15,19 +15,23 @@ import (
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
)
|
|
|
|
|
|
-// the walk/parser_visit function can't receive extra args
|
|
|
+// the walk/parserVisit function can't receive extra args
|
|
|
var hubdir, installdir string
|
|
|
|
|
|
-func parser_visit(path string, f os.DirEntry, err error) error {
|
|
|
+func validItemFileName(vname string, fauthor string, fname string) bool {
|
|
|
+ return (fauthor+"/"+fname == vname+".yaml") || (fauthor+"/"+fname == vname+".yml")
|
|
|
+}
|
|
|
+
|
|
|
+func parserVisit(path string, f os.DirEntry, err error) error {
|
|
|
var (
|
|
|
- target Item
|
|
|
- local bool
|
|
|
+ target Item
|
|
|
+ local bool
|
|
|
hubpath string
|
|
|
- inhub bool
|
|
|
- fname string
|
|
|
- ftype string
|
|
|
+ inhub bool
|
|
|
+ fname string
|
|
|
+ ftype string
|
|
|
fauthor string
|
|
|
- stage string
|
|
|
+ stage string
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
@@ -122,7 +126,7 @@ func parser_visit(path string, f os.DirEntry, err error) error {
|
|
|
log.Infof("%s is a symlink to %s that doesn't exist, deleting symlink", path, hubpath)
|
|
|
// remove the symlink
|
|
|
if err = os.Remove(path); err != nil {
|
|
|
- return fmt.Errorf("failed to unlink %s: %+v", path, err)
|
|
|
+ return fmt.Errorf("failed to unlink %s: %w", path, err)
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
@@ -171,8 +175,9 @@ func parser_visit(path string, f os.DirEntry, err error) error {
|
|
|
if fauthor != v.Author {
|
|
|
continue
|
|
|
}
|
|
|
+
|
|
|
// wrong file
|
|
|
- if CheckName(v.Name, fauthor, fname) {
|
|
|
+ if !validItemFileName(v.Name, fauthor, fname) {
|
|
|
continue
|
|
|
}
|
|
|
|
|
@@ -180,7 +185,7 @@ func parser_visit(path string, f os.DirEntry, err error) error {
|
|
|
log.Tracef("marking %s as downloaded", v.Name)
|
|
|
v.Downloaded = true
|
|
|
}
|
|
|
- } else if CheckSuffix(hubpath, v.RemotePath) {
|
|
|
+ } else if !hasPathSuffix(hubpath, v.RemotePath) {
|
|
|
// wrong file
|
|
|
// <type>/<stage>/<author>/<name>.yaml
|
|
|
continue
|
|
@@ -234,7 +239,7 @@ func parser_visit(path string, f os.DirEntry, err error) error {
|
|
|
if !match {
|
|
|
log.Tracef("got tainted match for %s : %s", v.Name, path)
|
|
|
|
|
|
- skippedTainted += 1
|
|
|
+ skippedTainted++
|
|
|
// the file and the stage is right, but the hash is wrong, it has been tainted by user
|
|
|
if !inhub {
|
|
|
v.LocalPath = path
|
|
@@ -297,7 +302,7 @@ func CollecDepsCheck(v *Item) error {
|
|
|
v.Tainted = true
|
|
|
}
|
|
|
|
|
|
- return fmt.Errorf("sub collection %s is broken: %s", val.Name, err)
|
|
|
+ return fmt.Errorf("sub collection %s is broken: %w", val.Name, err)
|
|
|
}
|
|
|
|
|
|
hubIdx[ptrtype][p] = val
|
|
@@ -353,7 +358,7 @@ func SyncDir(hub *csconfig.Hub, dir string) (error, []string) {
|
|
|
log.Errorf("failed %s : %s", cpath, err)
|
|
|
}
|
|
|
|
|
|
- err = filepath.WalkDir(cpath, parser_visit)
|
|
|
+ err = filepath.WalkDir(cpath, parserVisit)
|
|
|
if err != nil {
|
|
|
return err, warnings
|
|
|
}
|
|
@@ -387,12 +392,12 @@ func LocalSync(hub *csconfig.Hub) (error, []string) {
|
|
|
|
|
|
err, warnings := SyncDir(hub, hub.ConfigDir)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed to scan %s : %s", hub.ConfigDir, err), warnings
|
|
|
+ return fmt.Errorf("failed to scan %s: %w", hub.ConfigDir, err), warnings
|
|
|
}
|
|
|
|
|
|
err, _ = SyncDir(hub, hub.HubDir)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("failed to scan %s : %s", hub.HubDir, err), warnings
|
|
|
+ return fmt.Errorf("failed to scan %s: %w", hub.HubDir, err), warnings
|
|
|
}
|
|
|
|
|
|
return nil, warnings
|
|
@@ -413,7 +418,7 @@ func GetHubIdx(hub *csconfig.Hub) error {
|
|
|
ret, err := LoadPkgIndex(bidx)
|
|
|
if err != nil {
|
|
|
if !errors.Is(err, ReferenceMissingError) {
|
|
|
- log.Fatalf("Unable to load existing index : %v.", err)
|
|
|
+ return fmt.Errorf("unable to load existing index: %w", err)
|
|
|
}
|
|
|
|
|
|
return err
|
|
@@ -423,7 +428,7 @@ func GetHubIdx(hub *csconfig.Hub) error {
|
|
|
|
|
|
err, _ = LocalSync(hub)
|
|
|
if err != nil {
|
|
|
- log.Fatalf("Failed to sync Hub index with local deployment : %v", err)
|
|
|
+ return fmt.Errorf("failed to sync Hub index with local deployment : %w", err)
|
|
|
}
|
|
|
|
|
|
return nil
|
|
@@ -432,13 +437,13 @@ func GetHubIdx(hub *csconfig.Hub) error {
|
|
|
// LoadPkgIndex loads a local .index.json file and returns the map of parsers/scenarios/collections associated
|
|
|
func LoadPkgIndex(buff []byte) (map[string]map[string]Item, error) {
|
|
|
var (
|
|
|
- err error
|
|
|
- RawIndex map[string]map[string]Item
|
|
|
+ err error
|
|
|
+ RawIndex map[string]map[string]Item
|
|
|
missingItems []string
|
|
|
)
|
|
|
|
|
|
if err = json.Unmarshal(buff, &RawIndex); err != nil {
|
|
|
- return nil, fmt.Errorf("failed to unmarshal index : %v", err)
|
|
|
+ return nil, fmt.Errorf("failed to unmarshal index: %w", err)
|
|
|
}
|
|
|
|
|
|
log.Debugf("%d item types in hub index", len(ItemTypes))
|