get active policies from db and process them

This commit is contained in:
Volkan Şahin 2016-04-04 18:15:18 +03:00
parent 550cad1804
commit 75d818b6a1
8 changed files with 248 additions and 20 deletions

View file

@ -92,7 +92,6 @@ class AhenkDbService(object):
sql += orderby
self.cursor.execute(sql)
rows = self.cursor.fetchall()
return rows
except Exception as e:

View file

@ -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'])

View file

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

View file

@ -0,0 +1,63 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
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

View file

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

View file

@ -0,0 +1,89 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
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

View file

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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

View file

@ -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,11 +92,24 @@ class PluginManager(object):
return None
def processPolicy(self, policy):
#TODO user and ahenk
user_profiles = policy.ahenk_profiles
#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.plugin
plugin = profile.get_plugin()
plugin_name = plugin.name
if plugin_name in self.pluginQueueDict:
self.pluginQueueDict[plugin_name].put(profile, 1)
@ -127,3 +140,4 @@ class PluginManager(object):
def printQueueSize(self):
print("size " + str(len(self.pluginQueueDict)))