2016-02-29 10:48:00 +02:00
|
|
|
#!/usr/bin/python3
|
2016-02-18 16:38:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
2016-04-04 18:15:18 +03:00
|
|
|
import subprocess
|
2016-04-12 17:30:53 +03:00
|
|
|
import threading
|
|
|
|
|
2016-03-01 18:43:26 +02:00
|
|
|
from base.Scope import Scope
|
2016-04-12 17:30:53 +03:00
|
|
|
from base.model.Response import Response
|
2016-04-20 18:05:05 +03:00
|
|
|
from base.model.enum.MessageType import MessageType
|
2016-03-30 18:51:50 +03:00
|
|
|
|
2016-02-18 16:38:30 +02:00
|
|
|
|
2016-03-30 17:12:38 +03:00
|
|
|
class Context(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.data = {}
|
|
|
|
|
2016-03-30 18:51:50 +03:00
|
|
|
def put(self, var_name, data):
|
2016-03-30 17:12:38 +03:00
|
|
|
self.data[var_name] = data
|
|
|
|
|
2016-03-30 18:51:50 +03:00
|
|
|
def get(self, var_name):
|
|
|
|
return self.data[var_name]
|
|
|
|
|
2016-03-30 17:12:38 +03:00
|
|
|
def empty_data(self):
|
|
|
|
self.data = {}
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-04-04 18:15:18 +03:00
|
|
|
def execute(self, command):
|
2016-04-08 18:43:06 +03:00
|
|
|
return subprocess.Popen(command, shell=True)
|
2016-04-04 18:15:18 +03:00
|
|
|
|
2016-04-16 23:59:54 +03:00
|
|
|
#TODO send file,...
|
2016-03-30 18:51:50 +03:00
|
|
|
|
2016-03-01 18:43:26 +02:00
|
|
|
class Plugin(threading.Thread):
|
2016-03-30 17:12:38 +03:00
|
|
|
"""
|
|
|
|
This is a thread inherit class and have a queue.
|
|
|
|
Plugin class responsible for processing TASK or USER PLUGIN PROFILE.
|
|
|
|
"""
|
2016-03-23 17:15:27 +02:00
|
|
|
|
|
|
|
def __init__(self, name, InQueue):
|
2016-03-23 10:04:31 +02:00
|
|
|
threading.Thread.__init__(self)
|
2016-02-18 16:38:30 +02:00
|
|
|
self.name = name
|
2016-03-01 18:43:26 +02:00
|
|
|
self.InQueue = InQueue
|
2016-03-30 18:51:50 +03:00
|
|
|
|
2016-03-23 10:04:31 +02:00
|
|
|
scope = Scope.getInstance()
|
|
|
|
self.logger = scope.getLogger()
|
2016-03-30 18:51:50 +03:00
|
|
|
self.response_queue = scope.getResponseQueue()
|
|
|
|
self.messaging = scope.getMessageManager()
|
2016-04-12 17:30:53 +03:00
|
|
|
self.db_service = scope.getDbService()
|
|
|
|
self.messager = None
|
2016-03-30 18:51:50 +03:00
|
|
|
|
2016-03-30 17:12:38 +03:00
|
|
|
self.keep_run = True
|
|
|
|
self.context = Context()
|
2016-03-01 18:43:26 +02:00
|
|
|
|
2016-03-23 10:04:31 +02:00
|
|
|
def run(self):
|
2016-03-30 18:51:50 +03:00
|
|
|
|
2016-03-30 17:12:38 +03:00
|
|
|
while self.keep_run:
|
2016-03-23 10:04:31 +02:00
|
|
|
try:
|
2016-03-29 11:52:18 +03:00
|
|
|
item_obj = self.InQueue.get(block=True)
|
|
|
|
obj_name = item_obj.obj_name
|
|
|
|
if obj_name == "TASK":
|
2016-04-11 19:32:05 +03:00
|
|
|
command = Scope.getInstance().getPluginManager().findCommand(self.getName(), item_obj.get_command_cls_id().lower())
|
2016-03-30 18:51:50 +03:00
|
|
|
command.handle_task(item_obj, self.context)
|
2016-03-30 17:12:38 +03:00
|
|
|
# TODO create response message from context and item_obj. item_obj is task
|
|
|
|
|
2016-04-12 17:30:53 +03:00
|
|
|
response = Response(type=MessageType.TASK_STATUS.value, id=item_obj.get_id(), code=self.context.get('responseCode'), message=self.context.get('responseMessage'), data=self.context.get('responseData'), content_type=self.context.get('contentType'))
|
|
|
|
# self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG
|
|
|
|
Scope.getInstance().getMessager().send_direct_message(self.messaging.task_status_msg(response)) # TODO REMOVE
|
2016-03-30 17:12:38 +03:00
|
|
|
|
|
|
|
# Empty context for next use
|
|
|
|
self.context.empty_data()
|
|
|
|
|
2016-03-29 11:52:18 +03:00
|
|
|
elif obj_name == "PROFILE":
|
2016-04-06 14:28:29 +03:00
|
|
|
profile_data = item_obj.get_profile_data()
|
|
|
|
policy_module = Scope.getInstance().getPluginManager().findPolicyModule(item_obj.get_plugin().get_name())
|
2016-04-04 18:15:18 +03:00
|
|
|
self.context.put('username', item_obj.get_username())
|
2016-03-30 18:51:50 +03:00
|
|
|
policy_module.handle_policy(profile_data, self.context)
|
2016-04-12 17:30:53 +03:00
|
|
|
|
|
|
|
execution_id = self.get_execution_id(item_obj.get_id())
|
|
|
|
policy_ver = self.get_policy_version(item_obj.get_id())
|
|
|
|
|
|
|
|
response = Response(type=MessageType.POLICY_STATUS.value, id=item_obj.get_id(), code=self.context.get('responseCode'), message=self.context.get('responseMessage'), data=self.context.get('responseData'), content_type=self.context.get('contentType'), execution_id=execution_id, policy_version=policy_ver)
|
|
|
|
# self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG
|
|
|
|
Scope.getInstance().getMessager().send_direct_message(self.messaging.policy_status_msg(response)) # TODO REMOVE
|
2016-03-30 17:12:38 +03:00
|
|
|
|
|
|
|
# Empty context for next use
|
|
|
|
self.context.empty_data()
|
|
|
|
|
|
|
|
elif obj_name == "KILL_SIGNAL":
|
|
|
|
self.keep_run = False
|
|
|
|
self.logger.debug('[Plugin] Killing queue ! Plugin Name : ' + str(self.name))
|
2016-04-20 16:30:29 +03:00
|
|
|
elif obj_name == "SAFE_MODE":
|
|
|
|
username = item_obj.username
|
|
|
|
safe_mode_module = Scope.getInstance().getPluginManager().find_safe_mode_module(self.name)
|
|
|
|
safe_mode_module.handle_safe_mode(username,self.context)
|
|
|
|
self.context.empty_data()
|
2016-03-29 11:52:18 +03:00
|
|
|
else:
|
2016-03-30 17:12:38 +03:00
|
|
|
self.logger.warning("[Plugin] Not supported object type " + obj_name)
|
2016-03-23 10:04:31 +02:00
|
|
|
except Exception as e:
|
2016-03-23 17:15:27 +02:00
|
|
|
# TODO error log here
|
2016-03-30 17:12:38 +03:00
|
|
|
self.logger.error("[Plugin] Plugin running exception " + str(e))
|
2016-02-18 16:38:30 +02:00
|
|
|
|
2016-04-12 17:30:53 +03:00
|
|
|
def get_execution_id(self, profile_id):
|
|
|
|
try:
|
|
|
|
return self.db_service.select_one_result('policy', 'execution_id', ' id={}'.format(profile_id))
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_policy_version(self, profile_id):
|
|
|
|
try:
|
|
|
|
return self.db_service.select_one_result('policy', 'version', ' id={}'.format(profile_id))
|
|
|
|
except Exception as e:
|
|
|
|
print(str(e))
|
|
|
|
return None
|
|
|
|
|
2016-02-18 16:38:30 +02:00
|
|
|
def getName(self):
|
|
|
|
return self.name
|