2022-10-17 07:24:07 +00:00
|
|
|
package hubtest
|
2021-10-04 15:14:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/appsec/appsec_rule"
|
2023-11-24 14:57:32 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
2024-01-15 08:16:03 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/maptools"
|
2023-12-07 11:21:04 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v2"
|
2021-10-04 15:14:52 +00:00
|
|
|
)
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
type Coverage struct {
|
|
|
|
Name string
|
2021-10-04 15:14:52 +00:00
|
|
|
TestsCount int
|
|
|
|
PresentIn map[string]bool //poorman's set
|
|
|
|
}
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
func (h *HubTest) GetAppsecCoverage() ([]Coverage, error) {
|
|
|
|
if len(h.HubIndex.GetItemMap(cwhub.APPSEC_RULES)) == 0 {
|
|
|
|
return nil, fmt.Errorf("no appsec rules in hub index")
|
|
|
|
}
|
|
|
|
|
|
|
|
// populate from hub, iterate in alphabetical order
|
2024-01-15 08:16:03 +00:00
|
|
|
pkeys := maptools.SortedKeys(h.HubIndex.GetItemMap(cwhub.APPSEC_RULES))
|
2023-12-07 11:21:04 +00:00
|
|
|
coverage := make([]Coverage, len(pkeys))
|
|
|
|
|
|
|
|
for i, name := range pkeys {
|
|
|
|
coverage[i] = Coverage{
|
|
|
|
Name: name,
|
|
|
|
TestsCount: 0,
|
|
|
|
PresentIn: make(map[string]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parser the expressions a-la-oneagain
|
|
|
|
appsecTestConfigs, err := filepath.Glob(".appsec-tests/*/config.yaml")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("while find appsec-tests config: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, appsecTestConfigPath := range appsecTestConfigs {
|
|
|
|
configFileData := &HubTestItemConfig{}
|
|
|
|
yamlFile, err := os.ReadFile(appsecTestConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to open appsec test config file '%s': %s", appsecTestConfigPath, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlFile, configFileData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, appsecRulesFile := range configFileData.AppsecRules {
|
|
|
|
appsecRuleData := &appsec_rule.CustomRule{}
|
|
|
|
yamlFile, err := os.ReadFile(appsecRulesFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to open appsec rule '%s': %s", appsecRulesFile, err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlFile, appsecRuleData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal: %v", err)
|
|
|
|
}
|
|
|
|
appsecRuleName := appsecRuleData.Name
|
|
|
|
|
|
|
|
for idx, cov := range coverage {
|
|
|
|
if cov.Name == appsecRuleName {
|
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[appsecTestConfigPath] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return coverage, nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
func (h *HubTest) GetParsersCoverage() ([]Coverage, error) {
|
2023-12-01 08:36:38 +00:00
|
|
|
if len(h.HubIndex.GetItemMap(cwhub.PARSERS)) == 0 {
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil, fmt.Errorf("no parsers in hub index")
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
// populate from hub, iterate in alphabetical order
|
2024-01-15 08:16:03 +00:00
|
|
|
pkeys := maptools.SortedKeys(h.HubIndex.GetItemMap(cwhub.PARSERS))
|
2023-11-24 14:57:32 +00:00
|
|
|
coverage := make([]Coverage, len(pkeys))
|
|
|
|
|
|
|
|
for i, name := range pkeys {
|
|
|
|
coverage[i] = Coverage{
|
|
|
|
Name: name,
|
2021-10-04 15:14:52 +00:00
|
|
|
TestsCount: 0,
|
|
|
|
PresentIn: make(map[string]bool),
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// parser the expressions a-la-oneagain
|
2021-10-04 15:14:52 +00:00
|
|
|
passerts, err := filepath.Glob(".tests/*/parser.assert")
|
|
|
|
if err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil, fmt.Errorf("while find parser asserts : %s", 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
|
|
|
for _, assert := range passerts {
|
|
|
|
file, err := os.Open(assert)
|
|
|
|
if err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil, fmt.Errorf("while reading %s : %s", assert, 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
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
log.Debugf("assert line : %s", line)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
match := parserResultRE.FindStringSubmatch(line)
|
2021-10-04 15:14:52 +00:00
|
|
|
if len(match) == 0 {
|
|
|
|
log.Debugf("%s doesn't match", line)
|
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
sidx := parserResultRE.SubexpIndex("parser")
|
2021-10-04 15:14:52 +00:00
|
|
|
capturedParser := match[sidx]
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
for idx, pcover := range coverage {
|
2023-11-24 14:57:32 +00:00
|
|
|
if pcover.Name == capturedParser {
|
2021-10-04 15:14:52 +00:00
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
parserNameSplit := strings.Split(pcover.Name, "/")
|
2021-10-04 15:14:52 +00:00
|
|
|
parserNameOnly := parserNameSplit[len(parserNameSplit)-1]
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
if parserNameOnly == capturedParser {
|
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
capturedParserSplit := strings.Split(capturedParser, "/")
|
|
|
|
capturedParserName := capturedParserSplit[len(capturedParserSplit)-1]
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
if capturedParserName == parserNameOnly {
|
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
if capturedParserName == parserNameOnly+"-logs" {
|
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
file.Close()
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
return coverage, nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
func (h *HubTest) GetScenariosCoverage() ([]Coverage, error) {
|
2023-12-07 11:21:04 +00:00
|
|
|
if len(h.HubIndex.GetItemMap(cwhub.SCENARIOS)) == 0 {
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil, fmt.Errorf("no scenarios in hub index")
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
// populate from hub, iterate in alphabetical order
|
2024-01-15 08:16:03 +00:00
|
|
|
pkeys := maptools.SortedKeys(h.HubIndex.GetItemMap(cwhub.SCENARIOS))
|
2023-11-24 14:57:32 +00:00
|
|
|
coverage := make([]Coverage, len(pkeys))
|
|
|
|
|
|
|
|
for i, name := range pkeys {
|
|
|
|
coverage[i] = Coverage{
|
|
|
|
Name: name,
|
2021-10-04 15:14:52 +00:00
|
|
|
TestsCount: 0,
|
|
|
|
PresentIn: make(map[string]bool),
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
2021-10-04 15:14:52 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
// parser the expressions a-la-oneagain
|
2021-10-04 15:14:52 +00:00
|
|
|
passerts, err := filepath.Glob(".tests/*/scenario.assert")
|
|
|
|
if err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil, fmt.Errorf("while find scenario asserts : %s", 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
|
|
|
for _, assert := range passerts {
|
|
|
|
file, err := os.Open(assert)
|
|
|
|
if err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil, fmt.Errorf("while reading %s : %s", assert, 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
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
log.Debugf("assert line : %s", line)
|
2023-11-24 14:57:32 +00:00
|
|
|
match := scenarioResultRE.FindStringSubmatch(line)
|
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
if len(match) == 0 {
|
|
|
|
log.Debugf("%s doesn't match", line)
|
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
sidx := scenarioResultRE.SubexpIndex("scenario")
|
|
|
|
scannerName := match[sidx]
|
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
for idx, pcover := range coverage {
|
2023-11-24 14:57:32 +00:00
|
|
|
if pcover.Name == scannerName {
|
2021-10-04 15:14:52 +00:00
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
scenarioNameSplit := strings.Split(pcover.Name, "/")
|
2021-10-04 15:14:52 +00:00
|
|
|
scenarioNameOnly := scenarioNameSplit[len(scenarioNameSplit)-1]
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if scenarioNameOnly == scannerName {
|
2021-10-04 15:14:52 +00:00
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
fixedProbingWord := strings.ReplaceAll(pcover.Name, "probbing", "probing")
|
|
|
|
fixedProbingAssert := strings.ReplaceAll(scannerName, "probbing", "probing")
|
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
if fixedProbingWord == fixedProbingAssert {
|
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if fmt.Sprintf("%s-detection", pcover.Name) == scannerName {
|
2021-10-04 15:14:52 +00:00
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
if fmt.Sprintf("%s-detection", fixedProbingWord) == fixedProbingAssert {
|
|
|
|
coverage[idx].TestsCount++
|
|
|
|
coverage[idx].PresentIn[assert] = true
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2021-10-04 15:14:52 +00:00
|
|
|
return coverage, nil
|
|
|
|
}
|