From 90ec8f4eacb53be74276b476bfb3ef51da42e72e Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 18 Sep 2019 11:38:28 -0500 Subject: T1424: Check for http error or redirect, when loading remote files. (ported from vyatta-cfg f051e369) --- python/vyos/remote.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'python/vyos') diff --git a/python/vyos/remote.py b/python/vyos/remote.py index 49936ec08..f8a21f068 100644 --- a/python/vyos/remote.py +++ b/python/vyos/remote.py @@ -121,16 +121,34 @@ def get_remote_config(remote_file): if request['protocol'] in ('scp', 'sftp'): check_and_add_host_key(request['host']) + redirect_opt = '' + + if request['protocol'] in ('http', 'https'): + redirect_opt = '-L' + # Try header first, and look for 'OK' or 'Moved' codes: + curl_cmd = 'curl {0} -q -I {1}'.format(redirect_opt, remote_file) + try: + curl_output = subprocess.check_output(curl_cmd, shell=True, + universal_newlines=True) + except subprocess.CalledProcessError: + sys.exit(1) + + return_vals = re.findall(r'^HTTP\/\d+\.?\d\s+(\d+)\s+(.*)$', + curl_output, re.MULTILINE) + for val in return_vals: + if int(val[0]) not in [200, 301, 302]: + print('HTTP error: {0} {1}'.format(*val)) + sys.exit(1) + if request['user'] and not request['passwd']: curl_cmd = 'curl -# -u {0} {1}'.format(request['user'], remote_file) else: - curl_cmd = 'curl -# {0}'.format(remote_file) + curl_cmd = 'curl {0} -# {1}'.format(redirect_opt, remote_file) - config_file = None try: config_file = subprocess.check_output(curl_cmd, shell=True, universal_newlines=True) - except subprocess.CalledProcessError as err: - print("Called process error: {}.".format(err)) + except subprocess.CalledProcessError: + config_file = None return config_file -- cgit v1.2.3 From d449e5bf41b568d998f2a2d2cfcdac4c3df4eba5 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 6 Oct 2019 15:18:31 +0200 Subject: Python/ifconfig: T1557: add return in front of self._cmd() calls --- python/vyos/ifconfig.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'python/vyos') diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py index a77cde5e7..d4b1a73c1 100644 --- a/python/vyos/ifconfig.py +++ b/python/vyos/ifconfig.py @@ -142,7 +142,7 @@ class Interface: # after interface removal no other commands should be allowed # to be called and instead should raise an Exception: cmd = 'ip link del dev {}'.format(self._ifname) - self._cmd(cmd) + return self._cmd(cmd) def get_mtu(self): """ @@ -205,7 +205,7 @@ class Interface: # Assemble command executed on system. Unfortunately there is no way # of altering the MAC address via sysfs cmd = 'ip link set dev {} address {}'.format(self._ifname, mac) - self._cmd(cmd) + return self._cmd(cmd) def set_arp_cache_tmo(self, tmo): @@ -293,7 +293,7 @@ class Interface: # Assemble command executed on system. Unfortunately there is no way # to up/down an interface via sysfs cmd = 'ip link set dev {} {}'.format(self._ifname, state) - self._cmd(cmd) + return self._cmd(cmd) def set_proxy_arp(self, enable): """ @@ -402,7 +402,7 @@ class Interface: else: if not is_intf_addr_assigned(self._ifname, addr): cmd = 'ip addr add "{}" dev "{}"'.format(addr, self._ifname) - self._cmd(cmd) + return self._cmd(cmd) def del_addr(self, addr): """ @@ -433,7 +433,7 @@ class Interface: else: if is_intf_addr_assigned(self._ifname, addr): cmd = 'ip addr del "{}" dev "{}"'.format(addr, self._ifname) - self._cmd(cmd) + return self._cmd(cmd) # replace dhcpv4/v6 with systemd.networkd? def _set_dhcp(self): @@ -470,7 +470,7 @@ class Interface: # now pass arguments to dhclient binary cmd += ' -4 -nw -cf {} -pf {} -lf {} {}'.format( self._dhcp_cfg_file, self._dhcp_pid_file, self._dhcp_lease_file, self._ifname) - self._cmd(cmd) + return self._cmd(cmd) def _del_dhcp(self): @@ -559,7 +559,7 @@ class Interface: # now pass arguments to dhclient binary cmd += ' -6 -nw -cf {} -pf {} -lf {} {}'.format( self._dhcpv6_cfg_file, self._dhcpv6_pid_file, self._dhcpv6_lease_file, self._ifname) - self._cmd(cmd) + return self._cmd(cmd) def _del_dhcpv6(self): @@ -582,8 +582,7 @@ class Interface: return None # stop dhclient - cmd = 'start-stop-daemon --stop --quiet --pidfile {}'.format( - self._dhcpv6_pid_file) + cmd = 'start-stop-daemon --stop --quiet --pidfile {}'.format(self._dhcpv6_pid_file) self._cmd(cmd) # accept router announcements on this interface @@ -802,7 +801,7 @@ class BridgeIf(Interface): >>> BridgeIf('br0').add_port('eth1') """ cmd = 'ip link set dev {} master {}'.format(interface, self._ifname) - self._cmd(cmd) + return self._cmd(cmd) def del_port(self, interface): """ @@ -813,7 +812,7 @@ class BridgeIf(Interface): >>> BridgeIf('br0').del_port('eth1') """ cmd = 'ip link set dev {} nomaster'.format(interface) - self._cmd(cmd) + return self._cmd(cmd) class VLANIf(Interface): """ @@ -972,7 +971,7 @@ class EthernetIf(VLANIf): self._ifname, enable) try: # An exception will be thrown if the settings are not changed - self._cmd(cmd) + return self._cmd(cmd) except CalledProcessError: pass @@ -1376,7 +1375,7 @@ class WireGuardIf(Interface): """ cmd = "wg set {0} peer {1} remove".format( self._ifname, str(peerkey)) - self._cmd(cmd) + return self._cmd(cmd) class VXLANIf(Interface, ): -- cgit v1.2.3 From 1257d7851866d42287018b38dd871f279b87286a Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 6 Oct 2019 16:16:50 +0200 Subject: Python/ifconfig: T1712: wait when changing interface state With some interfaces, for example bond vif, it take some time for the state change to really happen. Because of this later code, like starting DHCP client, might not work as expected as get_state() reports the old (real) state. Now when changing state of an interface we are (busy-)waiting up to 12.5 seconds before we inform the user that the interface could not be brought up. This should be more then enough time for any interface to start except when there is really no cable attached. --- python/vyos/ifconfig.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'python/vyos') diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py index d4b1a73c1..4ac605b54 100644 --- a/python/vyos/ifconfig.py +++ b/python/vyos/ifconfig.py @@ -293,7 +293,21 @@ class Interface: # Assemble command executed on system. Unfortunately there is no way # to up/down an interface via sysfs cmd = 'ip link set dev {} {}'.format(self._ifname, state) - return self._cmd(cmd) + tmp = self._cmd(cmd) + + # better safe then sorry - wait until the interface is really up + # but only for a given period of time to avoid potential deadlocks! + cnt = 0 + while self.get_state() != state: + cnt += 1 + if cnt == 50: + print('Interface {} could not be brought up in time ...'.format(self._ifname)) + break + + # sleep 250ms + sleep(0.250) + + return tmp def set_proxy_arp(self, enable): """ -- cgit v1.2.3