mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-22 22:32:21 +03:00
logs were added and some bugs were fixed
This commit is contained in:
parent
8eac2d6615
commit
c55432b6c8
1 changed files with 34 additions and 56 deletions
|
@ -2,19 +2,16 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
||||||
|
|
||||||
from base.config.ConfigManager import ConfigManager
|
|
||||||
from base.logger.AhenkLogger import Logger
|
|
||||||
from base.Scope import Scope
|
from base.Scope import Scope
|
||||||
from base.messaging.MessageSender import MessageSender
|
from base.messaging.MessageSender import MessageSender
|
||||||
from uuid import getnode as get_mac
|
from uuid import getnode as get_mac
|
||||||
import sys,logging,json,uuid
|
import json, uuid, netifaces, socket, datetime
|
||||||
import datetime,time,configparser
|
|
||||||
import netifaces,socket,re
|
|
||||||
|
|
||||||
class Registration():
|
class Registration():
|
||||||
|
|
||||||
#TODO try catches
|
|
||||||
|
|
||||||
|
#TODO keep catches and set logs
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
scope = Scope().getInstance()
|
scope = Scope().getInstance()
|
||||||
self.conf_manager = scope.getConfigurationManager()
|
self.conf_manager = scope.getConfigurationManager()
|
||||||
|
@ -28,35 +25,42 @@ class Registration():
|
||||||
if self.conf_manager.get('REGISTRATION', 'registered')=='false':
|
if self.conf_manager.get('REGISTRATION', 'registered')=='false':
|
||||||
self.re_register()
|
self.re_register()
|
||||||
else:
|
else:
|
||||||
self.logger.debug('[Registration] already registered')
|
self.logger.debug('[Registration] Ahenk already registered')
|
||||||
else:
|
else:
|
||||||
self.register(True)
|
self.register(True)
|
||||||
self.logger.debug('[Registration] ')
|
|
||||||
|
|
||||||
def registration_request(self):
|
def registration_request(self):
|
||||||
|
self.logger.debug('[Registration] Requesting registration')
|
||||||
message_sender=MessageSender(self.get_registration_request_message(),None)
|
message_sender=MessageSender(self.get_registration_request_message(),None)
|
||||||
message_sender.connect_to_server()
|
message_sender.connect_to_server()
|
||||||
|
|
||||||
def ldap_registration_request(self):
|
def ldap_registration_request(self):
|
||||||
|
self.logger.debug('[Registration] Requesting LDAP registration')
|
||||||
message_sender=MessageSender(self.get_ldap_registration_request_message(),None)
|
message_sender=MessageSender(self.get_ldap_registration_request_message(),None)
|
||||||
message_sender.connect_to_server()
|
message_sender.connect_to_server()
|
||||||
|
|
||||||
def confirm_registration(self,reg_reply): #event fire and keep here
|
def confirm_registration(self,reg_reply):
|
||||||
|
self.logger.debug('[Registration] Reading registration reply')
|
||||||
j = json.loads(reg_reply)
|
j = json.loads(reg_reply)
|
||||||
self.logger.info('[REGISTRATION] register reply: '+j['message'])
|
self.logger.debug('[Registration]'+j['message'])
|
||||||
status =str(j['status']).lower()
|
status =str(j['status']).lower()
|
||||||
dn=str(j['agentDn']).lower()
|
dn=str(j['agentDn']).lower()
|
||||||
|
self.logger.debug('[Registration] Registration status: '+str(status))
|
||||||
|
|
||||||
if str(status)=='registered' or str(status)=='registered_without_ldap':
|
if str(status)=='registered' or str(status)=='registered_without_ldap':
|
||||||
self.update_conf_file()
|
self.logger.debug('dn:'+dn)
|
||||||
|
self.update_conf_file(dn)
|
||||||
elif str(status)=='registration_error':
|
elif str(status)=='registration_error':
|
||||||
self.logger.info('[REGISTRATION] registration error')
|
self.logger.info('[Registration] Registration is failed. New registration request will send')
|
||||||
elif str(status)=='already_registered':
|
|
||||||
self.logger.info('[REGISTRATION]already registered')
|
|
||||||
self.re_register()
|
self.re_register()
|
||||||
self.registration_request()
|
self.registration_request()
|
||||||
|
elif str(status)=='already_exists':
|
||||||
|
self.update_conf_file(dn)
|
||||||
|
self.logger.info('[Registration] Ahenk already registered')
|
||||||
|
|
||||||
def update_conf_file(self):
|
|
||||||
|
def update_conf_file(self,dn=None):
|
||||||
|
self.logger.debug('[Registration] Registration configuration is updating...')
|
||||||
if self.conf_manager.has_section('CONNECTION') and self.conf_manager.get('REGISTRATION', 'from') is not None:
|
if self.conf_manager.has_section('CONNECTION') and self.conf_manager.get('REGISTRATION', 'from') is not None:
|
||||||
self.conf_manager.set('CONNECTION', 'uid',self.conf_manager.get('REGISTRATION', 'from'))
|
self.conf_manager.set('CONNECTION', 'uid',self.conf_manager.get('REGISTRATION', 'from'))
|
||||||
self.conf_manager.set('CONNECTION', 'password',self.conf_manager.get('REGISTRATION', 'password'))
|
self.conf_manager.set('CONNECTION', 'password',self.conf_manager.get('REGISTRATION', 'password'))
|
||||||
|
@ -64,11 +68,14 @@ class Registration():
|
||||||
self.conf_manager.set('REGISTRATION', 'registered','true')
|
self.conf_manager.set('REGISTRATION', 'registered','true')
|
||||||
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
||||||
self.conf_manager.write(configfile)
|
self.conf_manager.write(configfile)
|
||||||
|
self.logger.debug('[Registration] Registration configuration file is updated')
|
||||||
|
|
||||||
def is_registered(self):
|
def is_registered(self):
|
||||||
if self.conf_manager.has_section('REGISTRATION') and (self.conf_manager.get('REGISTRATION', 'registered')=='true'):
|
if self.conf_manager.has_section('REGISTRATION') and (self.conf_manager.get('REGISTRATION', 'registered')=='true'):
|
||||||
|
self.logger.debug('registered')
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
self.logger.debug('not registered')
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def is_ldap_registered(self):
|
def is_ldap_registered(self):
|
||||||
|
@ -78,54 +85,24 @@ class Registration():
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def get_registration_request_message(self):
|
def get_registration_request_message(self):
|
||||||
self.logger.debug('[Registration] creating registration message according to parameters of registration')
|
|
||||||
|
|
||||||
if self.conf_manager.has_section('REGISTRATION'):
|
if self.conf_manager.has_section('REGISTRATION'):
|
||||||
data = {}
|
return self.message_manager.registration_msg()
|
||||||
data['type'] = 'REGISTER'
|
|
||||||
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(self.conf_manager.get('REGISTRATION','timestamp'))
|
|
||||||
self.logger.debug('[Registration] json of registration message was created')
|
|
||||||
|
|
||||||
json_data = json.dumps(data)
|
|
||||||
self.logger.debug('[Registration] json converted to str')
|
|
||||||
return json_data
|
|
||||||
else:
|
else:
|
||||||
print("Registration section not found")
|
self.logger.error('[Registration] Registration section not found while trying to registration request')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_ldap_registration_request_message(self):
|
def get_ldap_registration_request_message(self):
|
||||||
self.logger.debug('[Registration] creating ldap registration message according to parameters of registration')
|
|
||||||
|
|
||||||
if self.conf_manager.has_section('REGISTRATION'):
|
if self.conf_manager.has_section('REGISTRATION'):
|
||||||
data = {}
|
return self.message_manager.ldap_registration_msg()
|
||||||
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(self.conf_manager.get('REGISTRATION','timestamp'))
|
|
||||||
self.logger.debug('[Registration] json of registration message was created')
|
|
||||||
|
|
||||||
json_data = json.dumps(data)
|
|
||||||
self.logger.debug('[Registration] json converted to str')
|
|
||||||
return json_data
|
|
||||||
else:
|
else:
|
||||||
print("Registration section not found")
|
self.logger.error('[Registration] Registration section not found while trying to ldap registration request')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def register(self,uuid_depend_mac):
|
def register(self,uuid_depend_mac):
|
||||||
self.logger.debug('[Registration] configuration parameters of registration is checking')
|
|
||||||
if self.conf_manager.has_section('REGISTRATION'):
|
if self.conf_manager.has_section('REGISTRATION'):
|
||||||
self.logger.debug('[Registration] REGISTRATION section is already created')
|
self.logger.info('[Registration] Registration section is already created')
|
||||||
else:
|
else:
|
||||||
self.logger.debug('[Registration] creating REGISTRATION section')
|
self.logger.debug('[Registration] Creating Registration section')
|
||||||
|
|
||||||
self.conf_manager.add_section('REGISTRATION')
|
self.conf_manager.add_section('REGISTRATION')
|
||||||
self.conf_manager.set('REGISTRATION', 'from',str(self.generate_uuid(uuid_depend_mac)))
|
self.conf_manager.set('REGISTRATION', 'from',str(self.generate_uuid(uuid_depend_mac)))
|
||||||
self.conf_manager.set('REGISTRATION', 'macAddresses',str(':'.join(("%012X" % get_mac())[i:i+2] for i in range(0, 12, 2))))
|
self.conf_manager.set('REGISTRATION', 'macAddresses',str(':'.join(("%012X" % get_mac())[i:i+2] for i in range(0, 12, 2))))
|
||||||
|
@ -137,12 +114,13 @@ class Registration():
|
||||||
self.conf_manager.set('REGISTRATION', 'registered','false')
|
self.conf_manager.set('REGISTRATION', 'registered','false')
|
||||||
|
|
||||||
#TODO self.conf_manager.configurationFilePath attribute error ? READ olacak
|
#TODO self.conf_manager.configurationFilePath attribute error ? READ olacak
|
||||||
self.logger.debug('[Registration] parameters were set up, section will write to configuration file')
|
self.logger.debug('[Registration] Parameters were set up, section will write to configuration file')
|
||||||
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
||||||
self.conf_manager.write(configfile)
|
self.conf_manager.write(configfile)
|
||||||
self.logger.debug('[Registration] REGISTRATION section wrote to configuration file successfully')
|
self.logger.debug('[Registration] REGISTRATION section wrote to configuration file successfully')
|
||||||
|
|
||||||
def unregister(self):
|
def unregister(self):
|
||||||
|
self.logger.debug('[Registration] Ahenk is unregistering...')
|
||||||
if self.conf_manager.has_section('REGISTRATION'):
|
if self.conf_manager.has_section('REGISTRATION'):
|
||||||
#TODO open this block if you want to be aware about unregistration
|
#TODO open this block if you want to be aware about unregistration
|
||||||
#message_sender=MessageSender(self.message_manager.unregister_msg(),None)
|
#message_sender=MessageSender(self.message_manager.unregister_msg(),None)
|
||||||
|
@ -153,26 +131,26 @@ class Registration():
|
||||||
self.conf_manager.set('CONNECTION', 'password','')
|
self.conf_manager.set('CONNECTION', 'password','')
|
||||||
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
||||||
self.conf_manager.write(configfile)
|
self.conf_manager.write(configfile)
|
||||||
|
self.logger.debug('[Registration] Ahenk is unregistered')
|
||||||
|
|
||||||
def re_register(self):
|
def re_register(self):
|
||||||
|
self.logger.debug('[Registration] Reregistrating...')
|
||||||
self.unregister()
|
self.unregister()
|
||||||
self.register(False)
|
self.register(False)
|
||||||
|
|
||||||
def generate_uuid(self,depend_mac=True):
|
def generate_uuid(self,depend_mac=True):
|
||||||
self.logger.debug('[Registration] universal user id will be created')
|
|
||||||
if depend_mac is False:
|
if depend_mac is False:
|
||||||
self.logger.debug('[Registration] uuid creating randomly')
|
self.logger.debug('[Registration] uuid creating randomly')
|
||||||
return uuid.uuid4() # make a random UUID
|
return uuid.uuid4() # make a random UUID
|
||||||
else:
|
else:
|
||||||
self.logger.debug('[Registration] uuid creating depends to mac address')
|
self.logger.debug('[Registration] uuid creating according to mac address')
|
||||||
return uuid.uuid3(uuid.NAMESPACE_DNS, str(get_mac()))# make a UUID using an MD5 hash of a namespace UUID and a mac address
|
return uuid.uuid3(uuid.NAMESPACE_DNS, str(get_mac()))# make a UUID using an MD5 hash of a namespace UUID and a mac address
|
||||||
|
|
||||||
def generate_password(self):
|
def generate_password(self):
|
||||||
return uuid.uuid4()
|
return uuid.uuid4()
|
||||||
|
|
||||||
def get_ipAddresses(self):
|
def get_ipAddresses(self):
|
||||||
self.logger.debug('[Registration] looking for network interces')
|
self.logger.debug('[Registration] looking for network interfaces')
|
||||||
ip_address=""
|
ip_address=""
|
||||||
for interface in netifaces.interfaces():
|
for interface in netifaces.interfaces():
|
||||||
if(str(interface) != "lo"):
|
if(str(interface) != "lo"):
|
||||||
|
|
Loading…
Reference in a new issue