From f94ed011e5fdc6dbf0eedecc8adedf052e0f2f3d Mon Sep 17 00:00:00 2001 From: PCoder Date: Wed, 13 Sep 2017 05:30:05 +0530 Subject: [PATCH] Remove keys from known_hosts on delete of a vm --- hosting/views.py | 54 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/hosting/views.py b/hosting/views.py index 09e45371..012040f4 100644 --- a/hosting/views.py +++ b/hosting/views.py @@ -1,4 +1,5 @@ import logging +import subprocess import uuid from django.conf import settings @@ -18,10 +19,10 @@ from django.views.generic import View, CreateView, FormView, ListView, \ DetailView, \ DeleteView, TemplateView, UpdateView from guardian.mixins import PermissionRequiredMixin -from oca.pool import WrongNameError, WrongIdError -from stored_messages.settings import stored_messages_settings -from stored_messages.models import Message +from oca.pool import WrongIdError from stored_messages.api import mark_read +from stored_messages.models import Message +from stored_messages.settings import stored_messages_settings from membership.models import CustomUser, StripeCustomer from opennebula_api.models import OpenNebulaManager @@ -981,6 +982,53 @@ class VirtualMachineView(LoginRequiredMixin, View): 'VM_ID': opennebula_vm_id} ) + # Remove all keys belonging to the IP(s) + # ssh-keygen -R ip_address + if vm_data.ipv4 is not None: + if ', ' in vm_data.ipv4: + vm_ips = vm_data.ipv4.split(', ') + for ip_address in vm_ips: + try: + subprocess.check_output( + ['ssh-keygen', '-R', ip_address]) + except subprocess.CalledProcessError as cpe: + logger.debug( + """Could not remove key belonging to {ip}. + Error details: {details}""".format(ip=ip_address, + details=str( + cpe))) + else: + try: + subprocess.check_output( + ['ssh-keygen', '-R', vm_data.ipv4]) + except subprocess.CalledProcessError as cpe: + logger.debug( + """Could not remove key belonging to {ip}. + Error details: {details}""".format(ip=vm_data.ipv4, + details=str(cpe))) + if vm_data.ipv6 is not None: + if ', ' in vm_data.ipv6: + vm_ips = vm_data.ipv6.split(', ') + for ip_address in vm_ips: + try: + subprocess.check_output( + ['ssh-keygen', '-R', ip_address]) + except subprocess.CalledProcessError as cpe: + logger.debug( + """Could not remove key belonging to {ip}. + Error details: {details}""".format(ip=ip_address, + details=str( + cpe))) + else: + try: + subprocess.check_output( + ['ssh-keygen', '-R', vm_data.ipv6]) + except subprocess.CalledProcessError as cpe: + logger.debug( + """Could not remove key belonging to {ip}. + Error details: {details}""".format(ip=vm_data.ipv6, + details=str(cpe))) + return HttpResponseRedirect(self.get_success_url())