From 13abc7d035f5484a7b32c53ff45fd88d2dd85d35 Mon Sep 17 00:00:00 2001 From: Plamen Dimitrov Date: Sat, 9 Apr 2022 08:05:30 +0300 Subject: [PATCH] Drop the cnfline subpackage and reuse all of its defaults for templates In particular, add defaults for already available templates through the old mk_config but also migrate the cnfline ones in the same location. --- setup.py | 2 +- src/arnied_wrapper.py | 61 ---- src/cnfline/build_cnfvar.py | 142 --------- src/cnfline/build_group.py | 81 ------ src/cnfline/build_intraclient.py | 59 ---- src/cnfline/build_key.py | 157 ---------- src/cnfline/build_nic.py | 62 ---- src/cnfline/build_provider.py | 84 ------ src/cnfline/build_proxy_accesslist.py | 52 ---- src/cnfline/build_proxy_profile.py | 37 --- src/cnfline/build_vpnconn.py | 91 ------ src/cnfline/cnfline.py | 64 ---- src/cnfline/configure_proxy_antivirus.py | 61 ---- src/cnfline/configure_webfilter.py | 72 ----- src/cnfvar/templates.py | 459 +++++++++++++++--------------- test/test_build_cnfvar.py | 155 ---------- test/test_cnfline.py | 74 ----- 17 files changed, 225 insertions(+), 1488 deletions(-) delete mode 100644 src/cnfline/__init__.py delete mode 100644 src/cnfline/build_cnfvar.py delete mode 100644 src/cnfline/build_group.py delete mode 100644 src/cnfline/build_intraclient.py delete mode 100644 src/cnfline/build_key.py delete mode 100644 src/cnfline/build_nic.py delete mode 100644 src/cnfline/build_provider.py delete mode 100644 src/cnfline/build_proxy_accesslist.py delete mode 100644 src/cnfline/build_proxy_profile.py delete mode 100644 src/cnfline/build_vpnconn.py delete mode 100644 src/cnfline/cnfline.py delete mode 100644 src/cnfline/configure_proxy_antivirus.py delete mode 100644 src/cnfline/configure_webfilter.py delete mode 100755 test/test_build_cnfvar.py delete mode 100644 test/test_cnfline.py diff --git a/setup.py b/setup.py index 01966be..44a68f5 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ setup(name='pyi2ncommon', author='Intra2net AG', author_email='info@intra2net.com', url='http://www.intra2net.com', - packages=['pyi2ncommon', 'pyi2ncommon.cnfline', 'pyi2ncommon.cnfvar'], + packages=['pyi2ncommon', 'pyi2ncommon.cnfvar'], package_dir={'pyi2ncommon': 'src'}, license_files=('COPYING.GPL', 'Linking-Exception.txt'), license='GPLv2 + linking exception', diff --git a/src/arnied_wrapper.py b/src/arnied_wrapper.py index e033aaf..af6acc0 100644 --- a/src/arnied_wrapper.py +++ b/src/arnied_wrapper.py @@ -58,7 +58,6 @@ import tempfile import logging log = logging.getLogger('pyi2ncommon.arnied_wrapper') -from .cnfline import build_cnfvar from . import cnfvar_old from . import sysmisc @@ -866,63 +865,3 @@ def generate_config_path(dumped=False): os.close(fd) os.unlink(filename) return filename - - -# enum -Delete = 0 -Update = 1 -Add = 2 -Child = 3 - - -def batch_update_cnf(cnf, vars): - """ - Perform a batch update of multiple cnf variables. - - :param cnf: CNF variable to update - :type cnf: BuildCnfVar object - :param vars: tuples of enumerated action and subtuple with data - :type vars: [(int, (str, int, str))] - :returns: updated CNF variable - :rtype: BuildCnfVar object - - The actions are indexed in the same order: delete, update, add, child. - """ - last = 0 - for (action, data) in vars: - if action == Update: - var, ref, val = data - last = cnf.update_cnf(var, ref, val) - elif action == Add: - var, ref, val = data - last = cnf.add_cnf(var, ref, val) - elif action == Delete: - last = cnf.del_cnf(data) - elif action == Child: # only one depth supported - var, ref, val = data - # do not update last - cnf.add_cnf(var, ref, val, different_parent_line_no=last) - return cnf - - -def build_cnf(kind, instance=0, vals=[], data="", filename=None): - """ - Build a CNF variable and save it in a config file. - - :param str kind: name of the CNF variable - :param int instance: instance number of the CNF variable - :param vals: tuples of enumerated action and subtuple with data - :type vals: [(int, (str, int, str))] - :param str data: data for the CNF variable - :param filename: optional custom name of the config file - :type filename: str or None - :returns: name of the saved config file - :rtype: str - """ - builder = build_cnfvar.BuildCnfVar(kind, instance=instance, data=data) - batch_update_cnf(builder, vals) - filename = generate_config_path(dumped=True) if filename is None else filename - [filename] = prep_config_paths([filename], DUMP_CONFIG_DIR) - builder.save(filename) - return filename - diff --git a/src/cnfline/__init__.py b/src/cnfline/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/cnfline/build_cnfvar.py b/src/cnfline/build_cnfvar.py deleted file mode 100644 index 8402f52..0000000 --- a/src/cnfline/build_cnfvar.py +++ /dev/null @@ -1,142 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -"""Basic functions for on-the-fly arnied cnf-var generator. - -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from .cnfline import CnfLine - - -class BuildCnfVar(object): - """Basic one the fly arnied cnfvar generator""" - def __init__(self, name, instance=0, data='', line_no=1): - self._parent = CnfLine(name, instance, data, line_no) - self._children = {} # key: lineno, value: CnfLine - - def __getattr__(self, name): - # Redirect all unknown attributes to "parent" cnfline - return getattr(self._parent, name) - - def find_child_line_no(self, name): - """Look for child line number of child NAME""" - for lineno, cnfline in self._children.items(): - if cnfline.name == name: - return lineno - - # Not found - return 0 - - def find_free_line_no(self): - """Find highest currently in use line number""" - highest_line_no = self.line_no - - for line_no, unused in self._children.items(): - if line_no > highest_line_no: - highest_line_no = line_no - - return highest_line_no+1 - - def find_free_child_instance(self, name): - """Find next free child instance of type NAME""" - highest_instance = -1 - - for unused, cnfline in self._children.items(): - if cnfline.name == name and cnfline.instance > highest_instance: - highest_instance = cnfline.instance - - return highest_instance+1 - - def update_cnf(self, - name, - instance, - data, - different_parent_line_no=0, - force_append=False): - """Update existing cnfline or create new one""" - if not force_append: - child_line_no = self.find_child_line_no(name) - else: - child_line_no = 0 - - if child_line_no == 0: - child_line_no = self.find_free_line_no() - - if instance == -1: - instance = self.find_free_child_instance(name) - - parent_line_no = self._parent.line_no - if different_parent_line_no: - parent_line_no = different_parent_line_no - - new_line = CnfLine(name, - instance, - data, - child_line_no, - parent_line_no) - - self._children[child_line_no] = new_line - return child_line_no - - def mark_as_own_parent(self, child_line_no): - """Remove parent <-> child relationship for - a given cnf line. We use this heavily - for the *configure_xxx.py* files""" - self._children[child_line_no].parent_line_no = 0 - - def add_cnf(self, - name, - instance, - data, - different_parent_line_no=0): - return self.update_cnf(name, - instance, - data, - different_parent_line_no, - force_append=True) - - def del_cnf(self, name): - """Delete cnfline with name""" - for lineno, cnfline in list(self._children.items()): - if cnfline.name == name: - del self._children[lineno] - - def add_defaults(self, defaults=None): - """Add default values from a simple dictionary""" - if defaults is None: - return - - for name, value in defaults.items(): - self.update_cnf(name, 0, value) - - def __str__(self): - rtn = str(self._parent) + '\n' - for unused, value in self._children.items(): - rtn = rtn + str(value) + '\n' - - return rtn - - def save(self, filename): - """Save string representation to disk.""" - with open(filename, 'w') as out: - out.write(str(self)) diff --git a/src/cnfline/build_group.py b/src/cnfline/build_group.py deleted file mode 100644 index 41efb30..0000000 --- a/src/cnfline/build_group.py +++ /dev/null @@ -1,81 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -from .build_cnfvar import BuildCnfVar -"""Class to create group objects on the fly""" - - -class BuildGroup(BuildCnfVar): - - def __init__(self, data='', instance=0, line_no=1): - BuildCnfVar.__init__(self, 'GROUP', instance, data, line_no) - - # the bare defaults the UI adds upon - # creation of new groups - defaults = { - 'GROUP_COMMENT': '', - } - - self.add_defaults(defaults) - - def comment(self, comment): - self.update_cnf('GROUP_COMMENT', 0, comment) - return self - - def enable_activesync(self): - self.update_cnf('GROUP_ACTIVESYNC_ENABLE', 0, '1') - return self - - def enable_xauth(self): - self.update_cnf('GROUP_XAUTH_ENABLE', 0, '1') - return self - - def enable_go_online(self): - self.update_cnf('GROUP_ACCESS_GO_ONLINE_ALLOWED', 0, '1') - return self - - def enable_remote_administration(self): - self.update_cnf('GROUP_ACCESS_REMOTE_ADMINISTRATION_ALLOWED', 0, '1') - return self - - def email_quota(self, quota): - self.update_cnf('GROUP_EMAIL_QUOTA', 0, quota) - return self - - def email_relay_rights_block_relay(self): - self.update_cnf('GROUP_EMAIL_RELAY_RIGHTS', 0, 'BLOCK_RELAY') - return self - - def email_relay_rights_from_intranet(self): - self.update_cnf('GROUP_EMAIL_RELAY_RIGHTS', 0, 'RELAY_FROM_INTRANET') - return self - - def email_relay_rights_from_everywhere(self): - self.update_cnf('GROUP_EMAIL_RELAY_RIGHTS', 0, 'RELAY_FROM_EVERYWHERE') - return self - - def emailfilter_ban_filterlist_ref(self, filterlist_ref): - self.update_cnf('GROUP_EMAILFILTER_BAN_FILTERLIST_REF', 0, - filterlist_ref) - return self - - def proxy_profile_ref(self, profile_ref): - self.update_cnf('GROUP_PROXY_PROFILE_REF', 0, profile_ref) - return self diff --git a/src/cnfline/build_intraclient.py b/src/cnfline/build_intraclient.py deleted file mode 100644 index c6accb7..0000000 --- a/src/cnfline/build_intraclient.py +++ /dev/null @@ -1,59 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -Create intraclients. -""" - -from . import build_cnfvar - -default_intraclient_name = "sample client" -default_intraclient_instance = 1 -default_cnfvars = { - "INTRACLIENT_COMMENT": default_intraclient_name + " comment", - "INTRACLIENT_DNS_RELAYING_ALLOWED": "1", - "INTRACLIENT_EMAIL_RELAYING_ALLOWED": "1", - "INTRACLIENT_FIREWALL_RULESET_REF": "5", - "INTRACLIENT_IP": None, - "INTRACLIENT_MAC": None, - "INTRACLIENT_PROXY_PROFILE_REF": "-1", -} - - -class BuildIntraclient(build_cnfvar.BuildCnfVar): - - def __init__(self, - data=default_intraclient_name, - instance=default_intraclient_instance, - line_no=1, - ip="192.168.0.42", - mac=None, - alias=None): - build_cnfvar.BuildCnfVar.__init__(self, - "INTRACLIENT", - instance, - data, line_no) - self.add_defaults(default_cnfvars) - - self.update_cnf("INTRACLIENT_IP", 0, ip) - self.update_cnf("INTRACLIENT_MAC", 0, mac or "") - - if alias is not None: - self.update_cnf("INTRACLIENT_ALIAS", 0, alias) diff --git a/src/cnfline/build_key.py b/src/cnfline/build_key.py deleted file mode 100644 index bf471dc..0000000 --- a/src/cnfline/build_key.py +++ /dev/null @@ -1,157 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -from .build_cnfvar import BuildCnfVar -"""Class to create own keys cnfvar on the fly """ - - -class BuildKey(BuildCnfVar): - def __init__(self, data='sample key', instance=0, line_no=1): - BuildCnfVar.__init__(self, 'KEY_OWN', instance, data, line_no) - - # set some dummy data for cnf_check - defaults = { - 'KEY_OWN_FINGERPRINT_MD5': '76:3B:CF:8E:CB:BF:A5:7D:CC:87:39:FA:CE:99:2E:96', - 'KEY_OWN_FINGERPRINT_SHA1': 'ED:5A:C6:D9:5B:BE:47:1F:B9:4F:CF:A3:80:3B:42:08:F4:00:16:96', - 'KEY_OWN_ID_X509': 'CN=some.net.lan', - 'KEY_OWN_ISSUER': 'CN=ab, C=fd, L=ab, ST=ab, O=ab, OU=ab/emailAddress=ab@ab.com', - 'KEY_OWN_KEYSIZE': '2048', - 'KEY_OWN_HASH_ALGO': 'SHA2_256', - 'KEY_OWN_PRIVATE_KEY': '-----BEGIN PRIVATE KEY-----\\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKk' + - 'ZTzqHXg41RZMiY+ywRZ037pBq8J3BkH\\n-----END PRIVATE KEY-----\\n', - 'KEY_OWN_PUBLIC_KEY': '-----BEGIN CERTIFICATE-----\\nMIIFCTCCAvGgAwIBAgIEVBGDuTANBgkqhkiG' + - '9w0BAQsFADCBgTEPMA0GA1UEAwwG\\n-----END CERTIFICATE-----\\n', - 'KEY_OWN_REQUEST': '-----BEGIN CERTIFICATE REQUEST-----\\nMIIDCzCCAfMCAQAwIjEgMB4GA1UEAww' + - 'XaW50cmFkZXYtYWllc\\n-----END CERTIFICATE REQUEST-----\\n', - 'KEY_OWN_SUBJECT': 'CN=some.net.lan', - 'KEY_OWN_VALIDFROM': '20140911T111257', - 'KEY_OWN_VALIDTILL': '20160731T134608', - 'KEY_OWN_TYPE': 'SELF', - - # the ones bellow should be set when using 'generate' to create the key - 'KEY_OWN_CREATE_CN': 'somehost', - 'KEY_OWN_CREATE_EMAIL': 'default@intra2net.com' - } - - self.add_defaults(defaults) - - def country(self, country): - self.update_cnf('KEY_OWN_CREATE_C', 0, country) - return self - - def state(self, state): - self.update_cnf('KEY_OWN_CREATE_ST', 0, state) - return self - - def city(self, city): - self.update_cnf('KEY_OWN_CREATE_L', 0, city) - return self - - def company(self, company): - self.update_cnf('KEY_OWN_CREATE_O', 0, company) - return self - - def department(self, department): - self.update_cnf('KEY_OWN_CREATE_OU', 0, department) - return self - - def computer_name(self, computer_name): - self.update_cnf('KEY_OWN_CREATE_CN', 0, computer_name) - return self - - def email(self, email): - self.update_cnf('KEY_OWN_CREATE_EMAIL', 0, email) - return self - - def days(self, days): - self.update_cnf('KEY_OWN_CREATE_DAYS', 0, days) - return self - - def keysize(self, keysize): - self.update_cnf('KEY_OWN_KEYSIZE', 0, keysize) - return self - - def hash_algo(self, hash_algo): - self.update_cnf('KEY_OWN_HASH_ALGO', 0, hash_algo) - return self - - def certchain(self, certchain): - self.update_cnf('KEY_OWN_CERTCHAIN', 0, certchain) - return self - - def cerchain_count(self, cerchain_count): - self.update_cnf('KEY_OWN_CERTCHAIN_CERTCOUNT', 0, cerchain_count) - return self - - def create_subjalt(self, create_subjalt): - self.update_cnf('KEY_OWN_CREATE_SUBJALT', 0, create_subjalt) - return self - - def create_subjalt_type(self, create_subjalt_type): - self.update_cnf('KEY_OWN_CREATE_SUBJALT_TYPE', 0, create_subjalt_type) - return self - - def fingerprint_md5(self, fingerprint_md5): - self.update_cnf('KEY_OWN_FINGERPRINT_MD5', 0, fingerprint_md5) - return self - - def fingerprint_sha1(self, fingerprint_sha1): - self.update_cnf('KEY_OWN_FINGERPRINT_SHA1', 0, fingerprint_sha1) - return self - - def id_x509(self, id_x509): - self.update_cnf('KEY_OWN_ID_X509', 0, id_x509) - return self - - def issuer(self, issuer): - self.update_cnf('KEY_OWN_ISSUER', 0, issuer) - return self - - def private_key(self, private_key): - self.update_cnf('KEY_OWN_PRIVATE_KEY', 0, private_key) - return self - - def public_key(self, public_key): - self.update_cnf('KEY_OWN_PUBLIC_KEY', 0, public_key) - return self - - def request(self, request): - self.update_cnf('KEY_OWN_REQUEST', 0, request) - return self - - def subject(self, subject): - self.update_cnf('KEY_OWN_SUBJECT', 0, subject) - return self - - def subject_alt(self, subject_alt): - self.update_cnf('KEY_OWN_SUBJECT_ALT', 0, subject_alt) - return self - - def key_type(self, key_type): - self.update_cnf('KEY_OWN_TYPE', 0, key_type) - return self - - def valid_from(self, valid_from): - self.update_cnf('KEY_OWN_VALIDFROM', 0, valid_from) - return self - - def valid_till(self, valid_till): - self.update_cnf('KEY_OWN_VALIDTILL', 0, valid_till) - return self diff --git a/src/cnfline/build_nic.py b/src/cnfline/build_nic.py deleted file mode 100644 index 3d870e5..0000000 --- a/src/cnfline/build_nic.py +++ /dev/null @@ -1,62 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -from .build_cnfvar import BuildCnfVar -"""Class to create user cnfvar objects on the fly""" - - -class BuildNIC(BuildCnfVar): - - def __init__(self, data='', instance=0, line_no=1): - BuildCnfVar.__init__(self, 'NIC', instance, '', line_no) - - # the bare defaults the UI adds upon - # creation of new groups - defaults = { - 'NIC_COMMENT': data, - 'NIC_DRIVER': 'virtio_net', - 'NIC_LAN_DNS_RELAYING_ALLOWED': "0", - 'NIC_LAN_EMAIL_RELAYING_ALLOWED': "0", - 'NIC_LAN_FIREWALL_RULESET_REF': "1", - 'NIC_LAN_IP': "192.168.1.1", - 'NIC_LAN_NAT_INTO': "0", - 'NIC_LAN_NETMASK': "255.255.255.0", - 'NIC_LAN_PROXY_PROFILE_REF': "-1", - 'NIC_MAC': '02:00:00:00:20:00', - 'NIC_TYPE': 'DSLROUTER', - } - - self.add_defaults(defaults) - - def comment(self, comment): - self.update_cnf('NIC_COMMENT', 0, comment) - return self - - def nic_type(self, nic_type): - self.update_cnf('NIC_TYPE', 0, nic_type) - return self - - def lan_ip(self, lan_ip): - self.update_cnf('NIC_LAN_IP', 0, lan_ip) - return self - - def add_group_member_ref(self, group_ref): - self.add_cnf('USER_GROUP_MEMBER_REF', -1, group_ref) - return self diff --git a/src/cnfline/build_provider.py b/src/cnfline/build_provider.py deleted file mode 100644 index db051a1..0000000 --- a/src/cnfline/build_provider.py +++ /dev/null @@ -1,84 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -Create provider profiles. - -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from . import build_cnfvar - -# Defaults are extracted from data/shared_arnied/provider.cnf. -default_provider_name = "sample_provider" -default_provider_instance = 1 -default_cnfvars = { - "PROVIDER_PROXY_SERVER": "", - "PROVIDER_PROXY_PORT": "", - "PROVIDER_PROXY_PASSWORD": "", - "PROVIDER_PROXY_LOGIN": "", - "PROVIDER_NIC_REF": "1", - "PROVIDER_NETMASK": "255.255.0.0", - "PROVIDER_MTU_SIZE": "1500", - "PROVIDER_MODE": "ROUTER", - "PROVIDER_MAILTRANSFER_MODE": "IMMEDIATE", - "PROVIDER_LOCALIP": "", - "PROVIDER_IP": "", - "PROVIDER_FIREWALL_RULESET_REF": "7", - "PROVIDER_FALLBACK_TIMEOUT": "60", - "PROVIDER_FALLBACK_PROVIDER_REF": "-1", - "PROVIDER_EMAIL_RELAY_REF": "-1", - "PROVIDER_DYNDNS_WEBCHECKIP": "0", - "PROVIDER_DYNDNS_ENABLE": "1", - "PROVIDER_DNS_MODE": "IP", - "PROVIDER_DNS": "", - "PROVIDER_BWIDTH_MANAGEMENT_UPSTREAM_SPEED": "", - "PROVIDER_BWIDTH_MANAGEMENT_ENABLE": "0", - "PROVIDER_BWIDTH_MANAGEMENT_DOWNSTREAM_SPEED": "", - "PROVIDER_PINGCHECK_SERVERLIST_REF": "-2", -} - - -class BuildProvider(build_cnfvar.BuildCnfVar): - - def __init__(self, - data=default_provider_name, - instance=default_provider_instance, - line_no=1, - mode="ROUTER", - dns=None, - ip=None, - localip=None): - build_cnfvar.BuildCnfVar.__init__(self, - "PROVIDER", - instance, - data, - line_no) - self.add_defaults(default_cnfvars) - self.update_cnf("PROVIDER_MODE", 0, mode) - - if dns is not None: - self.update_cnf("PROVIDER_DNS", 0, dns) - if ip is not None: - self.update_cnf("PROVIDER_IP", 0, ip) - if localip is not None: - self.update_cnf("PROVIDER_LOCALIP", 0, localip) diff --git a/src/cnfline/build_proxy_accesslist.py b/src/cnfline/build_proxy_accesslist.py deleted file mode 100644 index 1b0a838..0000000 --- a/src/cnfline/build_proxy_accesslist.py +++ /dev/null @@ -1,52 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from .build_cnfvar import BuildCnfVar -"""Class to create proxy accesslists on the fly""" - - -class BuildProxyAccesslist(BuildCnfVar): - def __init__(self, data='', instance=0, line_no=1): - BuildCnfVar.__init__(self, 'PROXY_ACCESSLIST', instance, data, line_no) - - defaults = {'PROXY_ACCESSLIST_ENTRY_COUNT': '123', - 'PROXY_ACCESSLIST_MODE': '1', - 'PROXY_ACCESSLIST_SIZETYPE': 1, - 'PROXY_ACCESSLIST_TYPE': 0} - - self.add_defaults(defaults) - - def mode_whitelist(self): - self.update_cnf('PROXY_ACCESSLIST_MODE', 0, '0') - return self - - def mode_blacklist(self): - self.update_cnf('PROXY_ACCESSLIST_MODE', 0, '1') - return self - - def add_url(self, url): - self.add_cnf('PROXY_ACCESSLIST_URL', -1, url) - return self diff --git a/src/cnfline/build_proxy_profile.py b/src/cnfline/build_proxy_profile.py deleted file mode 100644 index 93c9ccf..0000000 --- a/src/cnfline/build_proxy_profile.py +++ /dev/null @@ -1,37 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from .build_cnfvar import BuildCnfVar -"""Class to create proxy profiles on the fly""" - - -class BuildProxyProfile(BuildCnfVar): - def __init__(self, data='', instance=0, line_no=1): - BuildCnfVar.__init__(self, 'PROXY_PROFILE', instance, data, line_no) - - def add_accesslist_ref(self, accesslist_instance): - self.add_cnf('PROXY_PROFILE_ACCESSLIST_REF', -1, accesslist_instance) - return self diff --git a/src/cnfline/build_vpnconn.py b/src/cnfline/build_vpnconn.py deleted file mode 100644 index 5037ab2..0000000 --- a/src/cnfline/build_vpnconn.py +++ /dev/null @@ -1,91 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -Create vpn connections. - -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from . import build_cnfvar - -# Defaults are extracted from data/shared_arnied/vpnconn.cnf. -default_vpnconn_name = "sample_vpn" -default_vpnconn_instance = 1 -default_cnfvars = { - "VPNCONN_ACTIVATION": "ALWAYS", - "VPNCONN_DISABLED": "0", - "VPNCONN_DNS_RELAYING_ALLOWED": "1", - "VPNCONN_EMAIL_RELAYING_ALLOWED": "1", - "VPNCONN_ENCRYPTION_PROFILE_REF": "0", - "VPNCONN_FIREWALL_RULESET_REF": "5", - "VPNCONN_IKE_VERSION": "1", - "VPNCONN_KEY_FOREIGN_REF": "1", - "VPNCONN_KEY_OWN_REF": "1", - "VPNCONN_KEY_TYPE": "PUBLIC", - "VPNCONN_LAN_NAT_IP": "", - "VPNCONN_LAN_NAT_MODE": "UNMODIFIED", - "VPNCONN_LAN_NAT_NETWORK": "", - "VPNCONN_LAN_NIC_REF": "2", - "VPNCONN_LAN_NET": "172.17.0.0", - "VPNCONN_LAN_NETMASK": "255.255.0.0", - "VPNCONN_LAN_TYPE": "NIC", - "VPNCONN_LIFETIME_IKE": "480", - "VPNCONN_LIFETIME_IPSECSA": "60", - "VPNCONN_OFFLINE_DETECTION_SEC": "60", - "VPNCONN_PEER_DNS": "", - "VPNCONN_PEER_IP": None, - "VPNCONN_PEER_TYPE": "IP", - "VPNCONN_PROXY_PROFILE_REF": "-2", - "VPNCONN_PSK": "", - "VPNCONN_PSK_FOREIGN_ID": "", - "VPNCONN_PSK_FOREIGN_ID_TYPE": "IP", - "VPNCONN_PSK_OWN_ID": "", - "VPNCONN_PSK_OWN_ID_TYPE": "IP", - "VPNCONN_REMOTE_INET_NAT": "1", - "VPNCONN_REMOTE_MODECONFIG_IP": "192.168.99.1", - "VPNCONN_REMOTE_NAT_ENABLE": "0", - "VPNCONN_REMOTE_NAT_NETWORK": "", - "VPNCONN_REMOTE_NET": "172.18.0.0", - "VPNCONN_REMOTE_NETMASK": "255.255.0.0", - "VPNCONN_REMOTE_TYPE": "CUSTOM", - "VPNCONN_RETRIES": "3", - "VPNCONN_SECURED": "ESP", - "VPNCONN_XAUTH_SERVER_ENABLE": "0" -} - - -class BuildVPNConn(build_cnfvar.BuildCnfVar): - - def __init__(self, - data=default_vpnconn_name, - instance=default_vpnconn_instance, - line_no=1, - peer_ip="172.16.1.172"): - build_cnfvar.BuildCnfVar.__init__(self, - "VPNCONN", - instance, - data, - line_no) - self.add_defaults(default_cnfvars) - - self.update_cnf("VPNCONN_PEER_IP", 0, peer_ip) diff --git a/src/cnfline/cnfline.py b/src/cnfline/cnfline.py deleted file mode 100644 index fa25136..0000000 --- a/src/cnfline/cnfline.py +++ /dev/null @@ -1,64 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -class CnfLine(object): - """Represents an arnied cnfline""" - - def __init__(self, - name='', - instance=0, - data='', - line_no=1, - parent_line_no=0): - self.name = name - self.instance = instance - self.data = data - self.line_no = line_no - self.parent_line_no = parent_line_no - - if len(self.name) == 0: - raise ValueError("You can't leave the cnfvar name empty") - - if line_no == 0: - raise ValueError('Zero is not a valid line number') - - def __str__(self): - """Build cnfline string representation""" - - # Sanity checks - if len(self.name) == 0: - raise ValueError("Can't display empty cnfvar name") - if self.line_no == 0: - raise ValueError('Zero is not a valid line number') - - if self.parent_line_no: - rtn = '{0} ({1})'.format(self.line_no, self.parent_line_no) - else: - rtn = '{0}'.format(self.line_no) - - rtn += ' {0},{1}: "{2}"'.format(self.name, self.instance, self.data) - - return rtn diff --git a/src/cnfline/configure_proxy_antivirus.py b/src/cnfline/configure_proxy_antivirus.py deleted file mode 100644 index 1a97ccf..0000000 --- a/src/cnfline/configure_proxy_antivirus.py +++ /dev/null @@ -1,61 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from .build_cnfvar import BuildCnfVar -"""Class to configure the proxy antivirus cnfvars on the fly""" - - -class ConfigureProxyAntivirus(BuildCnfVar): - def __init__(self, enabled='1', line_no=1): - BuildCnfVar.__init__(self, 'VIRSCAN_PROXY_ENABLE', - 0, enabled, line_no) - - def automatic_unblock(self, block_minutes): - line_no = self.update_cnf('VIRSCAN_PROXY_AUTOMATIC_UNBLOCK', 0, block_minutes) - self.mark_as_own_parent(line_no) - return self - - def debug_log(self, enabled='1'): - line_no = self.update_cnf('VIRSCAN_PROXY_DEBUG_LOG', 0, enabled) - self.mark_as_own_parent(line_no) - return self - - def add_pass_site(self, url): - sites_parent = self.update_cnf('VIRSCAN_PROXY_PASS_SITES', 0, '') - self.mark_as_own_parent(sites_parent) - - self.add_cnf('VIRSCAN_PROXY_PASS_SITES_ADDR', -1, url, sites_parent) - return self - - def clear_pass_sites(self): - self.del_cnf('VIRSCAN_PROXY_PASS_SITES_ADDR') - self.del_cnf('VIRSCAN_PROXY_PASS_SITES') - return self - - def warn_admin(self, enabled='1'): - line_no = self.update_cnf('VIRSCAN_PROXY_WARN_ADMIN', 0, enabled) - self.mark_as_own_parent(line_no) - return self diff --git a/src/cnfline/configure_webfilter.py b/src/cnfline/configure_webfilter.py deleted file mode 100644 index 3aa73f0..0000000 --- a/src/cnfline/configure_webfilter.py +++ /dev/null @@ -1,72 +0,0 @@ -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -""" -.. note:: DEPRECATED! Please do not extend this or add new uses of this module, - use :py:mod:`pyi2ncommon.arnied_api` or :py:mod:`pyi2ncommon.cnfvar` - instead! -""" - -from .build_cnfvar import BuildCnfVar -"""Class to configure the webfilter cnfvars on the fly""" - - -class ConfigureWebfilter(BuildCnfVar): - def __init__(self, enabled='1', line_no=1): - BuildCnfVar.__init__(self, 'PROXY_WEBFILTER_ENABLE', - 0, enabled, line_no) - - def block_drugs(self, enabled='1'): - line_no = self.update_cnf('PROXY_WEBFILTER_BLOCK_DRUGS', 0, enabled) - self.mark_as_own_parent(line_no) - return self - - def block_gambling(self, enabled='1'): - line_no = self.update_cnf('PROXY_WEBFILTER_BLOCK_GAMBLING', 0, enabled) - self.mark_as_own_parent(line_no) - return self - - def block_pornography(self, enabled='1'): - line_no = self.update_cnf('PROXY_WEBFILTER_BLOCK_PORNOGRAPHY', - 0, enabled) - self.mark_as_own_parent(line_no) - return self - - def block_violence(self, enabled='1'): - line_no = self.update_cnf('PROXY_WEBFILTER_BLOCK_VIOLENCE', 0, enabled) - self.mark_as_own_parent(line_no) - return self - - def block_warez(self, enabled='1'): - line_no = self.update_cnf('PROXY_WEBFILTER_BLOCK_WAREZ', 0, enabled) - self.mark_as_own_parent(line_no) - return self - - def add_pass_site(self, url): - sites_parent = self.update_cnf('PROXY_WEBFILTER_PASS_SITES', 0, '') - self.mark_as_own_parent(sites_parent) - - self.add_cnf('PROXY_WEBFILTER_PASS_SITES_URL', -1, url, sites_parent) - return self - - def clear_pass_sites(self): - self.del_cnf('PROXY_WEBFILTER_PASS_SITES_URL') - self.del_cnf('PROXY_WEBFILTER_PASS_SITES') - return self diff --git a/src/cnfvar/templates.py b/src/cnfvar/templates.py index 1a68960..d2ae7cb 100644 --- a/src/cnfvar/templates.py +++ b/src/cnfvar/templates.py @@ -80,7 +80,140 @@ group_defaults = { "GROUP_EMAIL_RELAY_RIGHTS": "RELAY_FROM_INTRANET", "GROUP_PROXY_PROFILE_REF": "1", } - +#: UI defaults for an intraclient instance +intraclient_defaults = { + "INTRACLIENT_COMMENT": "", + "INTRACLIENT_DNS_RELAYING_ALLOWED": "1", + "INTRACLIENT_EMAIL_RELAYING_ALLOWED": "1", + "INTRACLIENT_FIREWALL_RULESET_REF": "5", + "INTRACLIENT_IP": None, + "INTRACLIENT_MAC": None, + "INTRACLIENT_PROXY_PROFILE_REF": "-1", +} +#: UI defaults for a NIC instance +nic_defaults = { + "NIC_COMMENT": "", + "NIC_DRIVER": "virtio_net", + "NIC_LAN_DNS_RELAYING_ALLOWED": "0", + "NIC_LAN_EMAIL_RELAYING_ALLOWED": "0", + "NIC_LAN_FIREWALL_RULESET_REF": "1", + "NIC_LAN_IP": "192.168.1.1", + "NIC_LAN_NAT_INTO": "0", + "NIC_LAN_NETMASK": "255.255.255.0", + "NIC_LAN_PROXY_PROFILE_REF": "-1", + "NIC_MAC": "02:00:00:00:20:00", + "NIC_TYPE": "DSLROUTER", +} +#: UI defaults for a provider instance +provider_defaults = { + "PROVIDER_PROXY_SERVER": "", + "PROVIDER_PROXY_PORT": "", + "PROVIDER_PROXY_PASSWORD": "", + "PROVIDER_PROXY_LOGIN": "", + "PROVIDER_NIC_REF": "1", + "PROVIDER_NETMASK": "255.255.0.0", + "PROVIDER_MTU_SIZE": "1500", + "PROVIDER_MODE": "ROUTER", + "PROVIDER_MAILTRANSFER_MODE": "IMMEDIATE", + "PROVIDER_LOCALIP": "", + "PROVIDER_IP": "", + "PROVIDER_FIREWALL_RULESET_REF": "7", + "PROVIDER_FALLBACK_TIMEOUT": "60", + "PROVIDER_FALLBACK_PROVIDER_REF": "-1", + "PROVIDER_EMAIL_RELAY_REF": "-1", + "PROVIDER_DYNDNS_WEBCHECKIP": "0", + "PROVIDER_DYNDNS_ENABLE": "1", + "PROVIDER_DNS_MODE": "IP", + "PROVIDER_DNS": "", + "PROVIDER_BWIDTH_MANAGEMENT_UPSTREAM_SPEED": "", + "PROVIDER_BWIDTH_MANAGEMENT_ENABLE": "0", + "PROVIDER_BWIDTH_MANAGEMENT_DOWNSTREAM_SPEED": "", + "PROVIDER_PINGCHECK_SERVERLIST_REF": "-2", +} +#: UI defaults for a port forwarding instance +port_forwarding_defaults = { + "PORT_FORWARDING_DST_IP_REF": "1", + "PORT_FORWARDING_DST_PORT": "", + "PORT_FORWARDING_DST_PORT_END": "", + "PORT_FORWARDING_PROTOCOL_TYPE": "TCP", + "PORT_FORWARDING_SRC_PORT": "", + "PORT_FORWARDING_SRC_PORT_END": "", +} +#: UI defaults for a firewall ruleset instance +firewall_ruleset_defaults = { + "FIREWALL_RULESET_PROFILE_TYPE": "FULL", +} +#: UI defaults for a proxy accesslist instance +proxy_accesslist_defaults = { + "PROXY_ACCESSLIST_ENTRY_COUNT": "123", + "PROXY_ACCESSLIST_MODE": "1", + "PROXY_ACCESSLIST_SIZETYPE": "1", + "PROXY_ACCESSLIST_TYPE": "0", +} +#: UI defaults for a key instance +key_own_defaults = { + "KEY_OWN_FINGERPRINT_MD5": "76:3B:CF:8E:CB:BF:A5:7D:CC:87:39:FA:CE:99:2E:96", + "KEY_OWN_FINGERPRINT_SHA1": "ED:5A:C6:D9:5B:BE:47:1F:B9:4F:CF:A3:80:3B:42:08:F4:00:16:96", + "KEY_OWN_ID_X509": "CN=some.net.lan", + "KEY_OWN_ISSUER": "CN=ab, C=fd, L=ab, ST=ab, O=ab, OU=ab/emailAddress=ab@ab.com", + "KEY_OWN_KEYSIZE": "2048", + "KEY_OWN_HASH_ALGO": "SHA2_256", + "KEY_OWN_PRIVATE_KEY": "-----BEGIN PRIVATE KEY-----\\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKk" + + "ZTzqHXg41RZMiY+ywRZ037pBq8J3BkH\\n-----END PRIVATE KEY-----\\n", + "KEY_OWN_PUBLIC_KEY": "-----BEGIN CERTIFICATE-----\\nMIIFCTCCAvGgAwIBAgIEVBGDuTANBgkqhkiG" + + "9w0BAQsFADCBgTEPMA0GA1UEAwwG\\n-----END CERTIFICATE-----\\n", + "KEY_OWN_REQUEST": "-----BEGIN CERTIFICATE REQUEST-----\\nMIIDCzCCAfMCAQAwIjEgMB4GA1UEAww" + + "XaW50cmFkZXYtYWllc\\n-----END CERTIFICATE REQUEST-----\\n", + "KEY_OWN_SUBJECT": "CN=some.net.lan", + "KEY_OWN_VALIDFROM": "20140911T111257", + "KEY_OWN_VALIDTILL": "20160731T134608", + "KEY_OWN_TYPE": "SELF", + # the ones bellow should be set when using 'generate' to create the key + "KEY_OWN_CREATE_CN": "somehost", + "KEY_OWN_CREATE_EMAIL": "default@intra2net.com" +} +#: UI defaults for a VPN connection instance +vpnconn_defaults = { + "VPNCONN_ACTIVATION": "ALWAYS", + "VPNCONN_DISABLED": "0", + "VPNCONN_DNS_RELAYING_ALLOWED": "1", + "VPNCONN_EMAIL_RELAYING_ALLOWED": "1", + "VPNCONN_ENCRYPTION_PROFILE_REF": "0", + "VPNCONN_FIREWALL_RULESET_REF": "5", + "VPNCONN_IKE_VERSION": "1", + "VPNCONN_KEY_FOREIGN_REF": "1", + "VPNCONN_KEY_OWN_REF": "1", + "VPNCONN_KEY_TYPE": "PUBLIC", + "VPNCONN_LAN_NAT_IP": "", + "VPNCONN_LAN_NAT_MODE": "UNMODIFIED", + "VPNCONN_LAN_NAT_NETWORK": "", + "VPNCONN_LAN_NIC_REF": "2", + "VPNCONN_LAN_NET": "172.17.0.0", + "VPNCONN_LAN_NETMASK": "255.255.0.0", + "VPNCONN_LAN_TYPE": "NIC", + "VPNCONN_LIFETIME_IKE": "480", + "VPNCONN_LIFETIME_IPSECSA": "60", + "VPNCONN_OFFLINE_DETECTION_SEC": "60", + "VPNCONN_PEER_DNS": "", + "VPNCONN_PEER_IP": None, + "VPNCONN_PEER_TYPE": "IP", + "VPNCONN_PROXY_PROFILE_REF": "-2", + "VPNCONN_PSK": "", + "VPNCONN_PSK_FOREIGN_ID": "", + "VPNCONN_PSK_FOREIGN_ID_TYPE": "IP", + "VPNCONN_PSK_OWN_ID": "", + "VPNCONN_PSK_OWN_ID_TYPE": "IP", + "VPNCONN_REMOTE_INET_NAT": "1", + "VPNCONN_REMOTE_MODECONFIG_IP": "192.168.99.1", + "VPNCONN_REMOTE_NAT_ENABLE": "0", + "VPNCONN_REMOTE_NAT_NETWORK": "", + "VPNCONN_REMOTE_NET": "172.18.0.0", + "VPNCONN_REMOTE_NETMASK": "255.255.0.0", + "VPNCONN_REMOTE_TYPE": "CUSTOM", + "VPNCONN_RETRIES": "3", + "VPNCONN_SECURED": "ESP", + "VPNCONN_XAUTH_SERVER_ENABLE": "0" +} ############################################################################### @@ -142,271 +275,127 @@ def group(name, instance=-1, **kwargs): return group_cnf -def nic(instance=0, nictype="NATLAN", - ip="1.2.3.4", netmask="255.255.0.0", mac="00:00:00:00:00:00", - suffix="host"): +def nic(instance=-1, **kwargs): """ - Generate and save a nic configuration file. - - :param int instance: instance number (for multiple nics, -1 for next available) - :param str nictype: type of the nic - :param str ip: IP address of the nic - :param str netmask: network mask of the nic - :param str mac: MAC address of the nic - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + Generate a nic cnf variable. + + :param int instance: instance number for the nic + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create arnied nic configuration") - nic_obj = batch_update_cnf( - build_nic.BuildNIC(data="", instance=instance, line_no=1), - [(Update, ("NIC_TYPE", 0, nictype)), - (Update, ("NIC_LAN_IP", 0, ip)), - (Update, ("NIC_LAN_NETMASK", 0, netmask)), - (Update, ("NIC_MAC", 0, mac))]) - nic_cnf = "nic-%d-%s.cnf" % (time.time(), suffix) - [nic_cnf] = aw.prep_config_paths([nic_cnf], aw.DUMP_CONFIG_DIR) - logging.info("Saving nic configuration to %s", nic_cnf) - nic_obj.save(nic_cnf) + log.info(f"Generating a nic cnfvar") + nic_cnf = template("nic", "", instance=instance, defaults=nic_defaults, **kwargs) return nic_cnf -def intraclient(name="intraclient", instance=1, - ip="1.2.3.4", mac="00:00:00:00:00:00", - fwrules=5, suffix="host"): +def intraclient(name, instance=-1, **kwargs): """ - Generate and save an intraclient configuration file. - - :param str name: name of the intraclient - :param int instance: instance number (for multiple clients, -1 for next available) - :param str ip: IP address of the intraclient - :param str mac: MAC address of the intraclient - :param int fwrules: instance of the firewall rules to use - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + Generate an intraclient cnf variable. + + :param str name: name for the intraclient + :param int instance: instance number for the intraclient + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create arnied intraclient configuration") - intraclient_obj = batch_update_cnf( - build_intraclient.BuildIntraclient(data=name, instance=instance), - [(Update, ("INTRACLIENT_IP", 0, ip)), - (Update, ("INTRACLIENT_MAC", 0, mac)), - (Update, ("INTRACLIENT_FIREWALL_RULESET_REF", 0, fwrules))]) - - intraclient_cnf = "intraclient-%d-%s.cnf" % (time.time(), suffix) - [intraclient_cnf] = aw.prep_config_paths([intraclient_cnf], aw.DUMP_CONFIG_DIR) - logging.info("Saving intraclient configuration to %s", intraclient_cnf) - intraclient_obj.save(intraclient_cnf) + log.info(f"Generating an intraclient {name} cnfvar") + intraclient_cnf = template("intraclient", name, instance=instance, + defaults=intraclient_defaults, **kwargs) return intraclient_cnf -def provider(name="provider", instance=1, mode="ROUTER", ip="1.2.3.4", localip=None, - netmask="255.255.0.0", dnsmode="IP", dns="1.2.3.4", fwrules=5, - dialretry=None, timeout="", mtumode="AUTO", - vlanid=None, mtusize=None, login=None, password=None, - modemip=None, providerid=None, localdhcp=None, - suffix="host"): +def provider(name, ip, dns, instance=-1, **kwargs): """ - Generate and save a provider configuration file. + Generate a provider cnf variable. - :param str name: name of the provider - :param int instance: instance number (for multiple clients, -1 for next available) - :param str mode: provider mode + :param str name: name for the provider :param str ip: IP address of the provider - :param localip: IP address of the configured machine (valid for some configurations) - :type localip: str or None - :param str netmask: netmask of the provider - :param str dnsmode: dnsmode of the provider :param str dns: IP address of the DNS server - :param int fwrules: instance of the firewall rules to use - :param any args: lots of detailed configuration - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + :param int instance: instance number for the provider + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create arnied provider configuration") - - def add_or_del(var, field): - if var is not None: - return Add, (field, 0, str(var)) - return Delete, field - provider_obj = batch_update_cnf( - build_provider.BuildProvider(data=name, instance=instance), - [(Update, ("PROVIDER_MODE", 0, mode)), - ip and (Update, ("PROVIDER_IP", 0, ip)) - or (Delete, "PROVIDER_IP"), - localip - and (Update, ("PROVIDER_LOCALIP", 0, localip)) - or (Delete, "PROVIDER_LOCALIP"), - netmask and (Update, ("PROVIDER_NETMASK", 0, - netmask)) - or (Delete, "PROVIDER_NETMASK"), - (Update, ("PROVIDER_TIMEOUT", 0, timeout)), - (Update, ("PROVIDER_DNS_MODE", 0, dnsmode)), - (Update, ("PROVIDER_DNS", 0, - dns if dnsmode == "IP" else "")), - (Update, ("PROVIDER_MTU_MODE", 0, mtumode)), - (Update, ("PROVIDER_MTU_SIZE", 0, - mtusize if mtumode != "AUTO" else "")), - (Update, ("PROVIDER_FIREWALL_RULESET_REF", 0, str(fwrules))), - add_or_del(vlanid, "PROVIDER_VLAN_ID"), - add_or_del(dialretry, "PROVIDER_DIAL_RETRY"), - add_or_del(login, "PROVIDER_LOGIN"), - add_or_del(password, "PROVIDER_PASSWORD"), - add_or_del(modemip, "PROVIDER_MODEM_IP"), - add_or_del(providerid, "PROVIDER_PROVIDERID"), - add_or_del(localdhcp, "PROVIDER_LOCAL_DHCP")]) - provider_cnf = "provider-%d-%s.cnf" % (time.time(), suffix) - [provider_cnf] = aw.prep_config_paths([provider_cnf], aw.DUMP_CONFIG_DIR) - logging.info("Saving provider configuration to %s", provider_cnf) - provider_obj.save(provider_cnf) + log.info(f"Generating a provider {name} cnfvar") + provider_cnf = template("provider", name, instance=instance, + defaults=provider_defaults, **kwargs) + provider_cnf.children.single_with_name("provider_ip").value = ip + provider_cnf.children.single_with_name("provider_dns").value = dns return provider_cnf -def provider_proxy(mode="ROUTER", ip="1.2.3.4", localip=None, proxy_port=3128, fwrules=7, suffix="host"): +def port_forwarding(name, src_port="1234", dst_port="1234", instance=-1, **kwargs): """ - Generate and save a provider configuration file for proxy. - - :param str mode: provider mode - :param str ip: IP address of the provider (and DNS server) - :param localip: IP address of the configured machine (valid for some configurations) - :type localip: str or None - :param int proxy_port: port for the provider proxy - :param int fwrules: instance of the firewall rules to use - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + Generate a port forwarding cnf variable. + + :param str name: name for the port forwarding mapping + :param str src_port: forwarded source port + :param str dst_port: forwarded destination port + :param int instance: instance number for the port forwarding mapping + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create arnied provider configuration.") - provider_obj = batch_update_cnf( - build_provider.BuildProvider(), - [(Update, ("PROVIDER_MODE", 0, mode)), - (Update, ("PROVIDER_DNS", 0, ip)), - (Update, ("PROVIDER_DYNDNS_ENABLE", 0, "0")), - (Update, ("PROVIDER_IP", 0, ip)), - (Update, ("PROVIDER_PROXY_SERVER", 0, ip)), - (Update, ("PROVIDER_PROXY_PORT", 0, str(proxy_port))), - localip - and (Update, ("PROVIDER_LOCALIP", 0, localip)) - or (Delete, "PROVIDER_LOCALIP"), - (Update, ("PROVIDER_DNS_MODE", 0, "IP")), - (Update, ("PROVIDER_FIREWALL_RULESET_REF", 0, str(fwrules)))]) - provider_cnf = "provider-%d-%s.cnf" % (time.time(), suffix) - [provider_cnf] = aw.prep_config_paths([provider_cnf], aw.DUMP_CONFIG_DIR) - logging.info("Saving provider configuration to %s", provider_cnf) - provider_obj.save(provider_cnf) - return provider_cnf + log.info(f"Generating a port forwarding {name} cnfvar") + port_forwarding_cnf = template("port_forwarding", name, instance=instance, + defaults=port_forwarding_defaults, **kwargs) + port_forwarding_cnf.children.single_with_name("port_forwarding_src_port").value = src_port + port_forwarding_cnf.children.single_with_name("port_forwarding_dst_port").value = dst_port + return port_forwarding_cnf -def port_forwarding(src_port="1234", src_port_end="", - dst_port="1234", dst_port_end="", - dst_ip_ref="1", protocol_type="TCP", - suffix="host"): +def firewall_ruleset(name, instance=-1, **kwargs): """ - Generate and save a port forwarding configuration file. + Generate a firewall ruleset cnf variable. - :param str src_port: forwarded source port - :param str src_port_end: forwarded source port end for a port range - :param str dst_port: forwarded destination port - :param str dst_port_end: forwarded destination port end for a port range - :param str dst_ip_ref: destination nic instance for a port range - :param str protocol_type: port forwarding protocol type - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + :param str name: name for the firewall ruleset + :param int instance: instance number for the firewall ruleset + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create port forwarding configuration") - value_id = "test" - portforward_client_cnf = "portforward-%d-%s.cnf" % (time.time(), suffix) - return build_cnf("PORT_FORWARDING", - data=value_id, - filename=portforward_client_cnf, - vals=[(Child, ("PORT_FORWARDING_DST_IP_REF", 0, dst_ip_ref)), - (Child, ("PORT_FORWARDING_DST_PORT", 0, dst_port)), - (Child, ("PORT_FORWARDING_DST_PORT_END", 0, dst_port_end)), - (Child, ("PORT_FORWARDING_PROTOCOL_TYPE", 0, protocol_type)), - (Child, ("PORT_FORWARDING_SRC_PORT", 0, src_port)), - (Child, ("PORT_FORWARDING_SRC_PORT_END", 0, src_port_end))]) - - -def firewall_ruleset_simple(suffix="host"): + log.info(f"Generating a firewall ruleset {name} cnfvar") + firewall_ruleset_cnf = template("firewall_ruleset", name, instance=instance, + defaults=firewall_ruleset_defaults, **kwargs) + return firewall_ruleset_cnf + + +def proxy_accesslist(name, instance=-1, **kwargs): """ - Generate and save a simple firewall ruleset configuration file. + Generate a proxy accesslist cnf variable. - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + :param str name: name for the proxy accesslist + :param int instance: instance number for the proxy accesslist + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create firewall ruleset") - fw_cnf = "fw-%d-%s.cnf" % (time.time(), suffix) - return build_cnf("FIREWALL_RULESET", - instance=101, - data="Port Forwarding libfirewall test", - filename=fw_cnf, - vals=[(Update, ("FIREWALL_RULESET_PROFILE_TYPE", 0, "SIMPLE_PROVIDER")), - (Update, ("FIREWALL_RULESET_PROVIDER_HTTPS_OPEN", 0, "0")), - (Update, ("FIREWALL_RULESET_PROVIDER_POP3SIMAPS_OPEN", 0, "0")), - (Update, ("FIREWALL_RULESET_PROVIDER_PORT_FORWARDING_ENABLE", 0, "1")), - (Update, ("FIREWALL_RULESET_PROVIDER_SMTP_OPEN", 0, "0")), - (Update, ("FIREWALL_RULESET_PROVIDER_HTTP_OPEN", 0, "0")), - (Update, ("FIREWALL_RULESET_PROVIDER_VPN_OPEN", 0, "0"))]) - - -def firewall_ruleset_port(suffix="host"): + log.info(f"Generating a proxy accesslist {name} cnfvar") + proxy_accesslist_cnf = template("proxy_accesslist", name, instance=instance, + defaults=proxy_accesslist_defaults, **kwargs) + return proxy_accesslist_cnf + + +def key_own(name, instance=-1, **kwargs): """ - Generate and save a firewall ruleset configuration file for port forwarding. + Generate an own key cnf variable. - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + :param str name: name for the own key + :param int instance: instance number for the own key + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create firewall ruleset") - fw_portforward_cnf = "fw-portforward-%d-%s.cnf" % (time.time(), suffix) - return build_cnf("FIREWALL_RULESET", - instance=100, - data="Port forwarding only", - filename=fw_portforward_cnf, - vals=[(Update, ("FIREWALL_RULESET_AUTOMATIC_ANSWER_RULE", 0, "1")), - (Update, ("FIREWALL_RULESET_PROFILE_TYPE", 0, "FULL")), - (Add, ("FIREWALL_RULESET_RULE", 1, "")), - (Child, ("FIREWALL_RULESET_RULE_ACTION", 0, "ACCEPT")), - (Child, ("FIREWALL_RULESET_RULE_CHECK_CONNECTION_STATUS", 0, "PORTFORWARDING")), - (Child, ("FIREWALL_RULESET_RULE_CHECK_TCP_FLAGS", 0, "DISABLED")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_FOR_ACTION_ENABLE", 0, "0")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_FOR_LOG_ENABLE", 0, "0")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_PACKETS_AVERAGE_COUNT", 0, "")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_PACKETS_AVERAGE_PERIOD", 0, "SEC")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_PACKETS_PEAK_COUNT", 0, "")), - (Child, ("FIREWALL_RULESET_RULE_LOG_ENABLE", 0, "0")), - (Child, ("FIREWALL_RULESET_RULE_LOG_MESSAGE", 0, "")), - (Child, ("FIREWALL_RULESET_RULE_TIME_INCLUDE_TIME_REF", 0, "-1")), - (Update, ("FIREWALL_RULESET_USAGE", 0, "PROVIDER"))]) - - -def firewall_ruleset_dmz(suffix="host"): + log.info(f"Generating an own key {name} cnfvar") + key_own_cnf = template("key_own", name, instance=instance, + defaults=key_own_defaults, **kwargs) + return key_own_cnf + + +def vpnconn(name, instance=-1, **kwargs): """ - Generate and save a firewall ruleset configuration file for DMZ. + Generate a vpn connection cnf variable. - :param str suffix: optional suffix to use for config identification - :returns: generated config filename - :rtype: str + :param str name: name for the vpn connection + :param int instance: instance number for the vpn connection + :returns: generated cnf variable + :rtype: :py:class:`Cnf` """ - log.info("Create firewall ruleset") - fw_dmz_cnf = "fw-dmz-%d-%s.cnf" % (time.time(), suffix) - return build_cnf("FIREWALL_RULESET", - instance=100, - data="DMZ firewall rules", - filename=fw_dmz_cnf, - vals=[(Update, ("FIREWALL_RULESET_AUTOMATIC_ANSWER_RULE", 0, "1")), - (Update, ("FIREWALL_RULESET_PROFILE_TYPE", 0, "FULL")), - (Add, ("FIREWALL_RULESET_RULE", 1, "")), - (Child, ("FIREWALL_RULESET_RULE_ACTION", 0, "ACCEPT")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_FOR_ACTION_ENABLE", 0, "0")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_FOR_LOG_ENABLE", 0, "0")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_PACKETS_AVERAGE_COUNT", 0, "")), - (Child, ("FIREWALL_RULESET_RULE_LIMIT_PACKETS_PEAK_COUNT", 0, "")), - (Child, ("FIREWALL_RULESET_RULE_LOG_ENABLE", 0, "0")), - (Child, ("FIREWALL_RULESET_RULE_LOG_MESSAGE", 0, "")), - (Child, ("FIREWALL_RULESET_RULE_SERVICE_INCLUDE_SERVICEGROUP_REF", 0, "6")), - (Child, ("FIREWALL_RULESET_RULE_DST_INCLUDE_CLIENT_REF", 0, "2")), - (Update, ("FIREWALL_RULESET_USAGE", 0, "LANVPN"))]) + log.info(f"Generating a vpn connection {name} cnfvar") + vpnconn_cnf = template("vpnconn", name, instance=instance, + defaults=vpnconn_defaults, **kwargs) + return vpnconn_cnf diff --git a/test/test_build_cnfvar.py b/test/test_build_cnfvar.py deleted file mode 100755 index 1ea3e76..0000000 --- a/test/test_build_cnfvar.py +++ /dev/null @@ -1,155 +0,0 @@ -#!/usr/bin/env python - -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -"""Unit test for build_cnfvar.py""" -import unittest -import os -from src.cnfline.build_cnfvar import BuildCnfVar - -TEST_CONFIG_FILENAME = 'some_config.cnf' - - -class BuildCnfVarTest(unittest.TestCase): - def tearDown(self): - if os.path.isfile(TEST_CONFIG_FILENAME): - os.unlink(TEST_CONFIG_FILENAME) - - def test_simple(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - - self.assertEqual('FOOBAR', cnfvar.name) - self.assertEqual(123, cnfvar.instance) - self.assertEqual('some_data', cnfvar.data) - self.assertEqual(10, cnfvar.line_no) - - def test_find_child_line_no(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - new_child_line_no = cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - - self.assertEqual(new_child_line_no, - cnfvar.find_child_line_no('FOOBAR_CHILD')) - - # Should not be found - self.assertEqual(0, cnfvar.find_child_line_no('FOOBAR_SPACESHARK')) - - def test_find_free_line_no(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - self.assertEqual(11, cnfvar.find_free_line_no()) - - cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - self.assertEqual(12, cnfvar.find_free_line_no()) - - def test_find_free_child_instance(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - - self.assertEqual(0, cnfvar.find_free_child_instance('FOOBAR_OTHER')) - self.assertEqual(1, cnfvar.find_free_child_instance('FOOBAR_CHILD')) - - def test_update_cnf(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - - # Update existing cnfvar - cnfvar.update_cnf('FOOBAR_CHILD', 0, 'abc') - - self.assertEqual('10 FOOBAR,123: "some_data"\n' - '11 (10) FOOBAR_CHILD,0: "abc"\n', str(cnfvar)) - - def test_string_output(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - cnfvar.update_cnf('FOOBAR_CHILD', 0, 'abc') - cnfvar.add_cnf('FOOBAR_CHILD', 1, 'more data') - - self.assertEqual('10 FOOBAR,123: "some_data"\n' - '11 (10) FOOBAR_CHILD,0: "abc"\n' - '12 (10) FOOBAR_CHILD,1: "more data"\n', str(cnfvar)) - - def test_del_cnf(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - cnfvar.add_cnf('FOOBAR_CHILD', 1, 'more data') - - cnfvar.del_cnf('FOOBAR_CHILD') - - self.assertEqual('10 FOOBAR,123: "some_data"\n', str(cnfvar)) - - def test_add_different_parent_no(self): - cnfvar = BuildCnfVar('FOOBAR', 123, 'some_data', 10) - sub_parent = cnfvar.add_cnf('FOOBAR_CHILD', 0, 'xxx') - cnfvar.add_cnf('FOOBAR_OTHER', 0, 'foo') - cnfvar.add_cnf('FOOBAR_CHILD_TYPE', 1, 'spaceshark', sub_parent) - cnfvar.add_cnf('FOOBAR_OTHER2', 0, 'foo2') - - self.assertEqual('10 FOOBAR,123: "some_data"\n' - '11 (10) FOOBAR_CHILD,0: "xxx"\n' - '12 (10) FOOBAR_OTHER,0: "foo"\n' - '13 (11) FOOBAR_CHILD_TYPE,1: "spaceshark"\n' - '14 (10) FOOBAR_OTHER2,0: "foo2"\n', str(cnfvar)) - - def test_add_defaults(self): - cnfvar = BuildCnfVar('FOOBAR', 0, 'some_data') - - defaults = {'FOOBAR_SOMETHING': 'abc', - 'FOOBAR_MODE': 'optimize'} - cnfvar.add_defaults(defaults) - - self.assertTrue('1 FOOBAR,0: "some_data"\n' - '2 (1) FOOBAR_SOMETHING,0: "abc"\n' - '3 (1) FOOBAR_MODE,0: "optimize"\n' == str(cnfvar) - or '1 FOOBAR,0: "some_data"\n' - '2 (1) FOOBAR_MODE,0: "optimize"\n' - '3 (1) FOOBAR_SOMETHING,0: "abc"\n' == str(cnfvar)) - - def test_mark_as_own_parent(self): - cnfvar = BuildCnfVar('FOOBAR_SOMETHING', 123, 'some_data', 10) - - line_no = cnfvar.add_cnf('FOOBAR_OTHER', 0, 'xxx') - cnfvar.mark_as_own_parent(line_no) - - self.assertEqual('10 FOOBAR_SOMETHING,123: "some_data"\n' - '11 FOOBAR_OTHER,0: "xxx"\n', str(cnfvar)) - - def test_save(self): - cnfvar = BuildCnfVar('FOOBAR', 0, 'some_data') - - defaults = {'FOOBAR_SOMETHING': 'abc', - 'FOOBAR_MODE': 'optimize'} - cnfvar.add_defaults(defaults) - - cnfvar.save(TEST_CONFIG_FILENAME) - with open(TEST_CONFIG_FILENAME, 'r') as input: - read_back = input.read() - - self.assertTrue('1 FOOBAR,0: "some_data"\n' - '2 (1) FOOBAR_SOMETHING,0: "abc"\n' - '3 (1) FOOBAR_MODE,0: "optimize"\n' == read_back - or '1 FOOBAR,0: "some_data"\n' - '2 (1) FOOBAR_MODE,0: "optimize"\n' - '3 (1) FOOBAR_SOMETHING,0: "abc"\n' == read_back) - - os.unlink(TEST_CONFIG_FILENAME) - - -if __name__ == '__main__': - unittest.main() diff --git a/test/test_cnfline.py b/test/test_cnfline.py deleted file mode 100644 index 6999ab6..0000000 --- a/test/test_cnfline.py +++ /dev/null @@ -1,74 +0,0 @@ -#!/usr/bin/env python - -# The software in this package is distributed under the GNU General -# Public License version 2 (with a special exception described below). -# -# A copy of GNU General Public License (GPL) is included in this distribution, -# in the file COPYING.GPL. -# -# As a special exception, if other files instantiate templates or use macros -# or inline functions from this file, or you compile this file and link it -# with other works to produce a work based on this file, this file -# does not by itself cause the resulting work to be covered -# by the GNU General Public License. -# -# However the source code for this file must still be made available -# in accordance with section (3) of the GNU General Public License. -# -# This exception does not invalidate any other reasons why a work based -# on this file might be covered by the GNU General Public License. -# -# Copyright (c) 2016-2018 Intra2net AG - -import unittest -from src.cnfline.cnfline import CnfLine -from src.cnfline.build_nic import BuildNIC - - -class CnfLineTest(unittest.TestCase): - def test_simple(self): - line = CnfLine('MY_NAME', 123, 'my_data', 888, 456) - - self.assertEqual('MY_NAME', line.name) - self.assertEqual(123, line.instance) - self.assertEqual('my_data', line.data) - self.assertEqual(888, line.line_no) - self.assertEqual(456, line.parent_line_no) - - def test_deny_empty_name(self): - with self.assertRaises(ValueError): - CnfLine('') - - def test_deny_lineno_zero(self): - with self.assertRaises(ValueError): - CnfLine('foobar', 0, 'some_data', 0) - - def test_str_output_parent(self): - line = CnfLine('MY_NAME', 123, 'my_data', 10, 0) - self.assertEqual('10 MY_NAME,123: "my_data"', str(line)) - - def test_str_output_child(self): - line = CnfLine('MY_NAME', 123, 'my_data', 10, 456) - self.assertEqual('10 (456) MY_NAME,123: "my_data"', str(line)) - - -class BuildNICTest(unittest.TestCase): - def test_nic_comment(self): - nic = BuildNIC('my comment', 10, 100) - - cnf_text = str(nic) - self.assertTrue('NIC_COMMENT,0: "my comment"' in cnf_text) - self.assertEqual('', nic.data) - - def test_change_comment(self): - nic = BuildNIC('initial comment', 10, 100) - nic.comment('new comment') - - cnf_text = str(nic) - self.assertTrue('NIC_COMMENT,0: "new comment"' in cnf_text) - self.assertTrue('initial comment"' not in cnf_text) - self.assertEqual('', nic.data) - - -if __name__ == '__main__': - unittest.main() -- 1.7.1