2016-03-02 18:31:29 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: Volkan Şahin <volkansah.in> <bm.volkansahin@gmail.com>
|
|
|
|
|
|
|
|
from base.Scope import Scope
|
2016-03-07 12:55:14 +02:00
|
|
|
from base.messaging.MessageSender import MessageSender
|
2016-03-02 18:31:29 +02:00
|
|
|
from uuid import getnode as get_mac
|
2016-03-11 17:37:38 +02:00
|
|
|
import json, uuid, netifaces, socket, datetime
|
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
|
|
|
|
class Registration():
|
|
|
|
|
|
|
|
|
2016-03-11 17:37:38 +02:00
|
|
|
#TODO keep catches and set logs
|
2016-03-02 18:31:29 +02:00
|
|
|
def __init__(self):
|
2016-03-04 19:23:21 +02:00
|
|
|
scope = Scope().getInstance()
|
2016-03-02 18:31:29 +02:00
|
|
|
self.conf_manager = scope.getConfigurationManager()
|
|
|
|
self.logger=scope.getLogger()
|
2016-03-07 17:59:02 +02:00
|
|
|
self.message_manager=scope.getMessageManager()
|
|
|
|
self.event_manager = scope.getEventManager()
|
|
|
|
|
|
|
|
self.event_manager.register_event('confirm_registration',self.confirm_registration)
|
|
|
|
|
2016-03-04 19:23:21 +02:00
|
|
|
if self.conf_manager.has_section('REGISTRATION'):
|
|
|
|
if self.conf_manager.get('REGISTRATION', 'registered')=='false':
|
|
|
|
self.re_register()
|
|
|
|
else:
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Ahenk already registered')
|
2016-03-04 19:23:21 +02:00
|
|
|
else:
|
2016-03-07 12:55:14 +02:00
|
|
|
self.register(True)
|
2016-03-04 19:23:21 +02:00
|
|
|
|
2016-03-07 12:55:14 +02:00
|
|
|
def registration_request(self):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Requesting registration')
|
2016-03-14 12:05:12 +02:00
|
|
|
message_sender=MessageSender(self.message_manager.registration_msg(),None)
|
2016-03-07 12:55:14 +02:00
|
|
|
message_sender.connect_to_server()
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-10 09:53:51 +02:00
|
|
|
def ldap_registration_request(self):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Requesting LDAP registration')
|
2016-03-14 12:05:12 +02:00
|
|
|
message_sender=MessageSender(self.message_manager.ldap_registration_msg(),None)
|
2016-03-10 09:53:51 +02:00
|
|
|
message_sender.connect_to_server()
|
|
|
|
|
2016-03-11 17:37:38 +02:00
|
|
|
def confirm_registration(self,reg_reply):
|
|
|
|
self.logger.debug('[Registration] Reading registration reply')
|
2016-03-07 17:59:02 +02:00
|
|
|
j = json.loads(reg_reply)
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration]'+j['message'])
|
2016-03-08 18:09:02 +02:00
|
|
|
status =str(j['status']).lower()
|
|
|
|
dn=str(j['agentDn']).lower()
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Registration status: '+str(status))
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-10 09:53:51 +02:00
|
|
|
if str(status)=='registered' or str(status)=='registered_without_ldap':
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('dn:'+dn)
|
|
|
|
self.update_conf_file(dn)
|
2016-03-10 09:53:51 +02:00
|
|
|
elif str(status)=='registration_error':
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.info('[Registration] Registration is failed. New registration request will send')
|
2016-03-07 17:59:02 +02:00
|
|
|
self.re_register()
|
|
|
|
self.registration_request()
|
2016-03-11 17:37:38 +02:00
|
|
|
elif str(status)=='already_exists':
|
|
|
|
self.update_conf_file(dn)
|
|
|
|
self.logger.info('[Registration] Ahenk already registered')
|
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
|
2016-03-11 17:37:38 +02:00
|
|
|
def update_conf_file(self,dn=None):
|
|
|
|
self.logger.debug('[Registration] Registration configuration is updating...')
|
2016-03-10 09:53:51 +02:00
|
|
|
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', 'password',self.conf_manager.get('REGISTRATION', 'password'))
|
|
|
|
self.conf_manager.set('REGISTRATION', 'dn',dn)
|
|
|
|
self.conf_manager.set('REGISTRATION', 'registered','true')
|
|
|
|
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
|
|
|
self.conf_manager.write(configfile)
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Registration configuration file is updated')
|
2016-03-10 09:53:51 +02:00
|
|
|
|
2016-03-04 19:23:21 +02:00
|
|
|
def is_registered(self):
|
|
|
|
if self.conf_manager.has_section('REGISTRATION') and (self.conf_manager.get('REGISTRATION', 'registered')=='true'):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('registered')
|
2016-03-04 19:23:21 +02:00
|
|
|
return True
|
|
|
|
else:
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('not registered')
|
2016-03-04 19:23:21 +02:00
|
|
|
return False
|
|
|
|
|
2016-03-10 09:53:51 +02:00
|
|
|
def is_ldap_registered(self):
|
|
|
|
if self.is_registered() and self.conf_manager.get('REGISTRATION', 'dn')!='' and self.conf_manager.get('REGISTRATION', 'dn') is not None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-03-07 12:55:14 +02:00
|
|
|
def register(self,uuid_depend_mac):
|
2016-03-02 18:31:29 +02:00
|
|
|
if self.conf_manager.has_section('REGISTRATION'):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.info('[Registration] Registration section is already created')
|
2016-03-02 18:31:29 +02:00
|
|
|
else:
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Creating Registration section')
|
2016-03-04 19:23:21 +02:00
|
|
|
self.conf_manager.add_section('REGISTRATION')
|
2016-03-07 12:55:14 +02:00
|
|
|
self.conf_manager.set('REGISTRATION', 'from',str(self.generate_uuid(uuid_depend_mac)))
|
2016-03-08 18:09:02 +02:00
|
|
|
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', 'ipAddresses',str(self.get_ipAddresses()))
|
2016-03-04 19:23:21 +02:00
|
|
|
self.conf_manager.set('REGISTRATION', 'hostname',str(socket.gethostname()))
|
|
|
|
self.conf_manager.set('REGISTRATION', 'timestamp',str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M")))
|
|
|
|
self.conf_manager.set('REGISTRATION', 'password',str(self.generate_password()))
|
|
|
|
self.conf_manager.set('REGISTRATION', 'dn','')
|
|
|
|
self.conf_manager.set('REGISTRATION', 'registered','false')
|
|
|
|
|
|
|
|
#TODO self.conf_manager.configurationFilePath attribute error ? READ olacak
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Parameters were set up, section will write to configuration file')
|
2016-03-07 12:55:14 +02:00
|
|
|
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
2016-03-04 19:23:21 +02:00
|
|
|
self.conf_manager.write(configfile)
|
2016-03-02 18:31:29 +02:00
|
|
|
self.logger.debug('[Registration] REGISTRATION section wrote to configuration file successfully')
|
|
|
|
|
2016-03-04 19:23:21 +02:00
|
|
|
def unregister(self):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Ahenk is unregistering...')
|
2016-03-04 19:23:21 +02:00
|
|
|
if self.conf_manager.has_section('REGISTRATION'):
|
2016-03-08 18:09:02 +02:00
|
|
|
#TODO open this block if you want to be aware about unregistration
|
|
|
|
#message_sender=MessageSender(self.message_manager.unregister_msg(),None)
|
|
|
|
#message_sender.connect_to_server()
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-04 19:23:21 +02:00
|
|
|
self.conf_manager.remove_section('REGISTRATION')
|
|
|
|
self.conf_manager.set('CONNECTION', 'uid','')
|
|
|
|
self.conf_manager.set('CONNECTION', 'password','')
|
|
|
|
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
|
|
|
self.conf_manager.write(configfile)
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Ahenk is unregistered')
|
2016-03-07 17:59:02 +02:00
|
|
|
|
2016-03-07 12:55:14 +02:00
|
|
|
def re_register(self):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] Reregistrating...')
|
2016-03-07 12:55:14 +02:00
|
|
|
self.unregister()
|
|
|
|
self.register(False)
|
2016-03-04 19:23:21 +02:00
|
|
|
|
2016-03-02 18:31:29 +02:00
|
|
|
def generate_uuid(self,depend_mac=True):
|
|
|
|
if depend_mac is False:
|
|
|
|
self.logger.debug('[Registration] uuid creating randomly')
|
|
|
|
return uuid.uuid4() # make a random UUID
|
|
|
|
else:
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] uuid creating according to mac address')
|
2016-03-02 18:31:29 +02:00
|
|
|
return uuid.uuid3(uuid.NAMESPACE_DNS, str(get_mac()))# make a UUID using an MD5 hash of a namespace UUID and a mac address
|
|
|
|
|
2016-03-04 19:23:21 +02:00
|
|
|
def generate_password(self):
|
|
|
|
return uuid.uuid4()
|
|
|
|
|
2016-03-08 18:09:02 +02:00
|
|
|
def get_ipAddresses(self):
|
2016-03-11 17:37:38 +02:00
|
|
|
self.logger.debug('[Registration] looking for network interfaces')
|
2016-03-02 18:31:29 +02:00
|
|
|
ip_address=""
|
|
|
|
for interface in netifaces.interfaces():
|
|
|
|
if(str(interface) != "lo"):
|
|
|
|
ip_address+=str(netifaces.ifaddresses(interface)[netifaces.AF_INET])
|
|
|
|
self.logger.debug('[Registration] returning ip addresses from every interfaces')
|
|
|
|
return ip_address
|