Skip to content

Commit

Permalink
Clean up comments when rendering nftables ACLs in Capirca
Browse files Browse the repository at this point in the history
- Newlines replaced with spaces instead of just removed
- Change double quotes to single quotes within comments, rather than escape doubles inside doubles
- Sanity check for maximum length

PiperOrigin-RevId: 618279002
  • Loading branch information
joeeip authored and Capirca Team committed Mar 22, 2024
1 parent 6d0dd11 commit 54b0189
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 5 additions & 3 deletions capirca/lib/aclgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,13 +610,15 @@ def TruncateWords(input_text, char_limit):
Returns:
truncated single string element within double quotes.
"""
CHAR_LIMIT = 126
hard_limit = 126
if char_limit > hard_limit:
char_limit = hard_limit

if isinstance(input_text, list):
# handle multiple comments. Remove newline.
# Handle multiple comments. Newline to space and double to single quotes.
sanitized_list = []
for i in input_text:
sanitized_list.append(i.replace('\n', ''))
sanitized_list.append(i.replace('\n', ' ').replace('\"', '\''))
comment = ' '.join(sanitized_list)
else:
comment = input_text
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/aclgenerator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def setUp(self):
'There is something very long about this comment that',
'will require it to be truncated in order for nftables',
'binary to be able to load the rulesets.'], '"There is something very long about this comment that will require it to be truncated in order for nftables binary to be able t"'),
(['some comment description', 'second comment item \nNewline'], '"some comment description second comment item Newline"'), ('a string comment', '"a string comment"'))
(['some "comment" description', 'second \'comment\' item\nNewline'], '"some \'comment\' description second \'comment\' item Newline"'), ('a string comment', '"a string comment"'))
def testTruncateWords(self, input_data, expected_output):
result = aclgenerator.TruncateWords(
input_data, 126)
Expand Down

0 comments on commit 54b0189

Please sign in to comment.