Skip to content

ConfigParser crashes with IndexError when config starts with a leading space #656

@julmanglano

Description

@julmanglano

Environment

  • Python version: 3.12
  • netutils version: 1.30

Expected Behavior

If the configuration starts with a leading space I 'd expect to receive a more specific indication about the error instead of an IndexError: list index out of range.
It seems that while there is logic in the library to handle this type of error (_remove_parents) not all parser classes are invoking that handling properly

Observed Behavior

When using parsers from this library, if the configuration string starts with a leading space or newline, the parser fails with the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 1321, in __init__
    super(IOSXRConfigParser, self).__init__(config)
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 536, in __init__
    super(CiscoConfigParser, self).__init__(config)
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 65, in __init__
    super(BaseSpaceConfigParser, self).__init__(config)
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 32, in __init__
    self.build_config_relationship()
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 1394, in build_config_relationship
    previous_config = self.config_lines[-1]
                      ~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

Steps to Reproduce

  1. Create a sample config starting where the first line that is not a comment has a leading space

sample.cfg

! Command: show running-config
! device: mal01-leaf-01 (cEOSLab, EOS-4.33.1F-39879738.4331F (engineering build))
 24.1.4
!
no aaa root
!
username admin privilege 15 role network-admin secret sha512 <redacted>
!
management api http-commands
   no shutdown
!
no service interface inactive port-id allocation disabled
!

  1. On the Python shell
>>> config= open("sample.cfg","r").read()
>>> from netutils.config.parser import EOSConfigParser
>>> parsed = EOSConfigParser(config)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 65, in __init__
    super(BaseSpaceConfigParser, self).__init__(config)
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 32, in __init__
    self.build_config_relationship()
  File "/home/julmanglano/repos/netutils/netutils/config/parser.py", line 320, in build_config_relationship
    previous_config = self.config_lines[-1]
                      ~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions