deleted firewall task

This commit is contained in:
Tuncay ÇOLAK 2020-04-03 01:26:34 +03:00
parent 6f24eae185
commit f329f63ba7
5 changed files with 0 additions and 197 deletions

View file

@ -1,47 +0,0 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author:Mine DOGAN <mine.dogan@agem.com.tr>
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()

View file

@ -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()

View file

@ -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

View file

@ -1,60 +0,0 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author:Mine DOGAN <mine.dogan@agem.com.tr>
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()

View file

@ -1,35 +0,0 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author:Mine DOGAN <mine.dogan@agem.com.tr>
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()