From f329f63ba72ba77a55245c2656df503fffb694e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tuncay=20=C3=87OLAK?= Date: Fri, 3 Apr 2020 01:26:34 +0300 Subject: [PATCH] deleted firewall task --- src/plugins/firewall/get-rules.py | 47 ------------------------ src/plugins/firewall/init.py | 39 -------------------- src/plugins/firewall/main.py | 16 --------- src/plugins/firewall/policy.py | 60 ------------------------------- src/plugins/firewall/shutdown.py | 35 ------------------ 5 files changed, 197 deletions(-) delete mode 100644 src/plugins/firewall/get-rules.py delete mode 100644 src/plugins/firewall/init.py delete mode 100644 src/plugins/firewall/main.py delete mode 100644 src/plugins/firewall/policy.py delete mode 100644 src/plugins/firewall/shutdown.py diff --git a/src/plugins/firewall/get-rules.py b/src/plugins/firewall/get-rules.py deleted file mode 100644 index 80b4795..0000000 --- a/src/plugins/firewall/get-rules.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- -# Author:Mine DOGAN - -import json - -from base.plugin.abstract_plugin import AbstractPlugin - - -class FirewallRules(AbstractPlugin): - def __init__(self, task, context): - super(FirewallRules, self).__init__() - self.task = task - self.context = context - self.logger = self.get_logger() - self.message_code = self.get_message_code() - - self.temp_file_name = str(self.generate_uuid()) - self.export_path = '{0}{1}'.format(str(self.Ahenk.received_dir_path()), self.temp_file_name) - - self.logger.debug('[FIREWALL] Parameters were initialized.') - - def handle_task(self): - try: - self.create_file(self.export_path) - self.logger.debug('[FIREWALL] Export rules to a temporary file...') - self.execute('/sbin/iptables-save > {}'.format(self.export_path)) - - self.logger.debug('[FIREWALL] Reading the file...') - with open(self.export_path, "r") as rules_file: - firewall_rules = rules_file.readlines() - - self.logger.info('[FIREWALL] Firewall task is handled successfully') - self.context.create_response(code=self.message_code.TASK_PROCESSED.value, - message='Güvenlik Duvarı kuralları başarıyla okundu.', - data=json.dumps({'firewallRules': firewall_rules}), - content_type=self.get_content_type().APPLICATION_JSON.value) - - except Exception as e: - self.logger.error('[FIREWALL] A problem occured while handling Firewall task: {0}'.format(str(e))) - self.context.create_response(code=self.message_code.TASK_ERROR.value, - message='Güvenlik Duvarı görevi çalıştırılırken bir hata oluştu.') - - -def handle_task(task, context): - get_rules = FirewallRules(task, context) - get_rules.handle_task() diff --git a/src/plugins/firewall/init.py b/src/plugins/firewall/init.py deleted file mode 100644 index d949df6..0000000 --- a/src/plugins/firewall/init.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- -import os -from base.plugin.abstract_plugin import AbstractPlugin - - -class Init(AbstractPlugin): - def __init__(self, context): - super(Init, self).__init__() - self.context = context - self.logger = self.get_logger() - self.plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__))) - self.initial_rules_file_path = self.plugin_path + '/iptables.rules' - self.logger.debug('[FIREWALL - init] Parameters were initialized.') - - def handle_mode(self): - - if self.is_installed('iptables-persistent') is False: - self.install_with_apt_get('iptables-persistent') - pass - - try: - if self.is_exist(self.initial_rules_file_path): - self.logger.debug('[FIREWALL - init] Adding initial rules temp file to iptables-restore as parameter...') - self.execute('/sbin/iptables-restore < {}'.format(self.initial_rules_file_path)) - - self.logger.debug('[FIREWALL - init] Save the rules...') - self.execute('service netfilter-persistent save') - - self.logger.debug('[FIREWALL - init] Restart the service...') - self.execute('service netfilter-persistent restart') - - except Exception as e: - self.logger.error('[FIREWALL - init] A problem occured while handling Firewall init.py: {0}'.format(str(e))) - - -def handle_mode(context): - init = Init(context) - init.handle_mode() diff --git a/src/plugins/firewall/main.py b/src/plugins/firewall/main.py deleted file mode 100644 index ab5a4ac..0000000 --- a/src/plugins/firewall/main.py +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- - - -def info(): - inf = dict() - inf['name'] = 'firewall' - inf['version'] = '1.0.0' - inf['support'] = 'debian' - inf['description'] = 'Firewall plugin provides to get firewall rules and changing them.' - inf['task'] = True - inf['user_oriented'] = False - inf['machine_oriented'] = True - inf['developer'] = 'mine.dogan@agem.com.tr' - - return inf \ No newline at end of file diff --git a/src/plugins/firewall/policy.py b/src/plugins/firewall/policy.py deleted file mode 100644 index dc30038..0000000 --- a/src/plugins/firewall/policy.py +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- -# Author:Mine DOGAN - -import json -import os -from base.plugin.abstract_plugin import AbstractPlugin - - -class FirewallRules(AbstractPlugin): - def __init__(self, profile_data, context): - super(FirewallRules, self).__init__() - self.profile_data = profile_data - self.context = context - self.logger = self.get_logger() - self.message_code = self.get_message_code() - - self.parameters = json.loads(self.profile_data) - self.plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__))) - self.rules = self.parameters['rules'] - self.temp_file_name = str(self.generate_uuid()) - self.file_path = '{0}{1}'.format(str(self.Ahenk.received_dir_path()), self.temp_file_name) - self.initial_rules_file_path = self.plugin_path + '/iptables.rules' - self.logger.debug('[FIREWALL] Parameters were initialized.') - - def handle_policy(self): - try: - if not self.is_exist(self.initial_rules_file_path): - self.logger.debug('[FIREWALL] Export initial rules to a temporary file...') - self.execute('/sbin/iptables-save > {}'.format(self.initial_rules_file_path)) - - self.logger.debug('[FIREWALL] Writing rules to temporary file...') - self.write_file(self.file_path, '{0}{1}'.format(self.rules, '\n')) - - self.logger.debug('[FIREWALL] Adding temp file to iptables-restore as parameter...') - result_code, p_out, p_err = self.execute('/sbin/iptables-restore < {}'.format(self.file_path)) - - if p_err != '': - raise Exception(p_err) - - self.logger.debug('[FIREWALL] Save the rules...') - self.execute('service netfilter-persistent save') - - self.logger.debug('[FIREWALL] Restart the service...') - self.execute('service netfilter-persistent restart') - - self.context.create_response(code=self.message_code.POLICY_PROCESSED.value, - message='Güvenlik Duvarı kuralları başarıyla kaydedildi.') - self.logger.info('[FIREWALL] Firewall policy is handled successfully') - - except Exception as e: - self.logger.error( - '[FIREWALL] A problem occured while handling Firewall policy: {0}'.format(str(e))) - self.context.create_response(code=self.message_code.POLICY_ERROR.value, - message='Güvenlik Duvarı profili uygulanırken bir hata oluştu: ' + str(e)) - - -def handle_policy(profile_data, context): - set_rules = FirewallRules(profile_data, context) - set_rules.handle_policy() diff --git a/src/plugins/firewall/shutdown.py b/src/plugins/firewall/shutdown.py deleted file mode 100644 index b697b27..0000000 --- a/src/plugins/firewall/shutdown.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/python3 -# -*- coding: utf-8 -*- -# Author:Mine DOGAN - -from base.plugin.abstract_plugin import AbstractPlugin -import os - -class Shutdown(AbstractPlugin): - def __init__(self, context): - super(Shutdown, self).__init__() - self.context = context - self.logger = self.get_logger() - self.plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__))) - self.initial_rules_file_path = self.plugin_path + '/iptables.rules' - self.logger.debug('[FIREWALL - shutdown] Parameters were initialized.') - - def handle_shutdown_mode(self): - try: - if self.is_exist(self.initial_rules_file_path): - self.logger.debug('[FIREWALL - shutdown] Adding initial rules temp file to iptables-restore as parameter...') - self.execute('/sbin/iptables-restore < {}'.format(self.initial_rules_file_path)) - - self.logger.debug('[FIREWALL - shutdown] Save the rules...') - self.execute('service netfilter-persistent save') - - self.logger.debug('[FIREWALL - shutdown] Restart the service...') - self.execute('service netfilter-persistent restart') - - except Exception as e: - self.logger.error('[FIREWALL - shutdown] A problem occured while handling Firewall shutdown.py: {0}'.format(str(e))) - - -def handle_mode(context): - shutdown = Shutdown(context) - shutdown.handle_shutdown_mode() \ No newline at end of file