Skip to content

Commit

Permalink
Merge branch 'master' into cscli-bouncers
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jan 25, 2024
2 parents f0d8c05 + 91b0fce commit 1e40257
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 44 deletions.
9 changes: 0 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,6 @@ issues:
- nosprintfhostport
text: "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"

- linters:
- wastedassign
text: "assigned to .*, but reassigned without using the value"

# https://github.com/timakin/bodyclose
- linters:
- bodyclose
Expand All @@ -310,11 +306,6 @@ issues:
- nonamedreturns
text: "named return .* with type .* found"

# https://github.com/alexkohler/nakedret#purpose
- linters:
- nakedret
text: "naked return in func .* with .* lines of code"

#
# Will fix, might be trickier
#
Expand Down
3 changes: 1 addition & 2 deletions cmd/crowdsec-cli/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ func SourceFromAlert(alert *models.Alert) string {

//try to compose a human friendly version
if *alert.Source.Value != "" && *alert.Source.Scope != "" {
scope := ""
scope = fmt.Sprintf("%s:%s", *alert.Source.Scope, *alert.Source.Value)
scope := fmt.Sprintf("%s:%s", *alert.Source.Scope, *alert.Source.Value)
extra := ""
if alert.Source.Cn != "" {
extra = alert.Source.Cn
Expand Down
10 changes: 5 additions & 5 deletions cmd/crowdsec-cli/copyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func copyFileContents(src, dst string) (err error) {
}

/*copy the file, ioutile doesn't offer the feature*/
func CopyFile(sourceSymLink, destinationFile string) (err error) {
func CopyFile(sourceSymLink, destinationFile string) error {
sourceFile, err := filepath.EvalSymlinks(sourceSymLink)
if err != nil {
log.Infof("Not a symlink : %s", err)
Expand All @@ -51,7 +51,7 @@ func CopyFile(sourceSymLink, destinationFile string) (err error) {

sourceFileStat, err := os.Stat(sourceFile)
if err != nil {
return
return err
}

if !sourceFileStat.Mode().IsRegular() {
Expand All @@ -63,21 +63,21 @@ func CopyFile(sourceSymLink, destinationFile string) (err error) {
destinationFileStat, err := os.Stat(destinationFile)
if err != nil {
if !os.IsNotExist(err) {
return
return err
}
} else {
if !(destinationFileStat.Mode().IsRegular()) {
return fmt.Errorf("copyFile: non-regular destination file %s (%q)", destinationFileStat.Name(), destinationFileStat.Mode().String())
}
if os.SameFile(sourceFileStat, destinationFileStat) {
return
return err
}
}

if err = os.Link(sourceFile, destinationFile); err != nil {
err = copyFileContents(sourceFile, destinationFile)
}

return
return err
}

3 changes: 1 addition & 2 deletions cmd/crowdsec-cli/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,9 @@ func (cli cliExplain) run(cmd *cobra.Command, args []string) error {
}
}
}()
tmpFile := ""
// we create a temporary log file if a log line/stdin has been provided
if logLine != "" || logFile == "-" {
tmpFile = filepath.Join(dir, "cscli_test_tmp.log")
tmpFile := filepath.Join(dir, "cscli_test_tmp.log")
f, err = os.Create(tmpFile)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions cmd/crowdsec-cli/require/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ func HubBranch(cfg *csconfig.Config) string {

return branch
}

func HubURLTemplate(cfg *csconfig.Config) string {
return cfg.Cscli.HubURLTemplate
}
4 changes: 2 additions & 2 deletions cmd/crowdsec-cli/require/require.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ func Notifications(c *csconfig.Config) error {
func RemoteHub(c *csconfig.Config) *cwhub.RemoteHubCfg {
// set branch in config, and log if necessary
branch := HubBranch(c)
urlTemplate := HubURLTemplate(c)
remote := &cwhub.RemoteHubCfg{
Branch: branch,
URLTemplate: "https://hub-cdn.crowdsec.net/%s/%s",
// URLTemplate: "http://localhost:8000/crowdsecurity/%s/hub/%s",
URLTemplate: urlTemplate,
IndexPath: ".index.json",
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/crowdsec/win_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type crowdsec_winservice struct {
config *csconfig.Config
}

func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
tick := time.Tick(500 * time.Millisecond)
Expand Down Expand Up @@ -59,7 +59,8 @@ func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest,
if err != nil {
log.Fatal(err)
}
return

return false, 0
}

func runService(name string) error {
Expand Down
5 changes: 2 additions & 3 deletions docker/docker_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# shellcheck disable=SC2292 # allow [ test ] syntax
# shellcheck disable=SC2310 # allow "if function..." syntax with -e

set -e
# set -e
shopt -s inherit_errexit

# match true, TRUE, True, tRuE, etc.
Expand Down Expand Up @@ -302,9 +302,8 @@ conf_set_if "$PLUGIN_DIR" '.config_paths.plugin_dir = strenv(PLUGIN_DIR)'

## Install hub items
cscli hub update
cscli hub upgrade

cscli_if_clean collections upgrade crowdsecurity/linux
cscli_if_clean parsers upgrade crowdsecurity/whitelists
cscli_if_clean parsers install crowdsecurity/docker-logs
cscli_if_clean parsers install crowdsecurity/cri-logs

Expand Down
1 change: 1 addition & 0 deletions docker/test/tests/test_flavors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_cscli_lapi(crowdsec, flavor):
assert "You can successfully interact with Local API (LAPI)" in stdout


@pytest.mark.skip(reason="currently broken by hub upgrade")
def test_flavor_content(crowdsec, flavor):
"""Test flavor contents"""
with crowdsec(flavor=flavor) as cs:
Expand Down
10 changes: 4 additions & 6 deletions pkg/acquisition/modules/appsec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,9 @@ func (r *AppsecRunner) AccumulateTxToEvent(evt *types.Event, req *appsec.ParsedR

req.Tx.Variables().All(func(v variables.RuleVariable, col collection.Collection) bool {
for _, variable := range col.FindAll() {
key := ""
if variable.Key() == "" {
key = variable.Variable().Name()
} else {
key = variable.Variable().Name() + "." + variable.Key()
key := variable.Variable().Name()
if variable.Key() != "" {
key += "." + variable.Key()
}
if variable.Value() == "" {
continue
Expand Down Expand Up @@ -214,7 +212,7 @@ func (r *AppsecRunner) AccumulateTxToEvent(evt *types.Event, req *appsec.ParsedR
evt.Appsec.HasOutBandMatches = true
}

name := ""
var name string
version := ""
hash := ""
ruleNameProm := fmt.Sprintf("%d", rule.Rule().ID())
Expand Down
7 changes: 7 additions & 0 deletions pkg/csconfig/cscli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ type CscliCfg struct {
Output string `yaml:"output,omitempty"`
Color string `yaml:"color,omitempty"`
HubBranch string `yaml:"hub_branch"`
HubURLTemplate string `yaml:"__hub_url_template__,omitempty"`
SimulationConfig *SimulationConfig `yaml:"-"`
DbConfig *DatabaseCfg `yaml:"-"`

SimulationFilePath string `yaml:"-"`
PrometheusUrl string `yaml:"prometheus_uri"`
}

const defaultHubURLTemplate = "https://hub-cdn.crowdsec.net/%s/%s"

func (c *Config) loadCSCLI() error {
if c.Cscli == nil {
c.Cscli = &CscliCfg{}
Expand All @@ -25,5 +28,9 @@ func (c *Config) loadCSCLI() error {
c.Cscli.PrometheusUrl = fmt.Sprintf("http://%s:%d/metrics", c.Prometheus.ListenAddr, c.Prometheus.ListenPort)
}

if c.Cscli.HubURLTemplate == "" {
c.Cscli.HubURLTemplate = defaultHubURLTemplate
}

return nil
}
3 changes: 2 additions & 1 deletion pkg/csconfig/cscli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TestLoadCSCLI(t *testing.T) {
},
},
expected: &CscliCfg{
PrometheusUrl: "http://127.0.0.1:6060/metrics",
PrometheusUrl: "http://127.0.0.1:6060/metrics",
HubURLTemplate: defaultHubURLTemplate,
},
},
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/database/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,11 @@ func formatAlertSource(alert *models.Alert) string {
func formatAlertAsString(machineID string, alert *models.Alert) []string {
src := formatAlertSource(alert)

/**/
msg := ""
msg := "empty scenario"
if alert.Scenario != nil && *alert.Scenario != "" {
msg = *alert.Scenario
} else if alert.Message != nil && *alert.Message != "" {
msg = *alert.Message
} else {
msg = "empty scenario"
}

reason := fmt.Sprintf("%s by %s", msg, src)
Expand Down Expand Up @@ -116,7 +113,7 @@ func formatAlertAsString(machineID string, alert *models.Alert) []string {
reason = fmt.Sprintf("%s for %d/%d decisions", msg, i+1, len(alert.Decisions))
}

machineIDOrigin := ""
var machineIDOrigin string
if machineID == "" {
machineIDOrigin = *decisionItem.Origin
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/hubtest/parser_assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (p *ParserAssert) AssertFile(testFile string) error {
}

match := variableRE.FindStringSubmatch(scanner.Text())
variable := ""
var variable string

if len(match) == 0 {
log.Infof("Couldn't get variable of line '%s'", scanner.Text())
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func Parse(ctx UnixParserCtx, xp types.Event, nodes []Node) (types.Event, error)
}
clog.Tracef("node (%s) ret : %v", node.rn, ret)
if ParseDump {
parserIdxInStage := 0
var parserIdxInStage int
StageParseMutex.Lock()
if len(StageParseCache[stage][node.Name]) == 0 {
StageParseCache[stage][node.Name] = make([]dumps.ParserResult, 0)
Expand Down
11 changes: 7 additions & 4 deletions test/bats/01_cscli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,18 @@ teardown() {
assert_output "&false"

# complex type
rune -0 cscli config show --key Config.PluginConfig
rune -0 cscli config show --key Config.Prometheus
assert_output - <<-EOT
&csconfig.PluginCfg{
User: "nobody",
Group: "nogroup",
&csconfig.PrometheusCfg{
Enabled: true,
Level: "full",
ListenAddr: "127.0.0.1",
ListenPort: 6060,
}
EOT
}


@test "cscli - required configuration paths" {
config=$(cat "${CONFIG_YAML}")
configdir=$(config_get '.config_paths.config_dir')
Expand Down
2 changes: 1 addition & 1 deletion test/bats/04_capi.bats
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ setup() {

config_set 'del(.api.server.online_client)'
rune -1 cscli capi status
assert_stderr --partial "no configuration for Central API (CAPI) in '$CONFIG_YAML'"
assert_stderr --regexp "no configuration for Central API \(CAPI\) in '$(echo $CONFIG_YAML|sed s#//#/#g)'"
}
@test "cscli capi status" {
Expand Down

0 comments on commit 1e40257

Please sign in to comment.