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

152 lines
6.3 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
import sys
2016-02-16 17:50:37 +02:00
sys.path.append('../..')
from base.Scope import Scope
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 = {}
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-12 17:31:38 +03:00
"""
2016-03-30 17:34:10 +03:00
def response_msg(self, response):
print("response message")
data = {}
2016-03-30 17:34:10 +03:00
data['type'] = response.get_type()
data['taskId'] = response.get_id()
2016-03-30 17:34:10 +03:00
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)
2016-03-30 17:34:10 +03:00
self.logger.debug('[Messaging] Response message was created')
return str(json_data)
2016-04-12 17:31:38 +03:00
"""
2016-03-30 17:34:10 +03:00
2016-04-03 00:58:33 +03:00
def task_status_msg(self, response):
data = {}
data['type'] = response.get_type()
data['taskId'] = 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] Task status message was created')
return str(json_data)
def policy_status_msg(self, response):
data = {}
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()
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 status message was created')
return str(json_data)
def login_msg(self, username):
data = {}
2016-03-08 18:05:42 +02:00
data['type'] = 'LOGIN'
data['username'] = username
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
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 = {}
2016-03-08 18:05:42 +02:00
data['type'] = 'LOGOUT'
data['username'] = str(username)
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
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 = {}
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-03-11 17:39:32 +02:00
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)
self.logger.debug('[Messaging] Get Policies message was created')
return json_data
def registration_msg(self):
data = {}
data['type'] = 'REGISTER'
2016-05-06 16:42:21 +03:00
data['from'] = self.db_service.select_one_result('registration', 'jid', ' 1=1') # str(self.conf_manager.get('REGISTRATION', 'from'))
data['password'] = self.db_service.select_one_result('registration', 'password', ' 1=1')
params = self.db_service.select_one_result('registration', 'params', ' 1=1')
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-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 = {}
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'))
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
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 = {}
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 ])
data['timestamp'] = str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))
json_data = json.dumps(data)
2016-03-11 17:39:32 +02:00
self.logger.debug('[Messaging] Unregister message was created')
return json_data