Skip to content

Commit

Permalink
failing test for banner stripping trailing newlines during parsing (#569
Browse files Browse the repository at this point in the history
)

* failing test for banner stripping trailing newlines during parsing

* possible fix to rstrip of banners

* fix all banner test

---------

Co-authored-by: Jeff Kala <[email protected]>
  • Loading branch information
jeffkala and Jeff Kala authored Sep 17, 2024
1 parent 33b26a4 commit b33408e
Show file tree
Hide file tree
Showing 18 changed files with 75 additions and 50 deletions.
10 changes: 5 additions & 5 deletions netutils/config/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _build_banner(self, config_line: str) -> t.Optional[str]:
line = "\n".join(banner_config)
if line.endswith("^C"):
banner, end, _ = line.rpartition("^C")
line = banner.rstrip() + end
line = banner + end
self._update_config_lines(line)
self._current_parents = self._current_parents[:-1]
try:
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def _build_banner(self, config_line: str) -> t.Optional[str]:
line = "\n".join(banner_config)
if line.endswith(self.delimiter):
banner, end, _ = line.rpartition(self.delimiter)
line = banner.rstrip() + end
line = banner + end
self._update_config_lines(line)
self._current_parents = self._current_parents[:-1]
try:
Expand Down Expand Up @@ -1481,7 +1481,7 @@ def _build_banner(self, config_line: str) -> t.Optional[str]:
line = "\n".join(banner_config)
if line.endswith("^C"):
banner, end, _ = line.rpartition("^C")
line = banner.rstrip() + end
line = banner + end
self._update_config_lines(line.strip())
self._current_parents = self._current_parents[:-1]
try:
Expand Down Expand Up @@ -1591,7 +1591,7 @@ def _build_banner(self, config_line: str) -> t.Optional[str]:
line = "\n".join(banner_config)
if line.endswith(self.banner_end):
banner, end, _ = line.rpartition(self.banner_end)
line = banner.rstrip() + end
line = banner + end
self._update_config_lines(line)
self._current_parents = self._current_parents[:-1]
try:
Expand Down Expand Up @@ -1671,7 +1671,7 @@ def _build_banner(self, config_line: str) -> t.Optional[str]:
line = "\n".join(banner_config)
if line.endswith(self.delimiter):
banner, end, _ = line.rpartition(self.delimiter)
line = banner.rstrip() + end
line = banner + end
self._update_config_lines(line)
self._current_parents = self._current_parents[:-1]
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"unordered_compliant": true
},
"banner": {
"actual": "banner login ^C\n******************\n TEST BANNER\n******************^C",
"actual": "banner login ^C\n******************\n TEST BANNER\n******************\n^C",
"cannot_parse": true,
"compliant": true,
"extra": "",
"intended": "banner login ^C\n******************\n TEST BANNER\n******************^C",
"intended": "banner login ^C\n******************\n TEST BANNER\n******************\n^C",
"missing": "",
"ordered_compliant": true,
"unordered_compliant": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"banner": {
"actual": "banner exec ~\n****************\nNetwork to Code\nNew York City, New York\n****************~\nbanner motd ~\n*********************!!!IMPORTANT NOTICE!!!************************\n* This is a restricted system. All connections are logged. *\n* If you are not authorized to connect to this system, log *\n* off now. *\n* *\n* Violators will be prosecuted to the full extent of the law. *\n*******************************************************************\n* *\n**********************!!!AVIS IMPORTANT!!!*************************\n* L'acces au present systeme est limite et tous ses acces sont *\n* actuellement utilises. Si vous n'etes pas autorise a vous y *\n* raccorder, veuillez quitter ce systeme immediatement. *\n* *\n* Tout contrevenant sera poursuivi en vertu des mesures prevues *\n* par la loi. *\n*******************************************************************~",
"actual": "banner exec ~\n****************\nNetwork to Code\nNew York City, New York\n****************\n~\nbanner motd ~\n*********************!!!IMPORTANT NOTICE!!!************************\n* This is a restricted system. All connections are logged. *\n* If you are not authorized to connect to this system, log *\n* off now. *\n* *\n* Violators will be prosecuted to the full extent of the law. *\n*******************************************************************\n* *\n**********************!!!AVIS IMPORTANT!!!*************************\n* L'acces au present systeme est limite et tous ses acces sont *\n* actuellement utilises. Si vous n'etes pas autorise a vous y *\n* raccorder, veuillez quitter ce systeme immediatement. *\n* *\n* Tout contrevenant sera poursuivi en vertu des mesures prevues *\n* par la loi. *\n*******************************************************************\n~",
"cannot_parse": true,
"compliant": true,
"extra": "",
"intended": "banner exec ~\n****************\nNetwork to Code\nNew York City, New York\n****************~\nbanner motd ~\n*********************!!!IMPORTANT NOTICE!!!************************\n* This is a restricted system. All connections are logged. *\n* If you are not authorized to connect to this system, log *\n* off now. *\n* *\n* Violators will be prosecuted to the full extent of the law. *\n*******************************************************************\n* *\n**********************!!!AVIS IMPORTANT!!!*************************\n* L'acces au present systeme est limite et tous ses acces sont *\n* actuellement utilises. Si vous n'etes pas autorise a vous y *\n* raccorder, veuillez quitter ce systeme immediatement. *\n* *\n* Tout contrevenant sera poursuivi en vertu des mesures prevues *\n* par la loi. *\n*******************************************************************~",
"intended": "banner exec ~\n****************\nNetwork to Code\nNew York City, New York\n****************\n~\nbanner motd ~\n*********************!!!IMPORTANT NOTICE!!!************************\n* This is a restricted system. All connections are logged. *\n* If you are not authorized to connect to this system, log *\n* off now. *\n* *\n* Violators will be prosecuted to the full extent of the law. *\n*******************************************************************\n* *\n**********************!!!AVIS IMPORTANT!!!*************************\n* L'acces au present systeme est limite et tous ses acces sont *\n* actuellement utilises. Si vous n'etes pas autorise a vous y *\n* raccorder, veuillez quitter ce systeme immediatement. *\n* *\n* Tout contrevenant sera poursuivi en vertu des mesures prevues *\n* par la loi. *\n*******************************************************************\n~",
"missing": "",
"ordered_compliant": true,
"unordered_compliant": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"unordered_compliant": true
},
"banner": {
"actual": "banner motd ^C\nThis is a test,\nbanner message.^C",
"actual": "banner motd ^C\nThis is a test,\nbanner message.\n^C",
"cannot_parse": true,
"compliant": true,
"extra": "",
"intended": "banner motd ^C\nThis is a test,\nbanner message.^C",
"intended": "banner motd ^C\nThis is a test,\nbanner message.\n^C",
"missing": "",
"ordered_compliant": true,
"unordered_compliant": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"remaining_cfg": "hostname dfw1-core-01\nbanner exec ~\n****************\nNetwork to Code\nNew York City, New York\n****************\n~",
"remaining_cfg": "hostname dfw1-core-01",
"section_not_found": [
"bgp"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
data = {
"compliant": False,
"missing": "banner exec ^C\n=========\nintended config exec banner\n-========^C",
"extra": "banner exec ^C\n=========\nactual config exec banner\n-========^C",
"missing": "banner exec ^C\n=========\nintended config exec banner\n-========\n^C",
"extra": "banner exec ^C\n=========\nactual config exec banner\n-========\n^C",
"cannot_parse": True,
"unordered_compliant": False,
"ordered_compliant": False,
"actual": "banner exec ^C\n=========\nactual config exec banner\n-========^C",
"intended": "banner exec ^C\n=========\nintended config exec banner\n-========^C",
"actual": "banner exec ^C\n=========\nactual config exec banner\n-========\n^C",
"intended": "banner exec ^C\n=========\nintended config exec banner\n-========\n^C",
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
data = {
"compliant": False,
"missing": "banner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||^C",
"extra": "banner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||^C",
"missing": "banner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||\n^C",
"extra": "banner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||\n^C",
"cannot_parse": True,
"unordered_compliant": False,
"ordered_compliant": False,
"actual": "banner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||^C",
"intended": "banner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||^C",
"actual": "banner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||\n^C",
"intended": "banner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||\n^C",
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
data = {
"compliant": False,
"missing": "banner exec ^C\n=========\nintended config exec banner\n-========^C\nbanner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||^C",
"extra": "banner exec ^C\n=========\nactual config exec banner\n-========^C\nbanner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||^C",
"missing": "banner exec ^C\n=========\nintended config exec banner\n-========\n^C\nbanner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||\n^C",
"extra": "banner exec ^C\n=========\nactual config exec banner\n-========\n^C\nbanner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||\n^C",
"cannot_parse": True,
"unordered_compliant": False,
"ordered_compliant": False,
"actual": "banner exec ^C\n=========\nactual config exec banner\n-========^C\nbanner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||^C",
"intended": "banner exec ^C\n=========\nintended config exec banner\n-========^C\nbanner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||^C",
"actual": "banner exec ^C\n=========\nactual config exec banner\n-========\n^C\nbanner motd ^C\n======\nactual config motd banner\n======\n || ($hostname) ||\n^C",
"intended": "banner exec ^C\n=========\nintended config exec banner\n-========\n^C\nbanner motd ^C\n======\nintended config motd banner\n======\n || ($hostname) ||\n^C",
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"cannot_parse": True,
"compliant": False,
"extra": "banner motd ^C^C",
"intended": "banner motd ^C\n" "actual banner example^C",
"missing": "banner motd ^C\n" "actual banner example^C",
"intended": "banner motd ^C\n" "actual banner example\n^C",
"missing": "banner motd ^C\n" "actual banner example\n^C",
"ordered_compliant": False,
"unordered_compliant": False,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from netutils.config.parser import ConfigLine

data = [
ConfigLine(config_line="hostname banner_with_newline", parents=()),
ConfigLine(config_line="banner login ^C", parents=()),
ConfigLine(
config_line="****************************************************\nWARNING TO UNAUTHORIZED USERS:\nThis system is for use by authorized users only.\n****************************************************\n^C",
parents=("banner login ^C",),
),
ConfigLine(config_line="line vty 0 4", parents=()),
ConfigLine(config_line=" transport ssh", parents=("line vty 0 4",)),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
hostname banner_with_newline
!
banner login ^C
****************************************************
WARNING TO UNAUTHORIZED USERS:
This system is for use by authorized users only.
****************************************************

^C
!
line vty 0 4
transport ssh
!
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
data = [
ConfigLine(config_line="hostname dual-banner", parents=()),
ConfigLine(config_line="banner exec ^C", parents=()),
ConfigLine(config_line="=========\nintended config exec banner\n-========^C", parents=("banner exec ^C",)),
ConfigLine(config_line="=========\nintended config exec banner\n-========\n^C", parents=("banner exec ^C",)),
ConfigLine(config_line="banner motd ^C", parents=()),
ConfigLine(
config_line="======\nintended config motd banner\n======\n || ($hostname) ||^C", parents=("banner motd ^C",)
config_line="======\nintended config motd banner\n======\n || ($hostname) ||\n^C", parents=("banner motd ^C",)
),
ConfigLine(config_line=None, parents=()),
]
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
ConfigLine(config_line="aaa authentication login default group AUTH local", parents=()),
ConfigLine(config_line="banner exec ~", parents=()),
ConfigLine(
config_line="****************\nNetwork to Code\nNew York City, New York\n****************~",
config_line="****************\nNetwork to Code\nNew York City, New York\n****************\n~",
parents=("banner exec ~",),
),
ConfigLine(config_line="banner motd ~", parents=()),
ConfigLine(
config_line="*********************!!!IMPORTANT NOTICE!!!************************\n* This is a restricted system. All connections are logged. *\n* If you are not authorized to connect to this system, log *\n* off now. *\n* *\n* Violators will be prosecuted to the full extent of the law. *\n*******************************************************************\n* *\n**********************!!!AVIS IMPORTANT!!!*************************\n* L'acces au present systeme est limite et tous ses acces sont *\n* actuellement utilises. Si vous n'etes pas autorise a vous y *\n* raccorder, veuillez quitter ce systeme immediatement. *\n* *\n* Tout contrevenant sera poursuivi en vertu des mesures prevues *\n* par la loi. *\n*******************************************************************~",
config_line="*********************!!!IMPORTANT NOTICE!!!************************\n* This is a restricted system. All connections are logged. *\n* If you are not authorized to connect to this system, log *\n* off now. *\n* *\n* Violators will be prosecuted to the full extent of the law. *\n*******************************************************************\n* *\n**********************!!!AVIS IMPORTANT!!!*************************\n* L'acces au present systeme est limite et tous ses acces sont *\n* actuellement utilises. Si vous n'etes pas autorise a vous y *\n* raccorder, veuillez quitter ce systeme immediatement. *\n* *\n* Tout contrevenant sera poursuivi en vertu des mesures prevues *\n* par la loi. *\n*******************************************************************\n~",
parents=("banner motd ~",),
),
ConfigLine(config_line="vrf internet", parents=()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ConfigLine(config_line="feature vpc", parents=()),
ConfigLine(config_line="feature lldp", parents=()),
ConfigLine(config_line="banner motd ^C", parents=()),
ConfigLine(config_line="This is a test,\nbanner message.^C", parents=("banner motd ^C",)),
ConfigLine(config_line="This is a test,\nbanner message.\n^C", parents=("banner motd ^C",)),
ConfigLine(config_line="logging level aaa 4", parents=()),
ConfigLine(config_line="logging level acllog 4", parents=()),
ConfigLine(config_line="logging level aclmgr 4", parents=()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
parents=(),
),
ConfigLine(
config_line="************************************************************************\n* firewall1.example.com * [PROD VM500 firewalls]\n************************************************************************\n* WARNING *\n* Unauthorized access to this device or devices attached to *\n* or accessible from this network is strictly prohibited. *\n* Possession of passwords or devices enabling access to this *\n* device or devices does not constitute authorization. Unauthorized *\n* access will be prosecuted to the fullest extent of the law. *\n* *\n************************************************************************^C",
config_line="************************************************************************\n* firewall1.example.com * [PROD VM500 firewalls]\n************************************************************************\n* WARNING *\n* Unauthorized access to this device or devices attached to *\n* or accessible from this network is strictly prohibited. *\n* Possession of passwords or devices enabling access to this *\n* device or devices does not constitute authorization. Unauthorized *\n* access will be prosecuted to the fullest extent of the law. *\n* *\n************************************************************************\n\n^C",
parents=('set deviceconfig system login-banner "',),
),
ConfigLine(
Expand Down
Loading

0 comments on commit b33408e

Please sign in to comment.