Ignore missing console/context.yaml if not explicitly required by config.yaml (#2726)
This commit is contained in:
parent
733f5e165b
commit
1e0bcedef5
|
@ -2,7 +2,9 @@ package alertcontext
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
|
@ -14,6 +16,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrNoContextData = errors.New("no context to send")
|
||||||
|
|
||||||
// this file is here to avoid circular dependencies between the configuration and the hub
|
// this file is here to avoid circular dependencies between the configuration and the hub
|
||||||
|
|
||||||
// HubItemWrapper is a wrapper around a hub item to unmarshal only the context part
|
// HubItemWrapper is a wrapper around a hub item to unmarshal only the context part
|
||||||
|
@ -25,7 +29,7 @@ type HubItemWrapper struct {
|
||||||
// mergeContext adds the context from src to dest.
|
// mergeContext adds the context from src to dest.
|
||||||
func mergeContext(dest map[string][]string, src map[string][]string) error {
|
func mergeContext(dest map[string][]string, src map[string][]string) error {
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
return fmt.Errorf("no context data to merge")
|
return ErrNoContextData
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range src {
|
for k, v := range src {
|
||||||
|
@ -86,8 +90,9 @@ func addContextFromFile(toSend map[string][]string, filePath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mergeContext(toSend, newContext)
|
err = mergeContext(toSend, newContext)
|
||||||
if err != nil {
|
if err != nil && !errors.Is(err, ErrNoContextData) {
|
||||||
log.Warningf("while merging context from %s: %s", filePath, err)
|
// having an empty console/context.yaml is not an error
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -125,8 +130,10 @@ func LoadConsoleContext(c *csconfig.Config, hub *cwhub.Hub) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := addContextFromFile(c.Crowdsec.ContextToSend, c.Crowdsec.ConsoleContextPath); err != nil {
|
if err := addContextFromFile(c.Crowdsec.ContextToSend, c.Crowdsec.ConsoleContextPath); err != nil {
|
||||||
if !ignoreMissing || !os.IsNotExist(err) {
|
if !errors.Is(err, fs.ErrNotExist) {
|
||||||
return err
|
return err
|
||||||
|
} else if !ignoreMissing {
|
||||||
|
log.Warningf("while merging context from %s: %s", c.Crowdsec.ConsoleContextPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,21 @@ teardown() {
|
||||||
assert_stderr --partial "loading console context from $CONTEXT_YAML"
|
assert_stderr --partial "loading console context from $CONTEXT_YAML"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "no error if context file is missing but not explicitly set" {
|
||||||
|
config_set "del(.crowdsec_service.console_context_path)"
|
||||||
|
rune -0 rm -f "$CONTEXT_YAML"
|
||||||
|
rune -0 cscli lapi context status --error
|
||||||
|
refute_stderr
|
||||||
|
assert_output --partial "No context found on this agent."
|
||||||
|
rune -0 "$CROWDSEC" -t
|
||||||
|
refute_stderr --partial "no such file or directory"
|
||||||
|
}
|
||||||
|
|
||||||
@test "error if context file is explicitly set but does not exist" {
|
@test "error if context file is explicitly set but does not exist" {
|
||||||
config_set ".crowdsec_service.console_context_path=strenv(CONTEXT_YAML)"
|
config_set ".crowdsec_service.console_context_path=strenv(CONTEXT_YAML)"
|
||||||
rune -0 rm -f "$CONTEXT_YAML"
|
rune -0 rm -f "$CONTEXT_YAML"
|
||||||
|
rune -1 cscli lapi context status --error
|
||||||
|
assert_stderr --partial "context.yaml: no such file or directory"
|
||||||
rune -1 "$CROWDSEC" -t
|
rune -1 "$CROWDSEC" -t
|
||||||
assert_stderr --partial "while checking console_context_path: stat $CONTEXT_YAML: no such file or directory"
|
assert_stderr --partial "while checking console_context_path: stat $CONTEXT_YAML: no such file or directory"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue