From 3891799d970f10a111f0a690537e1aa7e28c5deb Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Mon, 15 Jun 2020 10:14:34 -0500 Subject: config: T2568: add missing error checking --- python/vyos/config.py | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) (limited to 'python') diff --git a/python/vyos/config.py b/python/vyos/config.py index 54cb518c3..f94293e2d 100644 --- a/python/vyos/config.py +++ b/python/vyos/config.py @@ -100,23 +100,33 @@ class Config(object): # once the config system is initialized during boot; # before initialization, set to empty string if os.path.isfile('/tmp/vyos-config-status'): - running_config_text = self._run([self._cli_shell_api, '--show-active-only', '--show-show-defaults', '--show-ignore-edit', 'showConfig']) + try: + running_config_text = self._run([self._cli_shell_api, '--show-active-only', '--show-show-defaults', '--show-ignore-edit', 'showConfig']) + except VyOSError: + running_config_text = '' else: running_config_text = '' # Session config ("active") only exists in conf mode. # In op mode, we'll just use the same running config for both active and session configs. if self.in_session(): - session_config_text = self._run([self._cli_shell_api, '--show-working-only', '--show-show-defaults', '--show-ignore-edit', 'showConfig']) + try: + session_config_text = self._run([self._cli_shell_api, '--show-working-only', '--show-show-defaults', '--show-ignore-edit', 'showConfig']) + except VyOSError: + session_config_text = '' else: session_config_text = running_config_text - self._session_config = vyos.configtree.ConfigTree(session_config_text) if running_config_text: self._running_config = vyos.configtree.ConfigTree(running_config_text) else: self._running_config = None + if session_config_text: + self._session_config = vyos.configtree.ConfigTree(session_config_text) + else: + self._session_config = None + def _make_command(self, op, path): args = path.split() cmd = [self._cli_shell_api, op] + args @@ -193,6 +203,8 @@ class Config(object): This function cannot be used outside a configuration sessions. In operational mode scripts, use ``exists_effective``. """ + if not self._session_config: + return False if self._session_config.exists(self._make_path(path)): return True else: @@ -362,9 +374,12 @@ class Config(object): This function cannot be used outside a configuration session. In operational mode scripts, use ``return_effective_value``. """ - try: - value = self._session_config.return_value(self._make_path(path)) - except vyos.configtree.ConfigTreeError: + if self._session_config: + try: + value = self._session_config.return_value(self._make_path(path)) + except vyos.configtree.ConfigTreeError: + value = None + else: value = None if not value: @@ -387,9 +402,12 @@ class Config(object): This function cannot be used outside a configuration session. In operational mode scripts, use ``return_effective_values``. """ - try: - values = self._session_config.return_values(self._make_path(path)) - except vyos.configtree.ConfigTreeError: + if self._session_config: + try: + values = self._session_config.return_values(self._make_path(path)) + except vyos.configtree.ConfigTreeError: + values = [] + else: values = [] if not values: @@ -408,9 +426,12 @@ class Config(object): string list: child node names """ - try: - nodes = self._session_config.list_nodes(self._make_path(path)) - except vyos.configtree.ConfigTreeError: + if self._session_config: + try: + nodes = self._session_config.list_nodes(self._make_path(path)) + except vyos.configtree.ConfigTreeError: + nodes = [] + else: nodes = [] if not nodes: -- cgit v1.2.3