Made to use random passwords for creating opennebula users and managing their VMs.

This commit is contained in:
M.Ravi 2017-04-23 22:46:59 +05:30
parent a427fa8579
commit d10285e23d
2 changed files with 10 additions and 2 deletions

View file

@ -9,6 +9,8 @@ from django import template
from django.shortcuts import redirect from django.shortcuts import redirect
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.signals import user_logged_in from django.contrib.auth.signals import user_logged_in
import random
import string
import oca import oca
import socket import socket
@ -117,7 +119,7 @@ class HostingManageVMAdmin(admin.ModelAdmin):
# Function to initialize opennebula client based on the logged in # Function to initialize opennebula client based on the logged in
# user # user
def init_opennebula_client(self, opennebula_user): def init_opennebula_client(self, opennebula_user):
opennebula_user_password = 'a' opennebula_user_password = get_random_password()
self.client = oca.Client(opennebula_user + ':' + opennebula_user_password, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT) self.client = oca.Client(opennebula_user + ':' + opennebula_user_password, settings.OPENNEBULA_PROTOCOL + '://' + settings.OPENNEBULA_DOMAIN + ':' + settings.OPENNEBULA_PORT + settings.OPENNEBULA_ENDPOINT)
# Function that lists the VMs of the current user # Function that lists the VMs of the current user
@ -241,7 +243,7 @@ def user_logged_in_callback(sender, request, user, **kwargs):
# #
# 3. We user the user's email as the user name. # 3. We user the user's email as the user name.
if find_opennebula_user_by_name(str(user.email), client) == -1 : if find_opennebula_user_by_name(str(user.email), client) == -1 :
user_id = client.call('user.allocate', str(user.email), 'a', 'dummy') user_id = client.call('user.allocate', str(user.email), get_random_password(), 'dummy')
print("User " + str(user.email) + " does not exist. Created the user. User id = " + str(user_id)) print("User " + str(user.email) + " does not exist. Created the user. User id = " + str(user_id))
# Finds if an OpenNebula user with user_name exists. Returns the # Finds if an OpenNebula user with user_name exists. Returns the
@ -253,6 +255,10 @@ def find_opennebula_user_by_name(user_name, client):
if user.name == user_name : if user.name == user_name :
return user return user
return -1 return -1
# Returns random password that is needed by OpenNebula
def get_random_password():
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(20))
user_logged_in.connect(user_logged_in_callback) user_logged_in.connect(user_logged_in_callback)

View file

@ -46,5 +46,7 @@
</tbody> </tbody>
</table> </table>
</section> </section>
{% else %}
<h4>You do not have any VM.</h4>
{% endif %} {% endif %}
{% endblock %} {% endblock %}