github.com/crowdsecurity/crowdsec@v1.6.1/pkg/hubtest/parser.go (about)

     1  package hubtest
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
    10  )
    11  
    12  func (t *HubTestItem) installParserItem(item *cwhub.Item) error {
    13  	sourcePath, err := filepath.Abs(filepath.Join(t.HubPath, item.RemotePath))
    14  	if err != nil {
    15  		return fmt.Errorf("can't get absolute path of '%s': %w", sourcePath, err)
    16  	}
    17  
    18  	sourceFilename := filepath.Base(sourcePath)
    19  
    20  	// runtime/hub/parsers/s00-raw/crowdsecurity/
    21  	hubDirParserDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
    22  
    23  	// runtime/parsers/s00-raw/
    24  	itemTypeDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, item.Stage)
    25  
    26  	if err := os.MkdirAll(hubDirParserDest, os.ModePerm); err != nil {
    27  		return fmt.Errorf("unable to create folder '%s': %w", hubDirParserDest, err)
    28  	}
    29  
    30  	if err := os.MkdirAll(itemTypeDirDest, os.ModePerm); err != nil {
    31  		return fmt.Errorf("unable to create folder '%s': %w", itemTypeDirDest, err)
    32  	}
    33  
    34  	// runtime/hub/parsers/s00-raw/crowdsecurity/syslog-logs.yaml
    35  	hubDirParserPath := filepath.Join(hubDirParserDest, sourceFilename)
    36  	if err := Copy(sourcePath, hubDirParserPath); err != nil {
    37  		return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirParserPath, err)
    38  	}
    39  
    40  	// runtime/parsers/s00-raw/syslog-logs.yaml
    41  	parserDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
    42  	if err := os.Symlink(hubDirParserPath, parserDirParserPath); err != nil {
    43  		if !os.IsExist(err) {
    44  			return fmt.Errorf("unable to symlink parser '%s' to '%s': %w", hubDirParserPath, parserDirParserPath, err)
    45  		}
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  func (t *HubTestItem) installParserCustomFrom(parser string, customPath string) (bool, error) {
    52  	// we check if its a custom parser
    53  	customParserPath := filepath.Join(customPath, parser)
    54  	if _, err := os.Stat(customParserPath); os.IsNotExist(err) {
    55  		return false, nil
    56  	}
    57  
    58  	customParserPathSplit, customParserName := filepath.Split(customParserPath)
    59  	// because path is parsers/<stage>/<author>/parser.yaml and we wan't the stage
    60  	splitPath := strings.Split(customParserPathSplit, string(os.PathSeparator))
    61  	customParserStage := splitPath[len(splitPath)-3]
    62  
    63  	// check if stage exist
    64  	hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("parsers/%s", customParserStage))
    65  	if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
    66  		return false, fmt.Errorf("stage '%s' extracted from '%s' doesn't exist in the hub", customParserStage, hubStagePath)
    67  	}
    68  
    69  	stageDirDest := fmt.Sprintf("%s/parsers/%s/", t.RuntimePath, customParserStage)
    70  	if err := os.MkdirAll(stageDirDest, os.ModePerm); err != nil {
    71  		return false, fmt.Errorf("unable to create folder '%s': %w", stageDirDest, err)
    72  	}
    73  
    74  	customParserDest := filepath.Join(stageDirDest, customParserName)
    75  	// if path to parser exist, copy it
    76  	if err := Copy(customParserPath, customParserDest); err != nil {
    77  		return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customParserPath, customParserDest, err)
    78  	}
    79  
    80  	return true, nil
    81  }
    82  
    83  func (t *HubTestItem) installParserCustom(parser string) error {
    84  	for _, customPath := range t.CustomItemsLocation {
    85  		found, err := t.installParserCustomFrom(parser, customPath)
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		if found {
    91  			return nil
    92  		}
    93  	}
    94  
    95  	return fmt.Errorf("couldn't find custom parser '%s' in the following locations: %+v", parser, t.CustomItemsLocation)
    96  }
    97  
    98  func (t *HubTestItem) installParser(name string) error {
    99  	if item := t.HubIndex.GetItem(cwhub.PARSERS, name); item != nil {
   100  		return t.installParserItem(item)
   101  	}
   102  
   103  	return t.installParserCustom(name)
   104  }