-
Notifications
You must be signed in to change notification settings - Fork 2
Next Release #18
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
Open
grizz
wants to merge
12
commits into
main
Choose a base branch
from
prep-release
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Next Release #18
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d522109
poetry lock
grizz bf550ba
add filter tests
grizz bbade67
format
grizz 6830a01
poetry lock
grizz 26c8147
poc for render_template
grizz 580a3c6
poetry lock
grizz 98fc643
add address_to_mask and address_to_wildcard
mkomon 6090508
add line transformation filters
mkomon abbf1b3
move filters to single file, fix imports
mkomon 9b48690
add ip_to_ipv4 filter
mkomon b31a899
remove py3.7, poetry lock
grizz 48e5a7f
Merge branch 'main' into prep-release
grizz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import ipaddress | ||
import re | ||
from ipaddress import IPv4Address | ||
from jinja2 import pass_context | ||
|
||
|
||
r_ip_nn = re.compile("[\d.]{7,15}/\d{1,2}") | ||
|
||
__all__ = [ | ||
"make_variable_name", | ||
"render_template", | ||
"ip_address", | ||
"ip_interface", | ||
"ip_version", | ||
] | ||
|
||
|
||
@pass_context | ||
def render_template(context, value): | ||
""" | ||
Renders a template from the value. | ||
|
||
Does not get local variables. | ||
""" | ||
template = context.eval_ctx.environment.from_string(value) | ||
# print(f"parent {context.parent}") | ||
# print(f"ENV {context.get_all()['self_id']}") | ||
# print(f"self-id {context.get('self_id')}") | ||
result = template.render(**context) | ||
|
||
# not sure we need this | ||
# if context.eval_ctx.autoescape: | ||
# result = Markup(result) | ||
Comment on lines
+32
to
+33
Check noticeCode scanning / CodeQL Commented-out code
This comment appears to contain commented-out code.
|
||
|
||
return result | ||
|
||
|
||
def make_variable_name(value): | ||
""" | ||
Makes passed value into a variable name. | ||
""" | ||
value = str(value).translate(str.maketrans(" .:", "___")) | ||
return value | ||
|
||
|
||
def ip_address(value): | ||
""" | ||
Returns address of passed IP interface. | ||
""" | ||
return ipaddress.ip_interface(value).ip | ||
|
||
|
||
# XXX use this | ||
def ip_interface(value): | ||
""" | ||
Returns ip_interface of passed value. | ||
""" | ||
return ipaddress.ip_interface(value) | ||
|
||
|
||
# XXX when was interface added? | ||
def ip_version(value): | ||
""" | ||
Returns version of passed IP address. | ||
""" | ||
return ipaddress.ip_interface(value).version | ||
|
||
|
||
def address_to_mask(addr): | ||
""" | ||
Transform A.B.C.D/nn into A.B.C.D M.M.M.M subnet mask format. | ||
""" | ||
ipaddr4 = ipaddress.ip_interface(addr) | ||
return f"{ipaddr4.ip} {ipaddr4.netmask}" | ||
|
||
|
||
def address_to_wildcard(addr): | ||
""" | ||
Transform A.B.C.D/nn into A.B.C.D W.W.W.W subnet wildcard format. | ||
|
||
credit for the mask shenanigans to George Shuklin | ||
https://medium.com/opsops/wildcard-masks-operations-in-python-16acf1c35683 | ||
""" | ||
ipnet4 = ipaddress.ip_network(addr) | ||
wildcard = str(IPv4Address(int(IPv4Address(ipnet4.netmask))^(2**32-1))) | ||
return f"{ipnet4.network_address} {wildcard}" | ||
|
||
|
||
def line_to_mask(line): | ||
""" | ||
Search for any IPv4/nn tokens in the `line` argument and transform them into mask format. | ||
E.g. `permit ip 10.0.0.0/8` any -> `permit ip 10.0.0.0 255.0.0.0 any` | ||
""" | ||
tokens = line.split() | ||
return " ".join([address_to_mask(token) if r_ip_nn.match(token) else token for token in tokens]) | ||
|
||
|
||
def line_to_wildcard(line): | ||
""" | ||
Search for any IPv4/nn tokens in the `line` argument and transform them into wildcard format. | ||
E.g. `permit ip 10.0.0.0/8` any -> `permit ip 10.0.0.0 0.255.255.255 any` | ||
""" | ||
tokens = line.split() | ||
return " ".join([address_to_wildcard(token) if r_ip_nn.match(token) else token for token in tokens]) | ||
|
||
def ip_to_ipv4(line): | ||
""" | ||
Replace token "ip" with "ipv4" to satisfy IOS-XR syntax. | ||
""" | ||
tokens = line.split() | ||
return " ".join(["ipv4" if token == "ip" else token for token in tokens]) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
ip_address {{ ip4 | ip_address }} | ||
ip_interface {{ ip4 | ip_interface }} | ||
ip_version {{ ip4 | ip_version }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
from netom.filters import address_to_mask, address_to_wildcard, line_to_mask, line_to_wildcard | ||
import pytest | ||
Check noticeCode scanning / CodeQL Unused import
Import of 'pytest' is not used.
|
||
|
||
|
||
def test_address_to_mask(): | ||
assert address_to_mask("192.168.0.1/4") == "192.168.0.1 240.0.0.0" | ||
assert address_to_mask("192.168.0.1/8") == "192.168.0.1 255.0.0.0" | ||
assert address_to_mask("192.168.0.1/9") == "192.168.0.1 255.128.0.0" | ||
assert address_to_mask("192.168.0.1/16") == "192.168.0.1 255.255.0.0" | ||
assert address_to_mask("192.168.0.1/24") == "192.168.0.1 255.255.255.0" | ||
assert address_to_mask("192.168.0.1/25") == "192.168.0.1 255.255.255.128" | ||
assert address_to_mask("192.168.0.1/30") == "192.168.0.1 255.255.255.252" | ||
assert address_to_mask("192.168.0.1/31") == "192.168.0.1 255.255.255.254" | ||
assert address_to_mask("192.168.0.1/32") == "192.168.0.1 255.255.255.255" | ||
|
||
def test_address_to_wildcard(): | ||
assert address_to_wildcard("192.0.0.0/4") == "192.0.0.0 15.255.255.255" | ||
assert address_to_wildcard("192.0.0.0/8") == "192.0.0.0 0.255.255.255" | ||
assert address_to_wildcard("192.0.0.0/9") == "192.0.0.0 0.127.255.255" | ||
assert address_to_wildcard("192.168.0.0/16") == "192.168.0.0 0.0.255.255" | ||
assert address_to_wildcard("192.168.0.0/24") == "192.168.0.0 0.0.0.255" | ||
assert address_to_wildcard("192.168.0.0/25") == "192.168.0.0 0.0.0.127" | ||
assert address_to_wildcard("192.168.0.0/30") == "192.168.0.0 0.0.0.3" | ||
assert address_to_wildcard("192.168.0.0/31") == "192.168.0.0 0.0.0.1" | ||
assert address_to_wildcard("192.168.0.0/32") == "192.168.0.0 0.0.0.0" | ||
|
||
def test_line_to_mask(): | ||
assert line_to_mask("ip route 192.168.0.0/4 198.51.100.1") == "ip route 192.168.0.0 240.0.0.0 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/8 198.51.100.1") == "ip route 192.168.0.0 255.0.0.0 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/9 198.51.100.1") == "ip route 192.168.0.0 255.128.0.0 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/16 198.51.100.1") == "ip route 192.168.0.0 255.255.0.0 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/24 198.51.100.1") == "ip route 192.168.0.0 255.255.255.0 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/25 198.51.100.1") == "ip route 192.168.0.0 255.255.255.128 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/30 198.51.100.1") == "ip route 192.168.0.0 255.255.255.252 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/31 198.51.100.1") == "ip route 192.168.0.0 255.255.255.254 198.51.100.1" | ||
assert line_to_mask("ip route 192.168.0.0/32 198.51.100.1") == "ip route 192.168.0.0 255.255.255.255 198.51.100.1" | ||
|
||
def test_line_to_wildcard(): | ||
assert line_to_wildcard("permit tcp 192.0.0.0/4 any eq 22") == "permit tcp 192.0.0.0 15.255.255.255 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.0.0.0/8 any eq 22") == "permit tcp 192.0.0.0 0.255.255.255 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.0.0.0/9 any eq 22") == "permit tcp 192.0.0.0 0.127.255.255 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.168.0.0/16 any eq 22") == "permit tcp 192.168.0.0 0.0.255.255 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.168.0.0/24 any eq 22") == "permit tcp 192.168.0.0 0.0.0.255 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.168.0.0/25 any eq 22") == "permit tcp 192.168.0.0 0.0.0.127 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.168.0.0/30 any eq 22") == "permit tcp 192.168.0.0 0.0.0.3 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.168.0.0/31 any eq 22") == "permit tcp 192.168.0.0 0.0.0.1 any eq 22" | ||
assert line_to_wildcard("permit tcp 192.168.0.0/32 any eq 22") == "permit tcp 192.168.0.0 0.0.0.0 any eq 22" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
import os | ||
|
||
import netom | ||
|
||
filter_data = dict(ip4="10.0.0.1/24") | ||
|
||
|
||
def test_init(): | ||
assert netom.Render("test", "12") | ||
|
||
|
||
def test_filters(this_dir): | ||
expected = """ | ||
ip_address 10.0.0.1 | ||
ip_interface 10.0.0.1/24 | ||
ip_version 4 | ||
""" | ||
|
||
robj = netom.Render("netom0", "test-0", os.path.join(this_dir, "templates")) | ||
output = robj.render_string("filters.j2", filter_data) | ||
print(output) | ||
assert expected == output |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Module is imported with 'import' and 'import from'