Beispiel #1
0
def save_current_status_message(component_id, component_type, message_code, data=""):
    """Saves or merges the status_message
    Args:
        component_id(uuid canonical string)
        message_code(uuid cannonical string):Message id
        data (str): Message data
        component_type(str): Component tyep
    """
    result = True
    try:
        message = get_current_status_message(component_id, message_code)
        db.session.begin()
        if message is not None:

            message_data = message
            #message_data.creation_time = datetime.now()
            message_data.additional_info = data
            db.session.merge(message_data)
        else:
            message_data = Current_Status()
            message_data.id = uuid.uuid4().bytes
            message_data.component_id = get_bytes_from_uuid(component_id)
            message_data.component_type = component_type
            message_data.creation_time = datetime.utcnow()
            message_data.message_id = get_bytes_from_uuid(message_code)
            message_data.additional_info = data
            message_data.supressed = 0
            message_data.viewed = 0
            db.session.add(message_data)
        db.session.commit()
    except Exception as error:
        db.session.rollback()
        result = False

    return result
Beispiel #2
0
def db_insert_current_status_message(message_id,
                                     component_id,
                                     component_type,
                                     additional_info,
                                     replace,
                                     created=None):
    """Inserts a new notification on the system. The related message id should exists.
    Args:
        message_id (str:uuid string): Message id related with the notification
        component_id(str:uuid string): Component id related with the notification (Could be none for external messages)
        component_type(str): Component type. Allowed values: ('net','host','user','sensor','server','system','external')
        additional_info (str:json): Additional information you want to store.
    Returns:
        success(bool): True if the operation went well, False otherwise
        msg(str): A message string that will contain some kind of information in case of error"""

    if created is None:
        created = datetime.utcnow()

    if component_type not in [
            'net', 'host', 'user', 'sensor', 'server', 'system', 'external'
    ]:
        return False, "Invalid component_type"
    if component_type != "external" and component_id is None:
        return False, "Component id cannot be none for the given component_type"

    msg_id_binary = get_bytes_from_uuid(message_id)
    success, status_message = get_status_message_from_id(
        message_id=msg_id_binary, is_admin=True, serialize=False)
    if not success:
        return False, "The given message_id doesn't exist"
    if status_message is None:
        return False, "The given message_id doesn't exist. Message is None"
    component_id_binary = get_bytes_from_uuid(component_id)
    if (component_id_binary is None
            or component_id_binary == "") and component_type != "external":
        return False, "Invalid component_id"
    if replace is True:
        success, msg = delete_current_status_messages([msg_id_binary])
        if not success:
            return success, "Unable to remove previous messages for the given message ID."
    try:
        db.session.begin()
        current_status_message = Current_Status()
        current_status_message.id = uuid4().bytes
        current_status_message.component_type = component_type
        current_status_message.creation_time = created
        current_status_message.message_id = msg_id_binary
        current_status_message.additional_info = additional_info
        current_status_message.suppressed = 0
        current_status_message.viewed = 0
        current_status_message.component_id = component_id_binary
        db.session.add(current_status_message)
        db.session.commit()
    except Exception, e:
        db.session.rollback()
        return False, "%s" % str(e)
Beispiel #3
0
def save_current_status_message(component_id, component_type, message_code, data=""):
    result = True
    try:
        db.session.begin()
        message = get_current_status_message(component_id,message_code)
        if message is not None:
            message_data = message
            message_data.creation_time = datetime.now()
            db.session.merge(message_data)
        else:
            message_data = Current_Status()
            message_data.component_id = component_id
            message_data.component_type = component_type
            message_data.creation_time = datetime.now()
            message_data.message_id = message_code
            message_data.additional_info = data
            message_data.supressed = 0
            message_data.viewed = 'false'
            db.session.add(message_data)
        db.session.commit()
    except:
        db.session.rollback()
        result = False

    return result
Beispiel #4
0
 def append_trigger_message(self,
                            component_id,
                            component_type,
                            message_code,
                            data=""):
     """
     Creates a current status message and saves it in the database
     :param component_id: The component id - uuid canonical string
     :param message_code: Message type
     :param data: Current Status Message Additional Info
     :return: True is successful, False otherwise
     """
     rt = True
     try:
         message_data = Current_Status()
         message_data.component_id = component_id
         message_data.component_type = component_type
         message_data.creation_time = datetime.now()
         message_data.message_id = message_code
         message_data.additional_info = data
         message_data.supressed = 0
         message_data.viewed = 'false'
         self.__trigger_messages.append(message_data)
     except Exception, e:
         rt = False
Beispiel #5
0
def save_current_status_message(component_id, component_type, message_code, data=""):
    """Saves or merges the status_message
    Args:
        component_id(uuid canonical string)
        message_code(uuid cannonical string):Message id
        data (str): Message data
        component_type(str): Component tyep
    """
    result = True
    try:
        message = get_current_status_message(component_id, message_code)
        db.session.begin()
        if message is not None:

            message_data = message
            #message_data.creation_time = datetime.now()
            message_data.additional_info = data
            db.session.merge(message_data)
        else:
            message_data = Current_Status()
            message_data.id = uuid.uuid4().bytes
            message_data.component_id = get_bytes_from_uuid(component_id)
            message_data.component_type = component_type
            message_data.creation_time = datetime.utcnow()
            message_data.message_id = get_bytes_from_uuid(message_code)
            message_data.additional_info = data
            message_data.supressed = 0
            message_data.viewed = 0
            db.session.add(message_data)
        db.session.commit()
    except Exception as error:
        db.session.rollback()
        result = False

    return result
Beispiel #6
0
def given_create_or_update_current_status_entry(context, var_cid, var_mid, var_type, viewed):
    #Using merging http://docs.sqlalchemy.org/en/latest/orm/session.html#merging to create or update    
    # Check if we must create or delete
    print (context.alienvault[var_cid])
    print (context.alienvault[var_mid])
    try:
        entry = db.session.query(Current_Status).filter(and_(  Current_Status.component_id == get_bytes_from_uuid(context.alienvault[var_cid]),
             Current_Status.message_id ==  context.alienvault[var_mid])).one()
        entry.component_type =  context.alienvault[var_type]
        entry.viewed = viewed
    except  NoResultFound:
        # Create a new entry 
        entry = Current_Status()
        entry.id = uuid.uuid4().bytes
        entry.message_id = get_bytes_from_uuid(context.alienvault[var_mid])
        entry.component_id = get_bytes_from_uuid(context.alienvault[var_cid])
        entry.component_type = context.alienvault[var_type]
        entry.viewed = viewed
    try:
        db.session.begin()
        new_obj = db.session.merge(entry)
        db.session.commit()
    except Exception,e:
        print ("Error: %s" %str(e))
        db.session.rollback()
Beispiel #7
0
def db_insert_current_status_message(message_id, component_id, component_type, additional_info, replace, created=None):
    """Inserts a new notification on the system. The related message id should exists.
    Args:
        message_id (str:uuid string): Message id related with the notification
        component_id(str:uuid string): Component id related with the notification (Could be none for external messages)
        component_type(str): Component type. Allowed values: ('net','host','user','sensor','server','system','external')
        additional_info (str:json): Additional information you want to store.
    Returns:
        success(bool): True if the operation went well, False otherwise
        msg(str): A message string that will contain some kind of information in case of error"""

    if created is None:
        created = datetime.utcnow()

    if component_type not in ['net', 'host', 'user', 'sensor', 'server', 'system', 'external']:
        return False, "Invalid component_type"
    if component_type != "external" and component_id is None:
        return False, "Component id cannot be none for the given component_type"

    msg_id_binary = get_bytes_from_uuid(message_id)
    success, status_message = get_status_message_from_id(message_id=msg_id_binary, is_admin=True, serialize=False)
    if not success:
        return False, "The given message_id doesn't exist"
    if status_message is None:
        return False, "The given message_id doesn't exist. Message is None"
    component_id_binary = get_bytes_from_uuid(component_id)
    if (component_id_binary is None or component_id_binary == "") and component_type != "external":
        return False, "Invalid component_id"
    if replace is True:
        success, msg = delete_current_status_messages([msg_id_binary])
        if not success:
            return success, "Unable to remove previous messages for the given message ID."
    try:
        db.session.begin()
        current_status_message = Current_Status()
        current_status_message.id = uuid4().bytes
        current_status_message.component_type = component_type
        current_status_message.creation_time = created
        current_status_message.message_id = msg_id_binary
        current_status_message.additional_info = additional_info
        current_status_message.suppressed = 0
        current_status_message.viewed = 0
        current_status_message.component_id = component_id_binary
        db.session.add(current_status_message)
        db.session.commit()
    except Exception, e:
        db.session.rollback()
        return False, "%s" % str(e)
Beispiel #8
0
def given_gen_status_message(context,var_n,var_type):
    db.session.query(Current_Status).delete() # Delete all current_status entries
    ctypes = var_type.split(",")
    total = 0
    while (total < 100):
        ctype = random.choice (ctypes)
        
        assert (ctype in ['net','host','user','sensor','server','system']) == True, "Unknown type '%s'" % ctype
        entry = Current_Status()
        # Select a entry 
        try:
            c_id = {
                'user': str(uuid.UUID(db.session.query(Users).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'net' : str(uuid.UUID(db.session.query(Net).order_by(func.rand()).limit(1).one().serialize['id'])),
                'host': str(uuid.UUID(db.session.query(Host).order_by(func.rand()).limit(1).one().serialize['id'])),
                'sensor': str(uuid.UUID(db.session.query(Sensor).order_by(func.rand()).limit(1).one().serialize['id'])),
                'system': str(uuid.UUID(db.session.query(System).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'server':  str(uuid.UUID(db.session.query(Server).order_by(func.rand()).limit(1).one().serialize['id']))
    
          }.get(ctype)
        except NoResultFound:
           assert None,"Can't load a asset of type '%s'" % ctype
       # We have now the component_id
       # Select a msg_id
        try:
           msg_entry = db.session.query(Status_Message).order_by(func.rand()).limit(1).one().serialize
        except NoResultFound:
           assert None,"Can't load a message entry"
        entry.id = get_bytes_from_uuid (str(uuid.uuid1()))
        entry.message_id = get_bytes_from_uuid(msg_entry['id'])
        entry.component_id =  get_bytes_from_uuid (c_id)
      
        entry.component_type = ctype
        entry.viewed = False
        entry.suppressed = False 
        entry.additional_info = """{"id": "Random generate message"}"""
        db.session.begin() 
        db.session.merge(entry)
        db.session.commit()
        total = total + 1
Beispiel #9
0
def given_gen_status_message(context,var_n):
    db.session.query(Current_Status).delete() # Delete all current_status entries
    total = 0
    total_msg =  _return_total_status()
    assert int(var_n) <= total_msg, "We don't have enought messages and asset to generate %d current_status entries " % int(var_n)
    while total < int(var_n):
        ctype = random.choice (['net','host','user','sensor','server','system'])
        
        entry = Current_Status()
        try:
            c_id = {
                'user': str(uuid.UUID(db.session.query(Users).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'net' : str(uuid.UUID(db.session.query(Net).order_by(func.rand()).limit(1).one().serialize['id'])),
                'host': str(uuid.UUID(db.session.query(Host).order_by(func.rand()).limit(1).one().serialize['id'])),
                'sensor': str(uuid.UUID(db.session.query(Sensor).order_by(func.rand()).limit(1).one().serialize['id'])),
                'system': str(uuid.UUID(db.session.query(System).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'server':  str(uuid.UUID(db.session.query(Server).order_by(func.rand()).limit(1).one().serialize['id'])),
    
          }.get(ctype)
        except NoResultFound:
           assert None,"Can't load a asset of type '%s'" % ctype
       # We have now the component_id
       # Select a msg_id
        try:
           msg_entry = db.session.query(Status_Message).order_by(func.rand()).limit(1).one().serialize
        except NoResultFound:
           assert None,"Can't load a message entry"
        entry.id = get_bytes_from_uuid (str(uuid.uuid1()))
        entry.message_id = get_bytes_from_uuid(msg_entry['id'])
        entry.component_id =  get_bytes_from_uuid (c_id)
        entry.component_type = ctype
        entry.viewed = random.choice([True, False])
        entry.additional_info = """{"msg_id": "Random generate message"}"""
        # check
        q = db.session.query(Current_Status).filter(and_( Current_Status.message_id == entry.message_id, Current_Status.component_id == entry.component_id)).all()
        if len(q) > 0:
            continue
        db.session.begin() 
        db.session.merge(entry)
        db.session.commit()
        total = total + 1
Beispiel #10
0
 def append_trigger_message(self, component_id, component_type, message_code, data=""):
     """
     Creates a current status message and saves it in the database
     :param component_id: The component id - uuid canonical string
     :param message_code: Message type
     :param data: Current Status Message Additional Info
     :return: True is successful, False otherwise
     """
     rt = True
     try:
         message_data = Current_Status()
         message_data.component_id = component_id
         message_data.component_type = component_type
         message_data.creation_time = datetime.now()
         message_data.message_id = message_code
         message_data.additional_info = data
         message_data.supressed = 0
         message_data.viewed = 'false'
         self.__trigger_messages.append(message_data)
     except Exception, e:
         rt = False
Beispiel #11
0
def given_gen_status_message(context,var_n,var_type):
    db.session.query(Current_Status).delete() # Delete all current_status entries
    ctypes = var_type.split(",")
    total = 0
    while (total < 100):
        ctype = random.choice (ctypes)
        
        assert (ctype in ['net','host','user','sensor','server','system']) == True, "Unknown type '%s'" % ctype
        entry = Current_Status()
        # Select a entry 
        try:
            c_id = {
                'user': str(uuid.UUID(db.session.query(Users).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'net' : str(uuid.UUID(db.session.query(Net).order_by(func.rand()).limit(1).one().serialize['id'])),
                'host': str(uuid.UUID(db.session.query(Host).order_by(func.rand()).limit(1).one().serialize['id'])),
                'sensor': str(uuid.UUID(db.session.query(Sensor).order_by(func.rand()).limit(1).one().serialize['id'])),
                'system': str(uuid.UUID(db.session.query(System).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'server':  str(uuid.UUID(db.session.query(Server).order_by(func.rand()).limit(1).one().serialize['id']))
    
          }.get(ctype)
        except NoResultFound:
           assert None,"Can't load a asset of type '%s'" % ctype
       # We have now the component_id
       # Select a msg_id
        try:
           msg_entry = db.session.query(Status_Message).order_by(func.rand()).limit(1).one().serialize
        except NoResultFound:
           assert None,"Can't load a message entry"
        entry.id = get_bytes_from_uuid (str(uuid.uuid1()))
        entry.message_id = get_bytes_from_uuid(msg_entry['id'])
        entry.component_id =  get_bytes_from_uuid (c_id)
      
        entry.component_type = ctype
        entry.viewed = False
        entry.suppressed = False 
        entry.additional_info = """{"id": "Random generate message"}"""
        db.session.begin() 
        db.session.merge(entry)
        db.session.commit()
        total = total + 1
Beispiel #12
0
def given_gen_status_message(context,var_n):
    db.session.query(Current_Status).delete() # Delete all current_status entries
    total = 0
    total_msg =  _return_total_status()
    assert int(var_n) <= total_msg, "We don't have enought messages and asset to generate %d current_status entries " % int(var_n)
    while total < int(var_n):
        ctype = random.choice (['net','host','user','sensor','server','system'])
        
        entry = Current_Status()
        try:
            c_id = {
                'user': str(uuid.UUID(db.session.query(Users).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'net' : str(uuid.UUID(db.session.query(Net).order_by(func.rand()).limit(1).one().serialize['id'])),
                'host': str(uuid.UUID(db.session.query(Host).order_by(func.rand()).limit(1).one().serialize['id'])),
                'sensor': str(uuid.UUID(db.session.query(Sensor).order_by(func.rand()).limit(1).one().serialize['id'])),
                'system': str(uuid.UUID(db.session.query(System).order_by(func.rand()).limit(1).one().serialize['uuid'])),
                'server':  str(uuid.UUID(db.session.query(Server).order_by(func.rand()).limit(1).one().serialize['id'])),
    
          }.get(ctype)
        except NoResultFound:
           assert None,"Can't load a asset of type '%s'" % ctype
       # We have now the component_id
       # Select a msg_id
        try:
           msg_entry = db.session.query(Status_Message).order_by(func.rand()).limit(1).one().serialize
        except NoResultFound:
           assert None,"Can't load a message entry"
        entry.id = get_bytes_from_uuid (str(uuid.uuid1()))
        entry.message_id = get_bytes_from_uuid(msg_entry['id'])
        entry.component_id =  get_bytes_from_uuid (c_id)
        entry.component_type = ctype
        entry.viewed = random.choice([True, False])
        entry.additional_info = """{"msg_id": "Random generate message"}"""
        # check
        q = db.session.query(Current_Status).filter(and_( Current_Status.message_id == entry.message_id, Current_Status.component_id == entry.component_id)).all()
        if len(q) > 0:
            continue
        db.session.begin() 
        db.session.merge(entry)
        db.session.commit()
        total = total + 1
Beispiel #13
0
def load_mcserver_messages(message_list):
    """Adds or updates messages coming from the mcserver

    Args:
        message_list[Status_Message]

    Returns:
        success (bool): True if successful, False elsewhere
        result  (str): Error message (if any)
    """
    result = ""
    success = True
    try:
        db.session.begin()
        for msg in message_list:
            msg_id_str = str(msg['msg_id'])
            msg_id_binary = get_bytes_from_uuid(msg_id_str)
            additional_info_json = ""
            if msg['additional_info'] is not None and msg[
                    'additional_info'] != "":
                try:
                    additional_info_json = json.dumps(msg['additional_info'])
                except Exception as e:
                    api_log.warning(
                        "Message with an invalid additional_info %s -  %s" %
                        (msg_id_str, str(e)))
                    additional_info_json = ""
            success, status_message = get_status_message_from_id(
                message_id=msg_id_binary, is_admin=True, serialize=False)
            if success:
                #update values:
                status_message.level = Status_Message.get_level_integer_from_string(
                    str(msg['level']))
                status_message.title = msg['title']
                status_message.description = msg['description']
                status_message.type = msg['type']
                success, current_status_message = get_current_status_from_message_id(
                    msg_id_str)
                if not success or len(current_status_message) != 1:
                    api_log.error(
                        "Invalid external message %s. Current_Status: %s, tuples(%s)"
                        % (msg_id_str, success, len(current_status_message)))
                    continue
                current_status_message[
                    0].additional_info = additional_info_json
                db.session.merge(current_status_message[0])
                db.session.merge(status_message)
            else:
                new_msg = Status_Message()
                new_msg.id = msg_id_binary
                new_msg.level = Status_Message.get_level_integer_from_string(
                    str(msg['level']))
                new_msg.title = msg['title']
                new_msg.description = msg['description']
                new_msg.type = msg['type']
                new_msg.expire = datetime.strptime(msg['valid_to'],
                                                   "%Y-%m-%dT%H:%M:%S")
                new_msg.actions = ""
                new_msg.alternative_actions = ""
                new_msg.source = "external"
                current_status_message = Current_Status()
                current_status_message.id = uuid4().bytes
                current_status_message.component_type = 'external'
                current_status_message.creation_time = datetime.strptime(
                    msg['valid_from'], "%Y-%m-%dT%H:%M:%S")
                current_status_message.message_id = new_msg.id
                current_status_message.additional_info = ""
                current_status_message.suppressed = 0
                current_status_message.viewed = 0
                current_status_message.additional_info = additional_info_json
                db.session.add(new_msg)
                db.session.add(current_status_message)
        db.session.commit()
    except Exception, e:
        success = False
        result = "[load_mcserver_messages(] Error: %s" % str(e)
        db.session.rollback()
Beispiel #14
0
def load_mcserver_messages(message_list):
    """Adds or updates messages coming from the mcserver

    Args:
        message_list[Status_Message]

    Returns:
        success (bool): True if successful, False elsewhere
        result  (str): Error message (if any)
    """
    result = ""
    success = True
    try:
        db.session.begin()
        for msg in message_list:
            msg_id_str = str(msg['msg_id'])
            msg_id_binary = get_bytes_from_uuid(msg_id_str)
            additional_info_json = ""
            if msg['additional_info'] is not None and msg['additional_info'] != "":
                try:
                    additional_info_json = json.dumps(msg['additional_info'])
                except Exception as e:
                    api_log.warning("Message with an invalid additional_info %s -  %s" % (msg_id_str, str(e)))
                    additional_info_json = ""
            success, status_message = get_status_message_from_id(message_id=msg_id_binary, is_admin=True,
                                                                 serialize=False)
            if success:
                #update values:
                status_message.level = Status_Message.get_level_integer_from_string(str(msg['level']))
                status_message.title = msg['title']
                status_message.description = msg['description']
                status_message.type = msg['type']
                success, current_status_message = get_current_status_from_message_id(msg_id_str)
                if not success or len(current_status_message) != 1:
                    api_log.error("Invalid external message %s. Current_Status: %s, tuples(%s)" % (
                        msg_id_str, success, len(current_status_message)))
                    continue
                current_status_message[0].additional_info = additional_info_json
                db.session.merge(current_status_message[0])
                db.session.merge(status_message)
            else:
                new_msg = Status_Message()
                new_msg.id = msg_id_binary
                new_msg.level = Status_Message.get_level_integer_from_string(str(msg['level']))
                new_msg.title = msg['title']
                new_msg.description = msg['description']
                new_msg.type = msg['type']
                new_msg.expire = datetime.strptime(msg['valid_to'], "%Y-%m-%dT%H:%M:%S")
                new_msg.actions = ""
                new_msg.alternative_actions = ""
                new_msg.source = "external"
                current_status_message = Current_Status()
                current_status_message.id = uuid4().bytes
                current_status_message.component_type = 'external'
                current_status_message.creation_time = datetime.strptime(msg['valid_from'], "%Y-%m-%dT%H:%M:%S")
                current_status_message.message_id = new_msg.id
                current_status_message.additional_info = ""
                current_status_message.suppressed = 0
                current_status_message.viewed = 0
                current_status_message.additional_info = additional_info_json
                db.session.add(new_msg)
                db.session.add(current_status_message)
        db.session.commit()
    except Exception, e:
        success = False
        result = "[load_mcserver_messages(] Error: %s" % str(e)
        db.session.rollback()