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

193 lines
7.7 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 json
2016-02-16 17:50:37 +02:00
from base.scope import Scope
from base.system.system import System
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().get_instance()
self.logger = scope.get_logger()
self.conf_manager = scope.get_configuration_manager()
self.db_service = scope.get_db_service()
self.event_manger = scope.get_event_manager()
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('Missing plugin message was created')
2016-05-06 16:42:21 +03:00
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('Task status message was created')
2016-04-03 00:58:33 +03:00
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('Policy status message was created')
2016-04-03 00:58:33 +03:00
return str(json_data)
2017-08-14 11:17:18 +03:00
def login_msg(self, username,ip=None):
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()
2017-08-14 11:17:18 +03:00
data['userIp'] = ip
2017-08-14 11:33:30 +03:00
self.logger.debug('USER IP : '+ str(ip)+ ' IPADDRESSES : '+ str(System.Hardware.Network.ip_addresses()).replace('[', '').replace(']', ''))
data['hardware.monitors'] = str(System.Hardware.monitors()),
data['hardware.screens'] = str(System.Hardware.screens()),
data['hardware.usbDevices'] = str(System.Hardware.usb_devices()),
data['hardware.printers'] = str(System.Hardware.printers()),
data['hardware.systemDefinitions'] = str(System.Hardware.system_definitions()),
json_data = json.dumps(data)
self.logger.debug('Login message was created')
return json_data
def logout_msg(self, username,ip):
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()
data['userIp'] = ip
json_data = json.dumps(data)
self.logger.debug('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 + '\'')
2016-03-30 17:34:10 +03:00
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('Get Policies message was created')
2016-03-11 17:39:32 +02:00
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('Registration message was created')
2016-03-11 17:39:32 +02:00
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)
self.logger.debug('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)
self.logger.debug('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('Agreement request message was created')
2016-06-27 17:14:11 +03:00
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)')
2016-06-28 12:24:02 +03:00
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('Agreement answer message was created')
2016-06-28 12:24:02 +03:00
return json_data