Skip to content

Commit

Permalink
Merge pull request #2750 from c-po/configdict-T5894
Browse files Browse the repository at this point in the history
configdict: T5894: add get_config_dict() flag with_pki
  • Loading branch information
c-po committed Jan 4, 2024
2 parents c08fea7 + b152b52 commit ada6b10
Show file tree
Hide file tree
Showing 21 changed files with 78 additions and 86 deletions.
4 changes: 2 additions & 2 deletions interface-definitions/include/accel-ppp/client-ip-pool.xml.i
Expand Up @@ -7,7 +7,7 @@
<description>Name of IP pool</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
<children>
Expand Down Expand Up @@ -37,7 +37,7 @@
<description>Name of IP pool</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
</leafNode>
Expand Down
2 changes: 1 addition & 1 deletion interface-definitions/include/accel-ppp/default-pool.xml.i
Expand Up @@ -7,7 +7,7 @@
<description>Default IP pool</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
</leafNode>
Expand Down
Expand Up @@ -10,7 +10,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand All @@ -26,7 +26,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand Down
Expand Up @@ -28,7 +28,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand All @@ -44,7 +44,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand All @@ -60,7 +60,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand Down Expand Up @@ -185,7 +185,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand Down
@@ -0,0 +1,3 @@
<!-- include start from constraint/alpha-numeric-hyphen-underscore-dot.xml.i -->
<regex>[-_a-zA-Z0-9.]+</regex>
<!-- include end -->
2 changes: 1 addition & 1 deletion interface-definitions/include/route-map.xml.i
Expand Up @@ -10,7 +10,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand Down
9 changes: 9 additions & 0 deletions interface-definitions/pki.xml.in
Expand Up @@ -9,6 +9,9 @@
<tagNode name="ca">
<properties>
<help>Certificate Authority</help>
<constraint>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
<children>
<leafNode name="certificate">
Expand Down Expand Up @@ -64,6 +67,9 @@
<tagNode name="certificate">
<properties>
<help>Certificate</help>
<constraint>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
<children>
<leafNode name="certificate">
Expand Down Expand Up @@ -109,6 +115,9 @@
<tagNode name="dh">
<properties>
<help>Diffie-Hellman parameters</help>
<constraint>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
<children>
<leafNode name="parameters">
Expand Down
2 changes: 1 addition & 1 deletion interface-definitions/policy.xml.in
Expand Up @@ -476,7 +476,7 @@
<description>Route map name</description>
</valueHelp>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Name of route-map can only contain alpha-numeric letters, hyphen and underscores</constraintErrorMessage>
</properties>
Expand Down
6 changes: 3 additions & 3 deletions interface-definitions/service_dhcp-server.xml.in
Expand Up @@ -38,7 +38,7 @@
<properties>
<help>Peer name used to identify connection</help>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Invalid failover peer name. May only contain letters, numbers and .-_</constraintErrorMessage>
</properties>
Expand Down Expand Up @@ -78,7 +78,7 @@
<properties>
<help>Name of DHCP shared network</help>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Invalid shared network name. May only contain letters, numbers and .-_</constraintErrorMessage>
</properties>
Expand Down Expand Up @@ -251,7 +251,7 @@
<properties>
<help>DHCP lease range</help>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Invalid range name, may only be alphanumeric, dot and hyphen</constraintErrorMessage>
</properties>
Expand Down
6 changes: 3 additions & 3 deletions interface-definitions/service_dhcpv6-server.xml.in
Expand Up @@ -34,7 +34,7 @@
<properties>
<help>DHCPv6 shared network name</help>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Invalid DHCPv6 shared network name. May only contain letters, numbers and .-_</constraintErrorMessage>
</properties>
Expand Down Expand Up @@ -185,7 +185,7 @@
<properties>
<help>NIS domain name for client to use</help>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Invalid NIS domain name</constraintErrorMessage>
</properties>
Expand All @@ -207,7 +207,7 @@
<properties>
<help>NIS+ domain name for client to use</help>
<constraint>
<regex>[-_a-zA-Z0-9.]+</regex>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
<constraintErrorMessage>Invalid NIS+ domain name. May only contain letters, numbers and .-_</constraintErrorMessage>
</properties>
Expand Down
14 changes: 12 additions & 2 deletions python/vyos/config.py
Expand Up @@ -29,7 +29,7 @@
its own set of operations.
*Leaf nodes* (such as "address" in interfaces) can have values, but cannot
have children.
have children.
Leaf nodes can have one value, multiple values, or no values at all.
For example, "system host-name" is a single-value leaf node,
Expand Down Expand Up @@ -258,7 +258,9 @@ def verify_mangling(self, key_mangling):
def get_config_dict(self, path=[], effective=False, key_mangling=None,
get_first_key=False, no_multi_convert=False,
no_tag_node_value_mangle=False,
with_defaults=False, with_recursive_defaults=False):
with_defaults=False,
with_recursive_defaults=False,
with_pki=False):
"""
Args:
path (str list): Configuration tree path, can be empty
Expand All @@ -274,6 +276,7 @@ def get_config_dict(self, path=[], effective=False, key_mangling=None,
del kwargs['no_multi_convert']
del kwargs['with_defaults']
del kwargs['with_recursive_defaults']
del kwargs['with_pki']

lpath = self._make_path(path)
root_dict = self.get_cached_root_dict(effective)
Expand All @@ -298,6 +301,13 @@ def get_config_dict(self, path=[], effective=False, key_mangling=None,
else:
conf_dict = ConfigDict(conf_dict)

if with_pki and conf_dict:
pki_dict = self.get_config_dict(['pki'], key_mangling=('-', '_'),
no_tag_node_value_mangle=True,
get_first_key=True)
if pki_dict:
conf_dict['pki'] = pki_dict

# save optional args for a call to get_config_defaults
setattr(conf_dict, '_dict_kwargs', kwargs)

Expand Down
13 changes: 9 additions & 4 deletions python/vyos/configdict.py
Expand Up @@ -163,6 +163,9 @@ def node_changed(conf, path, key_mangling=None, recursive=False, expand_nodes=No
output.extend(list(tmp['delete'].keys()))
if expand_nodes & Diff.ADD:
output.extend(list(tmp['add'].keys()))

# remove duplicate keys from list, this happens when a node (e.g. description) is altered
output = list(dict.fromkeys(output))
return output

def get_removed_vlans(conf, path, dict):
Expand Down Expand Up @@ -424,7 +427,7 @@ def get_pppoe_interfaces(conf, vrf=None):

return pppoe_interfaces

def get_interface_dict(config, base, ifname='', recursive_defaults=True):
def get_interface_dict(config, base, ifname='', recursive_defaults=True, with_pki=False):
"""
Common utility function to retrieve and mangle the interfaces configuration
from the CLI input nodes. All interfaces have a common base where value
Expand Down Expand Up @@ -456,7 +459,8 @@ def get_interface_dict(config, base, ifname='', recursive_defaults=True):
get_first_key=True,
no_tag_node_value_mangle=True,
with_defaults=True,
with_recursive_defaults=recursive_defaults)
with_recursive_defaults=recursive_defaults,
with_pki=with_pki)

# If interface does not request an IPv4 DHCP address there is no need
# to keep the dhcp-options key
Expand Down Expand Up @@ -620,7 +624,7 @@ def get_vlan_ids(interface):

return vlan_ids

def get_accel_dict(config, base, chap_secrets):
def get_accel_dict(config, base, chap_secrets, with_pki=False):
"""
Common utility function to retrieve and mangle the Accel-PPP configuration
from different CLI input nodes. All Accel-PPP services have a common base
Expand All @@ -635,7 +639,8 @@ def get_accel_dict(config, base, chap_secrets):
dict = config.get_config_dict(base, key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True,
with_recursive_defaults=True)
with_recursive_defaults=True,
with_pki=with_pki)

# set CPUs cores to process requests
dict.update({'thread_count' : get_half_cpus()})
Expand Down
3 changes: 1 addition & 2 deletions smoketest/scripts/cli/test_service_https.py
Expand Up @@ -395,8 +395,7 @@ def test_api_reset(self):

@ignore_warning(InsecureRequestWarning)
def test_api_config_file_load_http(self):
"""Test load config from HTTP URL
"""
# Test load config from HTTP URL
address = '127.0.0.1'
key = 'VyOS-key'
url = f'https://{address}/config-file'
Expand Down
15 changes: 3 additions & 12 deletions src/conf_mode/interfaces_ethernet.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2021 VyOS maintainers and contributors
# Copyright (C) 2019-2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
Expand Down Expand Up @@ -150,19 +150,12 @@ def get_config(config=None):
else:
conf = Config()

# This must be called prior to get_interface_dict(), as this function will
# alter the config level (config.set_level())
pki = conf.get_config_dict(['pki'], key_mangling=('-', '_'),
get_first_key=True, no_tag_node_value_mangle=True)

base = ['interfaces', 'ethernet']
ifname, ethernet = get_interface_dict(conf, base)
ifname, ethernet = get_interface_dict(conf, base, with_pki=True)

if 'is_bond_member' in ethernet:
update_bond_options(conf, ethernet)

if 'deleted' not in ethernet:
if pki: ethernet['pki'] = pki

tmp = is_node_changed(conf, base + [ifname, 'speed'])
if tmp: ethernet.update({'speed_duplex_changed': {}})

Expand All @@ -171,8 +164,6 @@ def get_config(config=None):

return ethernet



def verify_speed_duplex(ethernet: dict, ethtool: Ethtool):
"""
Verify speed and duplex
Expand Down
16 changes: 6 additions & 10 deletions src/conf_mode/interfaces_openvpn.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2019-2023 VyOS maintainers and contributors
# Copyright (C) 2019-2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
Expand Down Expand Up @@ -89,16 +89,12 @@ def get_config(config=None):
conf = Config()
base = ['interfaces', 'openvpn']

ifname, openvpn = get_interface_dict(conf, base)
ifname, openvpn = get_interface_dict(conf, base, with_pki=True)
openvpn['auth_user_pass_file'] = '/run/openvpn/{ifname}.pw'.format(**openvpn)

if 'deleted' in openvpn:
return openvpn

openvpn['pki'] = conf.get_config_dict(['pki'], key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True)

if is_node_changed(conf, base + [ifname, 'openvpn-option']):
openvpn.update({'restart_required': {}})
if is_node_changed(conf, base + [ifname, 'enable-dco']):
Expand Down Expand Up @@ -167,9 +163,10 @@ def verify_pki(openvpn):
raise ConfigError(f'Invalid shared-secret on openvpn interface {interface}')

# If PSK settings are correct, warn about its deprecation
DeprecationWarning("OpenVPN shared-secret support will be removed in future VyOS versions.\n\
Please migrate your site-to-site tunnels to TLS.\n\
You can use self-signed certificates with peer fingerprint verification, consult the documentation for details.")
DeprecationWarning('OpenVPN shared-secret support will be removed in future '\
'VyOS versions. Please migrate your site-to-site tunnels to '\
'TLS. You can use self-signed certificates with peer fingerprint '\
'verification, consult the documentation for details.')

if tls:
if (mode in ['server', 'client']) and ('ca_certificate' not in tls):
Expand Down Expand Up @@ -729,4 +726,3 @@ def apply(openvpn):
except ConfigError as e:
print(e)
exit(1)

6 changes: 1 addition & 5 deletions src/conf_mode/interfaces_sstpc.py
Expand Up @@ -45,7 +45,7 @@ def get_config(config=None):
else:
conf = Config()
base = ['interfaces', 'sstpc']
ifname, sstpc = get_interface_dict(conf, base)
ifname, sstpc = get_interface_dict(conf, base, with_pki=True)

# We should only terminate the SSTP client session if critical parameters
# change. All parameters that can be changed on-the-fly (like interface
Expand All @@ -57,10 +57,6 @@ def get_config(config=None):
# bail out early - no need to further process other nodes
break

# Load PKI certificates for later processing
sstpc['pki'] = conf.get_config_dict(['pki'], key_mangling=('-', '_'),
get_first_key=True,
no_tag_node_value_mangle=True)
return sstpc

def verify(sstpc):
Expand Down

0 comments on commit ada6b10

Please sign in to comment.