Ejemplo n.º 1
0
 def __init__(self):
     self.sdnc = get_sdn_connect(logger)
     self.logger = self.sdnc.logger
     self.config = self.sdnc.config
     self.sdnc.config['TYPE'] = 'None'
     self.sdnc.get_sdn_context()
     self.sdnc.config['TYPE'] = 'faucet'
     self.sdnc.get_sdn_context()
     self.job_queue = queue.Queue()
     self.prom = prom
     endpoint = endpoint_factory('foo')
     endpoint.endpoint_data = {
         'active': 0, 'ipv4_subnet': '12.12.12.12/24', 'ipv6_subnet': '', 'ipv4_rdns': '', 'ipv6_rdns': '', 'controller_type': 'faucet', 'controller': '', 'name': '', 'ipv4': '12.12.12.12', 'ipv6': '', 'ether_vendor': 'foo', 'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
     endpoint.metadata = {'mac_addresses': {'00:00:00:00:00:00': {'classification': {'labels': ['developer workstation', 'foo', 'bar'], 'confidences': [0.8, 0.2, 0.0]}}}, 'ipv4_addresses': {
         '12.12.12.12': {'os': 'windows'}}, 'ipv6_addresses': {'1212::1': {'os': 'windows'}}}
     endpoint.operate()
     self.sdnc.endpoints[endpoint.name] = endpoint
     endpoint = endpoint_factory('foo2')
     endpoint.endpoint_data = {
         'active': 0, 'ipv4_subnet': '12.12.12.12/24', 'ipv6_subnet': '', 'ipv4_rdns': '', 'ipv6_rdns': '', 'controller_type': 'faucet', 'controller': '', 'name': '', 'ipv4': '12.12.12.12', 'ipv6': '', 'ether_vendor': 'foo', 'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
     endpoint.metadata = {'mac_addresses': {'00:00:00:00:00:00': {'classification': {'labels': ['developer workstation', 'foo', 'bar'], 'confidences': [0.8, 0.2, 0.0]}}}, 'ipv4_addresses': {
         '12.12.12.12': {'os': 'windows'}}, 'ipv6_addresses': {'1212::1': {'os': 'windows'}}}
     endpoint.queue_next('operate')
     self.sdnc.endpoints[endpoint.name] = endpoint
     endpoint = endpoint_factory('foo3')
     endpoint.endpoint_data = {
         'active': 0, 'ipv4_subnet': '12.12.12.12/24', 'ipv6_subnet': '', 'ipv4_rdns': '', 'ipv6_rdns': '', 'controller_type': 'faucet', 'controller': '', 'name': '', 'ipv4': '12.12.12.12', 'ipv6': '', 'ether_vendor': 'foo', 'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
     endpoint.metadata = {'mac_addresses': {'00:00:00:00:00:00': {'classification': {'labels': ['developer workstation', 'foo', 'bar'], 'confidences': [0.8, 0.2, 0.0]}}}, 'ipv4_addresses': {
         '12.12.12.12': {'os': 'windows'}}, 'ipv6_addresses': {'1212::1': {'os': 'windows'}}}
     self.sdnc.endpoints[endpoint.name] = endpoint
     self.results = 0
Ejemplo n.º 2
0
def test_commands():
    commands = Commands(config=get_test_config(),
                        faucetconfgetsetter_cl=FaucetLocalConfGetSetter)
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    commands.sdnc.endpoints = {}
    commands.sdnc.endpoints[endpoint.name] = endpoint

    commands.what_is('foo')
    commands.history_of('foo')
    commands.acls_of('foo')
    commands.where_is('foo')
    commands.show_devices('foo bar')
    commands.show_devices('all')
    commands.change_devices('foo')
    commands.remove('foo')
    commands.clear_ignored('foo')
    commands.clear_ignored('ignored')
    commands.ignore('foo')
    commands.remove_ignored('foo')

    endpoint2 = endpoint_factory('foo2')
    endpoint2.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    commands.sdnc.endpoints[endpoint2.name] = endpoint2
    commands.what_is('00:00:00:00:00:00')
Ejemplo n.º 3
0
def test_config():
    """
    Tests Config Operations
    """
    def check_config(obj, endpoints):
        for endpoint in endpoints:
            obj.mac_table[endpoint.endpoint_data['mac']] = [
                endpoint.endpoint_data]
        obj.mirror_mac('00:00:00:00:00:00', 't1-1', 1)
        obj.mirror_mac('00:00:00:00:00:00', 0x1, 2)
        obj.mirror_mac('00:00:00:00:00:00', 't1-1', 2)
        obj.mirror_mac('00:00:00:00:00:00', 't1-1', 3)
        obj.mirror_mac('00:00:00:00:00:00', 't2-1', 5)
        obj.mirror_mac('00:00:00:00:00:00', 'bad', 6)
        obj.unmirror_mac('00:00:00:00:00:01', None, None)
        obj.unmirror_mac('00:00:00:00:00:01', 't1-1', 1)
        obj.unmirror_mac('00:00:00:00:00:01', 't1-1', 3)
        obj.mirror_mac('00:00:00:00:00:01', 't1-1', 3)
        obj.unmirror_mac('00:00:00:00:00:01', 't1-1', 3)
        obj.update_acls()
        obj.update_acls(endpoints=endpoints,
                        rules_file=os.path.join(os.getcwd(), 'config/rules.yaml'))

    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 't1-1', 'port': '1', 'ipv4': '0.0.0.0', 'ipv6': '1212::1'}
    endpoint.metadata = {'mac_addresses': {'00:00:00:00:00:00': {'1551805502.0': {'labels': ['developer workstation']}}}, 'ipv4_addresses': {
        '0.0.0.0': {'os': 'windows'}}, 'ipv6_addresses': {'1212::1': {'os': 'windows'}}}
    endpoint2 = endpoint_factory('foo')
    endpoint2.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:01', 'segment': 't1-1', 'port': '3', 'ipv4': '0.0.0.0', 'ipv6': '1212::1'}
    endpoint2.metadata = {'mac_addresses': {'00:00:00:00:00:01': {'1551805502.0': {'labels': ['developer workstation']}}}, 'ipv4_addresses': {
        '0.0.0.0': {'os': 'windows'}}, 'ipv6_addresses': {'1212::1': {'os': 'windows'}}}
    endpoints = [endpoint, endpoint2]

    with tempfile.TemporaryDirectory() as tmpdir:
        faucetconfgetsetter_cl = FaucetLocalConfGetSetter
        faucetconfgetsetter_cl.DEFAULT_CONFIG_FILE = os.path.join(
            tmpdir, 'faucet.yaml')
        shutil.copy(SAMPLE_CONFIG, faucetconfgetsetter_cl.DEFAULT_CONFIG_FILE)
        parser = _get_proxy(
            faucetconfgetsetter_cl=faucetconfgetsetter_cl,
            mirror_ports={'t1-1': 2},
            proxy_mirror_ports={'sx': ['s1', 99]})
        parser.frpc.faucet_conf = yaml.safe_load(
            faucetconfgetsetter_cl.DEFAULT_CONFIG_FILE)
        parser2 = _get_proxy(faucetconfgetsetter_cl=faucetconfgetsetter_cl)
        parser2.frpc.faucet_conf = yaml.safe_load(
            faucetconfgetsetter_cl.DEFAULT_CONFIG_FILE)
        config = Config().get_config()
        proxy = _get_proxy(
            faucetconfgetsetter_cl=faucetconfgetsetter_cl, config=config)
        check_config(parser, endpoints)
        check_config(parser2, endpoints)
        check_config(proxy, endpoints)
Ejemplo n.º 4
0
def test_clear_filters():
    s = get_sdn_connect(logger)
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
    s.endpoints[endpoint.name] = endpoint
    s.clear_filters()
    s = get_sdn_connect(logger)
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
    s.endpoints[endpoint.name] = endpoint
    s.clear_filters()
Ejemplo n.º 5
0
def test_mirror_endpoint():
    s = get_sdn_connect(logger)
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
    s.endpoints[endpoint.name] = endpoint
    s.mirror_endpoint(endpoint)
Ejemplo n.º 6
0
def test_Endpoint():
    """Tests Endpoint."""
    endpoint = endpoint_factory('foo')
    b = endpoint.encode()
    c = EndpointDecoder(b).get_endpoint()
    a = {'tenant': 'foo', 'mac': '00:00:00:00:00:00'}
    assert Endpoint.make_hash(a)
Ejemplo n.º 7
0
def test_Collector():
    """
    Tests Collector
    """
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {'mac': '00:00:00:00:00:00'}
    a = Collector(endpoint, 'foo')
    a.start_collector()
    a.stop_collector()
    a.get_collectors()
    a.host_has_active_collectors('foo')
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'mac': '00:00:00:00:00:00',
        'container_id': 'foo'
    }
    a = Collector(endpoint, 'foo')
    a.stop_collector()
Ejemplo n.º 8
0
def test_get_prev_ipv6_oses():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    GetData._get_prev_ipv6_oses(endpoint)
Ejemplo n.º 9
0
def test_get_last_seen():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    endpoint.unknown()
    GetData._get_last_seen(endpoint)
Ejemplo n.º 10
0
def test_get_port():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    port = GetData._get_port(endpoint)
    assert port == '1'
Ejemplo n.º 11
0
def test_times_next():
    endpoint = endpoint_factory('foo')
    endpoint.queue_next('operate')
    time.sleep(1)
    endpoint.copro_queue_next('copro_coprocess')
    time.sleep(1)
    assert endpoint.state_timeout(0)
    assert endpoint.copro_state_timeout(0)
    endpoint.trigger_next()
    endpoint.copro_trigger_next()
Ejemplo n.º 12
0
def test_get_name():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    name = GetData._get_name(endpoint)
    assert name == 'foo'
Ejemplo n.º 13
0
def test_get_history():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    history = GetData._get_history(endpoint)
    assert history == 'No history recorded yet.'
Ejemplo n.º 14
0
def test_get_vlan():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'vlan': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    vlan = GetData._get_vlan(endpoint)
    assert vlan == 'foo'
Ejemplo n.º 15
0
def test_get_switch():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    switch = GetData._get_switch(endpoint)
    assert switch == 'foo'
Ejemplo n.º 16
0
def test_endpoint_by_hash():
    s = get_sdn_connect(logger)
    endpoint = s.endpoint_by_hash('foo')
    assert endpoint == None
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
    s.endpoints[endpoint.name] = endpoint
    endpoint2 = s.endpoint_by_hash('foo')
    assert endpoint == endpoint2
Ejemplo n.º 17
0
def test_get_next_state():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    state = GetData._get_next_state(endpoint)
    assert state == 'None'
Ejemplo n.º 18
0
def test_get_ignored():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    ignored = GetData._get_ignored(endpoint)
    assert ignored == 'False'
Ejemplo n.º 19
0
def test_endpoints_by_mac():
    s = get_sdn_connect(logger)
    endpoints = s.endpoints_by_mac('00:00:00:00:00:01')
    assert endpoints == []
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
    s.endpoints[endpoint.name] = endpoint
    endpoint2 = s.endpoints_by_mac('00:00:00:00:00:00')
    assert [endpoint] == endpoint2
Ejemplo n.º 20
0
def test_display_results():
    parser = Parser()
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'vlan': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1',
        'ipv4': '0.0.0.0',
        'ipv6': '1212::1'
    }
    endpoints = [endpoint]
    parser.display_results(
        endpoints,
        ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4', 'IPv6'],
        ipv4_only=False,
        ipv6_only=True)
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv6'],
        ipv4_only=False,
        ipv4_and_ipv6=True)
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4'],
        ipv4_only=False,
        ipv4_and_ipv6=True)
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4'],
        ipv4_only=False,
        ipv4_and_ipv6=True,
        unique=True)
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4'],
        ipv4_only=False,
        ipv4_and_ipv6=True,
        nonzero=True)
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4'],
        ipv4_only=False,
        ipv4_and_ipv6=True,
        nonzero=True,
        unique=True)
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4'],
        ipv4_only=False,
        ipv4_and_ipv6=True,
        nonzero=True,
        unique=True,
        output_format='csv')
    parser.display_results(
        endpoints, ['ID', 'MAC Address', 'Switch', 'Port', 'VLAN', 'IPv4'],
        ipv4_only=False,
        ipv4_and_ipv6=True,
        nonzero=True,
        unique=True,
        output_format='json')
Ejemplo n.º 21
0
def test_get_ipv4():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1',
        'ipv4': '0.0.0.0'
    }
    ipv4 = GetData._get_ipv4(endpoint)
    assert ipv4 == '0.0.0.0'
Ejemplo n.º 22
0
def test_get_ipv6():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1',
        'ipv6': '1212::1'
    }
    ipv6 = GetData._get_ipv6(endpoint)
    assert ipv6 == '1212::1'
Ejemplo n.º 23
0
def test_get_controller_type():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1',
        'controller_type': 'foo'
    }
    controller_type = GetData._get_controller_type(endpoint)
    assert controller_type == 'foo'
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    controller_type = GetData._get_controller_type(endpoint)
    assert controller_type == NO_DATA
Ejemplo n.º 24
0
def test_get_acls():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1',
        'ipv4': '0.0.0.0'
    }
    endpoint.acl_data = []
    acls = GetData._get_acls(endpoint)
    assert acls == '[]'
Ejemplo n.º 25
0
def test_show_endpoints():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1', 'ipv4': '0.0.0.0', 'ipv6': '1212::1'}
    endpoint.metadata = {'mac_addresses': {'00:00:00:00:00:00': {'classification': {'labels': ['developer workstation']}}}, 'ipv4_addresses': {
        '0.0.0.0': {'os': 'windows'}}, 'ipv6_addresses': {'1212::1': {'os': 'windows'}}}
    s = get_sdn_connect(logger)
    s.endpoints[endpoint.name] = endpoint
    s.show_endpoints('all')
    s.show_endpoints('state active')
    s.show_endpoints('state ignored')
    s.show_endpoints('state unknown')
    s.show_endpoints('os windows')
    s.show_endpoints('role developer-workstation')
Ejemplo n.º 26
0
def test_get_ipv6_os():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1',
        'ipv6': '1212::1'
    }
    endpoint.metadata = {'ipv6_addresses': {}}
    ipv6_os = GetData._get_ipv6_os(endpoint)
    assert ipv6_os == NO_DATA
    endpoint.metadata = {'ipv6_addresses': {'1212::1': {'short_os': 'foo'}}}
    ipv6_os = GetData._get_ipv6_os(endpoint)
    assert ipv6_os == 'foo'
Ejemplo n.º 27
0
def test_get_prev_state():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    endpoint.p_prev_state = None
    prev_state = GetData._get_prev_state(endpoint)
    assert prev_state == None
    endpoint.unknown()
    GetData._get_prev_state(endpoint)
    endpoint.queue()
    GetData._get_prev_state(endpoint)
Ejemplo n.º 28
0
def test_actions():
    """
    Tests Actions
    """
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    s = get_sdn_connect(logger)
    a = Actions(endpoint, s.sdnc)
    a.mirror_endpoint()
    a.unmirror_endpoint()
    a.coprocess_endpoint()
    a.uncoprocess_endpoint()
Ejemplo n.º 29
0
def test_actions_nosdn():
    """
    Tests Actions with no SDN controller
    """
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    s = get_sdn_connect(logger)
    s.sdnc = None
    a = Actions(endpoint, s.sdnc)
    a.mirror_endpoint()
    a.unmirror_endpoint()
    a.coprocess_endpoint()
    a.uncoprocess_endpoint()
Ejemplo n.º 30
0
def test_get_pcap_labels():
    endpoint = endpoint_factory('foo')
    endpoint.endpoint_data = {
        'tenant': 'foo',
        'mac': '00:00:00:00:00:00',
        'segment': 'foo',
        'port': '1'
    }
    endpoint.metadata = {'mac_addresses': {}}
    pcap_labels = GetData._get_pcap_labels(endpoint)
    assert pcap_labels == NO_DATA
    endpoint.metadata = {
        'mac_addresses': {
            '00:00:00:00:00:00': {
                'pcap_labels': 'foo'
            }
        }
    }
    pcap_labels = GetData._get_pcap_labels(endpoint)
    assert pcap_labels == 'foo'