Beispiel #1
0
    class func():

        def __init__(self):
            self.logger = logger
            self.faucet_event = []
            self.controller = Config().get_config()
            self.controller['max_concurrent_reinvestigations'] = 10
            self.s = SDNConnect()
            if 'POSEIDON_TRAVIS' in os.environ:
                self.s.r = redis.StrictRedis(host='localhost',
                                             port=6379,
                                             db=0,
                                             decode_responses=True)
            endpoint = Endpoint('foo')
            endpoint.endpoint_data = {
                'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
            endpoint.mirror()
            endpoint.known()
            self.s.endpoints.append(endpoint)
            endpoint = Endpoint('foo2')
            endpoint.endpoint_data = {
                'tenant': 'foo', 'mac': '00:00:00:00:00:00', 'segment': 'foo', 'port': '1'}
            endpoint.mirror()
            endpoint.known()
            self.s.endpoints.append(endpoint)
            self.s.store_endpoints()
            self.s.get_stored_endpoints()
Beispiel #2
0
 class func():
     def __init__(self):
         self.logger = logger
         self.faucet_event = []
         self.controller = Config().get_config()
         self.controller['max_concurrent_reinvestigations'] = 10
         self.s = SDNConnect(self.controller)
         endpoint = endpoint_factory('foo')
         endpoint.endpoint_data = {
             'tenant': 'foo',
             'mac': '00:00:00:00:00:00',
             'segment': 'foo',
             'port': '1'
         }
         endpoint.mirror()
         endpoint.known()
         self.s.endpoints[endpoint.name] = endpoint
         endpoint = endpoint_factory('foo2')
         endpoint.endpoint_data = {
             'tenant': 'foo',
             'mac': '00:00:00:00:00:00',
             'segment': 'foo',
             'port': '1'
         }
         endpoint.mirror()
         endpoint.known()
         self.s.endpoints[endpoint.name] = endpoint
         self.s.store_endpoints()
         self.s.get_stored_endpoints()
Beispiel #3
0
    class MockMonitor(Monitor):
        def __init__(self):
            self.logger = logger
            self.fa_rabbit_routing_key = 'FAUCET.Event'
            self.faucet_event = None
            self.controller = Config().get_config()
            self.s = SDNConnect(self.controller)
            self.s.controller['TYPE'] = 'None'
            self.s.get_sdn_context()
            self.s.controller['TYPE'] = 'bcf'
            self.s.get_sdn_context()
            self.s.controller['TYPE'] = 'faucet'
            self.s.get_sdn_context()
            self.job_queue = queue.Queue()
            self.m_queue = queue.Queue()
            endpoint = endpoint_factory('foo')
            endpoint.endpoint_data = {
                'tenant': 'foo',
                'mac': '00:00:00:00:00:00',
                'segment': 'foo',
                'port': '1'
            }
            endpoint.mirror()
            endpoint.p_prev_states.append((endpoint.state, int(time.time())))
            self.s.endpoints[endpoint.name] = endpoint
            endpoint = endpoint_factory('foo2')
            endpoint.endpoint_data = {
                'tenant': 'foo',
                'mac': '00:00:00:00:00:00',
                'segment': 'foo',
                'port': '1'
            }
            endpoint.p_next_state = 'mirror'
            endpoint.queue()
            endpoint.p_prev_states.append((endpoint.state, int(time.time())))
            self.s.endpoints[endpoint.name] = endpoint
            endpoint = endpoint_factory('foo3')
            endpoint.endpoint_data = {
                'tenant': 'foo',
                'mac': '00:00:00:00:00:00',
                'segment': 'foo',
                'port': '1'
            }
            self.s.endpoints[endpoint.name] = endpoint
            self.s.store_endpoints()
            self.s.get_stored_endpoints()
            self.results = 0

        def get_q_item(self, q, timeout=1):
            if not self.results:
                self.results += 1
                return (True, ('foo', {'data': {}}))
            return (False, None)

        def bad_get_q_item(self, q, timeout=1):
            return (False, ('bar', {'data': {}}))

        def format_rabbit_message(self, item):
            return ({'data': {}}, False)
Beispiel #4
0
    class MockMonitor(Monitor):
        def __init__(self):
            self.logger = logger
            self.fa_rabbit_routing_key = 'FAUCET.Event'
            self.faucet_event = None
            self.controller = Config().get_config()
            self.s = SDNConnect(self.controller)
            self.s.controller['TYPE'] = 'None'
            self.s.get_sdn_context()
            self.s.controller['TYPE'] = 'bcf'
            self.s.get_sdn_context()
            self.s.controller['TYPE'] = 'faucet'
            self.s.get_sdn_context()
            if 'POSEIDON_TRAVIS' in os.environ:
                self.s.r = redis.StrictRedis(host='localhost',
                                             port=6379,
                                             db=0,
                                             decode_responses=True)
            endpoint = endpoint_factory('foo')
            endpoint.endpoint_data = {
                'tenant': 'foo',
                'mac': '00:00:00:00:00:00',
                'segment': 'foo',
                'port': '1'
            }
            endpoint.mirror()
            endpoint.p_prev_states.append((endpoint.state, int(time.time())))
            self.s.endpoints[endpoint.name] = endpoint
            endpoint = endpoint_factory('foo2')
            endpoint.endpoint_data = {
                'tenant': 'foo',
                'mac': '00:00:00:00:00:00',
                'segment': 'foo',
                'port': '1'
            }
            endpoint.p_next_state = 'mirror'
            endpoint.queue()
            endpoint.p_prev_states.append((endpoint.state, int(time.time())))
            self.s.endpoints[endpoint.name] = endpoint
            endpoint = endpoint_factory('foo3')
            endpoint.endpoint_data = {
                'tenant': 'foo',
                'mac': '00:00:00:00:00:00',
                'segment': 'foo',
                'port': '1'
            }
            self.s.endpoints[endpoint.name] = endpoint
            self.s.store_endpoints()
            self.s.get_stored_endpoints()

        def get_q_item(self):
            return (True, ('foo', {'data': {}}))

        def bad_get_q_item(self):
            return (False, ('bar', {'data': {}}))

        def format_rabbit_message(self, item):
            return ({'data': {}}, False)
class Commands:
    def __init__(self):
        self.states = [
            'active', 'inactive', 'known', 'unknown', 'mirroring', 'abnormal',
            'shutdown', 'reinvestigating', 'queued'
        ]
        self.sdnc = SDNConnect()
        self.sdnc.get_stored_endpoints()

    def _get_endpoints(self, args, idx):
        ''' get endpoints that match '''
        eps = []
        device = args.rsplit(' ', 1)[idx]
        name_endpoint = self.sdnc.endpoint_by_name(device)
        if name_endpoint:
            eps.append(name_endpoint)
            return eps
        hash_endpoint = self.sdnc.endpoint_by_hash(device)
        if hash_endpoint:
            eps.append(hash_endpoint)
            return eps
        ip_endpoints = self.sdnc.endpoints_by_ip(device)
        if len(ip_endpoints) > 0:
            return ip_endpoints
        mac_endpoints = self.sdnc.endpoints_by_mac(device)
        if len(mac_endpoints) > 0:
            return mac_endpoints
        return eps

    def what_is(self, args):
        ''' what is a specific thing '''
        endpoints = []
        eps = self._get_endpoints(args, -1)
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
        return endpoints

    def history_of(self, args):
        ''' history of a specific thing '''
        endpoints = []
        eps = self._get_endpoints(args, -1)
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
        return endpoints

    def where_is(self, args):
        ''' where topologically is a specific thing '''
        endpoints = []
        eps = self._get_endpoints(args, -1)
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
        return endpoints

    def remove_inactives(self, args):
        ''' remove all inactive devices '''
        endpoints = []
        endpoint_names = []
        for endpoint in self.sdnc.endpoints:
            if endpoint.state == 'inactive':
                endpoints.append(endpoint)
                endpoint_names.append(endpoint.name)
        self.sdnc.publish_action('poseidon.action.remove.inactives',
                                 json.dumps(endpoint_names))
        return endpoints

    def remove_ignored(self, args):
        ''' remove all ignored devices '''
        endpoints = []
        endpoint_names = []
        for endpoint in self.sdnc.endpoints:
            if endpoint.ignore == True:
                endpoints.append(endpoint)
                endpoint_names.append(endpoint.name)
        self.sdnc.publish_action('poseidon.action.remove.ignored',
                                 json.dumps(endpoint_names))
        return endpoints

    def ignore(self, args):
        ''' ignore a specific thing '''
        eps = []
        device = args.rsplit(' ', 1)[0]
        if device == 'inactive':
            for endpoint in self.sdnc.endpoints:
                if endpoint.state == 'inactive':
                    eps.append(endpoint)
        else:
            eps.append(self.sdnc.endpoint_by_name(device))
            eps.append(self.sdnc.endpoint_by_hash(device))
            eps += self.sdnc.endpoints_by_ip(device)
            eps += self.sdnc.endpoints_by_mac(device)

        endpoints = []
        endpoint_names = []
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
                endpoint_names.append(endpoint.name)
        self.sdnc.publish_action('poseidon.action.ignore',
                                 json.dumps(endpoint_names))
        return endpoints

    def clear_ignored(self, args):
        ''' stop ignoring a specific thing '''
        eps = []
        device = args.rsplit(' ', 1)[0]
        if device == 'ignored':
            for endpoint in self.sdnc.endpoints:
                if endpoint.ignore:
                    eps.append(endpoint)
        else:
            eps.append(self.sdnc.endpoint_by_name(device))
            eps.append(self.sdnc.endpoint_by_hash(device))
            eps += self.sdnc.endpoints_by_ip(device)
            eps += self.sdnc.endpoints_by_mac(device)

        endpoints = []
        endpoint_names = []
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
                endpoint_names.append(endpoint.name)
        self.sdnc.publish_action('poseidon.action.clear.ignored',
                                 json.dumps(endpoint_names))
        return endpoints

    def remove(self, args):
        ''' remove and forget about a specific thing until it's seen again '''
        endpoints = []
        endpoint_names = []
        eps = self._get_endpoints(args, 0)
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
                endpoint_names.append(endpoint.name)
        self.sdnc.publish_action('poseidon.action.remove',
                                 json.dumps(endpoint_names))
        return endpoints

    def show_devices(self, arg):
        '''
        show all devices that are of a specific filter. i.e. windows,
        developer workstation, abnormal, mirroring, etc.
        '''
        return self.sdnc.show_endpoints(arg)

    def change_devices(self, args):
        ''' change state of a specific thing '''
        eps = []
        endpoints = []
        endpoint_names = []
        device = args.split(' ', 1)[0]
        state = args.rsplit(' ', 1)[-1]
        eps.append(self.sdnc.endpoint_by_name(device))
        eps.append(self.sdnc.endpoint_by_hash(device))
        eps += self.sdnc.endpoints_by_ip(device)
        eps += self.sdnc.endpoints_by_mac(device)
        for endpoint in eps:
            if endpoint:
                endpoints.append(endpoint)
                endpoint_names.append((endpoint.name, state))
        self.sdnc.publish_action('poseidon.action.change',
                                 json.dumps(endpoint_names))
        return endpoints
Beispiel #6
0
class Commands:
    def __init__(self):
        self.states = [
            'active', 'inactive', 'known', 'unknown', 'mirroring', 'abnormal',
            'shutdown', 'reinvestigating', 'queued'
        ]
        self.controller = Config().get_config()
        self.sdnc = SDNConnect(self.controller)
        self.sdnc.get_stored_endpoints()

    def _publish_action(self, address, payload):
        if payload:
            self.sdnc.publish_action(address, json.dumps(payload))

    def _get_endpoints(self, args, idx, match_all=False):
        ''' get endpoints that match '''
        device = args.rsplit(' ', 1)[idx]
        endpoints = {}
        for match_func in (self.sdnc.endpoint_by_name,
                           self.sdnc.endpoint_by_hash,
                           self.sdnc.endpoints_by_ip,
                           self.sdnc.endpoints_by_mac):
            match = match_func(device)
            if match:
                if isinstance(match, list):
                    endpoints.update(
                        {endpoint.name: endpoint
                         for endpoint in match})
                else:
                    endpoints[match.name] = match
                if not match_all:
                    break
        return endpoints.values()

    def _inactive_endpoints(self):
        return [
            endpoint for endpoint in self.sdnc.endpoints.values()
            if endpoint.state == 'inactive'
        ]

    def _ignored_endpoints(self):
        return [
            endpoint for endpoint in self.sdnc.endpoints.values()
            if endpoint.ignore
        ]

    def what_is(self, args):
        ''' what is a specific thing '''
        return self._get_endpoints(args, -1)

    def history_of(self, args):
        ''' history of a specific thing '''
        return self._get_endpoints(args, -1)

    def acls_of(self, args):
        ''' ACL history of a specific thing '''
        return self._get_endpoints(args, -1)

    def where_is(self, args):
        ''' where topologically is a specific thing '''
        return self._get_endpoints(args, -1)

    def remove_inactives(self, args):
        ''' remove all inactive devices '''
        endpoints = self._inactive_endpoints()
        endpoint_names = [endpoint.name for endpoint in endpoints]
        self._publish_action('poseidon.action.remove.inactives',
                             endpoint_names)
        return endpoints

    def remove_ignored(self, args):
        ''' remove all ignored devices '''
        endpoints = self._ignored_endpoints()
        endpoint_names = [endpoint.name for endpoint in endpoints]
        self._publish_action('poseidon.action.remove.ignored', endpoint_names)
        return endpoints

    def ignore(self, args):
        ''' ignore a specific thing '''
        device = args.rsplit(' ', 1)[0]
        if device == 'inactive':
            endpoints = self._inactive_endpoints()
        else:
            endpoints = self._get_endpoints(args, 0, match_all=True)
        endpoint_names = [endpoint.name for endpoint in endpoints]
        self._publish_action('poseidon.action.ignore', endpoint_names)
        return endpoints

    def clear_ignored(self, args):
        ''' stop ignoring a specific thing '''
        device = args.rsplit(' ', 1)[0]
        if device == 'ignored':
            endpoints = self._ignored_endpoints()
        else:
            endpoints = self._get_endpoints(args, 0, match_all=True)
        endpoint_names = [endpoint.name for endpoint in endpoints]
        self._publish_action('poseidon.action.clear.ignored', endpoint_names)
        return endpoints

    def remove(self, args):
        ''' remove and forget about a specific thing until it's seen again '''
        endpoints = self._get_endpoints(args, 0)
        endpoint_names = [endpoint.name for endpoint in endpoints]
        self._publish_action('poseidon.action.remove', endpoint_names)
        return endpoints

    def show_devices(self, arg):
        '''
        show all devices that are of a specific filter. i.e. windows,
        developer workstation, abnormal, mirroring, etc.
        '''
        return self.sdnc.show_endpoints(arg)

    def change_devices(self, args):
        ''' change state of a specific thing '''
        state = args.rsplit(' ', 1)[-1]
        endpoints = self._get_endpoints(args, 0)
        endpoint_names = [(endpoint.name, state) for endpoint in endpoints]
        self._publish_action('poseidon.action.change', endpoint_names)
        return endpoints