def test_import_and_apply_configuration(self):
        '''Test importing an example configuration and applying it'''
        self.set_mac_addresses()

        nc = ndr_netcfg.NetworkConfiguration(IMPORT_CFG_TEST)
        nc.apply_configuration()

        # If everything worked as planned, we should successfully be able to get the index numbers
        # based on the new interface names

        self.assertEqual(self._dummy0_idx,
                         self._iproute.link_lookup(ifname='lan127')[0])
        self.assertEqual(self._dummy1_idx,
                         self._iproute.link_lookup(ifname='monitor234')[0])
    def configure_interfaces(self, config_file=None):
        '''Sets up interfaces for most tests'''

        nc = ndr_netcfg.NetworkConfiguration(config_file)
        nc.rename_interface("dummy0", "lan127")
        nc.set_configuration_method(
            "lan127", ndr_netcfg.InterfaceConfigurationMethods.STATIC)
        nc.add_static_addr("lan127", "10.1.177.2", 24)

        nc.rename_interface("dummy1", "monitor234")
        nc.set_configuration_method(
            "monitor234", ndr_netcfg.InterfaceConfigurationMethods.STATIC)
        nc.add_static_addr("monitor234", "10.2.177.2", 24)

        nc.apply_configuration()
        return nc
Beispiel #3
0
    def __init__(self,
                 netcfg_file='/persistant/etc/ndr/network_config.yml',
                 nmap_cfgfile='/persistant/etc/ndr/nmap_config.yml'):
        self.scan_interfaces = []
        self.networks_to_scan = []
        self.nmap_cfgfile = nmap_cfgfile

        # Handle our blacklists here.
        self.mac_address_config = {}
        self.ip_address_config = {}

        # Pull our interfaces from the NDR network configuration
        netcfg = ndr_netcfg.NetworkConfiguration(netcfg_file)
        interfaces = netcfg.get_all_managed_interfaces()

        # Loop through the interfaces we'll scan on
        if netcfg_file is not None:
            for interface in interfaces:
                if 'lan' not in interface.name:
                    continue # Interface we don't care about

                # Add this interface to networks we care about
                self.scan_interfaces.append(interface.name)

                # Append the networks we're configured for to the list
                for addr in interface.current_ip_addresses:
                    self.networks_to_scan.append(
                        addr.ip_network()
                    )

                # Deduplicate scan list
                self.networks_to_scan = list(set(self.networks_to_scan))

        # This config file is optional so it's non-fatal if we don't find it
        if nmap_cfgfile is not None:
            try:
                with open(nmap_cfgfile, 'r') as f:
                    config_dict = f.read()
                    cfg_dict = yaml.safe_load(config_dict)
                    if cfg_dict is not None: # What happens when pyYAML reads an empty file
                        self.from_dict(cfg_dict)

            except FileNotFoundError:
                self.nmap_cfgfile = None
Beispiel #4
0
    def configure_interfaces(self, config_file=None):
        '''Sets up interfaces for most tests'''

        nc = ndr_netcfg.NetworkConfiguration(config_file)
        nc.set_configuration_method(
            "lan127", ndr_netcfg.InterfaceConfigurationMethods.STATIC)
        nc.add_static_addr("lan127", "10.1.177.2", 24)

        nc.set_configuration_method(
            "monitor234", ndr_netcfg.InterfaceConfigurationMethods.STATIC)
        nc.add_static_addr("monitor234", "10.2.177.2", 24)

        # Create an IPv6 enabled interface
        nc.set_configuration_method(
            "lan322", ndr_netcfg.InterfaceConfigurationMethods.STATIC)
        nc.add_static_addr("lan322", "192.168.17.2", 28)
        nc.add_static_addr("lan322", "fdd1:2013:2f69:388f::122", 64)

        nc.apply_configuration()
        return nc
Beispiel #5
0
def main():
    '''Applies the network configuration saved in a config file'''
    parser = argparse.ArgumentParser(
        description="Interactively reconfigures the network interfaces for NDR"
    )
    parser.add_argument('-c',
                        '--config',
                        default='/persistant/etc/ndr/network_config.yml',
                        help='Network Configuration File')
    parser.add_argument(
        '--oneshot',
        action='store_true',
        help=
        'Only configures the network once, instead of running helper daemons')
    args = parser.parse_args()

    if os.getuid() != 0:
        print("ERROR: must be run as root")
        return

    net_config = ndr_netcfg.NetworkConfiguration(args.config)
    net_config.apply_configuration(oneshot=args.oneshot)