Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing the rule schema.yml file stream #567

Merged
merged 5 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- sphinx 4.2.0 to 4.3.0 and tzlocal==2.1 - [#561](https://github.com/jertel/elastalert2/pull/561) - @nsano-rururu
- jinja2 3.0.1 to 3.0.3 - [#562](https://github.com/jertel/elastalert2/pull/562) - @nsano-rururu
- Fix `get_rule_file_hash` TypeError - [#566](https://github.com/jertel/elastalert2/pull/566) - @JeffAshton
- Ensure `schema.yaml` stream closed - [#567](https://github.com/jertel/elastalert2/pull/567) - @JeffAshton

# 2.2.3

Expand Down
13 changes: 9 additions & 4 deletions elastalert/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
from elastalert.yaml import read_yaml


# load rules schema
def load_rule_schema():
schema_path = os.path.join(os.path.dirname(__file__), 'schema.yaml')
with open(schema_path) as schema_file:
schema_yml = yaml.load(schema_file, Loader=yaml.FullLoader)
return jsonschema.Draft7Validator(schema_yml)


class RulesLoader(object):
# import rule dependency
import_rules = {}
Expand Down Expand Up @@ -137,10 +145,7 @@ class RulesLoader(object):
jinja_environment = Environment(loader=FileSystemLoader(""))

def __init__(self, conf):
# schema for rule yaml
self.rule_schema = jsonschema.Draft7Validator(
yaml.load(open(os.path.join(os.path.dirname(__file__), 'schema.yaml')), Loader=yaml.FullLoader))
Copy link
Contributor Author

@JeffAshton JeffAshton Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chasing down unclosed streams from the yaml library and came across this one. Likely not an issue, but the with open pattern should be followed everywhere.


self.rule_schema = load_rule_schema()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing test coverage utilizes the rule_schema

self.base_config = copy.deepcopy(conf)

def load(self, conf, args=None):
Expand Down