mirror of
https://github.com/Pardus-LiderAhenk/ahenk
synced 2024-11-25 12:22:28 +03:00
registration conf section way was replaced to database. some db operations were updated according to dbservice new feature
This commit is contained in:
parent
e501afad33
commit
7721e763e7
2 changed files with 71 additions and 97 deletions
|
@ -33,40 +33,39 @@ class ExecutionManager(object):
|
|||
print("updating policies...")
|
||||
|
||||
policy = Policy(json.loads(arg))
|
||||
#TODO get username from pam
|
||||
#TODO get username
|
||||
username='volkan'
|
||||
|
||||
ahenk_policy_ver=self.db_service.select('policy',['version'],'type = \'A\'')
|
||||
user_policy_version=self.db_service.select('policy',['version'],'type = \'U\' and name = \''+username+'\'')
|
||||
installed_plugins=self.get_installed_plugins()
|
||||
missing_plugins=[]
|
||||
ahenk_policy_ver = self.db_service.select_one_result('policy', 'version', 'type = \'A\'')
|
||||
user_policy_version = self.db_service.select_one_result('policy', 'version', 'type = \'U\' and name = \''+username+'\'')
|
||||
installed_plugins = self.get_installed_plugins()
|
||||
missing_plugins = []
|
||||
|
||||
|
||||
if policy.ahenk_policy_version != ahenk_policy_ver[0][0]:
|
||||
ahenk_policy_id=self.db_service.select('policy',['id'],'type = \'A\'')
|
||||
self.db_service.delete('profile','id='+str(ahenk_policy_id[0][0]))
|
||||
self.db_service.update('policy',['version'],[str(policy.ahenk_policy_version)],'type=\'A\'')
|
||||
if policy.ahenk_policy_version != ahenk_policy_ver:
|
||||
ahenk_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'A\'')
|
||||
self.db_service.delete('profile', 'id='+str(ahenk_policy_id))
|
||||
self.db_service.update('policy', ['version'], [str(policy.ahenk_policy_version)], 'type=\'A\'')
|
||||
|
||||
for profile in policy.ahenk_profiles:
|
||||
profile_columns=['id','create_date','modify_date','label','description','overridable','active','deleted','profile_data','plugin']
|
||||
args=[str(ahenk_policy_id[0][0]),str(profile.create_date),str(profile.modify_date),str(profile.label),
|
||||
str(profile.description),str(profile.overridable),str(profile.active),str(profile.deleted),str(profile.profile_data),str(profile.plugin)]
|
||||
self.db_service.update('profile',profile_columns,args)
|
||||
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
||||
args = [str(ahenk_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label),
|
||||
str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)]
|
||||
self.db_service.update('profile', profile_columns, args)
|
||||
if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins:
|
||||
missing_plugins.append(profile.plugin.name)
|
||||
|
||||
else:
|
||||
print("already there ahenk policy")
|
||||
|
||||
if policy.user_policy_version != user_policy_version[0][0]:
|
||||
user_policy_id=self.db_service.select('policy',['id'],'type = \'U\' and name=\''+username+'\'')
|
||||
self.db_service.delete('profile','id='+str(user_policy_id[0][0]))
|
||||
self.db_service.update('policy',['version'],[str(policy.user_policy_version)],'type=\'U\' and name=\''+username+'\'')
|
||||
if policy.user_policy_version != user_policy_version:
|
||||
user_policy_id = self.db_service.select_one_result('policy', 'id', 'type = \'U\' and name=\''+username+'\'')
|
||||
self.db_service.delete('profile', 'id='+str(user_policy_id))
|
||||
self.db_service.update('policy', ['version'], [str(policy.user_policy_version)], 'type=\'U\' and name=\''+username+'\'')
|
||||
for profile in policy.user_profiles:
|
||||
profile_columns=['id','create_date','modify_date','label','description','overridable','active','deleted','profile_data','plugin']
|
||||
args = [str(user_policy_id[0][0]),str(profile.create_date),str(profile.modify_date),str(profile.label),
|
||||
str(profile.description),str(profile.overridable),str(profile.active),str(profile.deleted),str(profile.profile_data),str(profile.plugin)]
|
||||
self.db_service.update('profile',profile_columns,args)
|
||||
profile_columns = ['id', 'create_date', 'modify_date', 'label', 'description', 'overridable', 'active', 'deleted', 'profile_data', 'plugin']
|
||||
args = [str(user_policy_id), str(profile.create_date), str(profile.modify_date), str(profile.label),
|
||||
str(profile.description), str(profile.overridable), str(profile.active), str(profile.deleted), str(profile.profile_data), str(profile.plugin)]
|
||||
self.db_service.update('profile',profile_columns, args)
|
||||
if profile.plugin.name not in installed_plugins and profile.plugin.name not in missing_plugins:
|
||||
missing_plugins.append(profile.plugin.name)
|
||||
else:
|
||||
|
|
|
@ -5,142 +5,117 @@
|
|||
from base.Scope import Scope
|
||||
from base.messaging.AnonymousMessager import AnonymousMessager
|
||||
from uuid import getnode as get_mac
|
||||
import json, uuid, netifaces, socket, datetime
|
||||
import netifaces
|
||||
import datetime
|
||||
import socket
|
||||
import json
|
||||
import uuid
|
||||
|
||||
|
||||
class Registration():
|
||||
|
||||
|
||||
def __init__(self):
|
||||
scope = Scope().getInstance()
|
||||
self.conf_manager = scope.getConfigurationManager()
|
||||
self.logger=scope.getLogger()
|
||||
self.message_manager=scope.getMessageManager()
|
||||
self.logger = scope.getLogger()
|
||||
self.message_manager = scope.getMessageManager()
|
||||
self.event_manager = scope.getEventManager()
|
||||
self.messager = scope.getMessager()
|
||||
self.db_service = scope.getDbService()
|
||||
|
||||
self.event_manager.register_event('confirm_registration',self.confirm_registration)
|
||||
|
||||
if self.conf_manager.has_section('REGISTRATION'):
|
||||
if self.conf_manager.get('REGISTRATION', 'registered')=='false':
|
||||
self.re_register()
|
||||
else:
|
||||
self.logger.debug('[Registration] Ahenk already registered')
|
||||
if self.is_registered():
|
||||
self.logger.debug('[Registration] Ahenk already registered')
|
||||
else:
|
||||
self.register(True)
|
||||
|
||||
def registration_request(self):
|
||||
self.logger.debug('[Registration] Requesting registration')
|
||||
anon_messager = AnonymousMessager(self.message_manager.registration_msg(),None)
|
||||
anon_messager = AnonymousMessager(self.message_manager.registration_msg(), None)
|
||||
anon_messager.connect_to_server()
|
||||
|
||||
def ldap_registration_request(self):
|
||||
self.logger.debug('[Registration] Requesting LDAP registration')
|
||||
self.messager.send_Direct_message(self.message_manager.ldap_registration_msg())
|
||||
|
||||
def confirm_registration(self,reg_reply):
|
||||
def confirm_registration(self, reg_reply):
|
||||
self.logger.debug('[Registration] Reading registration reply')
|
||||
j = json.loads(reg_reply)
|
||||
self.logger.debug('[Registration]'+j['message'])
|
||||
status =str(j['status']).lower()
|
||||
dn=str(j['agentDn']).lower()
|
||||
self.logger.debug('[Registration] Registration status: '+str(status))
|
||||
self.logger.debug('[Registration]' + j['message'])
|
||||
status = str(j['status']).lower()
|
||||
dn = str(j['agentDn']).lower()
|
||||
self.logger.debug('[Registration] Registration status: ' + str(status))
|
||||
|
||||
if str(status)=='registered' or str(status)=='registered_without_ldap':
|
||||
self.logger.debug('dn:'+dn)
|
||||
if 'registered' == str(status) or 'registered_without_ldap' == str(status):
|
||||
self.logger.debug('dn:' + dn)
|
||||
self.update_conf_file(dn)
|
||||
elif str(status)=='registration_error':
|
||||
elif 'registration_error' == str(status):
|
||||
self.logger.info('[Registration] Registration is failed. New registration request will send')
|
||||
self.re_register()
|
||||
self.registration_request()
|
||||
elif str(status)=='already_exists':
|
||||
elif 'already_exists' == str(status):
|
||||
self.update_conf_file(dn)
|
||||
self.logger.info('[Registration] Ahenk already registered')
|
||||
|
||||
|
||||
def update_conf_file(self,dn=None):
|
||||
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:
|
||||
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')
|
||||
#TODO get file path?
|
||||
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
||||
self.conf_manager.write(configfile)
|
||||
self.logger.debug('[Registration] Registration configuration file is updated')
|
||||
self.db_service.update('registration', ['dn', 'registered'], [dn, 1], ' registered = 0')
|
||||
|
||||
def is_registered(self):
|
||||
if self.conf_manager.has_section('REGISTRATION') and (self.conf_manager.get('REGISTRATION', 'registered')=='true'):
|
||||
self.logger.debug('registered')
|
||||
registered = self.db_service.select_one_result('registration', 'registered', 'registered = 1')
|
||||
if registered == 1:
|
||||
return True
|
||||
else:
|
||||
self.logger.debug('not registered')
|
||||
return False
|
||||
|
||||
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:
|
||||
dn = self.db_service.select_one_result('registration', 'dn', 'registered = 1')
|
||||
if dn is not None and dn != '':
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def register(self,uuid_depend_mac):
|
||||
if self.conf_manager.has_section('REGISTRATION'):
|
||||
self.logger.info('[Registration] Registration section is already created')
|
||||
else:
|
||||
self.logger.debug('[Registration] Creating Registration section')
|
||||
self.conf_manager.add_section('REGISTRATION')
|
||||
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', 'ipAddresses',str(self.get_ipAddresses()))
|
||||
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')
|
||||
def register(self, uuid_depend_mac=False):
|
||||
|
||||
#TODO get file path?
|
||||
self.logger.debug('[Registration] Parameters were set up, section will write to configuration file')
|
||||
with open('/etc/ahenk/ahenk.conf', 'w') as configfile:
|
||||
self.conf_manager.write(configfile)
|
||||
self.logger.debug('[Registration] REGISTRATION section wrote to configuration file successfully')
|
||||
cols = ['jid', 'password', 'registered', 'params', 'timestamp']
|
||||
vals = [str(self.generate_uuid(uuid_depend_mac)), str(self.generate_password()), 0, str(self.get_registration_params()), str(datetime.datetime.now().strftime("%d-%m-%Y %I:%M"))]
|
||||
|
||||
self.db_service.delete('registration', ' 1==1 ')
|
||||
self.db_service.update('registration', cols, vals)
|
||||
self.logger.debug('[Registration] Registration parameters were created')
|
||||
|
||||
def get_registration_params(self):
|
||||
params = {
|
||||
'ipAddresses': str(self.get_ipAddresses()),
|
||||
'macAddresses': str(':'.join(("%012X" % get_mac())[i:i + 2] for i in range(0, 12, 2))),
|
||||
'hostname': str(socket.gethostname())
|
||||
}
|
||||
|
||||
return json.dumps(params)
|
||||
|
||||
def unregister(self):
|
||||
self.logger.debug('[Registration] Ahenk is unregistering...')
|
||||
if self.conf_manager.has_section('REGISTRATION'):
|
||||
#TODO open this block if you want to be aware about unregistration
|
||||
#TODO messaging thread must be terminated
|
||||
#message_sender=MessageSender(self.message_manager.unregister_msg(),None)
|
||||
#message_sender.connect_to_server()
|
||||
|
||||
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)
|
||||
self.logger.debug('[Registration] Ahenk is unregistered')
|
||||
self.db_service.delete('registration', ' 1==1 ')
|
||||
self.logger.debug('[Registration] Ahenk is unregistered')
|
||||
|
||||
def re_register(self):
|
||||
self.logger.debug('[Registration] Reregistrating...')
|
||||
self.unregister()
|
||||
self.register(False)
|
||||
|
||||
def generate_uuid(self,depend_mac=True):
|
||||
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
|
||||
return uuid.uuid4() # make a random UUID
|
||||
else:
|
||||
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):
|
||||
return uuid.uuid4()
|
||||
|
||||
def get_ipAddresses(self):
|
||||
self.logger.debug('[Registration] looking for network interfaces')
|
||||
ip_address=""
|
||||
ip_address = ""
|
||||
for interface in netifaces.interfaces():
|
||||
if(str(interface) != "lo"):
|
||||
ip_address+=str(netifaces.ifaddresses(interface)[netifaces.AF_INET])
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue