Example #1
0
    def test_uid_exists(self):
        """Testing function uid_exists."""
        # Testing with known good value
        expected = True
        result = db_agent.uid_exists(self.good_id)
        self.assertEqual(result, expected)

        # Testing with known bad value
        expected = False
        result = db_agent.uid_exists('bogus')
        self.assertEqual(result, expected)
Example #2
0
def _check_duplicates(information):
    """Check whether reported data reported is already in the database.

    Args:
        None

    Returns:
        valid: True if valid

    """
    # Initialize key variables
    valid = True

    # Check that we are evaluating a dict
    if isinstance(information, dict) is False:
        log_message = ('Ingest data is not a dictionary')
        log.log2warn(1116, log_message)
        valid = False
        return valid

    # Check that we have the correct keys in the dict
    if _check_primary_keys_exist(information) is False:
        valid = False
        return valid

    # Get values
    timestamp = int(information['timestamp'])
    uid = information['uid']
    hostname = information['hostname']

    # Check if there is a duplicate entry for this UID
    if db_agent.uid_exists(uid) is not False:
        idx_agent = db_agent.GetUID(uid).idx()

        # Check if host exists
        if db_host.hostname_exists(hostname) is True:
            idx_host = db_host.GetHost(hostname).idx()

            # Check for host / agent entry existence
            if db_hostagent.host_agent_exists(
                    idx_host, idx_agent) is True:
                # Check if this host / agent has been updated before
                last_timesamp = db_hostagent.GetHostAgent(
                    idx_host, idx_agent).last_timestamp()

                # Validate
                if timestamp <= last_timesamp:
                    log_message = (
                        'Data for UID %s, hostname %s at timestamp %s '
                        'is already found in database.'
                        '') % (uid, hostname, timestamp)
                    log.log2warn(1113, log_message)
                    valid = False

    # Return
    return valid
Example #3
0
    def _check_duplicates(self):
        """Method initializing the class.

        Args:
            None

        Returns:
            valid: True if valid

        """
        # Initialize key variables
        valid = True

        # Get values
        timestamp = int(self.information['timestamp'])
        uid = self.information['uid']
        hostname = self.information['hostname']

        # Check if there is a duplicate entry for this UID
        if db_agent.uid_exists(uid) is not False:
            idx_agent = db_agent.GetUID(uid).idx()

            # Check if host exists
            if db_host.hostname_exists(hostname) is True:
                idx_host = db_host.GetHost(hostname).idx()

                # Check for host / agent entry existence
                if db_hostagent.host_agent_exists(
                        idx_host, idx_agent) is True:
                    # Check if this host / agent has been updated before
                    last_timesamp = db_hostagent.GetIDX(
                        idx_host, idx_agent).last_timestamp()

                    # Validate
                    if timestamp <= last_timesamp:
                        valid = False

        # Return
        return valid
Example #4
0
    def _insert_agent(self):
        """Insert new agent into database.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        uid = self.ingest.uid()
        agent_name = self.ingest.agent()

        # Return if agent already exists in the table
        if agent.uid_exists(uid) is True:
            return

        # Prepare SQL query to read a record from the database.
        record = Agent(
            id=jm_general.encode(uid),
            name=jm_general.encode(agent_name))
        database = db.Database()
        database.add(record, 1081)