response model and minor bug fixes

This commit is contained in:
Volkan Şahin 2016-03-30 17:34:10 +03:00
parent cff628e5df
commit 84f9d4c32d
7 changed files with 116 additions and 47 deletions

View file

@ -134,6 +134,7 @@ class AhenkDeamon(BaseDaemon):
messageResponseQueue.start()
globalscope.setResponseQueue(responseQueue)
while True:
time.sleep(1)
@ -151,7 +152,7 @@ class AhenkDeamon(BaseDaemon):
logger = scope.getLogger()
if 'login' == str(params[0]):
message = scope.getMessageManager().policies_msg(params[1])
message = scope.getMessageManager().policy_request_msg(params[1])
scope.getMessager().send_direct_message(message)
logger.debug('[AhenkDeamon] login event is handled for user:' + params[1])
elif 'logout' == str(params[0]):

View file

@ -35,15 +35,13 @@ class ExecutionManager(object):
self.event_manager.register_event('EXECUTE_TASK', self.execute_task)
self.event_manager.register_event('EXECUTE_POLICY', self.execute_policy)
def execute_policy(self, arg):
self.logger.debug('[ExecutionManager] Updating policies...')
policy = Policy(json.loads(arg))
# TODO get username and machine uid
username = '_username'
machine_uid='_machine_uid'
username = 'volkan'
machine_uid='616161616161'
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 = \'' + username + '\'')
@ -59,6 +57,7 @@ class ExecutionManager(object):
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\'')
for profile in policy.ahenk_profiles:
args = [str(ahenk_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label),
str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)]
@ -90,11 +89,8 @@ class ExecutionManager(object):
# TODO check plugins
print("but first need these plugins:" + str(missing_plugins))
print("Executing policy")
self.task_manager.addPolicy(policy)
def get_installed_plugins(self):
plugins = self.db_service.select('plugin', ['name', 'version'])
p_list = []

View file

@ -3,8 +3,6 @@
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import datetime
import json
import os
import pwd
import sys
sys.path.append('../..')
@ -18,34 +16,23 @@ class Messaging(object):
self.logger = scope.getLogger()
self.conf_manager = scope.getConfigurationManager()
self.db_service = scope.getDbService()
self.event_manger = scope.getEventManager()
# TODO can use sh commands or api for getting username and timestamp
def policy_request_msg(self):
# TODO volkan
self.logger.debug('[Messaging] Creating policy request message')
ahenk_version = self.db_service.select('policy', ['version'], 'type = \'A\'')
username = 'volkan'
user_version = self.db_service.select('policy', ['version'], 'type = \'U\' and name = \'' + username + '\'')
if len(ahenk_version) == 0:
ahenk_version.append(-1)
if len(user_version) == 0:
user_version.append(-1)
def response_msg(self, response):
print("response message")
data = {}
data['type'] = 'POLICY_REQUEST'
data['username'] = username
data['ahenkPolicyVersion'] = str(''.join(ahenk_version[0]))
data['userPolicyVersion'] = str(''.join(user_version[0]))
data['type'] = response.get_type()
data['id'] = response.get_id()
data['responseCode'] = response.get_code()
data['responseMessage'] = response.get_message()
data['responseData'] = response.get_data()
data['contentType'] = response.get_content_type()
data['timestamp'] = response.get_timestamp()
json_data = json.dumps(data)
self.logger.debug('[Messaging] Policy request message was created')
print(json_data)
return json_data
self.logger.debug('[Messaging] Response message was created')
return str(json_data)
def login_msg(self, username):
data = {}
@ -65,12 +52,16 @@ class Messaging(object):
self.logger.debug('[Messaging] Logout message was created')
return json_data
def policies_msg(self, username):
def policy_request_msg(self, username):
data = {}
data['type'] = 'GET_POLICIES'
#TODO fetch db values
data['userPolicyVersion'] = '1'
data['machinePolicyVersion'] = '1'
user_policy_number = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \'' + username + '\'')
machine_policy_number = self.db_service.select_one_result('policy', 'version', 'type = \'A\'')
data['userPolicyVersion'] = user_policy_number
data['machinePolicyVersion'] = machine_policy_number
data['username'] = str(username)
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)

View file

@ -0,0 +1,19 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
from enum import Enum
class MessageCode(Enum):
TASK_RECEIVED = 'TASK_RECEIVED'
TASK_PROCESSED = 'TASK_PROCESSED'
TASK_ERROR = 'TASK_ERROR'
TASK_WARNING = 'TASK_WARNING'
TASK_TIMEOUT = 'TASK_TIMEOUT'
TASK_KILLED = 'TASK_KILLED'
POLICY_RECEIVED = 'POLICY_RECEIVED'
POLICY_PROCESSED = 'POLICY_PROCESSED'
POLICY_ERROR = 'POLICY_ERROR'
POLICY_WARNING = 'POLICY_WARNING'
POLICY_TIMEOUT = 'POLICY_TIMEOUT'
POLICY_KILLED = 'POLICY_KILLED'

View file

@ -3,12 +3,12 @@
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
from enum import Enum
class MessageType(Enum):
TASK_RECEIVED = 'TASK_RECEIVED'
TASK_PROCESSING = 'TASK_PROCESSING'
TASK_PROCESSED = 'TASK_PROCESSED'
TASK_ERROR = 'TASK_ERROR'
TASK_WARNING = 'TASK_WARNING'
POLICY_RECEIVED = 'POLICY_RECEIVED'
POLICY_PROCESSED = 'POLICY_PROCESSED'
TASK_STATUS = 'TASK_STATUS'
REGISTER = 'REGISTER'
UNREGISTER = 'UNREGISTER'
REGISTER_LDAP = 'REGISTER_LDAP'
GET_POLICIES = 'GET_POLICIES'
LOGIN = 'LOGIN'
LOGOUT = 'LOGOUT'
POLICY_STATUS = 'POLICY_STATUS'

View file

@ -0,0 +1,61 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import datetime
class Result(object):
"""docstring for Plugin"""
def __init__(self, type, id, code=None, message=None, context=None):
self.type = type
self.id = id
self.code = code
self.message = message
self.context = context.data
self.content_type = context.content_type
self.timestamp = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
def get_type(self):
return str(self.type)
def set_type(self, type):
self.type = type
def get_id(self):
return self.id
def set_id(self, id):
self.id = id
def get_code(self):
return str(self.code)
def set_code(self, code):
self.code = code
def get_message(self):
return self.id
def set_message(self, message):
self.message = message
def get_data(self):
return self.context.data
def set_data(self, data):
self.context.data = data
def get_content_type(self):
return self.context.content_type
def set_content_type(self, content_type):
self.context.content_type = content_type
def get_timestamp(self):
return self.timestamp
class Context():
def __init__(self, data=None, content_type=None):
self.data = type
self.content_type = id

View file

@ -23,12 +23,13 @@ class TaskManager(object):
self.logger.info('Task saved ')
# TODO send task received message
self.pluginManager.processTask(task)
except Exception as e:
# TODO error log here
self.logger.debug('Exception occured when adding task ' + str(e))
pass
def addPolicy(self,policy):
def addPolicy(self, policy):
try:
print("adding policy")
self.pluginManager.processPolicy(policy)