-
Notifications
You must be signed in to change notification settings - Fork 3
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
Adding methods for rules and decoders #73
Conversation
GJ! LGTM |
06/07/2023Small changes done around format of f'string use |
12/07/2023 UpdateAll changes were done. Tests:EvidencesRunning:
Results:
|
13/07/2023 UpdateThe linter is not functioning properly. |
try: | ||
with open(new_rule_filename, 'r') as file: | ||
new_rules = file.read() | ||
if overwrite: | ||
self.logger.info(message=f'Changing {rules_filename} to {new_rule_filename}') | ||
else: | ||
current_rules = self.get_file_content(host, rules_filename) | ||
index_rule = current_rules.rfind("</rule>") | ||
if index_rule != -1: | ||
new_rules = current_rules[:index_rule] + '</rule>\n' + new_rules + '\n</group>' | ||
self.logger.info(message=f'Adding rule from {new_rule_filename} to {rules_filename}') | ||
|
||
self.modify_file_content(host, rules_filename, new_rules) | ||
self.logger.info(message=f'Rules succefully updated') | ||
|
||
except FileNotFoundError: | ||
self.logger.error(message=f'The file {new_rule_filename} does not exist.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method should create the file if it does not exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 30e6c21
@@ -843,3 +843,57 @@ def is_manager(self, host): | |||
bool: True if host is manager | |||
""" | |||
return host in self.get_managers() | |||
|
|||
def create_rule(self, host, new_rule_filename, rules_filename, overwrite=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new_rule_filename
is not the filename, it is the filepath. In addition respect the plural for the variables
def create_rule(self, host, new_rule_filename, rules_filename, overwrite=True): | |
def create_rule(self, host, new_rules_filepath, rules_filename, overwrite=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 30e6c21
if overwrite: | ||
self.logger.info(message=f'Changing {rules_filename} to {new_rule_filename}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move logger to the start of the method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the new flow (adding creation of new file), it will make sense to keep it in this position
current_rules = self.get_file_content(host, rules_filename) | ||
index_rule = current_rules.rfind("</rule>") | ||
if index_rule != -1: | ||
new_rules = current_rules[:index_rule] + '</rule>\n' + new_rules + '\n</group>' | ||
self.logger.info(message=f'Adding rule from {new_rule_filename} to {rules_filename}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do this? Why not simply append the new rules at the end of the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is because should go inside and follow the previous (preexisting) rule.
host (str): Host name. | ||
new_decoder_filename (str): New decoder filepath. | ||
rules_filename (str): Host decoder filename. | ||
overwrite (bool): replace the decoder file True, add decoders to decoders file, False. Defaults to True. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overwrite (bool): replace the decoder file True, add decoders to decoders file, False. Defaults to True. | |
overwrite (bool): Replace the decoder file. Defaults to True. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 30e6c21
host (str): Host name. | ||
new_rule_filename (str): New rules filepath. | ||
rules_filename (str): Host rules filename. | ||
overwrite (bool): replace the rules file True, add rules to rules file, False. Defaults to True. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overwrite (bool): replace the rules file True, add rules to rules file, False. Defaults to True. | |
overwrite (bool): Replace the rules file. Defaults to True. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 30e6c21
current_decoders = self.get_file_content(host, decoder_filename) | ||
index_decoder = current_decoders.rfind("</decoder>") | ||
if index_decoder != -1: | ||
new_decoders = current_decoders[:index_decoder] + '</decoder>\n' + new_decoders | ||
self.logger.info(message=f'Adding decoder from {new_decoder_filename} to {decoder_filename}') | ||
self.modify_file_content(host, decoder_filename, new_decoders) | ||
self.logger.info(message=f'Rules succefully updated') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 30e6c21
new_decoders = file.read() | ||
|
||
if overwrite: | ||
self.logger.info(message=f'Changing {decoder_filename} to {new_decoder_filename}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 30e6c21
UpdateFixes done after review. Tests:EvidencesRunning for rules:
Results:
|
Description
This PR includes new methods for adding and replacing rules and decoders.
Evidences
Running:
wh.change_decoders('manager1', '/home/akim/Desktop/decoder_total.xml')
wh.change_rules('manager1', '/home/akim/Desktop/rules_total.xml')
wh.add_decoder('manager1', '/home/akim/Desktop/decoder.xml', '/var/ossec/etc/decoders/local_decoder.xml')
wh.add_rule('manager1', '/home/akim/Desktop/rule.xml', '/var/ossec/etc/rules/local_rules.xml')
Results:
Changed '/var/ossec/etc/decoders/local_decoder.xml' by '/home/akim/Desktop/decoder.xml' content.
Changed '/var/ossec/etc/rules/local_rules.xml' by '/home/akim/Desktop/rules_total.xml' content.
Adding a decoder from '/home/akim/Desktop/decoder.xml' to '/var/ossec/etc/decoders/local_decoder.xml'
Adding a rule from '/home/akim/Desktop/rule.xml' to '/var/ossec/etc/rules/local_rules.xml'