Ejemplo n.º 1
0
    def decrypt(self, value: typing.Union[str, bytes]) -> str:
        if isinstance(value, str):
            value = value.encode('utf-8')

        data: bytes = typing.cast(bytes, encoders.decode(value, 'base64'))
        decrypted: bytes

        try:
            # First, try new "cryptografy" decrpypting
            decrypted = self._rsa.decrypt(
                data,
                padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                             algorithm=hashes.SHA256(),
                             label=None))
        except Exception:  # If fails, try old method
            try:
                atfork()
                decrypted = self._oldRsa.decrypt(
                    encoders.decode(value, 'base64'))
                return decrypted.decode()
            except Exception:
                logger.exception('Decripting: %s', value)
                # logger.error(inspect.stack())
                return 'decript error'
        # logger.debug('Decripted: %s %s', data, decrypted)
        return decrypted.decode()
Ejemplo n.º 2
0
 def unmarshal(self, data):
     if data == b'':  # Can be empty
         return
     # We keep original data (maybe incomplete)
     try:
         data = encoders.decode(data, 'bz2')
     except Exception:  # With old zip encoding
         data = encoders.decode(data, 'zip')
     for pair in data.split(b'\2'):
         k, v = pair.split(b'\1')
         self.dict[k] = pickle.loads(v)
Ejemplo n.º 3
0
 def unmarshal(self, data):
     if data == b'':  # Can be empty
         return
     # We keep original data (maybe incomplete)
     try:
         data = encoders.decode(data, 'bz2')
     except Exception:  # With old zip encoding
         data = encoders.decode(data, 'zip')
     for pair in data.split(b'\2'):
         k, v = pair.split(b'\1')
         self.dict[k] = pickle.loads(v)
Ejemplo n.º 4
0
    def data(self):
        try:
            return encoders.decode(encoders.decode(self.content, 'base64'), 'zip')
        except Exception:
            logger.error('DBFile {} has errors and cannot be used'.format(self.name))
            try:
                self.delete()  # Autodelete, invalid...
                pass
            except Exception:
                logger.error('Could not even delete {}!!'.format(self.name))

            return ''
Ejemplo n.º 5
0
    def data(self):
        try:
            return encoders.decode(encoders.decode(self.content, 'base64'),
                                   'zip')
        except Exception:
            logger.error('DBFile %s has errors and cannot be used', self.name)
            try:
                self.delete()  # Autodelete, invalid...
            except Exception:
                logger.error('Could not even delete %s!!', self.name)

            return ''
Ejemplo n.º 6
0
 def unmarshal(self, data):
     if data == b'':  # Can be empty
         return
     # We keep original data (maybe incomplete)
     try:
         data = encoders.decode(data, 'bz2')
     except Exception:  # With old zip encoding
         data = encoders.decode(data, 'zip')
     # logger.debug('DATA: %s', data)
     for pair in data.split(b'\2'):
         k, v = pair.split(b'\1')
         # logger.debug('k: %s  ---   v: %s', k, v)
         self.dict[k.decode('utf8')] = pickle.loads(v)
Ejemplo n.º 7
0
    def AESDecrypt(self, text: bytes, key: bytes, base64: bool = False) -> bytes:
        if base64:
            text = encoders.decode(text, 'base64')

        cipher = AES.new(CryptoManager.AESKey(key, 16), AES.MODE_CBC, 'udsinitvectoruds')
        toDecode = cipher.decrypt(text)
        return toDecode[4:4 + struct.unpack('>i', toDecode[:4])[0]]
Ejemplo n.º 8
0
def getServicesPoolsCounters(servicePool, counter_type):
    # pylint: disable=no-value-for-parameter
    try:
        cacheKey = (servicePool and servicePool.id or 'all') + str(counter_type) + str(POINTS) + str(SINCE)
        to = getSqlDatetime()
        since = to - timedelta(days=SINCE)
        val = cache.get(cacheKey)
        if val is None:
            if servicePool is None:
                us = ServicePool()
                complete = True  # Get all deployed services stats
            else:
                us = servicePool
                complete = False
            val = []
            for x in counters.getCounters(us, counter_type, since=since, to=to, limit=POINTS, use_max=USE_MAX, all=complete):
                val.append({'stamp': x[0], 'value': int(x[1])})
            if len(val) > 2:
                cache.put(cacheKey, encoders.encode(pickle.dumps(val), 'zip') , 600)
            else:
                val = [{'stamp': since, 'value': 0}, {'stamp': to, 'value': 0}]
        else:
            val = pickle.loads(encoders.decode(val, 'zip'))

        return val
    except:
        logger.exception('exception')
        raise ResponseError('can\'t create stats for objects!!!')
Ejemplo n.º 9
0
    def unmarshal(self, s):
        data = s.decode('utf8').split('\t')
        if data[0] in ('v1', 'v2', 'v3', 'v4'):
            self._domain = data[1]
            self._ou = data[2]
            self._account = data[3]
            self._password = CryptoManager.manager().decrypt(data[4])

        if data[0] in ('v2', 'v3', 'v4'):
            self._group = data[6]
        else:
            self._group = ''

        if data[0] in ('v3', 'v4'):
            self._serverHint = data[7]
        else:
            self._serverHint = ''
            
        if data[0] == 'v4':
            self._ssl = data[8]
            self._removeOnExit = data[9]
        else:
            self._ssl = 'n'
            self._removeOnExit = 'y'

        super(WinDomainOsManager, self).unmarshal(encoders.decode(data[5], 'hex'))
Ejemplo n.º 10
0
    def unmarshal(self, data: bytes):
        if not data:  # Can be empty
            return
        # We keep original data (maybe incomplete)
        try:
            data = typing.cast(bytes, encoders.decode(data, 'bz2'))
        except Exception:  # With old zip encoding
            data = typing.cast(bytes, encoders.decode(data, 'zip'))
        # logger.debug('DATA: %s', data)
        for pair in data.split(b'\2'):
            k, v = pair.split(b'\1')
            # logger.debug('k: %s  ---   v: %s', k, v)
            self.attrs[k.decode('utf8')] = pickle.loads(v)

        for k, v in self.attrs.items():
            logger.debug('Marshall Autoattributes: %s=%s', k, v.getValue())
Ejemplo n.º 11
0
    def executeOneDelayedTask(self):
        now = getSqlDatetime()
        filt = Q(execution_time__lt=now) | Q(insert_date__gt=now + timedelta(seconds=30))
        # If next execution is before now or last execution is in the future (clock changed on this server, we take that task as executable)
        try:
            with transaction.atomic():  # Encloses
                # Throws exception if no delayed task is avilable
                task = dbDelayedTask.objects.select_for_update().filter(filt).order_by('execution_time')[0]  # @UndefinedVariable
                if task.insert_date > now + timedelta(seconds=30):
                    logger.warning('EXecuted {} due to insert_date being in the future!'.format(task.type))
                taskInstanceDump = encoders.decode(task.instance, 'base64')
                task.delete()
            taskInstance = loads(taskInstanceDump)
        except IndexError:
            return  # No problem, there is no waiting delayed task
        except Exception:
            # Transaction have been rolled back using the "with atomic", so here just return
            # Note that is taskInstance can't be loaded, this task will not be retried
            logger.exception('Executing one task')
            return

        if taskInstance is not None:
            logger.debug('Executing delayedTask:>{0}<'.format(task))
            taskInstance.env = Environment.getEnvForType(taskInstance.__class__)
            DelayedTaskThread(taskInstance).start()
Ejemplo n.º 12
0
 def get(self, skey, defValue=None):
     now = getSqlDatetime()
     logger.debug('Requesting key "{}" for cache "{}"'.format(
         skey, self._owner))
     try:
         key = self.__getKey(skey)
         logger.debug('Key: {}'.format(key))
         c = uds.models.Cache.objects.get(pk=key)  # @UndefinedVariable
         expired = now > c.created + timedelta(seconds=c.validity)
         if expired:
             return defValue
         try:
             logger.debug('value: {}'.format(c.value))
             val = pickle.loads(encoders.decode(c.value, 'base64'))
         except Exception:  # If invalid, simple do no tuse it
             logger.exception('Invalid pickle from cache')
             c.delete()
             return defValue
         Cache.hits += 1
         return val
     except uds.models.Cache.DoesNotExist:  # @UndefinedVariable
         Cache.misses += 1
         logger.debug('key not found: {}'.format(skey))
         return defValue
     except Exception as e:
         Cache.misses += 1
         logger.debug('Cache inaccesible: {}:{}'.format(skey, e))
         return defValue
Ejemplo n.º 13
0
 def unmarshal(self, data: bytes) -> None:
     values = data.decode('utf8').split('\t')
     if values[0] == 'v1':
         self._userAccount = values[1]
         self._password = cryptoManager().decrypt(values[2])
         super().unmarshal(
             typing.cast(bytes, encoders.decode(values[3], 'hex')))
Ejemplo n.º 14
0
    def unmarshal(self, data: bytes) -> None:
        values = data.decode('utf8').split('\t')
        if values[0] in ('v1', 'v2', 'v3', 'v4'):
            self._domain = values[1]
            self._ou = values[2]
            self._account = values[3]
            self._password = cryptoManager().decrypt(values[4])

        if values[0] in ('v2', 'v3', 'v4'):
            self._group = values[6]
        else:
            self._group = ''

        if values[0] in ('v3', 'v4'):
            self._serverHint = values[7]
        else:
            self._serverHint = ''

        if values[0] == 'v4':
            self._ssl = values[8]
            self._removeOnExit = values[9]
        else:
            self._ssl = 'n'
            self._removeOnExit = 'y'

        super().unmarshal(typing.cast(bytes, encoders.decode(values[5], 'hex')))
Ejemplo n.º 15
0
    def executeOneDelayedTask(self) -> None:
        now = getSqlDatetime()
        filt = Q(execution_time__lt=now) | Q(insert_date__gt=now +
                                             timedelta(seconds=30))
        # If next execution is before now or last execution is in the future (clock changed on this server, we take that task as executable)
        try:
            with transaction.atomic():  # Encloses
                # Throws exception if no delayed task is avilable
                task = DBDelayedTask.objects.select_for_update().filter(
                    filt).order_by('execution_time')[0]  # @UndefinedVariable
                if task.insert_date > now + timedelta(seconds=30):
                    logger.warning(
                        'EXecuted %s due to insert_date being in the future!',
                        task.type)
                taskInstanceDump = encoders.decode(task.instance, 'base64')
                task.delete()
            taskInstance = pickle.loads(taskInstanceDump)
        except IndexError:
            return  # No problem, there is no waiting delayed task
        except Exception:
            # Transaction have been rolled back using the "with atomic", so here just return
            # Note that is taskInstance can't be loaded, this task will not be retried
            logger.exception('Executing one task')
            return

        if taskInstance:
            logger.debug('Executing delayedTask:>%s<', task)
            taskInstance.env = Environment.getEnvForType(
                taskInstance.__class__)
            DelayedTaskThread(taskInstance).start()
Ejemplo n.º 16
0
    def get(self,
            skey: typing.Union[str, bytes],
            defValue: typing.Any = None) -> typing.Any:
        now: datetime = typing.cast(datetime, getSqlDatetime())
        # logger.debug('Requesting key "%s" for cache "%s"', skey, self._owner)
        try:
            key = self.__getKey(skey)
            # logger.debug('Key: %s', key)
            c: DBCache = DBCache.objects.get(pk=key)  # @UndefinedVariable
            # If expired
            if now > c.created + timedelta(seconds=c.validity):
                return defValue

            try:
                # logger.debug('value: %s', c.value)
                val = pickle.loads(
                    typing.cast(bytes, encoders.decode(c.value, 'base64')))
            except Exception:  # If invalid, simple do no tuse it
                logger.exception('Invalid pickle from cache. Removing it.')
                c.delete()
                return defValue

            Cache.hits += 1
            return val
        except DBCache.DoesNotExist:  # @UndefinedVariable
            Cache.misses += 1
            # logger.debug('key not found: %s', skey)
            return defValue
        except Exception:
            Cache.misses += 1
            # logger.debug('Cache inaccesible: %s:%s', skey, e)
            return defValue
Ejemplo n.º 17
0
 def get(self, skey, defValue=None):
     now = getSqlDatetime()
     logger.debug('Requesting key "{}" for cache "{}"'.format(skey, self._owner))
     try:
         key = self.__getKey(skey)
         logger.debug('Key: {}'.format(key))
         c = uds.models.Cache.objects.get(pk=key)  # @UndefinedVariable
         expired = now > c.created + timedelta(seconds=c.validity)
         if expired:
             return defValue
         try:
             logger.debug('value: {}'.format(c.value))
             val = pickle.loads(encoders.decode(c.value, 'base64'))
         except Exception:  # If invalid, simple do no tuse it
             logger.exception('Invalid pickle from cache')
             c.delete()
             return defValue
         Cache.hits += 1
         return val
     except uds.models.Cache.DoesNotExist:  # @UndefinedVariable
         Cache.misses += 1
         logger.debug('key not found: {}'.format(skey))
         return defValue
     except Exception as e:
         Cache.misses += 1
         logger.debug('Cache inaccesible: {}:{}'.format(skey, e))
         return defValue
Ejemplo n.º 18
0
    def unserializeForm(self, values):
        '''
        This method unserializes the values previously obtained using
        :py:meth:`serializeForm`, and stores
        the valid values form form fileds inside its corresponding field
        '''
        if values == b'':  # Has nothing
            return

        try:
            # Set all values to defaults ones
            for k in six.iterkeys(self._gui):
                if self._gui[k].isType(gui.InputField.HIDDEN_TYPE) and self._gui[k].isSerializable() is False:
                    # logger.debug('Field {0} is not unserializable'.format(k))
                    continue
                self._gui[k].value = self._gui[k].defValue

            values = encoders.decode(values, 'zip', True)
            if values == '':  # Has nothing
                return

            for txt in values.split('\002'):
                k, v = txt.split('\003')
                if k in self._gui:
                    try:
                        if v[0] == '\001':
                            val = pickle.loads(v[1:].encode('utf-8'))
                        else:
                            val = v
                    except Exception:
                        val = ''
                    self._gui[k].value = val
                # logger.debug('Value for {0}:{1}'.format(k, val))
        except Exception:
            logger.exception('Exception on unserialization on {}'.format(self.__class__))
Ejemplo n.º 19
0
    def unmarshal(self, s):
        data = s.decode('utf8').split('\t')
        if data[0] in ('v1', 'v2', 'v3', 'v4'):
            self._domain = data[1]
            self._ou = data[2]
            self._account = data[3]
            self._password = CryptoManager.manager().decrypt(data[4])

        if data[0] in ('v2', 'v3', 'v4'):
            self._group = data[6]
        else:
            self._group = ''

        if data[0] in ('v3', 'v4'):
            self._serverHint = data[7]
        else:
            self._serverHint = ''

        if data[0] == 'v4':
            self._ssl = data[8]
            self._removeOnExit = data[9]
        else:
            self._ssl = 'n'
            self._removeOnExit = 'y'

        super(WinDomainOsManager,
              self).unmarshal(encoders.decode(data[5], 'hex'))
def transformTransports(apps, schema_editor):
    """
    Move serialization to a better model (it's time, the mode is there since 1.1 :) )
    """
    model = apps.get_model("uds", 'Transport')
    for t in model.objects.all():
        if t.data_type == RDPTransport.typeType:
            values = unmarshalRDP(encoders.decode(t.data, 'base64'))
            rdp = RDPTransport(Environment.getTempEnv(), values)
            t.data = rdp.serialize()
            t.save()

        if t.data_type == TRDPTransport.typeType:
            values = unmarshalTRDP(encoders.decode(t.data, 'base64'))
            rdp = TRDPTransport(Environment.getTempEnv(), values)
            t.data = rdp.serialize()
            t.save()
Ejemplo n.º 21
0
def transformTransports(apps, schema_editor):
    """
    Move serialization to a better model (it's time, the mode is there since 1.1 :) )
    """
    model = apps.get_model("uds", 'Transport')
    for t in model.objects.all():
        if t.data_type == RDPTransport.typeType:
            values = unmarshalRDP(encoders.decode(t.data, 'base64'))
            rdp = RDPTransport(Environment.getTempEnv(), values)
            t.data = rdp.serialize()
            t.save()

        if t.data_type == TRDPTransport.typeType:
            values = unmarshalTRDP(encoders.decode(t.data, 'base64'))
            rdp = TRDPTransport(Environment.getTempEnv(), values)
            t.data = rdp.serialize()
            t.save()
Ejemplo n.º 22
0
    def locateByAttr1(self, attr1):
        if isinstance(attr1, (list, tuple)):
            query = dbStorage.objects.filter(owner=self._owner, attr1_in=attr1)  # @UndefinedVariable
        else:
            query = dbStorage.objects.filter(owner=self._owner, attr1=attr1)  # @UndefinedVariable

        for v in query:
            yield encoders.decode(v.data, 'base64')
Ejemplo n.º 23
0
    def filter(self, attr1):
        if attr1 is None:
            query = dbStorage.objects.filter(owner=self._owner)  # @UndefinedVariable
        else:
            query = dbStorage.objects.filter(owner=self._owner, attr1=attr1)  # @UndefinedVariable

        for v in query:  # @UndefinedVariable
            yield (v.key, encoders.decode(v.data, 'base64'), v.attr1)
Ejemplo n.º 24
0
 def getPickleByAttr1(self, attr1):
     try:
         return pickle.loads(
             encoders.decode(
                 dbStorage.objects.get(owner=self._owner, attr1=attr1).data,
                 'base64'))  # @UndefinedVariable
     except Exception:
         return None
Ejemplo n.º 25
0
    def AESDecrypt(self, text, key, base64=False):
        if base64:
            text = encoders.decode(text, 'base64')

        cipher = AES.new(CryptoManager.AESKey(key, 16), AES.MODE_CBC, 'udsinitvectoruds')
        toDecode = cipher.decrypt(text)
        return toDecode[4:4 + struct.unpack('>i', toDecode[:4])[0]]

        return
Ejemplo n.º 26
0
    def filter(self, attr1):
        if attr1 is None:
            query = dbStorage.objects.filter(
                owner=self._owner)  # @UndefinedVariable
        else:
            query = dbStorage.objects.filter(owner=self._owner,
                                             attr1=attr1)  # @UndefinedVariable

        for v in query:  # @UndefinedVariable
            yield (v.key, encoders.decode(v.data, 'base64'), v.attr1)
Ejemplo n.º 27
0
    def locateByAttr1(self, attr1):
        if isinstance(attr1, (list, tuple)):
            query = dbStorage.objects.filter(
                owner=self._owner, attr1_in=attr1)  # @UndefinedVariable
        else:
            query = dbStorage.objects.filter(owner=self._owner,
                                             attr1=attr1)  # @UndefinedVariable

        for v in query:
            yield encoders.decode(v.data, 'base64')
Ejemplo n.º 28
0
 def decrypt(self, value):
     if isinstance(value, six.text_type):
         value = value.encode('utf-8')
     # import inspect
     try:
         atfork()
         return six.text_type(self._rsa.decrypt(encoders.decode(value, 'base64')).decode('utf-8'))
     except Exception:
         logger.exception('Decripting: {0}'.format(value))
         # logger.error(inspect.stack())
         return 'decript error'
Ejemplo n.º 29
0
 def getPickleByAttr1(self, attr1: str, forUpdate: bool = False):
     try:
         query = DBStorage.objects.filter(owner=self._owner, attr1=attr1)
         if forUpdate:
             query = query.select_for_update()
         return pickle.loads(
             typing.cast(bytes,
                         encoders.decode(query[0].data,
                                         'base64')))  # @UndefinedVariable
     except Exception:
         return None
Ejemplo n.º 30
0
 def decrypt(self, value: typing.Union[str, bytes]) -> str:
     if isinstance(value, str):
         value = value.encode('utf-8')
     # import inspect
     try:
         # atfork()
         return str(self._rsa.decrypt(encoders.decode(value, 'base64')).decode('utf-8'))
     except Exception:
         logger.exception('Decripting: %s', value)
         # logger.error(inspect.stack())
         return 'decript error'
Ejemplo n.º 31
0
    def locateByAttr1(
        self, attr1: typing.Union[typing.Iterable[str],
                                  str]) -> typing.Iterable[bytes]:
        if isinstance(attr1, str):
            query = DBStorage.objects.filter(owner=self._owner,
                                             attr1=attr1)  # @UndefinedVariable
        else:
            query = DBStorage.objects.filter(
                owner=self._owner, attr1_in=attr1)  # @UndefinedVariable

        for v in query:
            yield typing.cast(bytes, encoders.decode(v.data, 'base64'))
Ejemplo n.º 32
0
 def decrypt(self, value):
     if isinstance(value, six.text_type):
         value = value.encode('utf-8')
     # import inspect
     try:
         atfork()
         return six.text_type(
             self._rsa.decrypt(encoders.decode(value,
                                               'base64')).decode('utf-8'))
     except Exception:
         logger.exception('Decripting: {0}'.format(value))
         # logger.error(inspect.stack())
         return 'decript error'
Ejemplo n.º 33
0
    def AESDecrypt(self,
                   text: bytes,
                   key: bytes,
                   base64: bool = False) -> bytes:
        if base64:
            text = typing.cast(bytes, encoders.decode(text, 'base64'))

        cipher = Cipher(algorithms.AES(CryptoManager.AESKey(key, 16)),
                        modes.CBC(b'udsinitvectoruds'),
                        backend=default_backend())
        decryptor = cipher.decryptor()

        toDecode = decryptor.update(text) + decryptor.finalize()
        return toDecode[4:4 + struct.unpack('>i', toDecode[:4])[0]]
Ejemplo n.º 34
0
 def get(self, skey, defValue=None):
     now = getSqlDatetime()
     # logger.debug('Requesting key "%s" for cache "%s"' % (skey, self._owner,))
     try:
         key = self.__getKey(skey)
         c = uds.models.Cache.objects.get(pk=key)  # @UndefinedVariable
         expired = now > c.created + timedelta(seconds=c.validity)
         if expired:
             return defValue
         val = pickle.loads(encoders.decode(c.value, 'base64'))
         Cache.hits += 1
         return val
     except uds.models.Cache.DoesNotExist:  # @UndefinedVariable
         Cache.misses += 1
         logger.debug('key not found: {}'.format(skey))
         return defValue
Ejemplo n.º 35
0
    def readData(self, skey, fromPickle=False):
        try:
            key = self.__getKey(skey)
            logger.debug('Accesing to {0} {1}'.format(skey, key))
            c = dbStorage.objects.get(pk=key)  # @UndefinedVariable
            val = encoders.decode(c.data, 'base64')

            if fromPickle:
                return val

            try:
                return val.decode('utf-8')  # Tries to encode in utf-8
            except:
                return val
        except dbStorage.DoesNotExist:  # @UndefinedVariable
            logger.debug('key not found')
            return None
Ejemplo n.º 36
0
    def readData(self, skey, fromPickle=False):
        try:
            key = self.__getKey(skey)
            logger.debug('Accesing to {0} {1}'.format(skey, key))
            c = dbStorage.objects.get(pk=key)  # @UndefinedVariable
            val = encoders.decode(c.data, 'base64')

            if fromPickle:
                return val

            try:
                return val.decode('utf-8')  # Tries to encode in utf-8
            except:
                return val
        except dbStorage.DoesNotExist:  # @UndefinedVariable
            logger.debug('key not found')
            return None
Ejemplo n.º 37
0
    def unserializeForm(self, values: bytes):
        """
        This method unserializes the values previously obtained using
        :py:meth:`serializeForm`, and stores
        the valid values form form fileds inside its corresponding field
        """
        if not values:  # Has nothing
            return

        try:
            # Set all values to defaults ones
            for k in self._gui:
                if self._gui[k].isType(
                        gui.InputField.HIDDEN_TYPE
                ) and self._gui[k].isSerializable() is False:
                    # logger.debug('Field {0} is not unserializable'.format(k))
                    continue
                self._gui[k].value = self._gui[k].defValue

            values = typing.cast(bytes, encoders.decode(values, 'zip'))
            if not values:  # Has nothing
                return

            for txt in values.split(b'\002'):
                kb, v = txt.split(b'\003')
                k = kb.decode('utf8')  # Convert name to unicode
                if k in self._gui:
                    try:
                        if v and v[0] == 1:
                            val = pickle.loads(v[1:])
                        elif v and v[0] == 4:
                            val = cryptoManager().AESDecrypt(
                                v[1:], UDSB, True).decode()
                        else:
                            val = v
                            # Ensure "legacy bytes" values are loaded correctly as unicode
                            if isinstance(val, bytes):
                                val = val.decode('utf_8')
                    except Exception:
                        logger.exception('Pickling {} from {}'.format(k, self))
                        val = ''
                    self._gui[k].value = val
                # logger.debug('Value for {0}:{1}'.format(k, val))
        except Exception:
            logger.exception('Exception on unserialization on %s',
                             self.__class__)
Ejemplo n.º 38
0
    def filter(self,
               attr1: typing.Optional[str] = None,
               forUpdate: bool = False
               ) -> typing.Iterable[typing.Tuple[str, bytes, str]]:
        if attr1 is None:
            query = DBStorage.objects.filter(
                owner=self._owner)  # @UndefinedVariable
        else:
            query = DBStorage.objects.filter(owner=self._owner,
                                             attr1=attr1)  # @UndefinedVariable

        if forUpdate:
            query = query.select_for_update()

        for v in query:  # @UndefinedVariable
            yield (v.key, typing.cast(bytes,
                                      encoders.decode(v.data,
                                                      'base64')), v.attr1)
Ejemplo n.º 39
0
    def readData(self,
                 skey: typing.Union[str, bytes],
                 fromPickle: bool = False
                 ) -> typing.Optional[typing.Union[str, bytes]]:
        try:
            key = self.__getKey(skey)
            logger.debug('Accesing to %s %s', skey, key)
            c: DBStorage = DBStorage.objects.get(pk=key)  # @UndefinedVariable
            val: bytes = typing.cast(bytes, encoders.decode(c.data, 'base64'))

            if fromPickle:
                return val

            try:
                return val.decode('utf-8')  # Tries to encode in utf-8
            except Exception:
                return val
        except DBStorage.DoesNotExist:  # @UndefinedVariable
            logger.debug('key not found')
            return None
Ejemplo n.º 40
0
    def executeOneDelayedTask(self):
        now = getSqlDatetime()
        filt = Q(execution_time__lt=now) | Q(insert_date__gt=now + timedelta(seconds=30))
        # If next execution is before now or last execution is in the future (clock changed on this server, we take that task as executable)
        taskInstance = None
        try:
            with transaction.atomic():  # Encloses
                task = dbDelayedTask.objects.select_for_update().filter(filt).order_by('execution_time')[0]  # @UndefinedVariable
                taskInstanceDump = encoders.decode(task.instance, 'base64')
                task.delete()
            taskInstance = loads(taskInstanceDump)
        except Exception:
            # Transaction have been rolled back using the "with atomic", so here just return
            # Note that is taskInstance can't be loaded, this task will not be retried
            return

        if taskInstance is not None:
            logger.debug('Executing delayedTask:>{0}<'.format(task))
            taskInstance.env = Environment.getEnvForType(taskInstance.__class__)
            DelayedTaskThread(taskInstance).start()
Ejemplo n.º 41
0
    def unserializeForm(self, values):
        """
        This method unserializes the values previously obtained using
        :py:meth:`serializeForm`, and stores
        the valid values form form fileds inside its corresponding field
        """
        if values == b'':  # Has nothing
            return

        try:
            # Set all values to defaults ones
            for k in six.iterkeys(self._gui):
                if self._gui[k].isType(gui.InputField.HIDDEN_TYPE) and self._gui[k].isSerializable() is False:
                    # logger.debug('Field {0} is not unserializable'.format(k))
                    continue
                self._gui[k].value = self._gui[k].defValue

            values = encoders.decode(values, 'zip')
            if values == b'':  # Has nothing
                return

            for txt in values.split(b'\002'):
                k, v = txt.split(b'\003')
                k = k.decode('utf8')  # Convert name to unicode
                if k in self._gui:
                    try:
                        if v[0] == 1:
                            val = pickle.loads(v[1:])
                        else:
                            val = v
                            # Ensure "legacy bytes" values are loaded correctly as unicode
                            if isinstance(val, bytes):
                                val = val.decode('utf_8')
                    except Exception:
                        # logger.exception('Pickling')
                        val = ''
                    self._gui[k].value = val
                # logger.debug('Value for {0}:{1}'.format(k, val))
        except Exception:
            logger.exception('Exception on unserialization on {}'.format(self.__class__))
Ejemplo n.º 42
0
def getServicesPoolsCounters(
        servicePool: typing.Optional[models.ServicePool],
        counter_type: int) -> typing.List[typing.Dict[str, typing.Any]]:
    # pylint: disable=no-value-for-parameter
    try:
        cacheKey = (servicePool and servicePool.id
                    or 'all') + str(counter_type) + str(POINTS) + str(SINCE)
        to = models.getSqlDatetime()
        since: datetime.datetime = to - datetime.timedelta(days=SINCE)

        val: typing.Any = cache.get(cacheKey)
        if not val:
            if not servicePool:
                us = models.ServicePool()
                complete = True  # Get all deployed services stats
            else:
                us = servicePool
                complete = False
            val = []
            for x in counters.getCounters(us,
                                          counter_type,
                                          since=since,
                                          to=to,
                                          max_intervals=POINTS,
                                          use_max=USE_MAX,
                                          all=complete):
                val.append({'stamp': x[0], 'value': int(x[1])})
            if len(val) > 2:
                cache.put(cacheKey, encoders.encode(pickle.dumps(val), 'zip'),
                          600)
            else:
                val = [{'stamp': since, 'value': 0}, {'stamp': to, 'value': 0}]
        else:
            val = pickle.loads(typing.cast(bytes, encoders.decode(val, 'zip')))

        return val
    except:
        logger.exception('exception')
        raise ResponseError('can\'t create stats for objects!!!')
Ejemplo n.º 43
0
    def unserializeForm(self, values):
        """
        This method unserializes the values previously obtained using
        :py:meth:`serializeForm`, and stores
        the valid values form form fileds inside its corresponding field
        """
        if values == b'':  # Has nothing
            return

        try:
            # Set all values to defaults ones
            for k in six.iterkeys(self._gui):
                if self._gui[k].isType(
                        gui.InputField.HIDDEN_TYPE
                ) and self._gui[k].isSerializable() is False:
                    # logger.debug('Field {0} is not unserializable'.format(k))
                    continue
                self._gui[k].value = self._gui[k].defValue

            values = encoders.decode(values, 'zip', True)
            if values == '':  # Has nothing
                return

            for txt in values.split('\002'):
                k, v = txt.split('\003')
                if k in self._gui:
                    try:
                        if v[0] == '\001':
                            val = pickle.loads(v[1:].encode('utf-8'))
                        else:
                            val = v
                    except Exception:
                        val = ''
                    self._gui[k].value = val
                # logger.debug('Value for {0}:{1}'.format(k, val))
        except Exception:
            logger.exception('Exception on unserialization on {}'.format(
                self.__class__))
Ejemplo n.º 44
0
 def getPickleByAttr1(self, attr1):
     try:
         return pickle.loads(encoders.decode(dbStorage.objects.get(owner=self._owner, attr1=attr1).data, 'base64'))  # @UndefinedVariable
     except Exception:
         return None
Ejemplo n.º 45
0
 def unmarshal(self, s):
     data = s.decode('utf8').split('\t')
     if data[0] == 'v1':
         self._userAccount = data[1]
         self._password = CryptoManager.manager().decrypt(data[2])
         super(WinRandomPassManager, self).unmarshal(encoders.decode(data[3], 'hex'))
Ejemplo n.º 46
0
 def decode64(data64):
     return encoders.decode(data64, 'base64')
Ejemplo n.º 47
0
 def unmarshal(self, s):
     data = s.decode('utf8').split('\t')
     if data[0] == 'v1':
         self._userAccount = data[1]
         super(LinuxRandomPassManager, self).unmarshal(encoders.decode(data[2], 'hex'))
Ejemplo n.º 48
0
def unscrambleId(request, id_):
    idd = cryptoManager().xor(encoders.decode(id_ + '==\n', 'base64', asText=False), request.session[SCRAMBLE_SES])
    return idd.decode('utf8').split('~')[0]
Ejemplo n.º 49
0
 def unserialize(self, str_):
     '''
     des-obfuscates the data and then de-serializes it via unmarshal method
     '''
     return self.unmarshal(encoders.decode(str_, 'base64'))