diff --git a/interface-definitions/service_config-sync.xml.in b/interface-definitions/service_config-sync.xml.in index cb51a33b1..e9ea9aa4b 100644 --- a/interface-definitions/service_config-sync.xml.in +++ b/interface-definitions/service_config-sync.xml.in @@ -1,518 +1,524 @@ <?xml version="1.0" encoding="UTF-8"?> <interfaceDefinition> <node name="service"> <children> <node name="config-sync" owner="${vyos_conf_scripts_dir}/service_config-sync.py"> <properties> <help>Configuration synchronization</help> </properties> <children> <node name="secondary"> <properties> <help>Secondary server parameters</help> </properties> <children> <leafNode name="address"> <properties> <help>IP address</help> <valueHelp> <format>ipv4</format> <description>IPv4 address to match</description> </valueHelp> <valueHelp> <format>ipv6</format> <description>IPv6 address to match</description> </valueHelp> <valueHelp> <format>hostname</format> <description>FQDN address to match</description> </valueHelp> <constraint> <validator name="ipv4-address"/> <validator name="ipv6-address"/> <validator name="fqdn"/> </constraint> </properties> </leafNode> <leafNode name="timeout"> <properties> <help>Connection API timeout</help> <valueHelp> <format>u32:1-3600</format> <description>Connection API timeout</description> </valueHelp> <constraint> <validator name="numeric" argument="--range 1-3600"/> </constraint> </properties> <defaultValue>60</defaultValue> </leafNode> <leafNode name="key"> <properties> <help>HTTP API key</help> </properties> </leafNode> </children> </node> <leafNode name="mode"> <properties> <help>Synchronization mode</help> <completionHelp> <list>load set</list> </completionHelp> <valueHelp> <format>load</format> <description>Load and replace configuration section</description> </valueHelp> <valueHelp> <format>set</format> <description>Set configuration section</description> </valueHelp> <constraint> <regex>(load|set)</regex> </constraint> </properties> </leafNode> <node name="section"> <properties> <help>Section for synchronization</help> </properties> <children> <leafNode name="firewall"> <properties> <help>Firewall</help> <valueless/> </properties> </leafNode> <node name="interfaces"> <properties> <help>Interfaces</help> </properties> <children> <leafNode name="bonding"> <properties> <help>Bonding interface</help> <valueless/> </properties> </leafNode> <leafNode name="bridge"> <properties> <help>Bridge interface</help> <valueless/> </properties> </leafNode> <leafNode name="dummy"> <properties> <help>Dummy interface</help> <valueless/> </properties> </leafNode> <leafNode name="ethernet"> <properties> <help>Ethernet interface</help> <valueless/> </properties> </leafNode> <leafNode name="geneve"> <properties> <help>GENEVE interface</help> <valueless/> </properties> </leafNode> <leafNode name="input"> <properties> <help>Input interface</help> <valueless/> </properties> </leafNode> <leafNode name="l2tpv3"> <properties> <help>L2TPv3 interface</help> <valueless/> </properties> </leafNode> <leafNode name="loopback"> <properties> <help>Loopback interface</help> <valueless/> </properties> </leafNode> <leafNode name="macsec"> <properties> <help>MACsec interface</help> <valueless/> </properties> </leafNode> <leafNode name="openvpn"> <properties> <help>OpenVPN interface</help> <valueless/> </properties> </leafNode> <leafNode name="pppoe"> <properties> <help>PPPoE interface</help> <valueless/> </properties> </leafNode> <leafNode name="pseudo-ethernet"> <properties> <help>Pseudo-Ethernet interface</help> <valueless/> </properties> </leafNode> <leafNode name="sstpc"> <properties> <help>SSTP client interface</help> <valueless/> </properties> </leafNode> <leafNode name="tunnel"> <properties> <help>Tunnel interface</help> <valueless/> </properties> </leafNode> <leafNode name="virtual-ethernet"> <properties> <help>Virtual Ethernet interface</help> <valueless/> </properties> </leafNode> <leafNode name="vti"> <properties> <help>Virtual tunnel interface</help> <valueless/> </properties> </leafNode> <leafNode name="vxlan"> <properties> <help>VXLAN interface</help> <valueless/> </properties> </leafNode> <leafNode name="wireguard"> <properties> <help>Wireguard interface</help> <valueless/> </properties> </leafNode> <leafNode name="wireless"> <properties> <help>Wireless interface</help> <valueless/> </properties> </leafNode> <leafNode name="wwan"> <properties> <help>WWAN interface</help> <valueless/> </properties> </leafNode> </children> </node> <leafNode name="nat"> <properties> <help>NAT</help> <valueless/> </properties> </leafNode> <leafNode name="nat66"> <properties> <help>NAT66</help> <valueless/> </properties> </leafNode> <leafNode name="pki"> <properties> <help>Public key infrastructure (PKI)</help> <valueless/> </properties> </leafNode> <leafNode name="policy"> <properties> <help>Routing policy</help> <valueless/> </properties> </leafNode> <node name="protocols"> <properties> <help>Routing protocols</help> </properties> <children> <leafNode name="babel"> <properties> <help>Babel Routing Protocol</help> <valueless/> </properties> </leafNode> <leafNode name="bfd"> <properties> <help>Bidirectional Forwarding Detection (BFD)</help> <valueless/> </properties> </leafNode> <leafNode name="bgp"> <properties> <help>Border Gateway Protocol (BGP)</help> <valueless/> </properties> </leafNode> <leafNode name="failover"> <properties> <help>Failover route</help> <valueless/> </properties> </leafNode> <leafNode name="igmp-proxy"> <properties> <help>Internet Group Management Protocol (IGMP) proxy</help> <valueless/> </properties> </leafNode> <leafNode name="isis"> <properties> <help>Intermediate System to Intermediate System (IS-IS)</help> <valueless/> </properties> </leafNode> <leafNode name="mpls"> <properties> <help>Multiprotocol Label Switching (MPLS)</help> <valueless/> </properties> </leafNode> <leafNode name="nhrp"> <properties> <help>Next Hop Resolution Protocol (NHRP) parameters</help> <valueless/> </properties> </leafNode> <leafNode name="ospf"> <properties> <help>Open Shortest Path First (OSPF)</help> <valueless/> </properties> </leafNode> <leafNode name="ospfv3"> <properties> <help>Open Shortest Path First (OSPF) for IPv6</help> <valueless/> </properties> </leafNode> <leafNode name="pim"> <properties> <help>Protocol Independent Multicast (PIM) and IGMP</help> <valueless/> </properties> </leafNode> <leafNode name="pim6"> <properties> <help>Protocol Independent Multicast for IPv6 (PIMv6) and MLD</help> <valueless/> </properties> </leafNode> <leafNode name="rip"> <properties> <help>Routing Information Protocol (RIP) parameters</help> <valueless/> </properties> </leafNode> <leafNode name="ripng"> <properties> <help>Routing Information Protocol (RIPng) parameters</help> <valueless/> </properties> </leafNode> <leafNode name="rpki"> <properties> <help>Resource Public Key Infrastructure (RPKI)</help> <valueless/> </properties> </leafNode> <leafNode name="segment-routing"> <properties> <help>Segment Routing</help> <valueless/> </properties> </leafNode> <leafNode name="static"> <properties> <help>Static Routing</help> <valueless/> </properties> </leafNode> </children> </node> <node name="qos"> <properties> <help>Quality of Service (QoS)</help> </properties> <children> <leafNode name="interface"> <properties> <help>Interface to apply QoS policy</help> <valueless/> </properties> </leafNode> <leafNode name="policy"> <properties> <help>Service Policy definitions</help> <valueless/> </properties> </leafNode> </children> </node> <node name="service"> <properties> <help>System services</help> </properties> <children> <leafNode name="console-server"> <properties> <help>Serial Console Server</help> <valueless/> </properties> </leafNode> <leafNode name="dhcp-relay"> <properties> <help>Host Configuration Protocol (DHCP) relay agent</help> <valueless/> </properties> </leafNode> <leafNode name="dhcp-server"> <properties> <help>Dynamic Host Configuration Protocol (DHCP) for DHCP server</help> <valueless/> </properties> </leafNode> <leafNode name="dhcpv6-relay"> <properties> <help>DHCPv6 Relay Agent parameters</help> <valueless/> </properties> </leafNode> <leafNode name="dhcpv6-server"> <properties> <help>DHCP for IPv6 (DHCPv6) server</help> <valueless/> </properties> </leafNode> <leafNode name="dns"> <properties> <help>Domain Name System (DNS) related services</help> <valueless/> </properties> </leafNode> <leafNode name="lldp"> <properties> <help>LLDP settings</help> <valueless/> </properties> </leafNode> <leafNode name="mdns"> <properties> <help>Multicast DNS (mDNS) parameters</help> <valueless/> </properties> </leafNode> <leafNode name="monitoring"> <properties> <help>Monitoring services</help> <valueless/> </properties> </leafNode> <leafNode name="ndp-proxy"> <properties> <help>Neighbor Discovery Protocol (NDP) Proxy</help> <valueless/> </properties> </leafNode> <leafNode name="ntp"> <properties> <help>Network Time Protocol (NTP) configuration</help> <valueless/> </properties> </leafNode> <leafNode name="snmp"> <properties> <help>Simple Network Management Protocol (SNMP)</help> <valueless/> </properties> </leafNode> <leafNode name="tftp-server"> <properties> <help>Trivial File Transfer Protocol (TFTP) server</help> <valueless/> </properties> </leafNode> <leafNode name="webproxy"> <properties> <help>Webproxy service settings</help> <valueless/> </properties> </leafNode> </children> </node> <node name="system"> <properties> <help>System parameters</help> </properties> <children> <leafNode name="conntrack"> <properties> <help>Connection Tracking</help> <valueless/> </properties> </leafNode> <leafNode name="flow-accounting"> <properties> <help>Flow accounting</help> <valueless/> </properties> </leafNode> <leafNode name="option"> <properties> <help>System Options</help> <valueless/> </properties> </leafNode> <leafNode name="sflow"> <properties> <help>sFlow</help> <valueless/> </properties> </leafNode> <leafNode name="static-host-mapping"> <properties> <help>Map host names to addresses</help> <valueless/> </properties> </leafNode> <leafNode name="sysctl"> <properties> <help>Configure kernel parameters at runtime</help> <valueless/> </properties> </leafNode> + <leafNode name="time-zone"> + <properties> + <help>Local time zone</help> + <valueless/> + </properties> + </leafNode> </children> </node> <leafNode name="vpn"> <properties> <help>Virtual Private Network (VPN)</help> <valueless/> </properties> </leafNode> <leafNode name="vrf"> <properties> <help>Virtual Routing and Forwarding</help> <valueless/> </properties> </leafNode> </children> </node> </children> </node> </children> </node> </interfaceDefinition> diff --git a/python/vyos/configsession.py b/python/vyos/configsession.py index 90842b749..ab7a631bb 100644 --- a/python/vyos/configsession.py +++ b/python/vyos/configsession.py @@ -1,249 +1,268 @@ # configsession -- the write API for the VyOS running config # Copyright (C) 2019-2023 VyOS maintainers and contributors # # This library is free software; you can redistribute it and/or modify it under the terms of # the GNU Lesser General Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License along with this library; # if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os import re import sys import subprocess from vyos.utils.process import is_systemd_service_running from vyos.utils.dict import dict_to_paths CLI_SHELL_API = '/bin/cli-shell-api' SET = '/opt/vyatta/sbin/my_set' DELETE = '/opt/vyatta/sbin/my_delete' COMMENT = '/opt/vyatta/sbin/my_comment' COMMIT = '/opt/vyatta/sbin/my_commit' DISCARD = '/opt/vyatta/sbin/my_discard' SHOW_CONFIG = ['/bin/cli-shell-api', 'showConfig'] LOAD_CONFIG = ['/bin/cli-shell-api', 'loadFile'] MIGRATE_LOAD_CONFIG = ['/usr/libexec/vyos/vyos-load-config.py'] SAVE_CONFIG = ['/usr/libexec/vyos/vyos-save-config.py'] INSTALL_IMAGE = ['/usr/libexec/vyos/op_mode/image_installer.py', '--action', 'add', '--no-prompt', '--image-path'] REMOVE_IMAGE = ['/usr/libexec/vyos/op_mode/image_manager.py', '--action', 'delete', '--no-prompt', '--image-name'] GENERATE = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'generate'] SHOW = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'show'] RESET = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'reset'] REBOOT = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'reboot'] POWEROFF = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'poweroff'] OP_CMD_ADD = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'add'] OP_CMD_DELETE = ['/opt/vyatta/bin/vyatta-op-cmd-wrapper', 'delete'] # Default "commit via" string APP = "vyos-http-api" # When started as a service rather than from a user shell, # the process lacks the VyOS-specific environment that comes # from bash configs, so we have to inject it # XXX: maybe it's better to do via a systemd environment file def inject_vyos_env(env): env['VYATTA_CFG_GROUP_NAME'] = 'vyattacfg' env['VYATTA_USER_LEVEL_DIR'] = '/opt/vyatta/etc/shell/level/admin' env['VYATTA_PROCESS_CLIENT'] = 'gui2_rest' env['VYOS_HEADLESS_CLIENT'] = 'vyos_http_api' env['vyatta_bindir']= '/opt/vyatta/bin' env['vyatta_cfg_templates'] = '/opt/vyatta/share/vyatta-cfg/templates' env['vyatta_configdir'] = '/opt/vyatta/config' env['vyatta_datadir'] = '/opt/vyatta/share' env['vyatta_datarootdir'] = '/opt/vyatta/share' env['vyatta_libdir'] = '/opt/vyatta/lib' env['vyatta_libexecdir'] = '/opt/vyatta/libexec' env['vyatta_op_templates'] = '/opt/vyatta/share/vyatta-op/templates' env['vyatta_prefix'] = '/opt/vyatta' env['vyatta_sbindir'] = '/opt/vyatta/sbin' env['vyatta_sysconfdir'] = '/opt/vyatta/etc' env['vyos_bin_dir'] = '/usr/bin' env['vyos_cfg_templates'] = '/opt/vyatta/share/vyatta-cfg/templates' env['vyos_completion_dir'] = '/usr/libexec/vyos/completion' env['vyos_configdir'] = '/opt/vyatta/config' env['vyos_conf_scripts_dir'] = '/usr/libexec/vyos/conf_mode' env['vyos_datadir'] = '/opt/vyatta/share' env['vyos_datarootdir']= '/opt/vyatta/share' env['vyos_libdir'] = '/opt/vyatta/lib' env['vyos_libexec_dir'] = '/usr/libexec/vyos' env['vyos_op_scripts_dir'] = '/usr/libexec/vyos/op_mode' env['vyos_op_templates'] = '/opt/vyatta/share/vyatta-op/templates' env['vyos_prefix'] = '/opt/vyatta' env['vyos_sbin_dir'] = '/usr/sbin' env['vyos_validators_dir'] = '/usr/libexec/vyos/validators' # if running the vyos-configd daemon, inject the vyshim env var if is_systemd_service_running('vyos-configd.service'): env['vyshim'] = '/usr/sbin/vyshim' return env class ConfigSessionError(Exception): pass class ConfigSession(object): """ The write API of VyOS. """ def __init__(self, session_id, app=APP): """ Creates a new config session. Args: session_id (str): Session identifier app (str): Application name, purely informational Note: The session identifier MUST be globally unique within the system. The best practice is to only have one ConfigSession object per process and used the PID for the session identifier. """ env_str = subprocess.check_output([CLI_SHELL_API, 'getSessionEnv', str(session_id)]) self.__session_id = session_id # Extract actual variables from the chunk of shell it outputs # XXX: it's better to extend cli-shell-api to provide easily readable output env_list = re.findall(r'([A-Z_]+)=([^;\s]+)', env_str.decode()) session_env = os.environ session_env = inject_vyos_env(session_env) for k, v in env_list: session_env[k] = v self.__session_env = session_env self.__session_env["COMMIT_VIA"] = app self.__run_command([CLI_SHELL_API, 'setupSession']) def __del__(self): try: output = subprocess.check_output([CLI_SHELL_API, 'teardownSession'], env=self.__session_env).decode().strip() if output: print("cli-shell-api teardownSession output for sesion {0}: {1}".format(self.__session_id, output), file=sys.stderr) except Exception as e: print("Could not tear down session {0}: {1}".format(self.__session_id, e), file=sys.stderr) def __run_command(self, cmd_list): p = subprocess.Popen(cmd_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=self.__session_env) (stdout_data, stderr_data) = p.communicate() output = stdout_data.decode() result = p.wait() if result != 0: raise ConfigSessionError(output) return output def get_session_env(self): return self.__session_env def set(self, path, value=None): if not value: value = [] else: value = [value] self.__run_command([SET] + path + value) def set_section(self, path: list, d: dict): try: for p in dict_to_paths(d): self.set(path + p) except (ValueError, ConfigSessionError) as e: raise ConfigSessionError(e) def delete(self, path, value=None): if not value: value = [] else: value = [value] self.__run_command([DELETE] + path + value) def load_section(self, path: list, d: dict): try: self.delete(path) if d: for p in dict_to_paths(d): self.set(path + p) except (ValueError, ConfigSessionError) as e: raise ConfigSessionError(e) + def set_section_tree(self, d: dict): + try: + if d: + for p in dict_to_paths(d): + self.set(p) + except (ValueError, ConfigSessionError) as e: + raise ConfigSessionError(e) + + def load_section_tree(self, mask: dict, d: dict): + try: + if mask: + for p in dict_to_paths(mask): + self.delete(p) + if d: + for p in dict_to_paths(d): + self.set(p) + except (ValueError, ConfigSessionError) as e: + raise ConfigSessionError(e) + def comment(self, path, value=None): if not value: value = [""] else: value = [value] self.__run_command([COMMENT] + path + value) def commit(self): out = self.__run_command([COMMIT]) return out def discard(self): self.__run_command([DISCARD]) def show_config(self, path, format='raw'): config_data = self.__run_command(SHOW_CONFIG + path) if format == 'raw': return config_data def load_config(self, file_path): out = self.__run_command(LOAD_CONFIG + [file_path]) return out def migrate_and_load_config(self, file_path): out = self.__run_command(MIGRATE_LOAD_CONFIG + [file_path]) return out def save_config(self, file_path): out = self.__run_command(SAVE_CONFIG + [file_path]) return out def install_image(self, url): out = self.__run_command(INSTALL_IMAGE + [url]) return out def remove_image(self, name): out = self.__run_command(REMOVE_IMAGE + [name]) return out def generate(self, path): out = self.__run_command(GENERATE + path) return out def show(self, path): out = self.__run_command(SHOW + path) return out def reboot(self, path): out = self.__run_command(REBOOT + path) return out def reset(self, path): out = self.__run_command(RESET + path) return out def poweroff(self, path): out = self.__run_command(POWEROFF + path) return out def add_container_image(self, name): out = self.__run_command(OP_CMD_ADD + ['container', 'image'] + [name]) return out def delete_container_image(self, name): out = self.__run_command(OP_CMD_DELETE + ['container', 'image'] + [name]) return out def show_container_image(self): out = self.__run_command(SHOW + ['container', 'image']) return out diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 423fe01ed..e4b282d72 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -1,460 +1,484 @@ # configtree -- a standalone VyOS config file manipulation library (Python bindings) # Copyright (C) 2018-2022 VyOS maintainers and contributors # # This library is free software; you can redistribute it and/or modify it under the terms of # the GNU Lesser General Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License along with this library; # if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import os import re import json from ctypes import cdll, c_char_p, c_void_p, c_int, c_bool LIBPATH = '/usr/lib/libvyosconfig.so.0' def replace_backslash(s, search, replace): """Modify quoted strings containing backslashes not of escape sequences""" def replace_method(match): result = match.group().replace(search, replace) return result p = re.compile(r'("[^"]*[\\][^"]*"\n|\'[^\']*[\\][^\']*\'\n)') return p.sub(replace_method, s) def escape_backslash(string: str) -> str: """Escape single backslashes in quoted strings""" result = replace_backslash(string, '\\', '\\\\') return result def unescape_backslash(string: str) -> str: """Unescape backslashes in quoted strings""" result = replace_backslash(string, '\\\\', '\\') return result def extract_version(s): """ Extract the version string from the config string """ t = re.split('(^//)', s, maxsplit=1, flags=re.MULTILINE) return (s, ''.join(t[1:])) def check_path(path): # Necessary type checking if not isinstance(path, list): raise TypeError("Expected a list, got a {}".format(type(path))) else: pass class ConfigTreeError(Exception): pass class ConfigTree(object): def __init__(self, config_string=None, address=None, libpath=LIBPATH): if config_string is None and address is None: raise TypeError("ConfigTree() requires one of 'config_string' or 'address'") self.__config = None self.__lib = cdll.LoadLibrary(libpath) # Import functions self.__from_string = self.__lib.from_string self.__from_string.argtypes = [c_char_p] self.__from_string.restype = c_void_p self.__get_error = self.__lib.get_error self.__get_error.argtypes = [] self.__get_error.restype = c_char_p self.__to_string = self.__lib.to_string self.__to_string.argtypes = [c_void_p, c_bool] self.__to_string.restype = c_char_p self.__to_commands = self.__lib.to_commands self.__to_commands.argtypes = [c_void_p, c_char_p] self.__to_commands.restype = c_char_p self.__to_json = self.__lib.to_json self.__to_json.argtypes = [c_void_p] self.__to_json.restype = c_char_p self.__to_json_ast = self.__lib.to_json_ast self.__to_json_ast.argtypes = [c_void_p] self.__to_json_ast.restype = c_char_p self.__set_add_value = self.__lib.set_add_value self.__set_add_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__set_add_value.restype = c_int self.__delete_value = self.__lib.delete_value self.__delete_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__delete_value.restype = c_int self.__delete = self.__lib.delete_node self.__delete.argtypes = [c_void_p, c_char_p] self.__delete.restype = c_int self.__rename = self.__lib.rename_node self.__rename.argtypes = [c_void_p, c_char_p, c_char_p] self.__rename.restype = c_int self.__copy = self.__lib.copy_node self.__copy.argtypes = [c_void_p, c_char_p, c_char_p] self.__copy.restype = c_int self.__set_replace_value = self.__lib.set_replace_value self.__set_replace_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__set_replace_value.restype = c_int self.__set_valueless = self.__lib.set_valueless self.__set_valueless.argtypes = [c_void_p, c_char_p] self.__set_valueless.restype = c_int self.__exists = self.__lib.exists self.__exists.argtypes = [c_void_p, c_char_p] self.__exists.restype = c_int self.__list_nodes = self.__lib.list_nodes self.__list_nodes.argtypes = [c_void_p, c_char_p] self.__list_nodes.restype = c_char_p self.__return_value = self.__lib.return_value self.__return_value.argtypes = [c_void_p, c_char_p] self.__return_value.restype = c_char_p self.__return_values = self.__lib.return_values self.__return_values.argtypes = [c_void_p, c_char_p] self.__return_values.restype = c_char_p self.__is_tag = self.__lib.is_tag self.__is_tag.argtypes = [c_void_p, c_char_p] self.__is_tag.restype = c_int self.__set_tag = self.__lib.set_tag self.__set_tag.argtypes = [c_void_p, c_char_p] self.__set_tag.restype = c_int self.__get_subtree = self.__lib.get_subtree self.__get_subtree.argtypes = [c_void_p, c_char_p] self.__get_subtree.restype = c_void_p self.__destroy = self.__lib.destroy self.__destroy.argtypes = [c_void_p] if address is None: config_section, version_section = extract_version(config_string) config_section = escape_backslash(config_section) config = self.__from_string(config_section.encode()) if config is None: msg = self.__get_error().decode() raise ValueError("Failed to parse config: {0}".format(msg)) else: self.__config = config self.__version = version_section else: self.__config = address self.__version = '' self.__migration = os.environ.get('VYOS_MIGRATION') def __del__(self): if self.__config is not None: self.__destroy(self.__config) def __str__(self): return self.to_string() def _get_config(self): return self.__config def get_version_string(self): return self.__version def to_string(self, ordered_values=False): config_string = self.__to_string(self.__config, ordered_values).decode() config_string = unescape_backslash(config_string) config_string = "{0}\n{1}".format(config_string, self.__version) return config_string def to_commands(self, op="set"): commands = self.__to_commands(self.__config, op.encode()).decode() commands = unescape_backslash(commands) return commands def to_json(self): return self.__to_json(self.__config).decode() def to_json_ast(self): return self.__to_json_ast(self.__config).decode() def set(self, path, value=None, replace=True): """Set new entry in VyOS configuration. path: configuration path e.g. 'system dns forwarding listen-address' value: value to be added to node, e.g. '172.18.254.201' replace: True: current occurance will be replaced False: new value will be appended to current occurances - use this for adding values to a multi node """ check_path(path) path_str = " ".join(map(str, path)).encode() if value is None: self.__set_valueless(self.__config, path_str) else: if replace: self.__set_replace_value(self.__config, path_str, str(value).encode()) else: self.__set_add_value(self.__config, path_str, str(value).encode()) if self.__migration: print(f"- op: set path: {path} value: {value} replace: {replace}") def delete(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__delete(self.__config, path_str) if (res != 0): raise ConfigTreeError(f"Path doesn't exist: {path}") if self.__migration: print(f"- op: delete path: {path}") def delete_value(self, path, value): check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__delete_value(self.__config, path_str, value.encode()) if (res != 0): if res == 1: raise ConfigTreeError(f"Path doesn't exist: {path}") elif res == 2: raise ConfigTreeError(f"Value doesn't exist: '{value}'") else: raise ConfigTreeError() if self.__migration: print(f"- op: delete_value path: {path} value: {value}") def rename(self, path, new_name): check_path(path) path_str = " ".join(map(str, path)).encode() newname_str = new_name.encode() # Check if a node with intended new name already exists new_path = path[:-1] + [new_name] if self.exists(new_path): raise ConfigTreeError() res = self.__rename(self.__config, path_str, newname_str) if (res != 0): raise ConfigTreeError("Path [{}] doesn't exist".format(path)) if self.__migration: print(f"- op: rename old_path: {path} new_path: {new_path}") def copy(self, old_path, new_path): check_path(old_path) check_path(new_path) oldpath_str = " ".join(map(str, old_path)).encode() newpath_str = " ".join(map(str, new_path)).encode() # Check if a node with intended new name already exists if self.exists(new_path): raise ConfigTreeError() res = self.__copy(self.__config, oldpath_str, newpath_str) if (res != 0): msg = self.__get_error().decode() raise ConfigTreeError(msg) if self.__migration: print(f"- op: copy old_path: {old_path} new_path: {new_path}") def exists(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__exists(self.__config, path_str) if (res == 0): return False else: return True def list_nodes(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res_json = self.__list_nodes(self.__config, path_str).decode() res = json.loads(res_json) if res is None: raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) else: return res def return_value(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res_json = self.__return_value(self.__config, path_str).decode() res = json.loads(res_json) if res is None: raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) else: return res def return_values(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res_json = self.__return_values(self.__config, path_str).decode() res = json.loads(res_json) if res is None: raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) else: return res def is_tag(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__is_tag(self.__config, path_str) if (res >= 1): return True else: return False def set_tag(self, path): check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__set_tag(self.__config, path_str) if (res == 0): return True else: raise ConfigTreeError("Path [{}] doesn't exist".format(path_str)) def get_subtree(self, path, with_node=False): check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__get_subtree(self.__config, path_str, with_node) subt = ConfigTree(address=res) return subt def show_diff(left, right, path=[], commands=False, libpath=LIBPATH): if left is None: left = ConfigTree(config_string='\n') if right is None: right = ConfigTree(config_string='\n') if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)): raise TypeError("Arguments must be instances of ConfigTree") if path: if (not left.exists(path)) and (not right.exists(path)): raise ConfigTreeError(f"Path {path} doesn't exist") check_path(path) path_str = " ".join(map(str, path)).encode() __lib = cdll.LoadLibrary(libpath) __show_diff = __lib.show_diff __show_diff.argtypes = [c_bool, c_char_p, c_void_p, c_void_p] __show_diff.restype = c_char_p __get_error = __lib.get_error __get_error.argtypes = [] __get_error.restype = c_char_p res = __show_diff(commands, path_str, left._get_config(), right._get_config()) res = res.decode() if res == "#1@": msg = __get_error().decode() raise ConfigTreeError(msg) res = unescape_backslash(res) return res def union(left, right, libpath=LIBPATH): if left is None: left = ConfigTree(config_string='\n') if right is None: right = ConfigTree(config_string='\n') if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)): raise TypeError("Arguments must be instances of ConfigTree") __lib = cdll.LoadLibrary(libpath) __tree_union = __lib.tree_union __tree_union.argtypes = [c_void_p, c_void_p] __tree_union.restype = c_void_p __get_error = __lib.get_error __get_error.argtypes = [] __get_error.restype = c_char_p res = __tree_union( left._get_config(), right._get_config()) tree = ConfigTree(address=res) return tree +def mask_inclusive(left, right, libpath=LIBPATH): + if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)): + raise TypeError("Arguments must be instances of ConfigTree") + + try: + __lib = cdll.LoadLibrary(libpath) + __mask_tree = __lib.mask_tree + __mask_tree.argtypes = [c_void_p, c_void_p] + __mask_tree.restype = c_void_p + __get_error = __lib.get_error + __get_error.argtypes = [] + __get_error.restype = c_char_p + + res = __mask_tree(left._get_config(), right._get_config()) + except Exception as e: + raise ConfigTreeError(e) + if not res: + msg = __get_error().decode() + raise ConfigTreeError(msg) + + tree = ConfigTree(address=res) + + return tree + def reference_tree_to_json(from_dir, to_file, libpath=LIBPATH): try: __lib = cdll.LoadLibrary(libpath) __reference_tree_to_json = __lib.reference_tree_to_json __reference_tree_to_json.argtypes = [c_char_p, c_char_p] __get_error = __lib.get_error __get_error.argtypes = [] __get_error.restype = c_char_p res = __reference_tree_to_json(from_dir.encode(), to_file.encode()) except Exception as e: raise ConfigTreeError(e) if res == 1: msg = __get_error().decode() raise ConfigTreeError(msg) class DiffTree: def __init__(self, left, right, path=[], libpath=LIBPATH): if left is None: left = ConfigTree(config_string='\n') if right is None: right = ConfigTree(config_string='\n') if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)): raise TypeError("Arguments must be instances of ConfigTree") if path: if not left.exists(path): raise ConfigTreeError(f"Path {path} doesn't exist in lhs tree") if not right.exists(path): raise ConfigTreeError(f"Path {path} doesn't exist in rhs tree") self.left = left self.right = right self.__lib = cdll.LoadLibrary(libpath) self.__diff_tree = self.__lib.diff_tree self.__diff_tree.argtypes = [c_char_p, c_void_p, c_void_p] self.__diff_tree.restype = c_void_p check_path(path) path_str = " ".join(map(str, path)).encode() res = self.__diff_tree(path_str, left._get_config(), right._get_config()) # full diff config_tree and python dict representation self.full = ConfigTree(address=res) self.dict = json.loads(self.full.to_json()) # config_tree sub-trees self.add = self.full.get_subtree(['add']) self.sub = self.full.get_subtree(['sub']) self.inter = self.full.get_subtree(['inter']) self.delete = self.full.get_subtree(['del']) def to_commands(self): add = self.add.to_commands() delete = self.delete.to_commands(op="delete") return delete + "\n" + add diff --git a/src/helpers/vyos_config_sync.py b/src/helpers/vyos_config_sync.py index 77f7cd810..0604b2837 100755 --- a/src/helpers/vyos_config_sync.py +++ b/src/helpers/vyos_config_sync.py @@ -1,202 +1,204 @@ #!/usr/bin/env python3 # # Copyright (C) 2023-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 # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # import os import json import requests import urllib3 import logging -from typing import Optional, List, Union, Dict, Any +from typing import Optional, List, Tuple, Dict, Any from vyos.config import Config +from vyos.configtree import ConfigTree +from vyos.configtree import mask_inclusive from vyos.template import bracketize_ipv6 CONFIG_FILE = '/run/config_sync_conf.conf' # Logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.name = os.path.basename(__file__) # API API_HEADERS = {'Content-Type': 'application/json'} def post_request(url: str, data: str, headers: Dict[str, str]) -> requests.Response: """Sends a POST request to the specified URL Args: url (str): The URL to send the POST request to. data (Dict[str, Any]): The data to send with the POST request. headers (Dict[str, str]): The headers to include with the POST request. Returns: requests.Response: The response object representing the server's response to the request """ response = requests.post(url, data=data, headers=headers, verify=False, timeout=timeout) return response -def retrieve_config(section: Optional[List[str]] = None) -> Optional[Dict[str, Any]]: +def retrieve_config(sections: List[list[str]]) -> Tuple[Dict[str, Any], Dict[str, Any]]: """Retrieves the configuration from the local server. Args: - section: List[str]: The section of the configuration to retrieve. - Default is None. + sections: List[list[str]]: The list of sections of the configuration + to retrieve, given as list of paths. Returns: - Optional[Dict[str, Any]]: The retrieved configuration as a - dictionary, or None if an error occurred. + Tuple[Dict[str, Any],Dict[str,Any]]: The tuple (mask, config) where: + - mask: The tree of paths of sections, as a dictionary. + - config: The subtree of masked config data, as a dictionary. """ - if section is None: - section = [] - conf = Config() - config = conf.get_config_dict(section, get_first_key=True) - if config: - return config - return None + mask = ConfigTree('') + for section in sections: + mask.set(section) + mask_dict = json.loads(mask.to_json()) + + config = Config() + config_tree = config.get_config_tree() + masked = mask_inclusive(config_tree, mask) + config_dict = json.loads(masked.to_json()) + return mask_dict, config_dict def set_remote_config( address: str, key: str, - commands: List[Dict[str, Any]]) -> Optional[Dict[str, Any]]: + op: str, + mask: Dict[str, Any], + config: Dict[str, Any]) -> Optional[Dict[str, Any]]: """Loads the VyOS configuration in JSON format to a remote host. Args: address (str): The address of the remote host. key (str): The key to use for loading the configuration. - commands (list): List of set/load commands for request, given as: - [{'op': str, 'path': list[str], 'section': dict}, - ...] + op (str): The operation to perform (set or load). + mask (dict): The dict of paths in sections. + config (dict): The dict of masked config data. Returns: Optional[Dict[str, Any]]: The response from the remote host as a dictionary, or None if a RequestException occurred. """ headers = {'Content-Type': 'application/json'} # Disable the InsecureRequestWarning urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) url = f'https://{address}/configure-section' data = json.dumps({ - 'commands': commands, + 'op': op, + 'mask': mask, + 'config': config, 'key': key }) try: config = post_request(url, data, headers) return config.json() except requests.exceptions.RequestException as e: print(f"An error occurred: {e}") logger.error(f"An error occurred: {e}") return None def is_section_revised(section: List[str]) -> bool: from vyos.config_mgmt import is_node_revised return is_node_revised(section) def config_sync(secondary_address: str, secondary_key: str, sections: List[list[str]], mode: str): """Retrieve a config section from primary router in JSON format and send it to secondary router """ if not any(map(is_section_revised, sections)): return logger.info( f"Config synchronization: Mode={mode}, Secondary={secondary_address}" ) # Sync sections ("nat", "firewall", etc) - commands = [] - for section in sections: - config_json = retrieve_config(section=section) - # Check if config path deesn't exist, for example "set nat" - # we set empty value for config_json data - # As we cannot send to the remote host section "nat None" config - if not config_json: - config_json = {} - logger.debug( - f"Retrieved config for section '{section}': {config_json}") - - d = {'op': mode, 'path': section, 'section': config_json} - commands.append(d) + mask_dict, config_dict = retrieve_config(sections) + logger.debug( + f"Retrieved config for sections '{sections}': {config_dict}") set_config = set_remote_config(address=secondary_address, key=secondary_key, - commands=commands) + op=mode, + mask=mask_dict, + config=config_dict) logger.debug(f"Set config for sections '{sections}': {set_config}") if __name__ == '__main__': # Read configuration from file if not os.path.exists(CONFIG_FILE): logger.error(f"Post-commit: No config file '{CONFIG_FILE}' exists") exit(0) with open(CONFIG_FILE, 'r') as f: config_data = f.read() config = json.loads(config_data) mode = config.get('mode') secondary_address = config.get('secondary', {}).get('address') secondary_address = bracketize_ipv6(secondary_address) secondary_key = config.get('secondary', {}).get('key') sections = config.get('section') timeout = int(config.get('secondary', {}).get('timeout')) if not all([ mode, secondary_address, secondary_key, sections ]): logger.error( "Missing required configuration data for config synchronization.") exit(0) # Generate list_sections of sections/subsections # [ # ['interfaces', 'pseudo-ethernet'], ['interfaces', 'virtual-ethernet'], ['nat'], ['nat66'] # ] list_sections = [] for section, subsections in sections.items(): if subsections: for subsection in subsections: list_sections.append([section, subsection]) else: list_sections.append([section]) config_sync(secondary_address, secondary_key, list_sections, mode) diff --git a/src/services/vyos-http-api-server b/src/services/vyos-http-api-server index 77870a84c..ecbf6fcf9 100755 --- a/src/services/vyos-http-api-server +++ b/src/services/vyos-http-api-server @@ -1,938 +1,958 @@ #!/usr/share/vyos-http-api-tools/bin/python3 # # 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 # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import sys import grp import copy import json import logging import signal import traceback import threading from time import sleep from typing import List, Union, Callable, Dict from fastapi import FastAPI, Depends, Request, Response, HTTPException from fastapi import BackgroundTasks from fastapi.responses import HTMLResponse from fastapi.exceptions import RequestValidationError from fastapi.routing import APIRoute from pydantic import BaseModel, StrictStr, validator from starlette.middleware.cors import CORSMiddleware from starlette.datastructures import FormData from starlette.formparsers import FormParser, MultiPartParser from multipart.multipart import parse_options_header from uvicorn import Config as UvicornConfig from uvicorn import Server as UvicornServer from ariadne.asgi import GraphQL from vyos.config import Config from vyos.configtree import ConfigTree from vyos.configdiff import get_config_diff from vyos.configsession import ConfigSession from vyos.configsession import ConfigSessionError from vyos.defaults import api_config_state import api.graphql.state CFG_GROUP = 'vyattacfg' debug = True logger = logging.getLogger(__name__) logs_handler = logging.StreamHandler() logger.addHandler(logs_handler) if debug: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) # Giant lock! lock = threading.Lock() def load_server_config(): with open(api_config_state) as f: config = json.load(f) return config def check_auth(key_list, key): key_id = None for k in key_list: if k['key'] == key: key_id = k['id'] return key_id def error(code, msg): resp = {"success": False, "error": msg, "data": None} resp = json.dumps(resp) return HTMLResponse(resp, status_code=code) def success(data): resp = {"success": True, "data": data, "error": None} resp = json.dumps(resp) return HTMLResponse(resp) # Pydantic models for validation # Pydantic will cast when possible, so use StrictStr # validators added as needed for additional constraints # schema_extra adds anotations to OpenAPI, to add examples class ApiModel(BaseModel): key: StrictStr class BasePathModel(BaseModel): op: StrictStr path: List[StrictStr] @validator("path") def check_non_empty(cls, path): if not len(path) > 0: raise ValueError('path must be non-empty') return path class BaseConfigureModel(BasePathModel): value: StrictStr = None class ConfigureModel(ApiModel, BaseConfigureModel): class Config: schema_extra = { "example": { "key": "id_key", "op": "set | delete | comment", "path": ['config', 'mode', 'path'], } } class ConfigureListModel(ApiModel): commands: List[BaseConfigureModel] class Config: schema_extra = { "example": { "key": "id_key", "commands": "list of commands", } } class BaseConfigSectionModel(BasePathModel): section: Dict class ConfigSectionModel(ApiModel, BaseConfigSectionModel): pass class ConfigSectionListModel(ApiModel): commands: List[BaseConfigSectionModel] +class BaseConfigSectionTreeModel(BaseModel): + op: StrictStr + mask: Dict + config: Dict + +class ConfigSectionTreeModel(ApiModel, BaseConfigSectionTreeModel): + pass + class RetrieveModel(ApiModel): op: StrictStr path: List[StrictStr] configFormat: StrictStr = None class Config: schema_extra = { "example": { "key": "id_key", "op": "returnValue | returnValues | exists | showConfig", "path": ['config', 'mode', 'path'], "configFormat": "json (default) | json_ast | raw", } } class ConfigFileModel(ApiModel): op: StrictStr file: StrictStr = None class Config: schema_extra = { "example": { "key": "id_key", "op": "save | load", "file": "filename", } } class ImageModel(ApiModel): op: StrictStr url: StrictStr = None name: StrictStr = None class Config: schema_extra = { "example": { "key": "id_key", "op": "add | delete", "url": "imagelocation", "name": "imagename", } } class ContainerImageModel(ApiModel): op: StrictStr name: StrictStr = None class Config: schema_extra = { "example": { "key": "id_key", "op": "add | delete | show", "name": "imagename", } } class GenerateModel(ApiModel): op: StrictStr path: List[StrictStr] class Config: schema_extra = { "example": { "key": "id_key", "op": "generate", "path": ["op", "mode", "path"], } } class ShowModel(ApiModel): op: StrictStr path: List[StrictStr] class Config: schema_extra = { "example": { "key": "id_key", "op": "show", "path": ["op", "mode", "path"], } } class RebootModel(ApiModel): op: StrictStr path: List[StrictStr] class Config: schema_extra = { "example": { "key": "id_key", "op": "reboot", "path": ["op", "mode", "path"], } } class ResetModel(ApiModel): op: StrictStr path: List[StrictStr] class Config: schema_extra = { "example": { "key": "id_key", "op": "reset", "path": ["op", "mode", "path"], } } class PoweroffModel(ApiModel): op: StrictStr path: List[StrictStr] class Config: schema_extra = { "example": { "key": "id_key", "op": "poweroff", "path": ["op", "mode", "path"], } } class Success(BaseModel): success: bool data: Union[str, bool, Dict] error: str class Error(BaseModel): success: bool = False data: Union[str, bool, Dict] error: str responses = { 200: {'model': Success}, 400: {'model': Error}, 422: {'model': Error, 'description': 'Validation Error'}, 500: {'model': Error} } def auth_required(data: ApiModel): key = data.key api_keys = app.state.vyos_keys key_id = check_auth(api_keys, key) if not key_id: raise HTTPException(status_code=401, detail="Valid API key is required") app.state.vyos_id = key_id # override Request and APIRoute classes in order to convert form request to json; # do all explicit validation here, for backwards compatability of error messages; # the explicit validation may be dropped, if desired, in favor of native # validation by FastAPI/Pydantic, as is used for application/json requests class MultipartRequest(Request): _form_err = () @property def form_err(self): return self._form_err @form_err.setter def form_err(self, val): if not self._form_err: self._form_err = val @property def orig_headers(self): self._orig_headers = super().headers return self._orig_headers @property def headers(self): self._headers = super().headers.mutablecopy() self._headers['content-type'] = 'application/json' return self._headers async def form(self) -> FormData: if self._form is None: assert ( parse_options_header is not None ), "The `python-multipart` library must be installed to use form parsing." content_type_header = self.orig_headers.get("Content-Type") content_type, options = parse_options_header(content_type_header) if content_type == b"multipart/form-data": multipart_parser = MultiPartParser(self.orig_headers, self.stream()) self._form = await multipart_parser.parse() elif content_type == b"application/x-www-form-urlencoded": form_parser = FormParser(self.orig_headers, self.stream()) self._form = await form_parser.parse() else: self._form = FormData() return self._form async def body(self) -> bytes: if not hasattr(self, "_body"): forms = {} merge = {} body = await super().body() self._body = body form_data = await self.form() if form_data: endpoint = self.url.path logger.debug("processing form data") for k, v in form_data.multi_items(): forms[k] = v if 'data' not in forms: self.form_err = (422, "Non-empty data field is required") return self._body else: try: tmp = json.loads(forms['data']) except json.JSONDecodeError as e: self.form_err = (400, f'Failed to parse JSON: {e}') return self._body if isinstance(tmp, list): merge['commands'] = tmp else: merge = tmp if 'commands' in merge: cmds = merge['commands'] else: cmds = copy.deepcopy(merge) cmds = [cmds] for c in cmds: if not isinstance(c, dict): self.form_err = (400, f"Malformed command '{c}': any command must be JSON of dict") return self._body if 'op' not in c: self.form_err = (400, f"Malformed command '{c}': missing 'op' field") if endpoint not in ('/config-file', '/container-image', - '/image'): + '/image', '/configure-section'): if 'path' not in c: self.form_err = (400, f"Malformed command '{c}': missing 'path' field") elif not isinstance(c['path'], list): self.form_err = (400, f"Malformed command '{c}': 'path' field must be a list") elif not all(isinstance(el, str) for el in c['path']): self.form_err = (400, f"Malformed command '{0}': 'path' field must be a list of strings") if endpoint in ('/configure'): if not c['path']: self.form_err = (400, f"Malformed command '{c}': 'path' list must be non-empty") if 'value' in c and not isinstance(c['value'], str): self.form_err = (400, f"Malformed command '{c}': 'value' field must be a string") if endpoint in ('/configure-section'): - if 'section' not in c: - self.form_err = (400, - f"Malformed command '{c}': missing 'section' field") - elif not isinstance(c['section'], dict): + if 'section' not in c and 'config' not in c: self.form_err = (400, - f"Malformed command '{c}': 'section' field must be JSON of dict") + f"Malformed command '{c}': missing 'section' or 'config' field") if 'key' not in forms and 'key' not in merge: self.form_err = (401, "Valid API key is required") if 'key' in forms and 'key' not in merge: merge['key'] = forms['key'] new_body = json.dumps(merge) new_body = new_body.encode() self._body = new_body return self._body class MultipartRoute(APIRoute): def get_route_handler(self) -> Callable: original_route_handler = super().get_route_handler() async def custom_route_handler(request: Request) -> Response: request = MultipartRequest(request.scope, request.receive) try: response: Response = await original_route_handler(request) except HTTPException as e: return error(e.status_code, e.detail) except Exception as e: form_err = request.form_err if form_err: return error(*form_err) raise e return response return custom_route_handler app = FastAPI(debug=True, title="VyOS API", version="0.1.0", responses={**responses}, dependencies=[Depends(auth_required)]) app.router.route_class = MultipartRoute @app.exception_handler(RequestValidationError) async def validation_exception_handler(request, exc): return error(400, str(exc.errors()[0])) self_ref_msg = "Requested HTTP API server configuration change; commit will be called in the background" def call_commit(s: ConfigSession): try: s.commit() except ConfigSessionError as e: s.discard() if app.state.vyos_debug: logger.warning(f"ConfigSessionError:\n {traceback.format_exc()}") else: logger.warning(f"ConfigSessionError: {e}") def _configure_op(data: Union[ConfigureModel, ConfigureListModel, - ConfigSectionModel, ConfigSectionListModel], + ConfigSectionModel, ConfigSectionListModel, + ConfigSectionTreeModel], request: Request, background_tasks: BackgroundTasks): session = app.state.vyos_session env = session.get_session_env() endpoint = request.url.path # Allow users to pass just one command if not isinstance(data, (ConfigureListModel, ConfigSectionListModel)): data = [data] else: data = data.commands # We don't want multiple people/apps to be able to commit at once, # or modify the shared session while someone else is doing the same, # so the lock is really global lock.acquire() config = Config(session_env=env) status = 200 msg = None error_msg = None try: for c in data: op = c.op - path = c.path + if not isinstance(c, BaseConfigSectionTreeModel): + path = c.path if isinstance(c, BaseConfigureModel): if c.value: value = c.value else: value = "" # For vyos.configsession calls that have no separate value arguments, # and for type checking too cfg_path = " ".join(path + [value]).strip() elif isinstance(c, BaseConfigSectionModel): section = c.section + elif isinstance(c, BaseConfigSectionTreeModel): + mask = c.mask + config = c.config + if isinstance(c, BaseConfigureModel): if op == 'set': session.set(path, value=value) elif op == 'delete': if app.state.vyos_strict and not config.exists(cfg_path): raise ConfigSessionError(f"Cannot delete [{cfg_path}]: path/value does not exist") session.delete(path, value=value) elif op == 'comment': session.comment(path, value=value) else: raise ConfigSessionError(f"'{op}' is not a valid operation") elif isinstance(c, BaseConfigSectionModel): if op == 'set': session.set_section(path, section) elif op == 'load': session.load_section(path, section) else: raise ConfigSessionError(f"'{op}' is not a valid operation") + + elif isinstance(c, BaseConfigSectionTreeModel): + if op == 'set': + session.set_section_tree(config) + elif op == 'load': + session.load_section_tree(mask, config) + else: + raise ConfigSessionError(f"'{op}' is not a valid operation") # end for config = Config(session_env=env) d = get_config_diff(config) if d.is_node_changed(['service', 'https']): background_tasks.add_task(call_commit, session) msg = self_ref_msg else: session.commit() logger.info(f"Configuration modified via HTTP API using key '{app.state.vyos_id}'") except ConfigSessionError as e: session.discard() status = 400 if app.state.vyos_debug: logger.critical(f"ConfigSessionError:\n {traceback.format_exc()}") error_msg = str(e) except Exception as e: session.discard() logger.critical(traceback.format_exc()) status = 500 # Don't give the details away to the outer world error_msg = "An internal error occured. Check the logs for details." finally: lock.release() if status != 200: return error(status, error_msg) return success(msg) @app.post('/configure') def configure_op(data: Union[ConfigureModel, ConfigureListModel], request: Request, background_tasks: BackgroundTasks): return _configure_op(data, request, background_tasks) @app.post('/configure-section') def configure_section_op(data: Union[ConfigSectionModel, - ConfigSectionListModel], + ConfigSectionListModel, + ConfigSectionTreeModel], request: Request, background_tasks: BackgroundTasks): return _configure_op(data, request, background_tasks) @app.post("/retrieve") async def retrieve_op(data: RetrieveModel): session = app.state.vyos_session env = session.get_session_env() config = Config(session_env=env) op = data.op path = " ".join(data.path) try: if op == 'returnValue': res = config.return_value(path) elif op == 'returnValues': res = config.return_values(path) elif op == 'exists': res = config.exists(path) elif op == 'showConfig': config_format = 'json' if data.configFormat: config_format = data.configFormat res = session.show_config(path=data.path) if config_format == 'json': config_tree = ConfigTree(res) res = json.loads(config_tree.to_json()) elif config_format == 'json_ast': config_tree = ConfigTree(res) res = json.loads(config_tree.to_json_ast()) elif config_format == 'raw': pass else: return error(400, f"'{config_format}' is not a valid config format") else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/config-file') def config_file_op(data: ConfigFileModel, background_tasks: BackgroundTasks): session = app.state.vyos_session env = session.get_session_env() op = data.op msg = None try: if op == 'save': if data.file: path = data.file else: path = '/config/config.boot' msg = session.save_config(path) elif op == 'load': if data.file: path = data.file else: return error(400, "Missing required field \"file\"") session.migrate_and_load_config(path) config = Config(session_env=env) d = get_config_diff(config) if d.is_node_changed(['service', 'https']): background_tasks.add_task(call_commit, session) msg = self_ref_msg else: session.commit() else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(msg) @app.post('/image') def image_op(data: ImageModel): session = app.state.vyos_session op = data.op try: if op == 'add': if data.url: url = data.url else: return error(400, "Missing required field \"url\"") res = session.install_image(url) elif op == 'delete': if data.name: name = data.name else: return error(400, "Missing required field \"name\"") res = session.remove_image(name) else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/container-image') def container_image_op(data: ContainerImageModel): session = app.state.vyos_session op = data.op try: if op == 'add': if data.name: name = data.name else: return error(400, "Missing required field \"name\"") res = session.add_container_image(name) elif op == 'delete': if data.name: name = data.name else: return error(400, "Missing required field \"name\"") res = session.delete_container_image(name) elif op == 'show': res = session.show_container_image() else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/generate') def generate_op(data: GenerateModel): session = app.state.vyos_session op = data.op path = data.path try: if op == 'generate': res = session.generate(path) else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/show') def show_op(data: ShowModel): session = app.state.vyos_session op = data.op path = data.path try: if op == 'show': res = session.show(path) else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/reboot') def reboot_op(data: RebootModel): session = app.state.vyos_session op = data.op path = data.path try: if op == 'reboot': res = session.reboot(path) else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/reset') def reset_op(data: ResetModel): session = app.state.vyos_session op = data.op path = data.path try: if op == 'reset': res = session.reset(path) else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) @app.post('/poweroff') def poweroff_op(data: PoweroffModel): session = app.state.vyos_session op = data.op path = data.path try: if op == 'poweroff': res = session.poweroff(path) else: return error(400, f"'{op}' is not a valid operation") except ConfigSessionError as e: return error(400, str(e)) except Exception as e: logger.critical(traceback.format_exc()) return error(500, "An internal error occured. Check the logs for details.") return success(res) ### # GraphQL integration ### def graphql_init(app: FastAPI = app): from api.graphql.libs.token_auth import get_user_context api.graphql.state.init() api.graphql.state.settings['app'] = app # import after initializaion of state from api.graphql.bindings import generate_schema schema = generate_schema() in_spec = app.state.vyos_introspection if app.state.vyos_origins: origins = app.state.vyos_origins app.add_route('/graphql', CORSMiddleware(GraphQL(schema, context_value=get_user_context, debug=True, introspection=in_spec), allow_origins=origins, allow_methods=("GET", "POST", "OPTIONS"), allow_headers=("Authorization",))) else: app.add_route('/graphql', GraphQL(schema, context_value=get_user_context, debug=True, introspection=in_spec)) ### # Modify uvicorn to allow reloading server within the configsession ### server = None shutdown = False class ApiServerConfig(UvicornConfig): pass class ApiServer(UvicornServer): def install_signal_handlers(self): pass def reload_handler(signum, frame): global server logger.debug('Reload signal received...') if server is not None: server.handle_exit(signum, frame) server = None logger.info('Server stopping for reload...') else: logger.warning('Reload called for non-running server...') def shutdown_handler(signum, frame): global shutdown logger.debug('Shutdown signal received...') server.handle_exit(signum, frame) logger.info('Server shutdown...') shutdown = True def flatten_keys(d: dict) -> list[dict]: keys_list = [] for el in list(d['keys'].get('id', {})): key = d['keys']['id'][el].get('key', '') if key: keys_list.append({'id': el, 'key': key}) return keys_list def initialization(session: ConfigSession, app: FastAPI = app): global server try: server_config = load_server_config() except Exception as e: logger.critical(f'Failed to load the HTTP API server config: {e}') sys.exit(1) app.state.vyos_session = session app.state.vyos_keys = [] if 'keys' in server_config: app.state.vyos_keys = flatten_keys(server_config) app.state.vyos_debug = bool('debug' in server_config) app.state.vyos_strict = bool('strict' in server_config) app.state.vyos_origins = server_config.get('cors', {}).get('allow_origin', []) if 'graphql' in server_config: app.state.vyos_graphql = True if isinstance(server_config['graphql'], dict): if 'introspection' in server_config['graphql']: app.state.vyos_introspection = True else: app.state.vyos_introspection = False # default values if not set explicitly app.state.vyos_auth_type = server_config['graphql']['authentication']['type'] app.state.vyos_token_exp = server_config['graphql']['authentication']['expiration'] app.state.vyos_secret_len = server_config['graphql']['authentication']['secret_length'] else: app.state.vyos_graphql = False if app.state.vyos_graphql: graphql_init(app) config = ApiServerConfig(app, uds="/run/api.sock", proxy_headers=True) server = ApiServer(config) def run_server(): try: server.run() except OSError as e: logger.critical(e) sys.exit(1) if __name__ == '__main__': # systemd's user and group options don't work, do it by hand here, # else no one else will be able to commit cfg_group = grp.getgrnam(CFG_GROUP) os.setgid(cfg_group.gr_gid) # Need to set file permissions to 775 too so that every vyattacfg group member # has write access to the running config os.umask(0o002) signal.signal(signal.SIGHUP, reload_handler) signal.signal(signal.SIGTERM, shutdown_handler) config_session = ConfigSession(os.getpid()) while True: logger.debug('Enter main loop...') if shutdown: break if server is None: initialization(config_session) server.run() sleep(1)