Improoved the update functionality:
authorBarthaM@N3SIM1218.D03.arc.local
Fri, 29 Aug 2014 10:56:26 +0100
changeset 2199480e5ba1a82
parent 218 327f282364b9
child 220 f5805ee62d80
Improoved the update functionality:
- Additional validation of template existance
- Faster termination of worker threads
- Forced template folder cleanup
- etc.
OpenSecurity/bin/cygwin.py
OpenSecurity/bin/opensecurityd.pyw
OpenSecurity/bin/test_vmmanager.pyw
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/cygwin.py	Fri Aug 22 09:35:02 2014 +0100
     1.2 +++ b/OpenSecurity/bin/cygwin.py	Fri Aug 29 10:56:26 2014 +0100
     1.3 @@ -72,6 +72,7 @@
     1.4      theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
     1.5      #theClass.user_home = os.path.expanduser("~")
     1.6      theClass.user_home = os.environ['APPDATA']#os.path.expandvars("%APPDATA%")
     1.7 +    theClass.allow_exec = True 
     1.8      return theClass
     1.9  
    1.10              
    1.11 @@ -87,6 +88,7 @@
    1.12      vbox_man = ''
    1.13      win_cmd = ''
    1.14      user_home = ''
    1.15 +    allow_exec = True 
    1.16      """Some nifty methods working with Cygwin"""
    1.17      
    1.18      def __call__(self, command, arguments, wait_return=True, window = False):
    1.19 @@ -140,10 +142,22 @@
    1.20      def home():
    1.21          return Cygwin.user_home
    1.22      
    1.23 +    @staticmethod
    1.24 +    def allowExec():
    1.25 +        Cygwin.allow_exec = True
    1.26 +    
    1.27 +    @staticmethod
    1.28 +    def denyExec():
    1.29 +        Cygwin.allow_exec = False
    1.30 +    
    1.31      executeLock = threading.Lock()
    1.32      #executes command on host system
    1.33      @staticmethod
    1.34      def execute(program, arguments, wait_return=True, window = False, stdin = PIPE, stdout = PIPE, stderr = PIPE):
    1.35 +        if not Cygwin.allow_exec:
    1.36 +            logger.error('Execution cancelled by system (shutting down).')
    1.37 +            raise OpenSecurityException('Execution cancelled by system (shutting down).')
    1.38 +            
    1.39          _startupinfo = STARTUPINFO()
    1.40          if not window:
    1.41              _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
    1.42 @@ -254,13 +268,6 @@
    1.43          cmd = 'cygpath -u \'' + path + '\''
    1.44          return Cygwin.bashExecute(cmd)[1].rstrip('\n')
    1.45      
    1.46 -    @staticmethod
    1.47 -    def checkResult(result):
    1.48 -        #if result[0] != 0:
    1.49 -        #    logger.error('Command failed:' + ''.join(result[2]))
    1.50 -        #    raise OpenSecurityException('Command failed:' + ''.join(result[2]))
    1.51 -        return result
    1.52 -                
    1.53  # start
    1.54  import os
    1.55  import win32api
     2.1 --- a/OpenSecurity/bin/opensecurityd.pyw	Fri Aug 22 09:35:02 2014 +0100
     2.2 +++ b/OpenSecurity/bin/opensecurityd.pyw	Fri Aug 29 10:56:26 2014 +0100
     2.3 @@ -155,12 +155,13 @@
     2.4              tmplateUUID = gvm_mgr.getTemplateUUID()
     2.5              if tmplateUUID != None:
     2.6                  logger.debug('found parent uuid ' + tmplateUUID)
     2.7 -                gvm_mgr.storageDetach(gvm_mgr.vmRootName)
     2.8 +                gvm_mgr.detachStorage(gvm_mgr.vmRootName)
     2.9                  gvm_mgr.removeSnapshots(tmplateUUID)
    2.10                  gvm_mgr.removeImage(tmplateUUID)
    2.11              else:
    2.12                  logger.debug('parent uuid not found')
    2.13              gvm_mgr.removeVM(gvm_mgr.vmRootName)
    2.14 +        gvm_mgr.removeVMFolder(gvm_mgr.vmRootName)
    2.15          
    2.16          trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_initial_import.log')
    2.17          trace_file = open(trace_file_name, 'w+')
    2.18 @@ -168,7 +169,7 @@
    2.19          vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova'
    2.20          initial_import_script = Cygwin.cygPath(os.path.abspath(os.path.join(os.path.split(__file__)[0], 'initial_vm.sh')))
    2.21          Cygwin.bashExecute('\\"' + initial_import_script + '\\" \'' + vm_image + '\'', wait_return = False, stdout = trace_file, stderr = trace_file) 
    2.22 -
    2.23 +        gvm_mgr.start()
    2.24          res = '{ "init_log": "' + trace_file_name.replace('\\', '\\\\') + '" }'
    2.25          return res
    2.26  
     3.1 --- a/OpenSecurity/bin/test_vmmanager.pyw	Fri Aug 22 09:35:02 2014 +0100
     3.2 +++ b/OpenSecurity/bin/test_vmmanager.pyw	Fri Aug 29 10:56:26 2014 +0100
     3.3 @@ -201,7 +201,7 @@
     3.4      #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
     3.5      
     3.6      #man.stopVM('SecurityDVM')
     3.7 -    #man.storageDetach('SecurityDVM')
     3.8 +    #man.detachStorage('SecurityDVM')
     3.9      #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
    3.10      #man.storageAttach('SecurityDVM')
    3.11      
     4.1 --- a/OpenSecurity/bin/vmmanager.pyw	Fri Aug 22 09:35:02 2014 +0100
     4.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Fri Aug 29 10:56:26 2014 +0100
     4.3 @@ -112,7 +112,6 @@
     4.4      browsingManager = None
     4.5      blacklistedRSD = None
     4.6      status_message = 'Starting up...'
     4.7 -
     4.8   
     4.9      def __init__(self):
    4.10          # only proceed if we have a working background environment
    4.11 @@ -202,8 +201,15 @@
    4.12          
    4.13          return True
    4.14  
    4.15 +    def template_installed(self):
    4.16 +        """ check if we do have our root VMs installed """
    4.17 +        vms = self.listVM()
    4.18 +        if not self.vmRootName in vms:
    4.19 +            self.status_message = 'Unable to locate root SecurityDVM. Please download and setup the initial image.'
    4.20 +            return False
    4.21 +        return True
    4.22 +        
    4.23      def backend_ok(self):
    4.24 -
    4.25          """check if the backend (VirtualBox) is sufficient for our task"""
    4.26  
    4.27          # ensure we have our system props
    4.28 @@ -221,20 +227,16 @@
    4.29              self.status_message = 'Unsure if suitable extension pack is installed. Please install the "Oracle VM VirtualBox Extension Pack" from https://www.virtualbox.org/wiki/Downloads.'
    4.30              return False
    4.31  
    4.32 -        # check if we do have our root VMs installed
    4.33 -        vms = self.listVM()
    4.34 -        if not self.vmRootName in vms:
    4.35 -            self.status_message = 'Unable to locate root SecurityDVM. Please download and setup the initial image.'
    4.36 +        # check the existing hostOnly network settings and try to reconfigure if faulty
    4.37 +        if not self.verifyHostOnlySettings():
    4.38              return False
    4.39 -
    4.40 +        
    4.41          # basically all seems nice and ready to rumble
    4.42          self.status_message = 'All is ok.'
    4.43 -
    4.44 -        self.verifyHostOnlySettings()
    4.45 -        
    4.46          return True
    4.47      
    4.48      def stop(self):
    4.49 +        Cygwin.denyExec()
    4.50          if self.rsdHandler != None:
    4.51              self.rsdHandler.stop()
    4.52              self.rsdHandler.join()
    4.53 @@ -244,17 +246,21 @@
    4.54              self.browsingManager.stop()
    4.55              self.browsingManager.join()
    4.56              self.browsingManager = None
    4.57 +        Cygwin.allowExec()
    4.58      
    4.59      def start(self):
    4.60          self.stop()
    4.61 -        self.browsingManager = BrowsingManager(self)
    4.62 -        self.browsingManager.start()
    4.63 -        self.rsdHandler = DeviceHandler(self)
    4.64 -        self.rsdHandler.start()
    4.65 +        Cygwin.allowExec()
    4.66 +        if self.backend_ok() and self.template_installed():
    4.67 +            self.browsingManager = BrowsingManager(self)
    4.68 +            self.browsingManager.start()
    4.69 +            self.rsdHandler = DeviceHandler(self)
    4.70 +            self.rsdHandler.start()
    4.71          
    4.72  
    4.73      def cleanup(self):
    4.74          self.stop()
    4.75 +        Cygwin.allowExec()
    4.76          ip = self.getHostOnlyIP(None)
    4.77          try:
    4.78              result = urllib2.urlopen('http://127.0.0.1:8090/netcleanup?'+'hostonly_ip='+ip).readline()
    4.79 @@ -320,14 +326,25 @@
    4.80      # check if the device is mass storage type
    4.81      @staticmethod
    4.82      def isMassStorageDevice(device):
    4.83 -        keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid
    4.84 -        key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname)
    4.85 -        devinfokeyname = win32api.RegEnumKey(key, 0)
    4.86 -        win32api.RegCloseKey(key)
    4.87 -
    4.88 -        devinfokey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname)
    4.89 -        value = win32api.RegQueryValueEx(devinfokey, 'SERVICE')[0]
    4.90 -        win32api.RegCloseKey(devinfokey)
    4.91 +        vidkey = None
    4.92 +        devinfokey = None
    4.93 +        value = ""
    4.94 +        try:
    4.95 +            keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid
    4.96 +            vidkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname)
    4.97 +            devinfokeyname = win32api.RegEnumKey(vidkey, 0)
    4.98 +            win32api.RegCloseKey(vidkey)
    4.99 +    
   4.100 +            devinfokey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname)
   4.101 +            value = win32api.RegQueryValueEx(devinfokey, 'SERVICE')[0]
   4.102 +            win32api.RegCloseKey(devinfokey)
   4.103 +        except Exception as ex:
   4.104 +            logger.error('Error reading registry.Exception details: %s' %ex)
   4.105 +        finally:
   4.106 +            if vidkey is not None:
   4.107 +                win32api.RegCloseKey(vidkey)
   4.108 +            if devinfokey is not None:
   4.109 +                win32api.RegCloseKey(devinfokey)
   4.110          
   4.111          return 'USBSTOR' in value
   4.112      
   4.113 @@ -498,8 +515,6 @@
   4.114          self.changeStorageType(template_storage,'immutable')
   4.115          self.attachStorage(self.vmRootName)
   4.116          
   4.117 -        #self.start()
   4.118 -
   4.119      #"SATA-0-0"="C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\Snapshots\{d0af827d-f13a-49be-8ac1-df20b13bda83}.vmdk"
   4.120      #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83"
   4.121      @staticmethod    
   4.122 @@ -918,10 +933,8 @@
   4.123                  self.started.set()
   4.124                  logger.info("Browsing SDVM running.")
   4.125                  self.restart.wait()
   4.126 -            except OpenSecurityException, e:
   4.127 -                logger.error(''.join(e))
   4.128 -            except:
   4.129 -                logger.error("Unexpected error: " + sys.exc_info()[0])
   4.130 +            except Exception as e:
   4.131 +                logger.error("Unexpected error: ".join(e))
   4.132                  logger.error("BrowsingHandler failed. Cleaning up")
   4.133                  #self.running= False
   4.134                  
   4.135 @@ -938,7 +951,6 @@
   4.136          self.running = False
   4.137          
   4.138      def run(self):
   4.139 -        
   4.140          self.existingRSDs = dict()
   4.141          self.attachedRSDs = self.vmm.getAttachedRSDs()
   4.142