diff --git a/opt/ahenk/base/database/AhenkDbService.py b/opt/ahenk/base/database/AhenkDbService.py index 9cb3abc..1aea7ff 100644 --- a/opt/ahenk/base/database/AhenkDbService.py +++ b/opt/ahenk/base/database/AhenkDbService.py @@ -92,7 +92,6 @@ class AhenkDbService(object): sql += orderby self.cursor.execute(sql) - rows = self.cursor.fetchall() return rows except Exception as e: diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index 24a381b..cdb259b 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -11,6 +11,8 @@ import subprocess from base.Scope import Scope from base.model.Policy import Policy +from base.model.PolicyBean import PolicyBean +from base.model.ProfileBean import ProfileBean from base.model.Task import Task from base.model.MessageType import MessageType @@ -29,7 +31,7 @@ class ExecutionManager(object): self.logger = scope.getLogger() self.db_service = scope.getDbService() - #TODO DEBUG + # TODO DEBUG self.event_manager.register_event(str(MessageType.EXECUTE_SCRIPT), self.execute_script) self.event_manager.register_event(str(MessageType.REQUEST_FILE), self.request_file) self.event_manager.register_event(str(MessageType.MOVE_FILE), self.move_file) @@ -41,7 +43,7 @@ class ExecutionManager(object): policy = Policy(json.loads(arg)) # TODO get username and machine uid - username = 'lider' + username = policy.username machine_uid = self.db_service.select_one_result('registration', 'jid', 'registered=1') ahenk_policy_ver = self.db_service.select_one_result('policy', 'version', 'type = \'A\'') @@ -90,7 +92,52 @@ class ExecutionManager(object): # TODO check plugins print("but first need these plugins:" + str(missing_plugins)) - self.task_manager.addPolicy(policy) + self.task_manager.addPolicy(self.get_active_policies(username)) + + def get_active_policies(self, username): + + """ + self.ahenk_policy_version = ahenk_policy_version + self.user_policy_version = user_policy_version + self.ahenk_profiles = ahenk_profiles + self.user_profiles = user_profiles + self.timestamp = timestamp + self.username = username + + + self.ahenk_execution_id = ahenk_execution_id + self.user_execution_id = user_execution_id + """ + + user_policy = self.db_service.select('policy', ['id', 'version', 'name'], ' type=\'U\' and name=\'' + username + '\'') + ahenk_policy = self.db_service.select('policy', ['id', 'version'], ' type=\'A\' ') + + policy = PolicyBean(username=username) + + if len(user_policy) > 0: + user_policy_version = user_policy[0][0] + policy.set_user_policy_version(user_policy_version) + user_profiles = self.db_service.select('profile', ['id', 'create_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'modify_date', 'plugin'], ' id=' + str(user_policy_version) + ' ') + + arr_profiles = [] + if len(user_profiles) > 0: + for profile in user_profiles: + arr_profiles.append(ProfileBean(profile[0], profile[1], profile[2], profile[3], profile[4], profile[5], profile[6], profile[7], profile[8], profile[9])) + policy.set_user_profiles(arr_profiles) + + if len(ahenk_policy) > 0: + ahenk_policy_version = ahenk_policy[0][0] + policy.set_ahenk_policy_version(ahenk_policy_version) + ahenk_profiles = self.db_service.select('profile', ['id', 'create_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'modify_date', 'plugin'], ' id=' + str(ahenk_policy_version) + ' ') + arr_profiles = [] + if len(ahenk_profiles) > 0: + for profile in user_profiles: + arr_profiles.append(ProfileBean(profile[0], profile[1], profile[2], profile[3], profile[4], profile[5], profile[6], profile[7], profile[8], profile[9])) + policy.set_ahenk_profiles(arr_profiles) + + print("") + return policy + def get_installed_plugins(self): plugins = self.db_service.select('plugin', ['name', 'version']) diff --git a/opt/ahenk/base/model/Policy.py b/opt/ahenk/base/model/Policy.py index d3dd247..c3eb093 100644 --- a/opt/ahenk/base/model/Policy.py +++ b/opt/ahenk/base/model/Policy.py @@ -30,7 +30,7 @@ class Policy(object): @property def timestamp(self): - self.request['timestamp'] + return self.policy['timestamp'] @property def user_profiles(self): @@ -42,6 +42,11 @@ class Policy(object): except Exception as e: return None + @property + def username(self): + return self.policy['username'] + + # TODO result mesajı dönerken döndür @property def ahenk_execution_id(self): return self.policy['agentCommandExecutionId'] @@ -50,7 +55,6 @@ class Policy(object): def user_execution_id(self): return self.policy['userCommandExecutionId'] - def to_string(self): return str(self.policy) diff --git a/opt/ahenk/base/model/PolicyBean.py b/opt/ahenk/base/model/PolicyBean.py new file mode 100644 index 0000000..44e8194 --- /dev/null +++ b/opt/ahenk/base/model/PolicyBean.py @@ -0,0 +1,63 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin + + +class PolicyBean(object): + """docstring for PolicyBean""" + + def __init__(self, ahenk_policy_version=None, user_policy_version=None, ahenk_profiles=None, user_profiles=None, timestamp=None, username=None, ahenk_execution_id=None, user_execution_id=None): + + self.ahenk_policy_version = ahenk_policy_version + self.user_policy_version = user_policy_version + self.ahenk_profiles = ahenk_profiles + self.user_profiles = user_profiles + self.timestamp = timestamp + self.username = username + self.ahenk_execution_id = ahenk_execution_id + self.user_execution_id = user_execution_id + + def get_ahenk_policy_version(self): + return self.ahenk_policy_version + + def set_ahenk_policy_version(self, ahenk_policy_version): + self.ahenk_policy_version = ahenk_policy_version + + def get_user_policy_version(self): + return self.user_policy_version + + def set_user_policy_version(self, user_policy_version): + self.user_policy_version = user_policy_version + + def get_ahenk_profiles(self): + return self.ahenk_profiles + + def set_ahenk_profiles(self, ahenk_profiles): + self.ahenk_profiles = ahenk_profiles + + def get_user_profiles(self): + return self.user_profiles + + def set_user_profiles(self, user_profiles): + self.user_profiles = user_profiles + + def get_timestamp(self): + return self.timestamp + + def set_timestamp(self, timestamp): + self.timestamp = timestamp + + def get_username(self): + return self.username + + def set_username(self, username): + self.username = username + + def get_ahenk_execution_id(self): + return self.ahenk_execution_id + + def set_ahenk_execution_id(self, ahenk_execution_id): + self.ahenk_execution_id = ahenk_execution_id + + def set_user_execution_id(self, user_execution_id): + self.user_execution_id = user_execution_id diff --git a/opt/ahenk/base/model/Profile.py b/opt/ahenk/base/model/Profile.py index f2b4b58..e492f68 100644 --- a/opt/ahenk/base/model/Profile.py +++ b/opt/ahenk/base/model/Profile.py @@ -11,6 +11,7 @@ class Profile(object): def __init__(self, message): self.profile = message + self.username = None @property def id(self): @@ -61,3 +62,9 @@ class Profile(object): @property def obj_name(self): return "PROFILE" + + def get_username(self): + return self.username + + def set_username(self, username): + self.username = username diff --git a/opt/ahenk/base/model/ProfileBean.py b/opt/ahenk/base/model/ProfileBean.py new file mode 100644 index 0000000..d0cacd8 --- /dev/null +++ b/opt/ahenk/base/model/ProfileBean.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin +import json + +from base.model.Plugin import Plugin + + +class ProfileBean(object): + """docstring for Profile""" + + def __init__(self, p_id, create_date, label, description, overridable, active, deleted, profile_data, modify_date, plugin, username=None): + self.id = p_id + self.create_date = create_date + self.modify_date = modify_date + self.label = label + self.description = description + self.overridable = overridable + self.active = active + self.deleted = deleted + self.profile_data = profile_data + self.plugin = Plugin(plugin) + self.username = username + + def get_id(self): + return self.id + + def set_id(self, p_id): + self.id = p_id + + def get_create_date(self): + return self.create_date + + def set_create_date(self, create_date): + self.create_date = create_date + + def get_modify_dateself(self): + return self.modify_date + + def set_modify_date(self, modify_date): + self.modify_date = modify_date + + def get_label(self): + return self.label + + def set_label(self, label): + self.label = label + + def get_description(self): + return self.modify_date + + def set_description(self, description): + self.description = description + + def get_overridable(self): + return self.overridable + + def set_overridable(self, overridable): + self.overridable = overridable + + def get_active(self): + return self.active + + def set_active(self, active): + self.active = active + + def get_deleted(self): + return self.deleted + + def set_deleted(self, deleted): + self.deleted = deleted + + def get_profile_data(self): + return self.profile_data + + def set_profile_data(self, profile_data): + self.profile_data = profile_data + + def get_plugin(self): + return self.plugin + + def set_plugin(self, plugin): + self.plugin = Plugin(plugin) + + def get_username(self): + return self.username + + def set_username(self, username): + self.username = username \ No newline at end of file diff --git a/opt/ahenk/base/plugin/Plugin.py b/opt/ahenk/base/plugin/Plugin.py index c34b8ef..2d3185a 100644 --- a/opt/ahenk/base/plugin/Plugin.py +++ b/opt/ahenk/base/plugin/Plugin.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN import threading - +import subprocess from base.Scope import Scope from base.model.Response import Response from base.model.MessageType import MessageType @@ -23,6 +23,9 @@ class Context(object): def empty_data(self): self.data = {} + def execute(self, command): + return subprocess.call(command, shell=True) + class Plugin(threading.Thread): """ @@ -70,10 +73,12 @@ class Plugin(threading.Thread): profile_data = item_obj.profile_data policy_module = Scope.getInstance().getPluginManager().findPolicyModule(plugin_name) + self.context.put('username', item_obj.get_username()) + policy_module.handle_policy(profile_data, self.context) #TODO Message Code keep - response = Response(type=MessageType.POLICY_STATUS, id=item_obj.id, code=MessageCode.POLICY_PROCESSED, message='__message__', data=self.context.get('data'), content_type=self.context.get('content_type'), execution_id='get_execution_id') + response = Response(type=self.context.get('message_type'), id=item_obj.id, code=self.context.get('message_code'), message=self.context.get('message'), data=self.context.get('data'), content_type=self.context.get('content_type'), execution_id='get_execution_id') #self.response_queue.put(self.messaging.response_msg(response)) #TODO DEBUG Scope.getInstance().getMessager().send_direct_message(self.messaging.response_msg(response))#TODO REMOVE diff --git a/opt/ahenk/base/plugin/PluginManager.py b/opt/ahenk/base/plugin/PluginManager.py index 4c01d2a..4f5f41e 100644 --- a/opt/ahenk/base/plugin/PluginManager.py +++ b/opt/ahenk/base/plugin/PluginManager.py @@ -82,7 +82,7 @@ class PluginManager(object): except Exception as e: self.logger.error('[PluginManager] Exception occurred when reloading plugins ' + str(e)) - def findPolicyModule(self,plugin_name): + def findPolicyModule(self, plugin_name): location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), plugin_name) if os.path.isdir(location) and "policy.py" in os.listdir(location): info = imp.find_module("policy", [location]) @@ -92,17 +92,30 @@ class PluginManager(object): return None def processPolicy(self, policy): - #TODO user and ahenk - user_profiles = policy.ahenk_profiles - for profile in user_profiles: - try: - plugin = profile.plugin - plugin_name = plugin.name - if plugin_name in self.pluginQueueDict: - self.pluginQueueDict[plugin_name].put(profile, 1) - except Exception as e: - print("Exception occured..") - self.logger.error("Policy profile not processed " + str(profile.plugin.name)) + #TODO do you need username in profile? + + username = policy.username + ahenk_profiles = policy.ahenk_profiles + user_profiles = policy.user_profiles + + if ahenk_profiles is not None: + for profile in ahenk_profiles: + self.process_profile(profile) + + if user_profiles is not None: + for profile in user_profiles: + profile.set_username(username) + self.process_profile(profile) + + def process_profile(self, profile): + try: + plugin = profile.get_plugin() + plugin_name = plugin.name + if plugin_name in self.pluginQueueDict: + self.pluginQueueDict[plugin_name].put(profile, 1) + except Exception as e: + print("Exception occured..") + self.logger.error("Policy profile not processed " + str(profile.plugin.name)) def checkPluginExists(self, plugin_name, version=None): @@ -127,3 +140,4 @@ class PluginManager(object): def printQueueSize(self): print("size " + str(len(self.pluginQueueDict))) +