示例#1
0
 def __init__(self,
              host="localhost",
              port=6379,
              password=None,
              db=0,
              namespace="",
              data_type="set"):
     self.host = host
     self.port = port
     self.password = password
     self.db = db  # Use a different database to the OTX data
     self.namespace = namespace
     self.data_type = data_type
     try:
         self.connection = RedisConnect(host=self.host,
                                        port=self.port,
                                        db=self.db,
                                        password=password).connect()
     except RedisNoConnException:
         self.connection = None
         pass
示例#2
0
 def __init__(self, host="localhost", port=6379, password=None, db=0, namespace="", data_type="set"):
     self.host = host
     self.port = port
     self.password = password
     self.db = db  # Use a different database to the OTX data
     self.namespace = namespace
     self.data_type = data_type
     try:
         self.connection = RedisConnect(host=self.host,
                                        port=self.port,
                                        db=self.db,
                                        password=password).connect()
     except RedisNoConnException:
         self.connection = None
         pass
示例#3
0
class RedisDB(object):
    """NMAP Scans database redis abstraction
    """
    def __init__(self,
                 host="localhost",
                 port=6379,
                 password=None,
                 db=0,
                 namespace="",
                 data_type="set"):
        self.host = host
        self.port = port
        self.password = password
        self.db = db  # Use a different database to the OTX data
        self.namespace = namespace
        self.data_type = data_type
        try:
            self.connection = RedisConnect(host=self.host,
                                           port=self.port,
                                           db=self.db,
                                           password=password).connect()
        except RedisNoConnException:
            self.connection = None
            pass

    def get_set_name(self):
        """Returns the NMAP Scans Set name
        Ex: AlienVault-NMAP-SCANS-NS-keys
        """
        return "AlienVault-{0}-keys".format(self.namespace)

    def get_namespace_key(self, key):
        """Returns the key with our namespace
        Ex: NMAP-SCANS-NS:55367639b45ff51548a4b58c
        """
        return "{0}:{1}".format(self.namespace, key)

    def store(self, key, value, expire=None):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)
            set_name = self.get_set_name()

            pipe = self.connection.pipeline()

            # Set the value
            if expire is None:
                pipe.set(self.get_namespace_key(key), value)
            else:
                pipe.setex(self.get_namespace_key(key), expire, value)

            if self.data_type == 'set':
                # add the key to the set
                pipe.sadd(set_name, key)
            elif self.data_type == 'zset':
                # add the key to the set
                pipe.zadd(set_name, 1, key)

            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))

    def set_key_value(self, key, value):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)

            pipe = self.connection.pipeline()
            pipe.set(self.get_namespace_key(key), value)

            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))

    def get(self, key):
        """Returns the NMAP Scan with the given key
        Args:
            key(id): The pulse id
        Returns:
            pulse(dic): A python dic containing the NMAP Scan information
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        value = self.connection.get(self.get_namespace_key(key))
        if not value:
            raise RedisDBKeyNotFound(key)

        return value

    def keys(self, start=0, end=-1, order='asc'):
        """Returns a list of keys (pulse ids)
        Args:
            void
        Returns:
            [<str>]: A list of strings containing all the keys
        """
        try:
            if self.data_type == 'set':
                keys = list(self.connection.smembers(self.get_set_name()))
            elif self.data_type == 'zset':
                if order == 'desc':
                    keys = list(
                        self.connection.zrevrange(self.get_set_name(), start,
                                                  end))
                else:
                    keys = list(
                        self.connection.zrange(self.get_set_name(), start,
                                               end))
        except Exception as err:
            raise RedisDBKeysCannotBeLoaded(str(err))

        return keys

    def flush(self):
        """Flushes the current database
        Args:
            void
        Returns:
            (int) The number of members deleted
        """
        keys = self.keys()
        keys.append(self.get_set_name())  # Remove the set name
        if not keys:
            return 0

        with self.connection.pipeline() as pipe:
            pipe.delete(*keys)
            pipe.execute()
        return len(keys)

    def flush_db(self):
        """Flushes the current database using flushdb
        Args:
            void
        Returns:
            void
        """
        self.connection.flushdb()

    def add(self, items):
        """Add new items"""
        # Must be overwrite.
        pass

    def delete_key(self, key):
        """Removes the element with the given key
        Args:
            key(id): The pulse id
        Returns:
            void
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        with self.connection.pipeline() as pipe:
            pipe.delete(self.get_namespace_key(key))
            if self.data_type == 'set':
                #remove the key to the set
                pipe.srem(self.get_set_name(), key)
            elif self.data_type == 'zset':
                #remove the key to the set
                pipe.zrem(self.get_set_name(), key)

            pipe.execute()

    def get_all(self):
        key_list = self.keys()
        elems = []
        for key in key_list:
            try:
                value = self.get(key)
                value = ast.literal_eval(value)
                elems.append(value)
            except:
                pass
        return elems

    def get_range(self, start, end, order='asc'):
        """Returns a list of N elements in an ORDERED SET
        Args:
            start(int): The start offset
            end(int): The end offset
        Returns:
            (list) The list of elements.
        """
        if self.data_type != 'zset':
            raise Exception(
                "The operation get_range is only valid for ORDERED SETS")

        key_list = self.keys(start, end, order)
        elems = []
        for key in key_list:
            try:
                value = self.get(key)
                value = ast.literal_eval(value)
                elems.append(value)
            except:
                pass
        return elems

    def update(self, key, value):
        pass
示例#4
0
class RedisDB(object):
    """NMAP Scans database redis abstraction
    """

    def __init__(self, host="localhost", port=6379, password=None, db=0, namespace="", data_type="set"):
        self.host = host
        self.port = port
        self.password = password
        self.db = db  # Use a different database to the OTX data
        self.namespace = namespace
        self.data_type = data_type
        try:
            self.connection = RedisConnect(host=self.host,
                                           port=self.port,
                                           db=self.db,
                                           password=password).connect()
        except RedisNoConnException:
            self.connection = None
            pass

    def get_set_name(self):
        """Returns the NMAP Scans Set name
        Ex: AlienVault-NMAP-SCANS-NS-keys
        """
        return "AlienVault-{0}-keys".format(self.namespace)

    def get_namespace_key(self, key):
        """Returns the key with our namespace
        Ex: NMAP-SCANS-NS:55367639b45ff51548a4b58c
        """
        return "{0}:{1}".format(self.namespace, key)


    def store(self, key, value, expire=None):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)
            set_name = self.get_set_name()

            pipe = self.connection.pipeline()
            
            # Set the value
            if expire is None:
                pipe.set(self.get_namespace_key(key), value)
            else:
                pipe.setex(self.get_namespace_key(key), expire, value)
            
            if self.data_type == 'set':
                # add the key to the set
                pipe.sadd(set_name, key)
            elif self.data_type == 'zset':
                # add the key to the set
                pipe.zadd(set_name, 1, key)
                
            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))

    def set_key_value(self, key, value):
        """Saves a given pair (key,value)
        Args:
            key(str): DB key
            value(str): DB JSON
        Returns:
            void
        Raises:
            RedisDBItemCannotBeSaved: When a item cannot be saved.
        """
        try:
            key = to_unicode(key)
            value = to_unicode(value)

            pipe = self.connection.pipeline()
            pipe.set(self.get_namespace_key(key), value)

            # run the commands
            pipe.execute()
        except Exception as err:
            raise RedisDBItemCannotBeSaved(str(err))

    def get(self, key):
        """Returns the NMAP Scan with the given key
        Args:
            key(id): The pulse id
        Returns:
            pulse(dic): A python dic containing the NMAP Scan information
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        value = self.connection.get(self.get_namespace_key(key))
        if not value:
            raise RedisDBKeyNotFound(key)

        return value

    def keys(self, start=0, end=-1, order='asc'):
        """Returns a list of keys (pulse ids)
        Args:
            void
        Returns:
            [<str>]: A list of strings containing all the keys
        """
        try:
            if self.data_type == 'set':
                keys = list(self.connection.smembers(self.get_set_name()))
            elif self.data_type == 'zset':
                if order == 'desc':
                    keys = list(self.connection.zrevrange(self.get_set_name(), start, end))
                else:
                    keys = list(self.connection.zrange(self.get_set_name(), start, end))
        except Exception as err:
            raise RedisDBKeysCannotBeLoaded(str(err))

        return keys

    def flush(self):
        """Flushes the current database
        Args:
            void
        Returns:
            (int) The number of members deleted
        """

        keys = [self.get_namespace_key(key) for key in self.keys()]
        keys.append(self.get_set_name())  # Remove the set name
        if not keys:
            return 0
        with self.connection.pipeline() as pipe:
            pipe.delete(*keys)
            pipe.execute()
        return len(keys)

    def flush_db(self):
        """Flushes the current database using flushdb
        Args:
            void
        Returns:
            void
        """
        self.connection.flushdb()

    def add(self, items):
        """Add new items"""
        # Must be overwrite.
        pass

    def delete_key(self, key):
        """Removes the element with the given key
        Args:
            key(id): The pulse id
        Returns:
            void
        Raises:
            RedisDBInvalidKey: Wrong formatted key
            RedisDBKeyNotFound: Item not found
        """
        key = to_unicode(key)
        if not key:
            raise RedisDBInvalidKey(key)

        with self.connection.pipeline() as pipe:
            pipe.delete(self.get_namespace_key(key))
            if self.data_type == 'set':
                #remove the key to the set
                pipe.srem(self.get_set_name(), key)
            elif self.data_type == 'zset':
                #remove the key to the set
                pipe.zrem(self.get_set_name(), key)

            pipe.execute()

    def get_all(self):
        key_list = self.keys()
        elems = []
        for key in key_list:
            try:
                value = self.get(key)
                value = ast.literal_eval(value)
                elems.append(value)
            except:
                pass
        return elems

    def get_range(self, start, end, order='asc'):
        """Returns a list of N elements in an ORDERED SET
        Args:
            start(int): The start offset
            end(int): The end offset
        Returns:
            (list) The list of elements.
        """
        if self.data_type != 'zset':
            raise Exception("The operation get_range is only valid for ORDERED SETS")
            
        key_list = self.keys(start, end, order)
        elems = []
        for key in key_list:
            try:
                value = self.get(key)
                value = ast.literal_eval(value)
                elems.append(value)
            except:
                pass
        return elems  

    def update(self, key, value):
        pass