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

Function ParseSourceConfig

pkg/acquisition/acquisition.go:228–306  ·  view source on GitHub ↗

ParseSourceConfig validates and configures one YAML document. It does not expand env variables, they must already be expanded. - return sentinel error for empty/comment-only documents - backward-compat source auto-detection (filename/filenames/journalctl_filter) - validate common fields - delegate

(ctx context.Context, yamlDoc []byte, metricsLevel metrics.AcquisitionMetricsLevel, hub *cwhub.Hub)

Source from the content-addressed store, hash-verified

226// - delegate per-source config validation to the appropriate module
227// - compile transform expression
228func ParseSourceConfig(ctx context.Context, yamlDoc []byte, metricsLevel metrics.AcquisitionMetricsLevel, hub *cwhub.Hub) (*ParsedSourceConfig, error) {
229 detectedType, err := detectType(bytes.NewReader(yamlDoc))
230 if err != nil {
231 return nil, err
232 }
233
234 // if there are not keys or only comments, the document will be skipped
235 empty, err := csyaml.IsEmptyYAML(bytes.NewReader(yamlDoc))
236 if err != nil {
237 return nil, err
238 }
239
240 if empty {
241 return nil, ErrEmptyYAMLDocument
242 }
243
244 var sub configuration.DataSourceCommonCfg
245
246 // can't be strict here, the doc contains specific datasource config too but we won't collect them now.
247 if err = yaml.UnmarshalWithOptions(yamlDoc, &sub); err != nil {
248 return nil, fmt.Errorf("failed to parse: %w", errors.New(yaml.FormatError(err, false, false)))
249 }
250
251 parsed := &ParsedSourceConfig{}
252
253 // report that the user did not specify a source
254 if sub.Source == "" {
255 parsed.SourceMissing = true
256 }
257
258 // report that the user specified a source that doesn't match with one detected from the presence of other fields
259 if detectedType != "" {
260 if sub.Source != "" && sub.Source != detectedType {
261 parsed.SourceOverridden = sub.Source
262 }
263
264 sub.Source = detectedType
265 }
266
267 parsed.Common = sub
268
269 // could not detect, alas
270 if sub.Source == "" {
271 return nil, errors.New("missing 'source' field")
272 }
273
274 // pre-check that the source is valid
275 _, err = registry.LookupFactory(sub.Source)
276 if err != nil {
277 return nil, err
278 }
279
280 // check for labels now, an error for missing labels has lower priority
281 // than missing or unknown source type
282 if len(sub.Labels) == 0 && sub.Source != "docker" {
283 // docker is the only source that does not require labels
284 return nil, errors.New("missing labels")
285 }

Callers 2

TestParseSourceConfigFunction · 0.85
sourcesFromFileFunction · 0.85

Calls 6

LookupFactoryFunction · 0.92
GetExprOptionsFunction · 0.92
detectTypeFunction · 0.85
DataSourceConfigureFunction · 0.85
NewReaderMethod · 0.80
CompileMethod · 0.45

Tested by 1

TestParseSourceConfigFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…