From c0935f08a61b8b18fbd19b6afa892321c3a87758 Mon Sep 17 00:00:00 2001 From: David Sommerseth Date: Thu, 2 Jun 2022 19:54:30 +0200 Subject: [PATCH] Add --force to allow overwriting an existing configuration profile If trying to download and import a configuration profile with the same name, it would need to be removed manually first to succeed. By adding this new --force argument, it will be removed before attempting to import the configuration profile. Signed-off-by: David Sommerseth --- openvpn/connector/configmgr.py | 26 +++++++++++++++++++------- openvpn/connector/main.py | 5 ++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/openvpn/connector/configmgr.py b/openvpn/connector/configmgr.py index 24cf1c9..1f57554 100644 --- a/openvpn/connector/configmgr.py +++ b/openvpn/connector/configmgr.py @@ -23,18 +23,20 @@ from openvpn3 import ConfigurationManager class ConfigImport(object): - def __init__(self, systembus, cfgname): + def __init__(self, systembus, cfgname, force=False): self.__system_bus = systembus self.__cfgmgr = ConfigurationManager(self.__system_bus) self.__config_name = cfgname.replace(' ', '') self._cfgobj = None + self.__overwrite = [] if "OpenVPN Cloud" != cfgname and self.__config_name != cfgname: print('** INFO ** Spaces stripped from configuration ' + 'name. New name: %s' % self.__config_name) - if self.__duplicate_check(self.__config_name) == True: - raise ValueError('Configuration profile name "%s" already exists' % self.__config_name) + if self.__duplicate_check(self.__config_name, force) == True: + if not force: + raise ValueError('Configuration profile name "%s" already exists' % self.__config_name) def GetConfigName(self): @@ -42,6 +44,13 @@ def GetConfigName(self): def Import(self, profile): + if len(self.__overwrite) > 0: + print('** Warning ** Removing old configuration profile with same name') + for cfg in self.__overwrite: + if 'OPENVPN_CLOUD_DEBUG' in os.environ: + print('.. Removing %s' % cfg.GetPath()) + cfg.Remove() + print('Importing VPN configuration profile "%s" ... ' % self.__config_name, end='', flush=True) self._cfgobj = self.__cfgmgr.Import(self.__config_name, @@ -68,15 +77,18 @@ def EnableOwnershipTransfer(self): print('Done') - def __duplicate_check(self, cfgname): + def __duplicate_check(self, cfgname, force): # NOTE: This will only look up configuration names # for the current user. If more users have imported # configuration profiles with the same name, this # will not be detected here. This will require - # improved support within the net.openvpn.v3.configuraiton + # improved support within the net.openvpn.v3.configuration # D-Bus service. + ret = False for cfg in self.__cfgmgr.FetchAvailableConfigs(): n = cfg.GetProperty('name') if cfgname == n: - return True - return False + ret = True + if force: + self.__overwrite.insert(0, cfg) + return ret diff --git a/openvpn/connector/main.py b/openvpn/connector/main.py index 6faa5b9..037f128 100644 --- a/openvpn/connector/main.py +++ b/openvpn/connector/main.py @@ -67,6 +67,8 @@ def main(): help='This value is provided by the OpenVPN Cloud web portal.') cli.add_argument('--name', metavar='NAME', nargs=1, default=['OpenVPN Cloud',], help='Configuration profile name to use. Default: "OpenVPN Cloud"') + cli.add_argument('--force', action='store_true', + help='Overwrite configuration profile if it already exists') cli.add_argument('--autoload-file-prefix', metavar='AUTOLOAD_FILE_PREFIX', nargs=1, default=['connector',], help='Configuration filename to use. Default: connector.conf') cli.add_argument('--no-start', action='store_true', @@ -79,6 +81,7 @@ def main(): token = None autoload_prefix = cliopts.autoload_file_prefix[0] config_name = cliopts.name[0] + force = cliopts.force and True start_config = not cliopts.no_start dco = cliopts.dco and True @@ -125,7 +128,7 @@ def main(): systembus = dbus.SystemBus() cfgimport = None if ConfigModes.UNITFILE == run_mode: - cfgimport = ConfigImport(systembus, config_name) + cfgimport = ConfigImport(systembus, config_name, force) # Download the profile from OpenVPN Cloud profile = ProfileFetch(token)