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

     1  package hubtest
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  
     9  	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
    10  	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
    11  )
    12  
    13  type HubTest struct {
    14  	CrowdSecPath              string
    15  	CscliPath                 string
    16  	HubPath                   string
    17  	HubTestPath               string //generic parser/scenario tests .tests
    18  	HubAppsecTestPath         string //dir specific to appsec tests .appsec-tests
    19  	HubIndexFile              string
    20  	TemplateConfigPath        string
    21  	TemplateProfilePath       string
    22  	TemplateSimulationPath    string
    23  	TemplateAcquisPath        string
    24  	TemplateAppsecProfilePath string
    25  	NucleiTargetHost          string
    26  	AppSecHost                string
    27  
    28  	HubIndex *cwhub.Hub
    29  	Tests    []*HubTestItem
    30  }
    31  
    32  const (
    33  	templateConfigFile        = "template_config.yaml"
    34  	templateSimulationFile    = "template_simulation.yaml"
    35  	templateProfileFile       = "template_profiles.yaml"
    36  	templateAcquisFile        = "template_acquis.yaml"
    37  	templateAppsecProfilePath = "template_appsec-profile.yaml"
    38  	TemplateNucleiFile        = `id: {{.TestName}}
    39  info:
    40    name: {{.TestName}}
    41    author: crowdsec
    42    severity: info
    43    description: {{.TestName}} testing
    44    tags: appsec-testing
    45  http:
    46  #this is a dummy request, edit the request(s) to match your needs
    47    - raw:
    48      - |
    49        GET /test HTTP/1.1
    50        Host: {{"{{"}}Hostname{{"}}"}}
    51  
    52      cookie-reuse: true
    53  #test will fail because we won't match http status 
    54      matchers:
    55      - type: status
    56        status:
    57         - 403
    58  `
    59  )
    60  
    61  func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool) (HubTest, error) {
    62  	hubPath, err := filepath.Abs(hubPath)
    63  	if err != nil {
    64  		return HubTest{}, fmt.Errorf("can't get absolute path of hub: %+v", err)
    65  	}
    66  
    67  	// we can't use hubtest without the hub
    68  	if _, err = os.Stat(hubPath); os.IsNotExist(err) {
    69  		return HubTest{}, fmt.Errorf("path to hub '%s' doesn't exist, can't run", hubPath)
    70  	}
    71  	// we can't use hubtest without crowdsec binary
    72  	if _, err = exec.LookPath(crowdsecPath); err != nil {
    73  		if _, err = os.Stat(crowdsecPath); os.IsNotExist(err) {
    74  			return HubTest{}, fmt.Errorf("path to crowdsec binary '%s' doesn't exist or is not in $PATH, can't run", crowdsecPath)
    75  		}
    76  	}
    77  
    78  	// we can't use hubtest without cscli binary
    79  	if _, err = exec.LookPath(cscliPath); err != nil {
    80  		if _, err = os.Stat(cscliPath); os.IsNotExist(err) {
    81  			return HubTest{}, fmt.Errorf("path to cscli binary '%s' doesn't exist or is not in $PATH, can't run", cscliPath)
    82  		}
    83  	}
    84  
    85  	if isAppsecTest {
    86  		HubTestPath := filepath.Join(hubPath, "./.appsec-tests/")
    87  		hubIndexFile := filepath.Join(hubPath, ".index.json")
    88  
    89  		local := &csconfig.LocalHubCfg{
    90  			HubDir:         hubPath,
    91  			HubIndexFile:   hubIndexFile,
    92  			InstallDir:     HubTestPath,
    93  			InstallDataDir: HubTestPath,
    94  		}
    95  
    96  		hub, err := cwhub.NewHub(local, nil, false, nil)
    97  		if err != nil {
    98  			return HubTest{}, fmt.Errorf("unable to load hub: %s", err)
    99  		}
   100  
   101  		return HubTest{
   102  			CrowdSecPath:              crowdsecPath,
   103  			CscliPath:                 cscliPath,
   104  			HubPath:                   hubPath,
   105  			HubTestPath:               HubTestPath,
   106  			HubIndexFile:              hubIndexFile,
   107  			TemplateConfigPath:        filepath.Join(HubTestPath, templateConfigFile),
   108  			TemplateProfilePath:       filepath.Join(HubTestPath, templateProfileFile),
   109  			TemplateSimulationPath:    filepath.Join(HubTestPath, templateSimulationFile),
   110  			TemplateAppsecProfilePath: filepath.Join(HubTestPath, templateAppsecProfilePath),
   111  			TemplateAcquisPath:        filepath.Join(HubTestPath, templateAcquisFile),
   112  			NucleiTargetHost:          DefaultNucleiTarget,
   113  			AppSecHost:                DefaultAppsecHost,
   114  			HubIndex:                  hub,
   115  		}, nil
   116  	}
   117  
   118  	HubTestPath := filepath.Join(hubPath, "./.tests/")
   119  
   120  	hubIndexFile := filepath.Join(hubPath, ".index.json")
   121  
   122  	local := &csconfig.LocalHubCfg{
   123  		HubDir:         hubPath,
   124  		HubIndexFile:   hubIndexFile,
   125  		InstallDir:     HubTestPath,
   126  		InstallDataDir: HubTestPath,
   127  	}
   128  
   129  	hub, err := cwhub.NewHub(local, nil, false, nil)
   130  	if err != nil {
   131  		return HubTest{}, fmt.Errorf("unable to load hub: %s", err)
   132  	}
   133  
   134  	return HubTest{
   135  		CrowdSecPath:           crowdsecPath,
   136  		CscliPath:              cscliPath,
   137  		HubPath:                hubPath,
   138  		HubTestPath:            HubTestPath,
   139  		HubIndexFile:           hubIndexFile,
   140  		TemplateConfigPath:     filepath.Join(HubTestPath, templateConfigFile),
   141  		TemplateProfilePath:    filepath.Join(HubTestPath, templateProfileFile),
   142  		TemplateSimulationPath: filepath.Join(HubTestPath, templateSimulationFile),
   143  		HubIndex:               hub,
   144  	}, nil
   145  }
   146  
   147  func (h *HubTest) LoadTestItem(name string) (*HubTestItem, error) {
   148  	HubTestItem := &HubTestItem{}
   149  
   150  	testItem, err := NewTest(name, h)
   151  	if err != nil {
   152  		return HubTestItem, err
   153  	}
   154  
   155  	h.Tests = append(h.Tests, testItem)
   156  
   157  	return testItem, nil
   158  }
   159  
   160  func (h *HubTest) LoadAllTests() error {
   161  	testsFolder, err := os.ReadDir(h.HubTestPath)
   162  	if err != nil {
   163  		return err
   164  	}
   165  
   166  	for _, f := range testsFolder {
   167  		if f.IsDir() {
   168  			if _, err := h.LoadTestItem(f.Name()); err != nil {
   169  				return fmt.Errorf("while loading %s: %w", f.Name(), err)
   170  			}
   171  		}
   172  	}
   173  
   174  	return nil
   175  }