MCPcopy
hub / github.com/crowdsecurity/crowdsec / LoadConsoleContext

Function LoadConsoleContext

pkg/alertcontext/config.go:102–142  ·  view source on GitHub ↗

LoadConsoleContext loads the context from the hub (if provided) and the file console_context_path.

(c *csconfig.Config, hub *cwhub.Hub)

Source from the content-addressed store, hash-verified

100
101// LoadConsoleContext loads the context from the hub (if provided) and the file console_context_path.
102func LoadConsoleContext(c *csconfig.Config, hub *cwhub.Hub) error {
103 c.Crowdsec.ContextToSend = make(map[string][]string, 0)
104
105 if hub != nil {
106 for _, item := range hub.GetInstalledByType(cwhub.CONTEXTS, true) {
107 // context in item files goes under the key 'context'
108 if err := addContextFromItem(c.Crowdsec.ContextToSend, item); err != nil {
109 return err
110 }
111 }
112 }
113
114 ignoreMissing := false
115
116 if c.Crowdsec.ConsoleContextPath != "" {
117 // if it's provided, it must exist
118 if _, err := os.Stat(c.Crowdsec.ConsoleContextPath); err != nil {
119 return fmt.Errorf("while checking console_context_path: %w", err)
120 }
121 } else {
122 c.Crowdsec.ConsoleContextPath = filepath.Join(c.ConfigPaths.ConfigDir, "console", "context.yaml")
123 ignoreMissing = true
124 }
125
126 if err := addContextFromFile(c.Crowdsec.ContextToSend, c.Crowdsec.ConsoleContextPath); err != nil {
127 if !errors.Is(err, fs.ErrNotExist) {
128 return err
129 } else if !ignoreMissing {
130 log.Warningf("while merging context from %s: %s", c.Crowdsec.ConsoleContextPath, err)
131 }
132 }
133
134 feedback, err := json.Marshal(c.Crowdsec.ContextToSend)
135 if err != nil {
136 return fmt.Errorf("serializing console context: %s", err)
137 }
138
139 log.Debugf("console context to send: %s", feedback)
140
141 return nil
142}

Callers 3

initCrowdsecFunction · 0.92
newContextAddCmdMethod · 0.92
newContextStatusCmdMethod · 0.92

Calls 3

addContextFromItemFunction · 0.85
addContextFromFileFunction · 0.85
GetInstalledByTypeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…