Exemple #1
0
    def login(self):
        """ Show login form.
        """

        if request.method != 'POST':
            cfg = NipapConfig()
            try:
                c.welcome_message = cfg.get('www', 'welcome_message')
            except NoOptionError:
                pass

            return render('login.html')

        # Verify username and password.
        auth_fact = AuthFactory()
        auth = auth_fact.get_auth(request.params.get('username'),
                                  request.params.get('password'), 'nipap')
        if not auth.authenticate():
            c.error = 'Invalid username or password'
            return render('login.html')

        # Mark user as logged in
        session['user'] = auth.username
        session['full_name'] = auth.full_name
        session['readonly'] = auth.readonly
        session['current_vrfs'] = {}
        session.save()

        # Send user back to the page he originally wanted to get to
        if session.get('path_before_login'):
            redirect(session['path_before_login'])

        else:
            # if previous target is unknown just send the user to a welcome page
            redirect(url(controller='prefix', action='list'))
Exemple #2
0
    def login(self):
        """ Show login form.
        """

        if request.method != 'POST':
            cfg = NipapConfig()
            try:
                c.welcome_message = cfg.get('www', 'welcome_message')
            except NoOptionError:
                pass

            return render('login.html')

        # Verify username and password.
        auth_fact = AuthFactory()
        auth = auth_fact.get_auth(request.params.get('username'), request.params.get('password'), 'nipap')
        if not auth.authenticate():
            c.error = 'Invalid username or password'
            return render('login.html')

        # Mark user as logged in
        session['user'] = auth.username
        session['full_name'] = auth.full_name
        session['current_vrfs'] = {}
        session.save()

        # Send user back to the page he originally wanted to get to
        if session.get('path_before_login'):
            redirect(session['path_before_login'])

        else:
            # if previous target is unknown just send the user to a welcome page
            redirect(url(controller='vrf', action='list'))
Exemple #3
0
    def setUp(self):

        # logging
        self.logger = logging.getLogger(self.__class__.__name__)

        # NIPAP
        self.cfg = NipapConfig('/etc/nipap/nipap.conf')
        self.nipap = Nipap()

        # create dummy auth object
        # As the authentication is performed before the query hits the Nipap
        # class, it does not matter what user we use here
        self.auth = SqliteAuth('local', 'unittest', 'unittest', 'unittest')
        self.auth.authenticated_as = 'unittest'
        self.auth.full_name = 'Unit test'

        # have to delete hosts before we can delete the rest
        self.nipap._execute(
            "DELETE FROM ip_net_plan WHERE masklen(prefix) = 32")
        # the rest
        self.nipap._execute("DELETE FROM ip_net_plan")
        # delete all except for the default VRF with id 0
        self.nipap._execute("DELETE FROM ip_net_vrf WHERE id > 0")
        # set default info for VRF 0
        self.nipap._execute(
            "UPDATE ip_net_vrf SET name = 'default', description = 'The default VRF, typically the Internet.' WHERE id = 0"
        )
        self.nipap._execute("DELETE FROM ip_net_pool")
        self.nipap._execute("DELETE FROM ip_net_asn")
Exemple #4
0
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config``
    object
    """
    config = PylonsConfig()
    
    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='nipapwww', paths=paths)

    config['routes.map'] = make_map(config)
    config['pylons.app_globals'] = app_globals.Globals(config)
    config['pylons.h'] = nipapwww.lib.helpers
    
    # Setup cache object as early as possible
    import pylons
    pylons.cache._push_object(config['pylons.app_globals'].cache)
    

    # Create the Jinja2 Environment
    jinja2_env = Environment(autoescape=True,
            extensions=['jinja2.ext.autoescape'],
            loader=FileSystemLoader(paths['templates']))
    config['pylons.app_globals'].jinja2_env = jinja2_env

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)
    config['pylons.strict_c'] = False

    # Make sure that there is a configuration object
    cfg = NipapConfig(config['nipap_config_path'], 
        { 'auth_cache_timeout': '3600' })

    # set XML-RPC URI in pynipap module
    pynipap.xmlrpc_uri = cfg.get('www', 'xmlrpc_uri')

    return config
Exemple #5
0
def load_environment(global_conf, app_conf):
    """Configure the Pylons environment via the ``pylons.config``
    object
    """
    config = PylonsConfig()

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(
        root=root,
        controllers=os.path.join(root, "controllers"),
        static_files=os.path.join(root, "public"),
        templates=[os.path.join(root, "templates")],
    )

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package="nipapwww", paths=paths)

    config["routes.map"] = make_map(config)
    config["pylons.app_globals"] = app_globals.Globals(config)
    config["pylons.h"] = nipapwww.lib.helpers

    # Setup cache object as early as possible
    import pylons

    pylons.cache._push_object(config["pylons.app_globals"].cache)

    # Create the Jinja2 Environment
    jinja2_env = Environment(loader=FileSystemLoader(paths["templates"]))
    config["pylons.app_globals"].jinja2_env = jinja2_env

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)
    config["pylons.strict_c"] = False

    # Make sure that there is a configuration object
    cfg = NipapConfig(config["nipap_config_path"], {"auth_cache_timeout": "3600"})

    # set XML-RPC URI in pynipap module
    pynipap.xmlrpc_uri = cfg.get("www", "xmlrpc_uri")

    return config
Exemple #6
0
    def clear_database(cls):
        cfg = NipapConfig('/etc/nipap/nipap.conf')
        n = Nipap()

        # have to delete hosts before we can delete the rest
        n._execute("DELETE FROM ip_net_plan WHERE masklen(prefix) = 32")
        # the rest
        n._execute("DELETE FROM ip_net_plan")
        # delete all except for the default VRF with id 0
        n._execute("DELETE FROM ip_net_vrf WHERE id > 0")
        # set default info for VRF 0
        n._execute("UPDATE ip_net_vrf SET name = 'default', description = 'The default VRF, typically the Internet.' WHERE id = 0")
        n._execute("DELETE FROM ip_net_pool")
        n._execute("DELETE FROM ip_net_asn")
Exemple #7
0
def _register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter."""
    from psycopg2 import extensions as _ext
    if not oid: oid = 869
    _ext.INET = _ext.new_type((oid, ), "INET",
            lambda data, cursor: data and Inet(data) or None)
    _ext.register_type(_ext.INET, conn_or_curs)
    return _ext.INET



if __name__ == '__main__':

    # connect to old database
    # Get database configuration
    cfg = NipapConfig(nipap_cfg_path)
    db_args = {}
    db_args['host'] = cfg.get('nipapd', 'db_host')
    db_args['database'] = 'nipap_old'
    db_args['user'] = cfg.get('nipapd', 'db_user')
    db_args['password'] = cfg.get('nipapd', 'db_pass')
    db_args['sslmode'] = cfg.get('nipapd', 'db_sslmode')
    # delete keys that are None, for example if we want to connect over a
    # UNIX socket, the 'host' argument should not be passed into the DSN
    if db_args['host'] is not None and db_args['host'] == '':
        db_args['host'] = None
    for key in db_args.copy():
        if db_args[key] is None:
            del(db_args[key])

    # Create database connection to old db
Exemple #8
0
def _register_inet(oid=None, conn_or_curs=None):
    """Create the INET type and an Inet adapter."""
    from psycopg2 import extensions as _ext
    if not oid: oid = 869
    _ext.INET = _ext.new_type((oid, ), "INET",
                              lambda data, cursor: data and Inet(data) or None)
    _ext.register_type(_ext.INET, conn_or_curs)
    return _ext.INET


if __name__ == '__main__':

    # connect to old database
    # Get database configuration
    cfg = NipapConfig(nipap_cfg_path)
    db_args = {}
    db_args['host'] = cfg.get('nipapd', 'db_host')
    db_args['database'] = 'nipap_old'
    db_args['user'] = cfg.get('nipapd', 'db_user')
    db_args['password'] = cfg.get('nipapd', 'db_pass')
    db_args['sslmode'] = cfg.get('nipapd', 'db_sslmode')
    # delete keys that are None, for example if we want to connect over a
    # UNIX socket, the 'host' argument should not be passed into the DSN
    if db_args['host'] is not None and db_args['host'] == '':
        db_args['host'] = None
    for key in db_args.copy():
        if db_args[key] is None:
            del (db_args[key])

    # Create database connection to old db
Exemple #9
0
    def setUp(self):
        """ Better start from a clean slate!
        """

        cfg = NipapConfig('/etc/nipap/nipap.conf')
        self.nipap = Nipap()

        # create dummy auth object
        # As the authentication is performed before the query hits the Nipap
        # class, it does not matter what user we use here
        self.auth = SqliteAuth('local', 'unittest', 'unittest', 'unittest')
        self.auth.authenticated_as = 'unittest'
        self.auth.full_name = 'Unit test'

        self.nipap._execute(
            "TRUNCATE ip_net_plan, ip_net_pool, ip_net_vrf, ip_net_log, ip_net_asn"
        )

        self.schema_attrs = {
            'name': 'test-schema1',
            'description': 'Test schema numero uno!'
        }
        self.schema_attrs['id'] = self.nipap.add_schema(
            self.auth, self.schema_attrs)
        self.schema_attrs2 = {
            'name': 'test-schema2',
            'description': 'Test schema numero dos!'
        }
        self.schema_attrs2['id'] = self.nipap.add_schema(
            self.auth, self.schema_attrs2)
        self.pool_attrs = {
            'name': 'test-pool1',
            'description': 'Test pool numero uno!',
            'default_type': 'assignment',
            'ipv4_default_prefix_length': 30,
            'ipv6_default_prefix_length': 112
        }
        self.pool_attrs['id'] = self.nipap.add_pool(
            self.auth, {'id': self.schema_attrs['id']}, self.pool_attrs)
        self.prefix_attrs1 = {
            'authoritative_source': 'nipaptest',
            'prefix': '1.3.3.0/24',
            'type': 'assignment',
            'description': ''
        }
        self.prefix_attrs1['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix_attrs1)
        self.prefix_attrs = {
            'authoritative_source': 'nipaptest',
            'prefix': '1.3.3.1/32',
            'type': 'host',
            'description': 'Test prefix numero uno!'
        }
        self.prefix_attrs['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix_attrs)
        self.prefix_attrs2 = {
            'authoritative_source': 'nipaptest',
            'prefix': '1.3.2.0/23',
            'type': 'reservation',
            'description': ''
        }
        self.prefix_attrs2['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix_attrs2)
        self.prefix_attrs3 = {
            'authoritative_source': 'nipaptest',
            'prefix': '1.3.0.0/16',
            'type': 'reservation',
            'description': ''
        }
        self.prefix_attrs3['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix_attrs3)
        self.prefix_attrs4 = {
            'authoritative_source': 'nipaptest',
            'prefix': '1.3.0.0/17',
            'type': 'reservation',
            'description': ''
        }
        self.prefix_attrs4['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs2['id']}, self.prefix_attrs4)

        self.prefix6_attrs1 = {
            'authoritative_source': 'nipaptest',
            'prefix': '2001:0db8:3:3::/112',
            'type': 'assignment',
            'description': ''
        }
        self.prefix6_attrs1['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix6_attrs1)
        self.prefix6_attrs = {
            'authoritative_source': 'nipaptest',
            'prefix': '2001:0db8:3:3::1/128',
            'type': 'host',
            'description': 'Test prefix numero uno!'
        }
        self.prefix6_attrs['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix6_attrs)
        self.prefix6_attrs2 = {
            'authoritative_source': 'nipaptest',
            'prefix': '2001:0db8:3:3::/64',
            'type': 'reservation',
            'description': ''
        }
        self.prefix6_attrs2['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix6_attrs2)
        self.prefix6_attrs3 = {
            'authoritative_source': 'nipaptest',
            'prefix': '2001:0db8:3:0::/48',
            'type': 'reservation',
            'description': ''
        }
        self.prefix6_attrs3['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs['id']}, self.prefix6_attrs3)
        self.prefix6_attrs4 = {
            'authoritative_source': 'nipaptest',
            'prefix': '2001:0db8:3:0::/56',
            'type': 'reservation',
            'description': ''
        }
        self.prefix6_attrs4['id'] = self.nipap.add_prefix(
            self.auth, {'id': self.schema_attrs2['id']}, self.prefix6_attrs4)