浏览代码

[parser/scenarios] defer yaml file closure (#2689)

* Defer close the fd's
* Convert fatals into return with errors
Laurence Jones 1 年之前
父节点
当前提交
4df4e5b3bf
共有 2 个文件被更改,包括 5 次插入3 次删除
  1. 2 1
      pkg/leakybucket/manager_load.go
  2. 3 2
      pkg/parser/stage.go

+ 2 - 1
pkg/leakybucket/manager_load.go

@@ -198,6 +198,7 @@ func LoadBuckets(cscfg *csconfig.CrowdsecServiceCfg, hub *cwhub.Hub, files []str
 			log.Errorf("Can't access leaky configuration file %s", f)
 			return nil, nil, err
 		}
+		defer bucketConfigurationFile.Close()
 		dec := yaml.NewDecoder(bucketConfigurationFile)
 		dec.SetStrict(true)
 		for {
@@ -224,7 +225,7 @@ func LoadBuckets(cscfg *csconfig.CrowdsecServiceCfg, hub *cwhub.Hub, files []str
 			}
 			ok, err := cwversion.Satisfies(bucketFactory.FormatVersion, cwversion.Constraint_scenario)
 			if err != nil {
-				log.Fatalf("Failed to check version : %s", err)
+				return nil, nil, fmt.Errorf("failed to check version : %s", err)
 			}
 			if !ok {
 				log.Errorf("can't load %s : %s doesn't satisfy scenario format %s, skip", bucketFactory.Name, bucketFactory.FormatVersion, cwversion.Constraint_scenario)

+ 3 - 2
pkg/parser/stage.go

@@ -57,6 +57,7 @@ func LoadStages(stageFiles []Stagefile, pctx *UnixParserCtx, ectx EnricherCtx) (
 		if err != nil {
 			return nil, fmt.Errorf("can't access parsing configuration file %s : %s", stageFile.Filename, err)
 		}
+		defer yamlFile.Close()
 		//process the yaml
 		dec := yaml.NewDecoder(yamlFile)
 		dec.SetStrict(true)
@@ -70,7 +71,7 @@ func LoadStages(stageFiles []Stagefile, pctx *UnixParserCtx, ectx EnricherCtx) (
 					log.Tracef("End of yaml file")
 					break
 				}
-				log.Fatalf("Error decoding parsing configuration file '%s': %v", stageFile.Filename, err)
+				return nil, fmt.Errorf("error decoding parsing configuration file '%s': %v", stageFile.Filename, err)
 			}
 
 			//check for empty bucket
@@ -85,7 +86,7 @@ func LoadStages(stageFiles []Stagefile, pctx *UnixParserCtx, ectx EnricherCtx) (
 			}
 			ok, err := cwversion.Satisfies(node.FormatVersion, cwversion.Constraint_parser)
 			if err != nil {
-				log.Fatalf("Failed to check version : %s", err)
+				return nil, fmt.Errorf("failed to check version : %s", err)
 			}
 			if !ok {
 				log.Errorf("%s : %s doesn't satisfy parser format %s, skip", node.Name, node.FormatVersion, cwversion.Constraint_parser)