2022-10-17 07:24:07 +00:00
|
|
|
package hubtest
|
2021-10-04 15:14:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
2021-10-04 15:14:52 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HubTest struct {
|
2023-12-07 11:21:04 +00:00
|
|
|
CrowdSecPath string
|
|
|
|
CscliPath string
|
|
|
|
HubPath string
|
|
|
|
HubTestPath string //generic parser/scenario tests .tests
|
|
|
|
HubAppsecTestPath string //dir specific to appsec tests .appsec-tests
|
|
|
|
HubIndexFile string
|
|
|
|
TemplateConfigPath string
|
|
|
|
TemplateProfilePath string
|
|
|
|
TemplateSimulationPath string
|
|
|
|
TemplateAcquisPath string
|
|
|
|
TemplateAppsecProfilePath string
|
2023-12-14 15:05:16 +00:00
|
|
|
NucleiTargetHost string
|
|
|
|
AppSecHost string
|
|
|
|
|
|
|
|
HubIndex *cwhub.Hub
|
|
|
|
Tests []*HubTestItem
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2023-12-07 11:21:04 +00:00
|
|
|
templateConfigFile = "template_config.yaml"
|
|
|
|
templateSimulationFile = "template_simulation.yaml"
|
|
|
|
templateProfileFile = "template_profiles.yaml"
|
|
|
|
templateAcquisFile = "template_acquis.yaml"
|
|
|
|
templateAppsecProfilePath = "template_appsec-profile.yaml"
|
2023-12-13 16:45:56 +00:00
|
|
|
TemplateNucleiFile = `id: {{.TestName}}
|
|
|
|
info:
|
|
|
|
name: {{.TestName}}
|
|
|
|
author: crowdsec
|
|
|
|
severity: info
|
|
|
|
description: {{.TestName}} testing
|
|
|
|
tags: appsec-testing
|
|
|
|
http:
|
|
|
|
#this is a dummy request, edit the request(s) to match your needs
|
|
|
|
- raw:
|
|
|
|
- |
|
|
|
|
GET /test HTTP/1.1
|
|
|
|
Host: {{"{{"}}Hostname{{"}}"}}
|
|
|
|
|
|
|
|
cookie-reuse: true
|
|
|
|
#test will fail because we won't match http status
|
|
|
|
matchers:
|
|
|
|
- type: status
|
|
|
|
status:
|
|
|
|
- 403
|
|
|
|
`
|
2021-10-04 15:14:52 +00:00
|
|
|
)
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
func NewHubTest(hubPath string, crowdsecPath string, cscliPath string, isAppsecTest bool) (HubTest, error) {
|
2023-11-24 14:57:32 +00:00
|
|
|
hubPath, err := filepath.Abs(hubPath)
|
2021-10-04 15:14:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return HubTest{}, fmt.Errorf("can't get absolute path of hub: %+v", err)
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
// we can't use hubtest without the hub
|
2023-11-24 14:57:32 +00:00
|
|
|
if _, err = os.Stat(hubPath); os.IsNotExist(err) {
|
2021-10-04 15:14:52 +00:00
|
|
|
return HubTest{}, fmt.Errorf("path to hub '%s' doesn't exist, can't run", hubPath)
|
|
|
|
}
|
|
|
|
// we can't use hubtest without crowdsec binary
|
2023-11-24 14:57:32 +00:00
|
|
|
if _, err = exec.LookPath(crowdsecPath); err != nil {
|
|
|
|
if _, err = os.Stat(crowdsecPath); os.IsNotExist(err) {
|
2021-10-04 15:14:52 +00:00
|
|
|
return HubTest{}, fmt.Errorf("path to crowdsec binary '%s' doesn't exist or is not in $PATH, can't run", crowdsecPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't use hubtest without cscli binary
|
2023-11-24 14:57:32 +00:00
|
|
|
if _, err = exec.LookPath(cscliPath); err != nil {
|
|
|
|
if _, err = os.Stat(cscliPath); os.IsNotExist(err) {
|
2021-10-04 15:14:52 +00:00
|
|
|
return HubTest{}, fmt.Errorf("path to cscli binary '%s' doesn't exist or is not in $PATH, can't run", cscliPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if isAppsecTest {
|
|
|
|
HubTestPath := filepath.Join(hubPath, "./.appsec-tests/")
|
|
|
|
hubIndexFile := filepath.Join(hubPath, ".index.json")
|
|
|
|
|
|
|
|
local := &csconfig.LocalHubCfg{
|
|
|
|
HubDir: hubPath,
|
|
|
|
HubIndexFile: hubIndexFile,
|
|
|
|
InstallDir: HubTestPath,
|
|
|
|
InstallDataDir: HubTestPath,
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := cwhub.NewHub(local, nil, false, nil)
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return HubTest{}, fmt.Errorf("unable to load hub: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return HubTest{
|
|
|
|
CrowdSecPath: crowdsecPath,
|
|
|
|
CscliPath: cscliPath,
|
|
|
|
HubPath: hubPath,
|
|
|
|
HubTestPath: HubTestPath,
|
|
|
|
HubIndexFile: hubIndexFile,
|
|
|
|
TemplateConfigPath: filepath.Join(HubTestPath, templateConfigFile),
|
|
|
|
TemplateProfilePath: filepath.Join(HubTestPath, templateProfileFile),
|
|
|
|
TemplateSimulationPath: filepath.Join(HubTestPath, templateSimulationFile),
|
|
|
|
TemplateAppsecProfilePath: filepath.Join(HubTestPath, templateAppsecProfilePath),
|
|
|
|
TemplateAcquisPath: filepath.Join(HubTestPath, templateAcquisFile),
|
2023-12-14 15:05:16 +00:00
|
|
|
NucleiTargetHost: DefaultNucleiTarget,
|
|
|
|
AppSecHost: DefaultAppsecHost,
|
2023-12-07 11:21:04 +00:00
|
|
|
HubIndex: hub,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
HubTestPath := filepath.Join(hubPath, "./.tests/")
|
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
hubIndexFile := filepath.Join(hubPath, ".index.json")
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
local := &csconfig.LocalHubCfg{
|
|
|
|
HubDir: hubPath,
|
|
|
|
HubIndexFile: hubIndexFile,
|
|
|
|
InstallDir: HubTestPath,
|
|
|
|
InstallDataDir: HubTestPath,
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := cwhub.NewHub(local, nil, false, nil)
|
2021-10-04 15:14:52 +00:00
|
|
|
if err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return HubTest{}, fmt.Errorf("unable to load hub: %s", err)
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return HubTest{
|
|
|
|
CrowdSecPath: crowdsecPath,
|
|
|
|
CscliPath: cscliPath,
|
|
|
|
HubPath: hubPath,
|
|
|
|
HubTestPath: HubTestPath,
|
|
|
|
HubIndexFile: hubIndexFile,
|
2023-12-07 11:21:04 +00:00
|
|
|
TemplateConfigPath: filepath.Join(HubTestPath, templateConfigFile),
|
|
|
|
TemplateProfilePath: filepath.Join(HubTestPath, templateProfileFile),
|
|
|
|
TemplateSimulationPath: filepath.Join(HubTestPath, templateSimulationFile),
|
2023-11-24 14:57:32 +00:00
|
|
|
HubIndex: hub,
|
2021-10-04 15:14:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HubTest) LoadTestItem(name string) (*HubTestItem, error) {
|
|
|
|
HubTestItem := &HubTestItem{}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
testItem, err := NewTest(name, h)
|
|
|
|
if err != nil {
|
|
|
|
return HubTestItem, err
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
h.Tests = append(h.Tests, testItem)
|
|
|
|
|
|
|
|
return testItem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HubTest) LoadAllTests() error {
|
2022-09-06 11:55:03 +00:00
|
|
|
testsFolder, err := os.ReadDir(h.HubTestPath)
|
2021-10-04 15:14:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range testsFolder {
|
|
|
|
if f.IsDir() {
|
|
|
|
if _, err := h.LoadTestItem(f.Name()); err != nil {
|
2023-06-29 09:34:59 +00:00
|
|
|
return fmt.Errorf("while loading %s: %w", f.Name(), err)
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
return nil
|
|
|
|
}
|