Esempio n. 1
0
def master_replica_discovery_rate_limiting():
    master = RedisServer(address="127.0.0.1", port=1234)
    replica = RedisServer(unix_socket=REDIS_SOCKET_PATH, master=master)

    filter = Filter(filter_name='test', nb_threads=5)
    filter.configure(FTEST_CONF_TEMPLATE)
    filter.start()

    try:
        # success
        filter.send_single(REDIS_LIST_TRIGGER)
    except Exception as e:
        logging.error(
            "master_replica_discovery_rate_limiting: Could not connect to test filter: {}"
            .format(function_name, e))
        return False

    # master shuts down
    master.stop()

    #brutal
    def thread_brute_time(filter, time_end):
        end = time() + time_end
        while time() < end:
            # accelerate throughput by avoiding constant calls to time()
            for i in range(0, 9):
                try:
                    filter.send_single(REDIS_LIST_TRIGGER)
                except:
                    return False
        return True

    thread_list = []

    for num in range(0, 5):
        thread_list.append(
            threading.Thread(target=thread_brute_time, args=(filter, 9)))

    # ought to crash if command fails
    initial_connections_num = replica.connect().info(
    )['total_connections_received']

    for thread in thread_list:
        thread.start()

    for thread in thread_list:
        thread.join()

    # new generated connections generated, minus the one generated by the call to get it
    new_connections = replica.connect().info(
    )['total_connections_received'] - initial_connections_num - 1

    if new_connections > 10:
        logging.error(
            "master_replica_discovery_rate_limiting: Wrong number of new connection attempts, "
            + "was supposed to have 10 new at most, but got ".format(
                new_connections))
        return False

    return True
Esempio n. 2
0
def fanomaly_connector_and_send_test():
    test_name = "fanomaly_connector_and_send_test"
    ret = True
    config_test =   '{{' \
                        '"redis_socket_path": "{redis_socket}",' \
                        '"alert_redis_list_name": "{redis_alert}",' \
                        '"log_file_path": "/var/log/darwin/alerts.log",' \
                        '"alert_redis_channel_name": "darwin.alerts"' \
                    '}}'.format(redis_socket=REDIS_SOCKET, redis_alert=REDIS_ALERT_LIST)

    config_buffer = '{{' \
                        '"redis_socket_path": "{redis_socket}",' \
                        '"input_format": [' \
                            '{{"name": "net_src_ip", "type": "string"}},' \
                            '{{"name": "net_dst_ip", "type": "string"}},' \
                            '{{"name": "net_dst_port", "type": "string"}},' \
                            '{{"name": "ip_proto", "type": "string"}}' \
                        '],' \
                        '"outputs": [' \
                            '{{' \
                                '"filter_type": "fanomaly",' \
                                '"filter_socket_path": "/tmp/anomaly.sock",' \
                                '"interval": 10,' \
                                '"required_log_lines": 5,' \
                                '"redis_lists": [{{' \
                                    '"source": "",' \
                                    '"name": "darwin_buffer_test"' \
                                '}}]' \
                            '}}' \
                        ']' \
                    '}}'.format(redis_socket=REDIS_SOCKET)

    # CONFIG
    buffer_filter = Buffer()
    buffer_filter.configure(config_buffer)

    test_filter = Filter(filter_name="anomaly",
                         socket_path="/tmp/anomaly.sock")
    test_filter.configure(config_test)

    # START FILTER
    if not buffer_filter.valgrind_start():
        print("Buffer did not start")
        return False

    if not test_filter.start():
        print("Anomaly did not start")
        return False

    # SEND TEST
    data = buffer_filter.get_test_data()

    darwin_api = DarwinApi(socket_path=buffer_filter.socket,
                           socket_type="unix")

    darwin_api.bulk_call(
        data,
        response_type="back",
    )

    sleep(15)

    # GET REDIS DATA AND COMPARE

    redis_data = buffer_filter.get_internal_redis_list_data(REDIS_ALERT_LIST)
    expected_data = '"details": {"ip": "192.168.110.2","udp_nb_host": 1.000000,"udp_nb_port": 252.000000,"tcp_nb_host": 0.000000,"tcp_nb_port": 0.000000,"icmp_nb_host": 0.000000,"distance": 246.193959}'

    if len(redis_data) != 1:
        logging.error("{}: Expecting a single element list.".format(test_name))
        ret = False

    redis_data = [a.decode() for a in redis_data]

    if (expected_data not in redis_data[0]):
        logging.error("{}: Expected this data : {} but got {} in redis".format(
            test_name, expected_data, redis_data))
        ret = False

    # CLEAN
    darwin_api.close()

    test_filter.clean_files()
    # ret = buffer_filter.valgrind_stop() or buffer_filter.valgrind_stop()
    # would erase upper ret if this function return True
    if not buffer_filter.valgrind_stop():
        ret = False

    if not test_filter.stop():
        ret = False

    return ret
Esempio n. 3
0
def thread_working_test():
    ret = True

    config_buffer = '{{' \
                        '"redis_socket_path": "{redis_socket}",' \
                        '"input_format": [' \
                            '{{"name": "net_src_ip", "type": "string"}},' \
                            '{{"name": "net_dst_ip", "type": "string"}},' \
                            '{{"name": "net_dst_port", "type": "string"}},' \
                            '{{"name": "ip_proto", "type": "string"}}' \
                        '],' \
                        '"outputs": [' \
                            '{{' \
                                '"filter_type": "fanomaly",' \
                                '"filter_socket_path": "/tmp/anomaly.sock",' \
                                '"interval": 10,' \
                                '"required_log_lines": 5,' \
                                '"redis_lists": [{{' \
                                    '"source": "",' \
                                    '"name": "darwin_buffer_anomaly"' \
                                '}}]' \
                            '}}' \
                        ']' \
                    '}}'.format(redis_socket=REDIS_SOCKET)

    config_test =   '{{' \
                        '"redis_socket_path": "{redis_socket}",' \
                        '"alert_redis_list_name": "{redis_alert}",' \
                        '"log_file_path": "/var/log/darwin/alerts.log",' \
                        '"alert_redis_channel_name": "darwin.alerts"' \
                    '}}'.format(redis_socket=REDIS_SOCKET, redis_alert=REDIS_ALERT_LIST)

    # CONFIG
    buffer_filter = Buffer()
    buffer_filter.configure(config_buffer)

    test_filter = Filter(filter_name="anomaly",
                         socket_path="/tmp/anomaly.sock")
    test_filter.configure(config_test)

    # START FILTER
    if not buffer_filter.valgrind_start():
        return False

    if not test_filter.start():
        print("Anomaly did not start")
        return False

    # SEND TEST
    darwin_api = DarwinApi(
        socket_path=buffer_filter.socket,
        socket_type="unix",
    )

    data = buffer_filter.get_test_data()

    darwin_api.bulk_call(
        data,
        response_type="back",
    )

    # We wait for the thread to activate
    sleep(15)

    redis_data = buffer_filter.get_internal_redis_set_data(
        "darwin_buffer_anomaly")

    if redis_data != set():
        logging.error(
            "thread_working_test : Expected no data in Redis but got {}".
            format(redis_data))
        ret = False

    # CLEAN
    darwin_api.close()

    # ret = buffer_filter.valgrind_stop() or buffer_filter.valgrind_stop()
    # would erase upper ret if this function return True
    if not buffer_filter.valgrind_stop():
        ret = False

    return ret