Skip to content

Commit

Permalink
Add --force to allow overwriting an existing configuration profile
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dsommers committed Jun 2, 2022
1 parent 54d2e40 commit c0935f0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
26 changes: 19 additions & 7 deletions openvpn/connector/configmgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@
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):
return self.__config_name


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,
Expand All @@ -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
5 changes: 4 additions & 1 deletion openvpn/connector/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c0935f0

Please sign in to comment.