Skip to content

Commit

Permalink
Move config profile file saving from main() to AutoloadConfig class
Browse files Browse the repository at this point in the history
This changes the AutoloadConfig API slightly, but it moves more of the
overall configuration and setup saving logic into this class instead.

Signed-off-by: David Sommerseth <[email protected]>
  • Loading branch information
dsommers committed Jan 11, 2022
1 parent 1e831e2 commit 3666624
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
36 changes: 26 additions & 10 deletions openvpn/connector/autoload.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# OpenVPN Connector Setup
# - Configure OpenVPN 3 Linux for OpenVPN Cloud
#
# Copyright (C) 2020 OpenVPN Inc. <[email protected]>
# Copyright (C) 2020 David Sommerseth <[email protected]>
# Copyright (C) 2020 - 2022 OpenVPN Inc. <[email protected]>
# Copyright (C) 2020 - 2022 David Sommerseth <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
Expand All @@ -20,18 +20,24 @@

import os
import json
from pathlib import Path

class AutoloadConfig(object):
def __init__(self, cfg):
cfgdir = os.path.dirname(cfg)
main_cfgname = os.path.basename(cfg)
al_cfgname = "%s.autoload" % ".".join(main_cfgname.split('.')[:-1])
self._cfgname = os.path.join(cfgdir, al_cfgname)
def __init__(self, profile, rootdir, cfgname_prefix):
self._profile = profile
self._rootdir = rootdir

al_cfgname = '%s.autoload' % cfgname_prefix
self._autoload_file = os.path.join(rootdir, al_cfgname)

vpn_cfgname = '%s.conf' % cfgname_prefix
self._config_file = os.path.join(rootdir, vpn_cfgname)

self._properties = {}


def GetConfigFilename(self):
return self._cfgname
def GetAutoloadFilename(self):
return self._autoload_file


def SetName(self, name):
Expand All @@ -52,10 +58,20 @@ def SetTunnelParams(self, key, value):


def Save(self):
# Ensure proper destination directories exists
config_dir = os.path.join(self._rootdir, 'etc','openvpn3','autoload')
Path(config_dir).mkdir(parents=True, exist_ok=True)

print('Saving VPN configuration profile to "%s" ... ' % self._config_file, end='', flush=True)
self._profile.Save(self._config_file)
print('Done')

print('Saving openvpn3-autoload config to "%s" ... ' % self._autoload_file, end='', flush=True)
j = json.dumps(self._properties, indent=4)
fp = open(self._cfgname, 'wb')
fp = open(self._autoload_file, 'wb')
fp.write(j.encode('utf-8'))
fp.close()
print('Done')


def _check_property_section(self, key, props):
Expand Down
15 changes: 1 addition & 14 deletions openvpn/connector/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import os
import argparse
import dbus
from pathlib import Path
from enum import Enum
from openvpn.connector.token import DecodeToken
from openvpn.connector.profile import ProfileFetch, DecryptError, DownloadError
Expand Down Expand Up @@ -121,24 +120,12 @@ def main():
print('Done')

if ConfigModes.AUTOLOAD == run_mode:
# Ensure proper destination directories exists
config_dir = os.path.join(rootdir, 'etc','openvpn3','autoload')
Path(config_dir).mkdir(parents=True, exist_ok=True)

cfg_filename = autoload_prefix + '.conf'
cfg = os.path.join(config_dir, cfg_filename)
print('Saving profile to "%s" ... ' % cfg, end='', flush=True)
profile.Save(cfg)
print('Done')

# Generate the openvpn3-autoload configuration
autoload = AutoloadConfig(cfg)
print('Saving openvpn3-autoload config to "%s" ... ' % autoload.GetConfigFilename(), end='', flush=True)
autoload = AutoloadConfig(profile, rootdir, autoload_prefix)
autoload.SetName(config_name)
autoload.SetAutostart(True)
autoload.SetTunnelParams('persist', True)
autoload.Save()
print('Done')

if start_config is True and '/' == rootdir and os.geteuid() == 0:
service = SystemdServiceUnit(dbus.SystemBus(), 'openvpn3-autoload.service')
Expand Down

0 comments on commit 3666624

Please sign in to comment.