LoadConsoleContext loads the context from the hub (if provided) and the file console_context_path.
(c *csconfig.Config, hub *cwhub.Hub)
| 100 | |
| 101 | // LoadConsoleContext loads the context from the hub (if provided) and the file console_context_path. |
| 102 | func 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 | } |
no test coverage detected
searching dependent graphs…