Example #1
0
    def test_get_label_by_id(self):
        self.fail_if_not_testing_env()
        self.clean_monkey_db()

        hostname_example = "a_hostname"
        ip_example = "1.1.1.1"
        linux_monkey = Monkey(guid=str(uuid.uuid4()),
                              description="Linux shay-Virtual-Machine",
                              hostname=hostname_example,
                              ip_addresses=[ip_example])
        linux_monkey.save()

        logger.debug(id(Monkey.get_label_by_id))

        cache_info_before_query = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        self.assertEqual(cache_info_before_query.hits, 0)
        self.assertEqual(cache_info_before_query.misses, 0)

        # not cached
        label = Monkey.get_label_by_id(linux_monkey.id)
        cache_info_after_query_1 = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        self.assertEqual(cache_info_after_query_1.hits, 0)
        self.assertEqual(cache_info_after_query_1.misses, 1)
        logger.debug("1) ID: {} label: {}".format(linux_monkey.id, label))

        self.assertIsNotNone(label)
        self.assertIn(hostname_example, label)
        self.assertIn(ip_example, label)

        # should be cached
        label = Monkey.get_label_by_id(linux_monkey.id)
        logger.debug("2) ID: {} label: {}".format(linux_monkey.id, label))
        cache_info_after_query_2 = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        self.assertEqual(cache_info_after_query_2.hits, 1)
        self.assertEqual(cache_info_after_query_2.misses, 1)

        # set hostname deletes the id from the cache.
        linux_monkey.set_hostname("Another hostname")

        # should be a miss
        label = Monkey.get_label_by_id(linux_monkey.id)
        logger.debug("3) ID: {} label: {}".format(linux_monkey.id, label))
        cache_info_after_query_3 = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        logger.debug("Cache info: {}".format(str(cache_info_after_query_3)))
        # still 1 hit only
        self.assertEqual(cache_info_after_query_3.hits, 1)
        self.assertEqual(cache_info_after_query_3.misses, 2)
Example #2
0
    def test_get_label_by_id(self):
        hostname_example = "a_hostname"
        ip_example = "1.1.1.1"
        linux_monkey = Monkey(
            guid=str(uuid.uuid4()),
            description="Linux shay-Virtual-Machine",
            hostname=hostname_example,
            ip_addresses=[ip_example],
        )
        linux_monkey.save()

        logger.debug(id(Monkey.get_label_by_id))

        cache_info_before_query = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        assert cache_info_before_query.hits == 0
        assert cache_info_before_query.misses == 0

        # not cached
        label = Monkey.get_label_by_id(linux_monkey.id)
        cache_info_after_query_1 = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        assert cache_info_after_query_1.hits == 0
        assert cache_info_after_query_1.misses == 1
        logger.debug("1) ID: {} label: {}".format(linux_monkey.id, label))

        assert label is not None
        assert hostname_example in label
        assert ip_example in label

        # should be cached
        label = Monkey.get_label_by_id(linux_monkey.id)
        logger.debug("2) ID: {} label: {}".format(linux_monkey.id, label))
        cache_info_after_query_2 = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        assert cache_info_after_query_2.hits == 1
        assert cache_info_after_query_2.misses == 1

        # set hostname deletes the id from the cache.
        linux_monkey.set_hostname("Another hostname")

        # should be a miss
        label = Monkey.get_label_by_id(linux_monkey.id)
        logger.debug("3) ID: {} label: {}".format(linux_monkey.id, label))
        cache_info_after_query_3 = Monkey.get_label_by_id.storage.backend.cache_info(
        )
        logger.debug("Cache info: {}".format(str(cache_info_after_query_3)))
        # still 1 hit only
        assert cache_info_after_query_3.hits == 1
        assert cache_info_after_query_3.misses == 2
Example #3
0
 def get_label_for_endpoint(endpoint_id):
     node_service = monkey_island.cc.services.node.NodeService
     if endpoint_id == ObjectId("000000000000000000000000"):
         return 'MonkeyIsland'
     if Monkey.is_monkey(endpoint_id):
         return Monkey.get_label_by_id(endpoint_id)
     else:
         return node_service.get_node_label(
             node_service.get_node_by_id(endpoint_id))
Example #4
0
    def get_edge_label(edge):
        node_service = monkey_island.cc.services.node.NodeService
        from_id = edge["from"]
        to_id = edge["to"]

        try:
            from_label = Monkey.get_label_by_id(from_id)
        except MonkeyNotFoundError:
            from_label = node_service.get_node_by_id(from_id)['domain_name']

        if to_id == ObjectId("000000000000000000000000"):
            to_label = 'MonkeyIsland'
        else:
            if Monkey.is_monkey(to_id):
                to_label = Monkey.get_label_by_id(to_id)
            else:
                to_label = node_service.get_node_label(
                    node_service.get_node_by_id(to_id))

        return "%s %s %s" % (from_label, RIGHT_ARROW, to_label)