From 0dd75963e82d6f20007d523bbd8a0bbe324f1e7f Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Fri, 10 Apr 2020 23:25:29 +0200 Subject: vpn: l2tp: T2264: use "with open()" when writing config --- src/conf_mode/vpn_l2tp.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/conf_mode/vpn_l2tp.py b/src/conf_mode/vpn_l2tp.py index 4cd28e0fe..b357be1ed 100755 --- a/src/conf_mode/vpn_l2tp.py +++ b/src/conf_mode/vpn_l2tp.py @@ -36,11 +36,6 @@ chap_secrets = l2tp_cnf_dir + '/chap-secrets' l2tp_conf = l2tp_cnf_dir + '/l2tp.config' -# config path creation -if not os.path.exists(l2tp_cnf_dir): - os.makedirs(l2tp_cnf_dir) - - default_config_data = { 'authentication': { 'radiussrv': {}, @@ -358,6 +353,9 @@ def generate(l2tp): if l2tp == None: return None + if not os.path.exists(l2tp_cnf_dir): + os.makedirs(l2tp_cnf_dir) + # Prepare Jinja2 template loader from files tmpl_path = os.path.join(vyos_data_dir['data'], 'templates', 'l2tp') fs_loader = FileSystemLoader(tmpl_path) @@ -365,15 +363,14 @@ def generate(l2tp): tmpl = env.get_template('l2tp.config.tmpl') config_text = tmpl.render(c) - open(l2tp_conf, 'w').write(config_text) + with open(l2tp_conf, 'w') as f: + f.write(config_text) if l2tp['auth_mode'] == 'local': tmpl = env.get_template('chap-secrets.tmpl') - chap_secrets_txt = tmpl.render(l2tp) - old_umask = os.umask(0o077) + config_text = tmpl.render(l2tp) with open(chap_secrets, 'w') as f: - f.write(chap_secrets_txt) - os.umask(old_umask) + f.write(config_text) return None -- cgit v1.2.3