Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

failing test for banner stripping trailing newlines during parsing #569

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1303,7 +1303,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 @@ -1474,7 +1474,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 @@ -1584,7 +1584,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 @@ -1664,7 +1664,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
Loading