From 84f9d4c32d983201525bb78623170255cae6bf8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 30 Mar 2016 17:34:10 +0300 Subject: [PATCH] response model and minor bug fixes --- opt/ahenk/ahenkd.py | 3 +- opt/ahenk/base/execution/ExecutionManager.py | 10 +--- opt/ahenk/base/messaging/Messaging.py | 51 +++++++--------- opt/ahenk/base/model/MessageCode.py | 19 ++++++ opt/ahenk/base/model/MessageType.py | 16 ++--- opt/ahenk/base/model/Result.py | 61 ++++++++++++++++++++ opt/ahenk/base/task/TaskManager.py | 3 +- 7 files changed, 116 insertions(+), 47 deletions(-) create mode 100644 opt/ahenk/base/model/MessageCode.py create mode 100644 opt/ahenk/base/model/Result.py diff --git a/opt/ahenk/ahenkd.py b/opt/ahenk/ahenkd.py index de7e9f1..700d26b 100755 --- a/opt/ahenk/ahenkd.py +++ b/opt/ahenk/ahenkd.py @@ -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]): diff --git a/opt/ahenk/base/execution/ExecutionManager.py b/opt/ahenk/base/execution/ExecutionManager.py index a53f25a..88394fa 100644 --- a/opt/ahenk/base/execution/ExecutionManager.py +++ b/opt/ahenk/base/execution/ExecutionManager.py @@ -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 = [] diff --git a/opt/ahenk/base/messaging/Messaging.py b/opt/ahenk/base/messaging/Messaging.py index 099fd1d..7b70677 100644 --- a/opt/ahenk/base/messaging/Messaging.py +++ b/opt/ahenk/base/messaging/Messaging.py @@ -3,8 +3,6 @@ # Author: Volkan Şahin 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) diff --git a/opt/ahenk/base/model/MessageCode.py b/opt/ahenk/base/model/MessageCode.py new file mode 100644 index 0000000..2a2bbe1 --- /dev/null +++ b/opt/ahenk/base/model/MessageCode.py @@ -0,0 +1,19 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin +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' diff --git a/opt/ahenk/base/model/MessageType.py b/opt/ahenk/base/model/MessageType.py index 49e223e..2df5dfe 100644 --- a/opt/ahenk/base/model/MessageType.py +++ b/opt/ahenk/base/model/MessageType.py @@ -3,12 +3,12 @@ # Author: İsmail BAŞARAN 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' diff --git a/opt/ahenk/base/model/Result.py b/opt/ahenk/base/model/Result.py new file mode 100644 index 0000000..49f97c0 --- /dev/null +++ b/opt/ahenk/base/model/Result.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Author: Volkan Şahin + +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 diff --git a/opt/ahenk/base/task/TaskManager.py b/opt/ahenk/base/task/TaskManager.py index b79f457..2c5bcc0 100644 --- a/opt/ahenk/base/task/TaskManager.py +++ b/opt/ahenk/base/task/TaskManager.py @@ -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)