github.com/crowdsecurity/crowdsec@v1.6.1/pkg/hubtest/postoverflow.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) installPostoverflowItem(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/postoverflows/s00-enrich/crowdsecurity/
    21  	hubDirPostoverflowDest := filepath.Join(t.RuntimeHubPath, filepath.Dir(item.RemotePath))
    22  
    23  	// runtime/postoverflows/s00-enrich
    24  	itemTypeDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, item.Stage)
    25  
    26  	if err := os.MkdirAll(hubDirPostoverflowDest, os.ModePerm); err != nil {
    27  		return fmt.Errorf("unable to create folder '%s': %w", hubDirPostoverflowDest, 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/postoverflows/s00-enrich/crowdsecurity/rdns.yaml
    35  	hubDirPostoverflowPath := filepath.Join(hubDirPostoverflowDest, sourceFilename)
    36  	if err := Copy(sourcePath, hubDirPostoverflowPath); err != nil {
    37  		return fmt.Errorf("unable to copy '%s' to '%s': %w", sourcePath, hubDirPostoverflowPath, err)
    38  	}
    39  
    40  	// runtime/postoverflows/s00-enrich/rdns.yaml
    41  	postoverflowDirParserPath := filepath.Join(itemTypeDirDest, sourceFilename)
    42  	if err := os.Symlink(hubDirPostoverflowPath, postoverflowDirParserPath); err != nil {
    43  		if !os.IsExist(err) {
    44  			return fmt.Errorf("unable to symlink postoverflow '%s' to '%s': %w", hubDirPostoverflowPath, postoverflowDirParserPath, err)
    45  		}
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  func (t *HubTestItem) installPostoverflowCustomFrom(postoverflow string, customPath string) (bool, error) {
    52  	// we check if its a custom postoverflow
    53  	customPostOverflowPath := filepath.Join(customPath, postoverflow)
    54  	if _, err := os.Stat(customPostOverflowPath); os.IsNotExist(err) {
    55  		return false, nil
    56  	}
    57  
    58  	customPostOverflowPathSplit := strings.Split(customPostOverflowPath, "/")
    59  	customPostoverflowName := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-1]
    60  	// because path is postoverflows/<stage>/<author>/parser.yaml and we wan't the stage
    61  	customPostoverflowStage := customPostOverflowPathSplit[len(customPostOverflowPathSplit)-3]
    62  
    63  	// check if stage exist
    64  	hubStagePath := filepath.Join(t.HubPath, fmt.Sprintf("postoverflows/%s", customPostoverflowStage))
    65  	if _, err := os.Stat(hubStagePath); os.IsNotExist(err) {
    66  		return false, fmt.Errorf("stage '%s' from extracted '%s' doesn't exist in the hub", customPostoverflowStage, hubStagePath)
    67  	}
    68  
    69  	stageDirDest := fmt.Sprintf("%s/postoverflows/%s/", t.RuntimePath, customPostoverflowStage)
    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  	customPostoverflowDest := filepath.Join(stageDirDest, customPostoverflowName)
    75  	// if path to postoverflow exist, copy it
    76  	if err := Copy(customPostOverflowPath, customPostoverflowDest); err != nil {
    77  		return false, fmt.Errorf("unable to copy custom parser '%s' to '%s': %w", customPostOverflowPath, customPostoverflowDest, err)
    78  	}
    79  
    80  	return true, nil
    81  }
    82  
    83  func (t *HubTestItem) installPostoverflowCustom(postoverflow string) error {
    84  	for _, customPath := range t.CustomItemsLocation {
    85  		found, err := t.installPostoverflowCustomFrom(postoverflow, 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 postoverflow '%s' in the following location: %+v", postoverflow, t.CustomItemsLocation)
    96  }
    97  
    98  func (t *HubTestItem) installPostoverflow(name string) error {
    99  	if hubPostOverflow := t.HubIndex.GetItem(cwhub.POSTOVERFLOWS, name); hubPostOverflow != nil {
   100  		return t.installPostoverflowItem(hubPostOverflow)
   101  	}
   102  
   103  	return t.installPostoverflowCustom(name)
   104  }