Skip to content

Commit d55e1d8

Browse files
maddychanCapirca Team
authored andcommitted
limit number of terms with counters in junipersrx
PiperOrigin-RevId: 879703357
1 parent f5d79c1 commit d55e1d8

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

capirca/lib/junipersrx.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class ConflictingTargetOptionsError(Error):
7070
class ConflictingApplicationSetsError(Error):
7171
pass
7272

73+
class SRXTooManyCountersError(Error):
74+
pass
75+
7376

7477
class IndentList(list):
7578

@@ -363,6 +366,7 @@ def _TranslatePolicy(self, pol, exp_info):
363366
MixedAddrBookTypesError: Global and Zone address books in the same policy
364367
ConflictingApplicationSetsError: When two duplicate named terms have
365368
conflicting application entries
369+
SRXTooManyCountersError: More than 256 terms with counters found
366370
"""
367371
current_date = datetime.datetime.utcnow().date()
368372
exp_info_date = current_date + datetime.timedelta(weeks=exp_info)
@@ -407,6 +411,15 @@ def _TranslatePolicy(self, pol, exp_info):
407411
'destination-zone which can only be '
408412
'used with global policy' % term.name)
409413

414+
num_counters = 0
415+
for term in terms:
416+
if term.counter:
417+
num_counters += 1
418+
if num_counters > 256:
419+
raise SRXTooManyCountersError(
420+
f'The policy has {num_counters} terms with counters, limit is 256.'
421+
)
422+
410423
# variables used to collect target-options and set defaults
411424
filter_type = ''
412425

tests/lib/junipersrx_test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,14 @@
553553
action:: accept
554554
}
555555
"""
556+
COUNTER_GOOD_TERM = """
557+
term counter-term-{counter_num} {{
558+
platform:: srx juniper
559+
protocol:: tcp
560+
counter:: count-name
561+
action:: accept
562+
}}
563+
"""
556564

557565
SUPPORTED_TOKENS = frozenset({
558566
'action',
@@ -1966,6 +1974,14 @@ def testEmptyApplications(self):
19661974
pattern = re.compile(r'delete: applications;')
19671975
self.assertTrue(pattern.search(str(''.join(output))), ''.join(output))
19681976

1977+
def testTooManyCounters(self):
1978+
policy_text = GOOD_HEADER
1979+
for i in range(270):
1980+
policy_text += COUNTER_GOOD_TERM.format(counter_num=i)
1981+
pol = policy.ParsePolicy(policy_text, self.naming)
1982+
self.assertRaises(
1983+
junipersrx.SRXTooManyCountersError, junipersrx.JuniperSRX, pol, EXP_INFO
1984+
)
19691985

19701986
if __name__ == '__main__':
19711987
absltest.main()

0 commit comments

Comments
 (0)