Ahenk/opt/ahenk/base/messaging/messaging.py

180 lines
7.2 KiB
Python
Raw Normal View History

2016-02-16 17:50:37 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
import datetime
import json
2016-02-16 17:50:37 +02:00
2016-06-27 17:36:12 +03:00
from base.system.system import System
from base.scope import Scope
2016-06-27 17:14:11 +03:00
from base.util.util import Util
2016-02-16 17:50:37 +02:00
# TODO Message Factory
2016-03-11 17:39:32 +02:00
class Messaging(object):
2016-02-21 03:24:54 +02:00
def __init__(self):
scope = Scope().getInstance()
self.logger = scope.getLogger()
2016-03-11 17:39:32 +02:00
self.conf_manager = scope.getConfigurationManager()
self.db_service = scope.getDbService()
2016-03-11 17:39:32 +02:00
self.event_manger = scope.getEventManager()
2016-05-06 16:42:21 +03:00
def missing_plugin_message(self, plugin):
data = dict()
2016-05-06 16:42:21 +03:00
data['type'] = 'MISSING_PLUGIN'
data['pluginName'] = plugin.get_name()
data['pluginVersion'] = plugin.get_version()
json_data = json.dumps(data)
self.logger.debug('[Messaging]Missing plugin message was created')
return str(json_data)
2016-04-03 00:58:33 +03:00
def task_status_msg(self, response):
data = dict()
2016-04-03 00:58:33 +03:00
data['type'] = response.get_type()
data['taskId'] = response.get_id()
data['responseCode'] = response.get_code()
data['responseMessage'] = response.get_message()
2016-07-11 17:57:37 +03:00
response_data = None
2016-07-13 17:30:27 +03:00
if response.get_data() is not None:
2016-07-11 17:57:37 +03:00
response_data = json.loads(str(response.get_data()))
data['responseData'] = response_data
2016-04-03 00:58:33 +03:00
data['contentType'] = response.get_content_type()
data['timestamp'] = response.get_timestamp()
json_data = json.dumps(data)
self.logger.debug('[Messaging] Task status message was created')
return str(json_data)
def policy_status_msg(self, response):
data = dict()
2016-04-03 00:58:33 +03:00
data['type'] = response.get_type()
2016-04-12 17:31:38 +03:00
data['policyVersion'] = response.get_policy_version()
data['commandExecutionId'] = response.get_execution_id()
2016-04-03 00:58:33 +03:00
data['responseCode'] = response.get_code()
data['responseMessage'] = response.get_message()
2016-07-11 17:57:37 +03:00
response_data = None
2016-07-13 17:30:27 +03:00
if response.get_data() is not None:
2016-07-11 17:57:37 +03:00
response_data = json.loads(str(response.get_data()))
data['responseData'] = response_data
2016-04-03 00:58:33 +03:00
data['contentType'] = response.get_content_type()
data['timestamp'] = response.get_timestamp()
json_data = json.dumps(data)
self.logger.debug('[Messaging] Policy status message was created')
return str(json_data)
def login_msg(self, username):
data = dict()
2016-03-08 18:05:42 +02:00
data['type'] = 'LOGIN'
data['username'] = username
2016-06-27 17:36:12 +03:00
data['ipAddresses'] = str(System.Hardware.Network.ip_addresses()).replace('[', '').replace(']', '')
data['timestamp'] = Util.timestamp()
json_data = json.dumps(data)
2016-03-11 17:39:32 +02:00
self.logger.debug('[Messaging] Login message was created')
return json_data
def logout_msg(self, username):
data = dict()
2016-03-08 18:05:42 +02:00
data['type'] = 'LOGOUT'
data['username'] = str(username)
2016-06-27 17:36:12 +03:00
data['timestamp'] = Util.timestamp()
json_data = json.dumps(data)
2016-03-11 17:39:32 +02:00
self.logger.debug('[Messaging] Logout message was created')
return json_data
2016-03-30 17:34:10 +03:00
def policy_request_msg(self, username):
data = dict()
2016-03-08 18:05:42 +02:00
data['type'] = 'GET_POLICIES'
2016-03-30 17:34:10 +03:00
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
2016-04-03 00:58:33 +03:00
data['agentPolicyVersion'] = machine_policy_number
2016-03-30 17:34:10 +03:00
data['username'] = str(username)
2016-06-27 17:36:12 +03:00
data['timestamp'] = Util.timestamp()
2016-03-11 17:39:32 +02:00
json_data = json.dumps(data)
self.logger.debug('[Messaging] Get Policies message was created')
return json_data
def registration_msg(self):
data = dict()
2016-03-11 17:39:32 +02:00
data['type'] = 'REGISTER'
2016-05-12 17:33:33 +03:00
data['from'] = self.db_service.select_one_result('registration', 'jid', ' 1=1')
2016-05-06 16:42:21 +03:00
data['password'] = self.db_service.select_one_result('registration', 'password', ' 1=1')
2016-05-12 17:33:33 +03:00
2016-05-06 16:42:21 +03:00
params = self.db_service.select_one_result('registration', 'params', ' 1=1')
2016-05-18 14:55:52 +03:00
data['data'] = json.loads(str(params))
2016-04-08 15:54:26 +03:00
json_params = json.loads(str(params))
data['macAddresses'] = json_params['macAddresses']
data['ipAddresses'] = json_params['ipAddresses']
data['hostname'] = json_params['hostname']
2016-05-12 17:33:33 +03:00
2016-05-06 16:42:21 +03:00
data['timestamp'] = self.db_service.select_one_result('registration', 'timestamp', ' 1=1')
2016-03-11 17:39:32 +02:00
json_data = json.dumps(data)
self.logger.debug('[Messaging] Registration message was created')
return json_data
def ldap_registration_msg(self):
data = dict()
2016-03-11 17:39:32 +02:00
data['type'] = 'REGISTER_LDAP'
data['from'] = str(self.conf_manager.get('REGISTRATION', 'from'))
data['password'] = str(self.conf_manager.get('REGISTRATION', 'password'))
data['macAddresses'] = str(self.conf_manager.get('REGISTRATION', 'macAddresses'))
data['ipAddresses'] = str(self.conf_manager.get('REGISTRATION', 'ipAddresses'))
data['hostname'] = str(self.conf_manager.get('REGISTRATION', 'hostname'))
2016-06-27 17:36:12 +03:00
data['timestamp'] = Util.timestamp()
json_data = json.dumps(data)
2016-03-11 17:39:32 +02:00
self.logger.debug('[Messaging] LDAP Registration message was created')
return json_data
def unregister_msg(self):
data = dict()
2016-03-08 18:05:42 +02:00
data['type'] = 'UNREGISTER'
2016-03-11 17:39:32 +02:00
data['from'] = str(self.conf_manager.get('REGISTRATION', 'from'))
data['password'] = str(self.conf_manager.get('REGISTRATION', 'password'))
data['macAddresses'] = str(self.conf_manager.get('REGISTRATION', 'macAddresses'))
data['ipAddresses'] = str(self.conf_manager.get('REGISTRATION', 'ipAddresses'))
data['hostname'] = str(self.conf_manager.get('REGISTRATION', 'hostname'))
# data['username'] = str(pwd.getpwuid( os.getuid() )[ 0 ])
2016-06-27 17:36:12 +03:00
data['timestamp'] = Util.timestamp()
json_data = json.dumps(data)
2016-03-11 17:39:32 +02:00
self.logger.debug('[Messaging] Unregister message was created')
return json_data
2016-06-27 17:14:11 +03:00
def agreement_request_msg(self):
data = dict()
2016-06-27 17:14:11 +03:00
data['type'] = 'REQUEST_AGREEMENT'
2016-06-30 17:04:44 +03:00
"""
2016-06-27 17:14:11 +03:00
contract_content = self.db_service.select_one_result('contract', 'content', 'id =(select MAX(id) from contract)')
if contract_content is not None and contract_content != '':
data['md5'] = Util.get_md5_text(contract_content)
else:
data['md5'] = ''
2016-06-30 17:04:44 +03:00
"""
2016-06-27 17:36:12 +03:00
data['timestamp'] = Util.timestamp()
2016-06-27 17:14:11 +03:00
json_data = json.dumps(data)
self.logger.debug('[Messaging] Agreement request message was created')
return json_data
2016-06-28 12:24:02 +03:00
def agreement_answer_msg(self, username, answer):
data = dict()
2016-06-29 12:39:18 +03:00
data['type'] = 'AGREEMENT_STATUS'
2016-06-28 12:24:02 +03:00
data['username'] = username
2016-06-30 17:04:44 +03:00
data['accepted'] = answer
2016-06-28 12:24:02 +03:00
data['timestamp'] = Util.timestamp()
contract_content = self.db_service.select_one_result('contract', 'content', 'id =(select MAX(id) from contract)')
if contract_content is not None and contract_content != '':
data['md5'] = Util.get_md5_text(contract_content)
else:
data['md5'] = ''
json_data = json.dumps(data)
self.logger.debug('[Messaging] Agreement answer message was created')
return json_data