fixing various bugs

This commit is contained in:
deflax 2016-04-01 02:37:21 +03:00
parent 24eb11bef3
commit 73e1eba43a
3 changed files with 37 additions and 16 deletions

View file

@ -21,15 +21,17 @@ def addclient(vmid, vmname, clientid, clientname, clientemail, vmpass):
ioconfig.logger.info('client[{}]> does not exist. creating...'.format(clientid)) ioconfig.logger.info('client[{}]> does not exist. creating...'.format(clientid))
#generate password and send it to the client #generate password and send it to the client
newpass = utils.genpassword(30) newpass = utils.genpassword(30)
ioconfig.logger.info('client[{}]> initial password is: {}'.format(clientid, newpass))
salt = bcrypt.gensalt() salt = bcrypt.gensalt()
b_newpass = newpass.encode('utf-8') b_newpass = newpass.encode('utf-8')
encpasswd = bcrypt.hashpw(b_newpass, salt).decode('utf-8') encpasswd = bcrypt.hashpw(b_newpass, salt).decode('utf-8')
vcard = { 'name':str(clientname), 'email':str(clientemail), 'encpasswd':str(encpasswd) } vcard = { 'name':str(clientname), 'email':str(clientemail), 'encpasswd':str(encpasswd) }
newclient = { str(clientid):vcard } newclient = { str(clientid):vcard }
clientsdb.update(newclient) clientsdb.update(newclient)
#TODO: 1. Send initial email to the user as we will use the internal auth from now on. #Send initial email to the user as we will use the internal auth from now on.
#TODO: 2. Sync with proxmaster-admin database (shell command could be used for this one) ###utils.sendmail(clientemail, 'W{} logged in.'.format)
ioconfig.logger.info('client[{}]> vmid {} is now owned by {} ({})'.format(clientemail, vmid, clientid, clientname)) #TODO: Sync with proxmaster-admin database (shell command could be used for this one)
ioconfig.logger.info('client[{}]> vmid {} is now owned by {} ({})'.format(clientid, vmid, clientemail, clientname))
#create initial vm template #create initial vm template
vmdata = { 'hostname':str(vmname), 'vmid':str(vmid), 'ownerid':str(clientid) } vmdata = { 'hostname':str(vmname), 'vmid':str(vmid), 'ownerid':str(clientid) }
@ -49,16 +51,16 @@ def setencpasswd(clientemail, newpass):
c_id = str(path[0]) c_id = str(path[0])
#check the returned path with forward query #check the returned path with forward query
query = clientsdb[c_id]['email'] query = clientsdb[c_id]['email']
ioconfig.logger.info('client[{}]> found. path={}'.format(clientemail, str(path))) #ioconfig.logger.info('client[{}]> path={}'.format(c_id, str(path)))
except: except:
raise raise
if query != clientemail: if query != clientemail:
ioconfig.logger.critical('clients.db> test query returns different vmname! check clients db for consistency!') ioconfig.logger.critical('clients> test query returns different vmname! check clients db for consistency!')
raise raise
else: else:
clientsdb[c_id]['encpasswd'] = encpasswd clientsdb[c_id]['encpasswd'] = encpasswd
ioconfig.logger.info('client[{}]> password changed!'.format(clientemail)) ioconfig.logger.info('client[{}]> {} password changed!'.format(c_id, clientemail))
writeclientsdb(clientsdb) writeclientsdb(clientsdb)
#TODO: Send new email to the client to notify the password change. This time sending the password in plain text is not needed. #TODO: Send new email to the client to notify the password change. This time sending the password in plain text is not needed.
@ -71,11 +73,10 @@ def validate(clientemail, password):
clientsdb = readclientsdb() clientsdb = readclientsdb()
path = utils.get_path(clientsdb, clientemail) path = utils.get_path(clientsdb, clientemail)
c_id = str(path[0]) c_id = str(path[0])
#check the returned path with forward query #ioconfig.logger.info('client[{}]> path={}'.format(c_id, str(path)))
ioconfig.logger.info('client[{}]> found. path={}'.format(clientemail, str(path)))
except: except:
raise raise
ioconfig.logger.warning('clients.db> {} was not found in the database!'.format(clientemail)) ioconfig.logger.warning('clients> {} was not found in the database!'.format(clientemail))
#log bad ips here... #log bad ips here...
return False return False
@ -86,12 +87,12 @@ def validate(clientemail, password):
if (hmac.compare_digest(bcrypt.hashpw(b_srvpass, b_encpass), b_encpass)): if (hmac.compare_digest(bcrypt.hashpw(b_srvpass, b_encpass), b_encpass)):
#login successful #login successful
ioconfig.logger.info('client[{}]> logged in successfully'.format(clientemail)) ioconfig.logger.info('client[{}]> {} logged in successfully'.format(c_id, clientemail))
#TODO: Notify admin #TODO: Notify admin
#3. generate vmlist to return the owned ids to the client. #3. generate vmlist to return the owned ids to the client.
return clientvms(clientsdb[c_id]) return clientvms(clientsdb[c_id])
else: else:
ioconfig.logger.warning('clients> {} ACCESS DENIED!'.format(vmid)) ioconfig.logger.warning('clients> {} ACCESS DENIED!'.format(clientemail))
#cant compare password #cant compare password
#TODO: Log attempts and block. #TODO: Log attempts and block.
return {} return {}
@ -102,8 +103,9 @@ def clientvms(vmlist):
#clear unused objects. perhaps there is a better way to do this but im kinda anxious today... #clear unused objects. perhaps there is a better way to do this but im kinda anxious today...
vmlist.pop('name') vmlist.pop('name')
vmlist.pop('email') vmlist.pop('email')
vmlist.pop('encpasswd')
response = [] response = {}
for vmid,data in vmlist.items(): for vmid,data in vmlist.items():
response = { str(vmid):data } response = { str(vmid):data }
return response return response
@ -120,7 +122,7 @@ def vmowner(vmid, vmname, verbose):
clientid = '0' #unknown owner clientid = '0' #unknown owner
clientname = 'unknown' clientname = 'unknown'
if verbose: if verbose:
ioconfig.logger.info('clients> the owner of ' + str(vmid) + ' (' + vmname + ') is ' + str(clientid) + ' (' + clientname + ')') ioconfig.logger.info('client[{}]> {} is the owner of {} ({})'.fotmat(str(clientid), clientname, str(vmid), vmname))
return clientid return clientid
@ -145,5 +147,7 @@ def writeclientsdb(clientsdb):
if __name__ == '__main__': if __name__ == '__main__':
#setencpasswd('abc@xyz.com', 'todos')
setencpasswd('daniel@deflax.net', 'todos') setencpasswd('daniel@deflax.net', 'todos')
print(validate('daniel@deflax.net', 'todos')) print(validate('daniel@deflax.net', 'todos'))

View file

@ -50,7 +50,7 @@ def sync(cached=True):
real_slave = { "id":slave_id, "slave":slave_name, "masterip":slave_masterip, "password":decoded_password } real_slave = { "id":slave_id, "slave":slave_name, "masterip":slave_masterip, "password":decoded_password }
optional_slave = {} optional_slave = {}
cache_file = 'cache-slave-' + slave_id + '.json' cache_file = 'cache-slave-' + slave_id + '.json'
prefix = 'cache> [' + slave_id + '] ' prefix = 'cache> [' + slave_name + '] '
# check if slave is in current region and include it in current dict if it is # check if slave is in current region and include it in current dict if it is
if slave_regionid == region_id: if slave_regionid == region_id:
@ -168,7 +168,7 @@ def query_region(region_name):
for region in all_regions: for region in all_regions:
if grid_data[region]['region'] == region_name: if grid_data[region]['region'] == region_name:
logger.info('region[{}]> found: id={}'.format(region_name, region)) logger.info('region[{}]> region id={}'.format(region_name, region))
return grid_data[region]['id'] return grid_data[region]['id']
break break
logger.error('grid> cant find region ' + region_name) logger.error('grid> cant find region ' + region_name)
@ -342,7 +342,7 @@ def query_vm(req_vmid):
logger.error('vm[{}]> type is unknown!'.format(vm_id)) logger.error('vm[{}]> type is unknown!'.format(vm_id))
raise raise
logger.info('vm[{}]> type {} found. path={} '.format(target, vm_type, str(path))) logger.info('vm[{}]> type={} path={}'.format(target, vm_type, str(path)))
return slave_id, vm_type return slave_id, vm_type

View file

@ -89,3 +89,20 @@ def chained_get(dct, *keys):
return functools.reduce(getter, keys, dct) return functools.reduce(getter, keys, dct)
def sendmail(FROM,TO,SUBJECT,TEXT,SERVER):
import smtplib
"""this is some test documentation in the function"""
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP(SERVER)
"New part"
server.starttls()
#server.login('username', 'password')
server.sendmail(FROM, TO, message)
server.quit()