2016-03-08 18:05:00 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
2016-03-11 17:36:11 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import stat
|
2016-03-11 17:36:11 +02:00
|
|
|
import subprocess
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-03-08 18:05:00 +02:00
|
|
|
from base.Scope import Scope
|
2016-03-17 17:55:27 +02:00
|
|
|
from base.model.Policy import Policy
|
2016-04-04 18:15:18 +03:00
|
|
|
from base.model.PolicyBean import PolicyBean
|
|
|
|
from base.model.ProfileBean import ProfileBean
|
2016-03-23 17:15:27 +02:00
|
|
|
from base.model.Task import Task
|
2016-03-30 10:51:32 +03:00
|
|
|
from base.model.MessageType import MessageType
|
2016-03-23 17:15:27 +02:00
|
|
|
|
2016-03-08 18:05:00 +02:00
|
|
|
|
|
|
|
class ExecutionManager(object):
|
2016-03-11 17:36:11 +02:00
|
|
|
"""docstring for FileTransferManager"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(ExecutionManager, self).__init__()
|
|
|
|
|
|
|
|
scope = Scope.getInstance()
|
|
|
|
self.config_manager = scope.getConfigurationManager()
|
|
|
|
self.event_manager = scope.getEventManager()
|
2016-03-14 17:16:26 +02:00
|
|
|
self.task_manager = scope.getTaskManager()
|
2016-03-18 18:42:36 +02:00
|
|
|
self.messager = scope.getMessager()
|
2016-03-23 17:15:27 +02:00
|
|
|
self.logger = scope.getLogger()
|
|
|
|
self.db_service = scope.getDbService()
|
2016-03-11 17:36:11 +02:00
|
|
|
|
2016-04-04 18:15:18 +03:00
|
|
|
# TODO DEBUG
|
2016-03-30 18:51:50 +03:00
|
|
|
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)
|
|
|
|
self.event_manager.register_event(str(MessageType.EXECUTE_TASK), self.execute_task)
|
|
|
|
self.event_manager.register_event('EXECUTE_POLICY', self.execute_policy)
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-03-29 16:11:39 +03:00
|
|
|
def execute_policy(self, arg):
|
2016-03-23 17:15:27 +02:00
|
|
|
self.logger.debug('[ExecutionManager] Updating policies...')
|
2016-03-17 17:55:27 +02:00
|
|
|
|
|
|
|
policy = Policy(json.loads(arg))
|
2016-03-29 16:11:39 +03:00
|
|
|
# TODO get username and machine uid
|
2016-04-04 18:15:18 +03:00
|
|
|
username = policy.username
|
2016-03-31 18:21:24 +03:00
|
|
|
machine_uid = self.db_service.select_one_result('registration', 'jid', 'registered=1')
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-03-23 16:41:54 +02:00
|
|
|
ahenk_policy_ver = self.db_service.select_one_result('policy', 'version', 'type = \'A\'')
|
2016-03-23 17:15:27 +02:00
|
|
|
user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + username + '\'')
|
2016-03-23 16:41:54 +02:00
|
|
|
installed_plugins = self.get_installed_plugins()
|
|
|
|
missing_plugins = []
|
2016-03-29 16:11:39 +03:00
|
|
|
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-03-23 16:41:54 +02:00
|
|
|
if policy.ahenk_policy_version != ahenk_policy_ver:
|
|
|
|
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
|
2016-03-29 16:11:39 +03:00
|
|
|
if ahenk_policy_id is not None:
|
|
|
|
self.db_service.delete('profile', 'id=' + str(ahenk_policy_id))
|
|
|
|
self.db_service.update('policy', ['version'], [str(policy.ahenk_policy_version)], 'type=\'A\'')
|
|
|
|
else:
|
|
|
|
self.db_service.update('policy', ['type', 'version', 'name'], ['A', str(policy.ahenk_policy_version), machine_uid])
|
|
|
|
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
|
2016-03-30 17:34:10 +03:00
|
|
|
|
2016-03-17 17:55:27 +02:00
|
|
|
for profile in policy.ahenk_profiles:
|
2016-03-23 16:41:54 +02:00
|
|
|
args = [str(ahenk_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label),
|
2016-03-23 17:15:27 +02:00
|
|
|
str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)]
|
2016-03-23 16:41:54 +02:00
|
|
|
self.db_service.update('profile', profile_columns, args)
|
2016-03-17 17:55:27 +02:00
|
|
|
if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins:
|
|
|
|
missing_plugins.append(profile.plugin.name)
|
|
|
|
|
|
|
|
else:
|
2016-03-23 17:15:27 +02:00
|
|
|
self.logger.debug('[ExecutionManager] Already there is ahenk policy')
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-03-23 16:41:54 +02:00
|
|
|
if policy.user_policy_version != user_policy_version:
|
2016-03-23 17:15:27 +02:00
|
|
|
user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + username + '\'')
|
2016-03-29 16:11:39 +03:00
|
|
|
if user_policy_id is not None:
|
|
|
|
self.db_service.delete('profile', 'id=' + str(user_policy_id))
|
|
|
|
self.db_service.update('policy', ['version'], [str(policy.user_policy_version)], 'type=\'U\' and name=\'' + username + '\'')
|
|
|
|
else:
|
|
|
|
self.db_service.update('policy', ['type', 'version', 'name'], ['U', str(policy.user_policy_version), username])
|
|
|
|
user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\'' + username + '\'')
|
|
|
|
|
2016-03-17 17:55:27 +02:00
|
|
|
for profile in policy.user_profiles:
|
2016-03-29 16:11:39 +03:00
|
|
|
args = [int(user_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label),
|
|
|
|
str(profile.description), int(profile.overridable), int(profile.active), int(profile.deleted), str(profile.profile_data), str(profile.plugin.to_string())]
|
2016-03-23 17:15:27 +02:00
|
|
|
self.db_service.update('profile', profile_columns, args)
|
2016-03-17 17:55:27 +02:00
|
|
|
if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins:
|
|
|
|
missing_plugins.append(profile.plugin.name)
|
|
|
|
else:
|
2016-03-23 17:15:27 +02:00
|
|
|
self.logger.debug('[ExecutionManager] Already there is user policy')
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
# TODO check plugins
|
|
|
|
print("but first need these plugins:" + str(missing_plugins))
|
2016-03-17 17:55:27 +02:00
|
|
|
|
2016-04-04 18:15:18 +03:00
|
|
|
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
|
|
|
|
|
2016-03-29 11:09:22 +03:00
|
|
|
|
2016-03-17 17:55:27 +02:00
|
|
|
def get_installed_plugins(self):
|
2016-03-23 17:15:27 +02:00
|
|
|
plugins = self.db_service.select('plugin', ['name', 'version'])
|
|
|
|
p_list = []
|
2016-03-17 17:55:27 +02:00
|
|
|
for p in plugins:
|
2016-03-23 17:15:27 +02:00
|
|
|
p_list.append(str(p[0]) + '-' + str(p[1]))
|
2016-03-17 17:55:27 +02:00
|
|
|
return p_list
|
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def execute_task(self, arg):
|
2016-03-14 17:42:17 +02:00
|
|
|
self.logger.debug('[ExecutionManager] Adding new task...')
|
2016-03-21 12:02:15 +02:00
|
|
|
task = Task(json.loads(arg))
|
2016-03-14 17:16:26 +02:00
|
|
|
self.task_manager.addTask(task)
|
2016-03-21 12:02:15 +02:00
|
|
|
self.logger.debug('[ExecutionManager] Task added')
|
2016-03-11 17:36:11 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def move_file(self, arg):
|
|
|
|
default_file_path = self.config_manager.get('CONNECTION', 'receiveFileParam')
|
2016-03-11 17:36:11 +02:00
|
|
|
j = json.loads(arg)
|
2016-03-23 17:15:27 +02:00
|
|
|
# msg_id =str(j['id']).lower()
|
2016-03-25 17:56:15 +02:00
|
|
|
target_file_path = str(j['filePath']).lower()
|
2016-03-23 17:15:27 +02:00
|
|
|
file_name = str(j['filename']).lower()
|
|
|
|
self.logger.debug('[ExecutionManager] ' + file_name + ' will be moved to ' + target_file_path)
|
|
|
|
shutil.move(default_file_path + file_name, target_file_path + file_name)
|
2016-03-11 17:36:11 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def execute_script(self, arg):
|
2016-03-11 17:36:11 +02:00
|
|
|
j = json.loads(arg)
|
2016-03-23 17:15:27 +02:00
|
|
|
# msg_id =str(j['id']).lower()
|
2016-03-25 17:56:15 +02:00
|
|
|
file_path = str(j['filePath']).lower()
|
2016-03-23 17:15:27 +02:00
|
|
|
time_stamp = str(j['timestamp']).lower()
|
2016-03-11 17:36:11 +02:00
|
|
|
self.logger.debug('[ExecutionManager] Making executable file (%s) for execution' % file_path)
|
|
|
|
st = os.stat(file_path)
|
|
|
|
os.chmod(file_path, st.st_mode | stat.S_IEXEC)
|
2016-03-23 17:15:27 +02:00
|
|
|
subprocess.call("/bin/sh " + file_path, shell=True)
|
2016-03-11 17:36:11 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
# need to move somewhere else
|
|
|
|
def request_file(self, arg):
|
2016-03-11 17:36:11 +02:00
|
|
|
j = json.loads(arg)
|
2016-03-23 17:15:27 +02:00
|
|
|
# msg_id =str(j['id']).lower()
|
2016-03-25 17:56:15 +02:00
|
|
|
file_path = str(j['filePath']).lower()
|
2016-03-23 17:15:27 +02:00
|
|
|
time_stamp = str(j['timestamp']).lower()
|
|
|
|
self.logger.debug('[ExecutionManager] Requested file is ' + file_path)
|
2016-03-18 18:42:36 +02:00
|
|
|
self.messager.send_file(file_path)
|
2016-03-11 17:36:11 +02:00
|
|
|
|
2016-03-23 17:15:27 +02:00
|
|
|
def get_md5_file(self, fname):
|
2016-03-11 17:36:11 +02:00
|
|
|
self.logger.debug('[ExecutionManager] md5 hashing')
|
|
|
|
hash_md5 = hashlib.md5()
|
2016-03-23 17:15:27 +02:00
|
|
|
with open(fname, 'rb') as f:
|
2016-03-11 17:36:11 +02:00
|
|
|
for chunk in iter(lambda: f.read(4096), b""):
|
|
|
|
hash_md5.update(chunk)
|
|
|
|
return str(hash_md5.hexdigest())
|