running multiple policies is coded, deleting unassigned or deleted policies from agent db is coded

This commit is contained in:
hasan 2020-06-19 15:27:00 +03:00
parent 7471cc705a
commit 2875258147
7 changed files with 188 additions and 147 deletions

View file

@ -32,11 +32,12 @@ class AhenkDbService(object):
'parameter_map BLOB', 'deleted INTEGER', 'plugin TEXT', 'cron_expr TEXT',
'file_server TEXT'])
self.check_and_create_table('policy',
['id INTEGER PRIMARY KEY AUTOINCREMENT', 'type TEXT', 'version TEXT', 'name TEXT',
'execution_id TEXT','expiration_date TEXT'])
['id INTEGER PRIMARY KEY AUTOINCREMENT', 'policy_id INTEGER',
'type TEXT', 'version TEXT', 'name TEXT',
'execution_id TEXT', 'expiration_date TEXT', 'assign_date TEXT'])
self.check_and_create_table('profile', ['id INTEGER', 'create_date TEXT', 'label TEXT', 'description TEXT',
'overridable INTEGER', 'active TEXT', 'deleted TEXT',
'profile_data TEXT', 'modify_date TEXT', 'plugin TEXT'])
'profile_data TEXT', 'modify_date TEXT', 'plugin TEXT', 'policy_id INTEGER'])
self.check_and_create_table('plugin',
['id INTEGER PRIMARY KEY AUTOINCREMENT', 'active TEXT', 'create_date TEXT',
'deleted TEXT', 'description TEXT', 'machine_oriented TEXT', 'modify_date TEXT',

View file

@ -4,6 +4,7 @@
import json
import time
import datetime
from base.file.file_transfer_manager import FileTransferManager
from base.model.enum.content_type import ContentType
from base.model.enum.message_code import MessageCode
@ -173,7 +174,10 @@ class ExecutionManager(object):
def execute_default_policy(self, username):
self.logger.debug('Executing active policies for {0} user...'.format(username))
self.task_manager.addPolicy(self.get_active_policies(username))
policy_list = self.get_active_policies(username)
for i in range(len(policy_list)):
self.task_manager.addPolicy(policy_list[i])
def update_scheduled_task(self, arg):
self.logger.debug('Working on scheduled task ...')
@ -215,77 +219,48 @@ class ExecutionManager(object):
def execute_policy(self, arg):
try:
j = json.loads(str(arg))
for i in range(len(j['executePolicyList'])):
policy = self.json_to_PolicyBean(json.loads(json.dumps(j['executePolicyList'][i])))
self.logger.debug('Updating policies...')
policy = self.json_to_PolicyBean(json.loads(arg))
# policy is deleted or unassigned on server
# delete on ahenk db
if policy.get_is_deleted():
existing_policy_id = self.db_service.select('policy', ['id'],
'type = \'U\' and name = \'' + policy.get_username() + '\'' +
'and policy_id = ' + str(policy.get_policy_id()))
existing_profile_list = self.db_service.select('profile',
['id', 'plugin', 'policy_id'],
' id=' + str(existing_policy_id[0][0]))
for profile in existing_profile_list:
self.db_service.delete('plugin', 'id=' + str(profile[1]))
self.db_service.delete('profile', 'id=' + str(existing_policy_id[0][0]))
self.db_service.delete('policy', 'type = \'U\' and name = \'' + policy.get_username() + '\'' +
'and policy_id = ' + str(policy.get_policy_id()))
else:
self.policy_executed[policy.get_username()] = True
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\'')
user_policy_version = self.db_service.select_one_result('policy', 'version',
'type = \'U\' and name = \'' + policy.get_username() + '\'')
'type = \'U\' and name = \'' + policy.get_username() + '\'' +
'and policy_id = ' + str(policy.get_policy_id()))
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active',
'deleted', 'profile_data', 'plugin']
'deleted', 'profile_data', 'policy_id', 'plugin']
plugin_columns = ['active', 'create_date', 'deleted', 'description', 'machine_oriented', 'modify_date',
'name',
'policy_plugin', 'user_oriented', 'version', 'task_plugin', 'x_based']
if policy.get_ahenk_policy_version() != ahenk_policy_ver:
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
if ahenk_policy_id is not None:
self.db_service.delete('profile', 'id=' + str(ahenk_policy_id))
self.db_service.delete('plugin', 'id=' + str(ahenk_policy_id))
self.db_service.update('policy', ['version', 'execution_id', 'expiration_date'],
[str(policy.get_ahenk_policy_version()), policy.agent_execution_id,
str(policy.agent_expiration_date)], 'type=\'A\'')
else:
self.db_service.update('policy', ['type', 'version', 'name', 'execution_id', 'expiration_date'],
['A', str(policy.get_ahenk_policy_version()), machine_uid,
policy.get_agent_execution_id(), policy.agent_expiration_date])
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
for profile in policy.get_ahenk_profiles():
plugin = profile.get_plugin()
plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()),
str(plugin.get_description()), str(plugin.get_machine_oriented()),
str(plugin.get_modify_date()), str(plugin.get_name()),
str(plugin.get_policy_plugin()),
str(plugin.get_user_oriented()), str(plugin.get_version()),
str(plugin.get_task_plugin()), str(plugin.get_x_based())]
plugin_id = self.db_service.update('plugin', plugin_columns, plugin_args)
profile_args = [str(ahenk_policy_id), str(profile.get_create_date()),
str(profile.get_modify_date()),
str(profile.get_label()), str(profile.get_description()),
str(profile.get_overridable()), str(profile.get_active()),
str(profile.get_deleted()),
str(profile.get_profile_data()), plugin_id]
self.db_service.update('profile', profile_columns, profile_args)
elif ahenk_policy_ver:
self.logger.debug('Already there is ahenk policy. Command Execution Id is updating')
self.db_service.update('policy', ['execution_id'], [policy.get_agent_execution_id()], 'type = \'A\'')
else:
self.logger.debug('There is no any Ahenk policy.')
if policy.get_user_policy_version() != user_policy_version:
# if user_policy_version is null that means this policy is not added to db
# add this policy to db
if user_policy_version is None:
self.db_service.update('policy', ['policy_id', 'type', 'version', 'name', 'execution_id', 'expiration_date', 'assign_date'],
[policy.get_policy_id(), 'U', str(policy.get_user_policy_version()),
policy.get_username(), policy.user_execution_id,
str(policy.user_expiration_date), str(policy.get_assign_date())])
user_policy_id = self.db_service.select_one_result('policy', 'id',
'type = \'U\' and name=\'' + policy.get_username() + '\'')
if user_policy_id is not None:
# TODO remove profiles' plugins
self.db_service.delete('profile', 'id=' + str(user_policy_id))
self.db_service.delete('plugin', 'id=' + str(user_policy_id))
self.db_service.update('policy', ['version', 'execution_id', 'expiration_date'],
[str(policy.get_user_policy_version()), policy.user_execution_id,
str(policy.user_expiration_date)],
'type=\'U\' and name=\'' + policy.get_username() + '\'')
else:
self.db_service.update('policy', ['type', 'version', 'name', 'execution_id', 'expiration_date'],
['U', str(policy.get_user_policy_version()), policy.get_username(),
policy.get_user_execution_id(), policy.user_expiration_date])
user_policy_id = self.db_service.select_one_result('policy', 'id',
'type = \'U\' and name=\'' + policy.get_username() + '\'')
'type = \'U\' and name=\'' + policy.get_username() + '\''
+ 'and policy_id = ' + str(policy.get_policy_id()))
for profile in policy.get_user_profiles():
plugin = profile.get_plugin()
@ -301,20 +276,64 @@ class ExecutionManager(object):
str(profile.get_label()), str(profile.get_description()),
str(profile.get_overridable()), str(profile.get_active()),
str(profile.get_deleted()),
str(profile.get_profile_data()), plugin_id]
str(profile.get_profile_data()), policy.get_policy_id(), plugin_id]
self.db_service.update('profile', profile_columns, profile_args)
elif user_policy_version:
self.logger.debug('Already there is user policy. . Command Execution Id is updating')
self.db_service.update('policy', ['execution_id'], [policy.get_user_execution_id()], 'type = \'U\'')
else:
self.logger.debug('There is no any user policy')
elif policy.get_user_policy_version() != user_policy_version:
# policy is in db but policy version is updated
# delete profiles and plugins of that policcy and then insert new profiles and plugins
existing_profile_list = self.db_service.select('profile',
['id', 'plugin', 'policy_id'],
' policy_id=\'' + str(policy.get_policy_id()) + '\'')
self.db_service.update('policy', ['version', 'execution_id', 'expiration_date', 'assign_date'],
[str(policy.get_user_policy_version()), policy.user_execution_id,
str(policy.user_expiration_date), str(policy.assign_date)],
'type=\'U\' and name=\'' + policy.get_username() + '\'' +
' and policy_id=\'' + str(policy.get_policy_id()) + '\'')
user_policy_id = self.db_service.select_one_result('policy', 'id',
'type = \'U\' and name=\'' + policy.get_username() + '\'' +
' and policy_id=\'' + str(policy.get_policy_id()) + '\'')
policy = self.get_active_policies(policy.get_username())
# update all profiles
for profile in existing_profile_list:
self.db_service.delete('profile', 'id=' + str(profile[0]))
self.db_service.delete('plugin', 'id=' + str(profile[1]))
# add new profile and policies
for profile in policy.get_user_profiles():
plugin = profile.get_plugin()
plugin_args = [str(plugin.get_active()), str(plugin.get_create_date()), str(plugin.get_deleted()),
str(plugin.get_description()), str(plugin.get_machine_oriented()),
str(plugin.get_modify_date()), str(plugin.get_name()),
str(plugin.get_policy_plugin()),
str(plugin.get_user_oriented()), str(plugin.get_version()),
str(plugin.get_task_plugin()), str(plugin.get_x_based())]
plugin_id = self.db_service.update('plugin', plugin_columns, plugin_args)
profile_args = [str(user_policy_id), str(profile.get_create_date()), str(profile.get_modify_date()),
str(profile.get_label()), str(profile.get_description()),
str(profile.get_overridable()), str(profile.get_active()),
str(profile.get_deleted()),
str(profile.get_profile_data()), policy.get_policy_id(), plugin_id]
self.db_service.update('profile', profile_columns, profile_args)
else:
existing_policy = self.db_service.select('policy', ['id', 'assign_date'],
'type = \'U\' and name = \'' + policy.get_username() + '\'' +
'and policy_id = ' + str(policy.get_policy_id()))
self.db_service.update('policy', ['assign_date'], [str(policy.get_assign_date())]
, 'type = \'U\' and name = \'' + policy.get_username() + '\'' +
'and policy_id = ' + str(policy.get_policy_id()))
policy_list = self.get_active_policies(j['username'])
# TODO check is null
self.task_manager.addPolicy(policy)
for i in range(len(policy_list)):
self.task_manager.addPolicy(policy_list[i])
except Exception as e:
self.logger.error('A problem occurred while executing policy. Erroe Message: {0}:'.format(str(e)))
self.logger.error('A problem occurred while executing policy. Error Message: {0}:'.format(str(e)))
def check_expiration(self, expiration):
current_timestamp = int(time.time()) * 1000
@ -329,8 +348,8 @@ class ExecutionManager(object):
try:
# TODO vt den gecerli son tarihi olani cek
user_policy = self.db_service.select('policy', ['id', 'version', 'name', 'expiration_date'],
' type=\'U\' and name=\'' + username + '\'')
user_policy_list = self.db_service.select('policy', ['id', 'version', 'name', 'policy_id', 'expiration_date'],
' type=\'U\' and name=\'' + username + '\' order by assign_date asc')
ahenk_policy = self.db_service.select('policy', ['id', 'version', 'expiration_date'], ' type=\'A\' ')
plugin_columns = ['id', 'active', 'create_date', 'deleted', 'description', 'machine_oriented',
@ -339,10 +358,13 @@ class ExecutionManager(object):
profile_columns = ['id', 'create_date', 'label', 'description', 'overridable', 'active', 'deleted',
'profile_data', 'modify_date', 'plugin']
policy = PolicyBean(username=username)
if len(user_policy) > 0 and self.check_expiration(user_policy[0][3]):
user_policy_version = user_policy[0][0]
policy_list = []
if len(user_policy_list) > 0:
for i in range(len(user_policy_list)):
policy = PolicyBean(username=username)
user_policy_version = user_policy_list[i][0]
policy_id = user_policy_list[i][3]
policy.set_user_policy_version(user_policy_version)
user_profiles = self.db_service.select('profile', profile_columns,
@ -360,31 +382,10 @@ class ExecutionManager(object):
arr_profiles.append(
ProfileBean(profile[0], profile[1], profile[2], profile[3], profile[4], profile[5],
profile[6],
profile[7], profile[8], plugin, policy.get_username()))
profile[7], profile[8], user_policy_list[i][3], plugin, policy.get_username()))
policy.set_user_profiles(arr_profiles)
if len(ahenk_policy) > 0 and self.check_expiration(ahenk_policy[0][2]):
ahenk_policy_version = ahenk_policy[0][0]
policy.set_ahenk_policy_version(ahenk_policy_version)
ahenk_profiles = self.db_service.select('profile', profile_columns,
' id=' + str(ahenk_policy_version) + ' ')
arr_profiles = []
if len(ahenk_profiles) > 0:
for profile in ahenk_profiles:
plu = self.db_service.select('plugin', plugin_columns, ' id=\'' + profile[9] + '\'')[0]
plugin = PluginBean(p_id=plu[0], active=plu[1], create_date=plu[2], deleted=plu[3],
description=plu[4], machine_oriented=plu[5], modify_date=plu[6],
name=plu[7],
policy_plugin=plu[8], user_oriented=plu[9], version=plu[10],
task_plugin=plu[11], x_based=plu[12])
arr_profiles.append(
ProfileBean(profile[0], profile[1], profile[2], profile[3], profile[4], profile[5],
profile[6],
profile[7], profile[8], plugin, policy.get_username()))
policy.set_ahenk_profiles(arr_profiles)
return policy
policy_list.append(policy)
return policy_list
except Exception as e:
self.logger.error('A problem occurred while getting active policies. Error Message : {0}'.format(str(e)))
@ -494,6 +495,7 @@ class ExecutionManager(object):
username = json_data['username']
ahenk_prof_json_arr = json_data['agentPolicyProfiles']
user_prof_json_arr = json_data['userPolicyProfiles']
policy_id = json_data['policyID']
ahenk_prof_arr = []
user_prof_arr = []
@ -508,7 +510,7 @@ class ExecutionManager(object):
task_plugin=plu['taskPlugin'], x_based=plu['xBased'])
ahenk_prof_arr.append(
ProfileBean(prof['id'], prof['createDate'], prof['label'], prof['description'], prof['overridable'],
prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'],
prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'], policy_id,
plugin, username))
if user_prof_json_arr is not None:
@ -522,16 +524,19 @@ class ExecutionManager(object):
task_plugin=plu['taskPlugin'], x_based=plu['xBased'])
user_prof_arr.append(
ProfileBean(prof['id'], prof['createDate'], prof['label'], prof['description'], prof['overridable'],
prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'],
prof['active'], prof['deleted'], json.dumps(prof['profileData']), prof['modifyDate'], policy_id,
plugin, username))
return PolicyBean(ahenk_policy_version=json_data['agentPolicyVersion'],
return PolicyBean(policy_id=json_data['policyID'],
ahenk_policy_version=json_data['agentPolicyVersion'],
user_policy_version=json_data['userPolicyVersion'], ahenk_profiles=ahenk_prof_arr,
user_profiles=user_prof_arr, timestamp=json_data['timestamp'], username=json_data['username'],
agent_execution_id=json_data['agentCommandExecutionId'],
user_execution_id=json_data['userCommandExecutionId'],
agent_expiration_date=json_data['agentPolicyExpirationDate'],
user_expiration_date=json_data['userPolicyExpirationDate'])
user_expiration_date=json_data['userPolicyExpirationDate'],
is_deleted=json_data['isDeleted'],
assign_date=json_data['assignDate'])
def login_response(self, msg):
jData = json.loads(msg)

View file

@ -105,6 +105,15 @@ class Messaging(object):
'type = \'U\' and name = \'' + username + '\'')
machine_policy_number = self.db_service.select_one_result('policy', 'version', 'type = \'A\'')
user_policy_list = self.db_service.select('policy', ['id', 'version', 'name', 'policy_id', 'assign_date'],
' type=\'U\' and name=\'' + username + '\'')
# to add policy_id and policy_version
user_policy_hash_list = dict()
if len(user_policy_list) > 0:
for i in range(len(user_policy_list)):
user_policy_hash_list[str(user_policy_list[i][3])] = [user_policy_list[i][1], user_policy_list[i][4]]
data['policyList'] = user_policy_hash_list
data['userPolicyVersion'] = user_policy_number
data['agentPolicyVersion'] = machine_policy_number

View file

@ -116,7 +116,6 @@ class Messenger(ClientXMPP):
self.logger.info("---------->Received message: {}".format(str(parameter_map)))
else:
self.logger.info('---------->Received message: {0}'.format(str(msg['body'])))
self.event_manger.fireEvent(message_type, str(msg['body']))
self.logger.debug('Fired event is: {0}'.format(message_type))
except Exception as e:

View file

@ -6,9 +6,10 @@
class PolicyBean(object):
"""docstring for PolicyBean"""
def __init__(self, ahenk_policy_version=None, user_policy_version=None, ahenk_profiles=None, user_profiles=None,
def __init__(self, policy_id=None, ahenk_policy_version=None, user_policy_version=None, ahenk_profiles=None, user_profiles=None,
timestamp=None, username=None, agent_execution_id=None, user_execution_id=None,
agent_expiration_date=None, user_expiration_date=None):
agent_expiration_date=None, user_expiration_date=None, is_deleted=None, assign_date=None):
self.policy_id = policy_id
self.ahenk_policy_version = ahenk_policy_version
self.user_policy_version = user_policy_version
self.ahenk_profiles = ahenk_profiles
@ -19,6 +20,14 @@ class PolicyBean(object):
self.user_execution_id = user_execution_id
self.agent_expiration_date = agent_expiration_date
self.user_expiration_date = user_expiration_date
self.is_deleted = is_deleted
self.assign_date = assign_date
def get_policy_id(self):
return self.policy_id
def set_policy_id(self, policy_id):
self.policy_id = policy_id
def get_ahenk_policy_version(self):
return self.ahenk_policy_version
@ -67,3 +76,15 @@ class PolicyBean(object):
def get_user_execution_id(self):
return self.user_execution_id
def set_is_deleted(self, is_deleted):
self.is_deleted = is_deleted
def get_is_deleted(self):
return self.is_deleted
def set_assign_date(self, assign_date):
self.assign_date = assign_date
def get_assign_date(self):
return self.assign_date

View file

@ -8,8 +8,7 @@ from base.model.plugin_bean import PluginBean
class ProfileBean(object):
"""docstring for Profile"""
def __init__(self, p_id=None, create_date=None, label=None, description=None, overridable=None, active=None, deleted=None, profile_data=None, modify_date=None, plugin=None, username=None):
def __init__(self, p_id=None, create_date=None, label=None, description=None, overridable=None, active=None, deleted=None, profile_data=None, modify_date=None, policy_id=None, plugin=None, username=None):
self.id = p_id
self.create_date = create_date
self.modify_date = modify_date
@ -19,6 +18,7 @@ class ProfileBean(object):
self.active = active
self.deleted = deleted
self.profile_data = profile_data
self.policy_id = policy_id
self.plugin = plugin
self.username = username
@ -47,7 +47,7 @@ class ProfileBean(object):
self.label = label
def get_description(self):
return self.modify_date
return self.description
def set_description(self, description):
self.description = description
@ -76,6 +76,12 @@ class ProfileBean(object):
def set_profile_data(self, profile_data):
self.profile_data = profile_data
def get_policy_id(self):
return self.policy_id
def set_policy_id(self, policy_id):
self.policy_id = policy_id
def get_plugin(self):
return self.plugin

View file

@ -202,7 +202,7 @@ class PluginManager(object):
for usr_profile in user_profiles:
if usr_profile.plugin.name == agent_profile.plugin.name:
same_plugin_profile = usr_profile
policy.get_user_policy_version()
if same_plugin_profile is not None:
if agent_profile.overridable.lower() == 'true':
self.logger.debug(