diff --git a/docs/vyos.vyos.vyos_vpp_acl_module.rst b/docs/vyos.vyos.vyos_vpp_acl_module.rst index a1e6d276..250958c4 100644 --- a/docs/vyos.vyos.vyos_vpp_acl_module.rst +++ b/docs/vyos.vyos.vyos_vpp_acl_module.rst @@ -1,1225 +1,1227 @@ .. _vyos.vyos.vyos_vpp_acl_module: ********************** vyos.vyos.vyos_vpp_acl ********************** **VPP dataplane ACL resource module** Version added: 7.0.0 .. contents:: :local: :depth: 1 Synopsis -------- - This module manages the access control lists of the accelerated dataplane (VPP) on devices running VyOS - IP and MAC ACLs, and their bindings to dataplane interfaces. Parameters ---------- .. raw:: html
Parameter Choices/Defaults Comments
config
dictionary
The VPP dataplane ACL configuration.
ip
dictionary
IP ACLs and their bindings.
acls
list / elements=dictionary
IP ACLs (vpp acl ip tag-name).
description
string
The ACL description.
+
VyOS does not accept the single quote character in values.
name
string / required
The ACL name.
rules
list / elements=dictionary
The ACL rules.
action
string
    Choices:
  • permit
  • deny
  • permit-reflect
The action for matching packets.
description
string
The rule description.
destination
dictionary
The destination to match.
port
string
The destination port or port range.
prefix
string
The IPv4 or IPv6 destination prefix.
number
integer / required
The rule number.
protocol
string
The protocol to match, as a name or number, or all.
source
dictionary
The source to match.
port
string
The source port or port range, for example 1024-65535.
prefix
string
The IPv4 or IPv6 source prefix.
tcp_flags
dictionary
The TCP flags to match.
is_not_set
list / elements=string
    Choices:
  • ack
  • cwr
  • ecn
  • fin
  • psh
  • rst
  • syn
  • urg
The TCP flags that must not be set.
is_set
list / elements=string
    Choices:
  • ack
  • cwr
  • ecn
  • fin
  • psh
  • rst
  • syn
  • urg
The TCP flags that must be set.
interfaces
list / elements=dictionary
The IP ACLs bound to dataplane interfaces.
input
list / elements=dictionary
The ACLs applied to incoming traffic, in the order of acl_tag.
acl_tag
integer / required
The position of the ACL in the list.
tag_name
string / required
The name of the IP ACL.
name
string / required
The interface name, for example eth1.
output
list / elements=dictionary
The ACLs applied to outgoing traffic, in the order of acl_tag.
acl_tag
integer / required
The position of the ACL in the list.
tag_name
string / required
The name of the IP ACL.
mac
dictionary
MAC ACLs and their bindings.
acls
list / elements=dictionary
MAC ACLs (vpp acl mac tag-name).
description
string
The ACL description.
name
string / required
The ACL name.
rules
list / elements=dictionary
The ACL rules.
action
string
    Choices:
  • permit
  • deny
The action for matching packets.
description
string
The rule description.
mac_address
string
The MAC address to match.
mac_mask
string
The mask applied to mac_address.
number
integer / required
The rule number.
prefix
string
The IPv4 or IPv6 prefix to match.
interfaces
list / elements=dictionary
The MAC ACLs bound to dataplane interfaces, one per interface.
name
string / required
The interface name, for example eth1.
tag_name
string
The name of the MAC ACL.
running_config
string
This option is used only with state parsed.
The value of this option should be the output received from the VyOS device by executing the command show configuration commands | grep 'vpp acl'.
The state parsed reads the configuration from running_config option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the parsed key within the result.
state
string
    Choices:
  • merged ←
  • replaced
  • overridden
  • deleted
  • gathered
  • rendered
  • parsed
The state the configuration should be left in.

Notes ----- .. note:: - Tested against VyOS 1.5.1. - This module works with connection ``network_cli``. - ACLs can only be bound to interfaces that are part of the dataplane (``vpp settings interface``). - IP and MAC ACLs are handled independently. With *state=replaced*, the listed ACLs and interface bindings are replaced as given; with *state=overridden*, ACLs and bindings that are not listed are also deleted, bindings before ACLs. - When not configured, VyOS uses the protocol ``all`` for IP rules and the mask ``ff:ff:ff:ff:ff:ff`` for MAC rules; the module reports only configured values. + - For more information on using Ansible to manage network devices see the :ref:`Ansible Network Guide ` Examples -------- .. code-block:: yaml # Using merged # Before state: # ------------- # # vyos@vyos:~$ show configuration commands | grep 'vpp acl' # - name: Permit HTTPS from partners into eth1 vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN description: Inbound web traffic rules: - number: 10 action: permit protocol: tcp source: prefix: 192.0.2.0/24 destination: prefix: 198.51.100.10/32 port: "443" - number: 20 action: deny interfaces: - name: eth1 input: - acl_tag: 1 tag_name: WEB-IN state: merged # Task output # ----------- # # commands: # - set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' # - set vpp acl ip tag-name WEB-IN rule 10 action permit # - set vpp acl ip tag-name WEB-IN rule 10 protocol tcp # - set vpp acl ip tag-name WEB-IN rule 10 source prefix 192.0.2.0/24 # - set vpp acl ip tag-name WEB-IN rule 10 destination prefix 198.51.100.10/32 # - set vpp acl ip tag-name WEB-IN rule 10 destination port 443 # - set vpp acl ip tag-name WEB-IN rule 20 action deny # - set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN # Using merged, MAC ACL - name: Permit one vendor's MAC addresses on eth1 vyos.vyos.vyos_vpp_acl: config: mac: acls: - name: MAC-FILTER rules: - number: 10 action: permit mac_address: 00:11:22:00:00:00 mac_mask: ff:ff:ff:00:00:00 interfaces: - name: eth1 tag_name: MAC-FILTER state: merged # Using replaced # Before state: # ------------- # # set vpp acl ip tag-name WEB-IN rule 10 action 'permit' # set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' # set vpp acl ip tag-name WEB-IN rule 20 action 'deny' - name: Replace the rules of WEB-IN vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN rules: - number: 10 action: permit state: replaced # Task output # ----------- # # commands: # - delete vpp acl ip tag-name WEB-IN rule 10 protocol # - delete vpp acl ip tag-name WEB-IN rule 20 # Using overridden - name: Keep only the WEB-IN ACL and its binding vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN rules: - number: 10 action: permit interfaces: - name: eth1 input: - acl_tag: 1 tag_name: WEB-IN state: overridden # Using deleted - name: Remove the ACL bindings of eth1 and the WEB-IN ACL vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN interfaces: - name: eth1 state: deleted - name: Delete all dataplane ACL configuration vyos.vyos.vyos_vpp_acl: state: deleted # Task output (all ACL configuration) # ----------------------------------- # # commands: # - delete vpp acl # Using gathered - name: Gather the dataplane ACL configuration vyos.vyos.vyos_vpp_acl: state: gathered # Using rendered - name: Render the commands for a MAC ACL vyos.vyos.vyos_vpp_acl: config: mac: acls: - name: MAC-FILTER rules: - number: 10 action: deny mac_address: 00:11:22:33:44:55 state: rendered # Task output # ----------- # # rendered: # - set vpp acl mac tag-name MAC-FILTER rule 10 action deny # - set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55 # Using parsed - name: Parse dataplane ACL configuration from device output vyos.vyos.vyos_vpp_acl: running_config: | set vpp acl ip tag-name WEB-IN rule 10 action 'permit' set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' state: parsed # Task output # ----------- # # parsed: # ip: # acls: # - name: WEB-IN # rules: # - number: 10 # action: permit # tcp_flags: # is_set: # - syn Return Values ------------- Common return values are documented `here `_, the following are the fields unique to this module: .. raw:: html
Key Returned Description
after
dictionary
when changed
The resulting configuration after module execution.

Sample:
This output will always be in the same format as the module argspec.
before
dictionary
when state is merged, replaced, overridden or deleted
The configuration prior to the module execution.

Sample:
This output will always be in the same format as the module argspec.
commands
list
when state is merged, replaced, overridden or deleted
The set of commands pushed to the remote device.

Sample:
['set vpp acl ip tag-name WEB-IN rule 10 action permit', 'set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN']
gathered
dictionary
when state is gathered
Facts about the network resource gathered from the remote device as structured data.

Sample:
This output will always be in the same format as the module argspec.
parsed
dictionary
when state is parsed
The device native config provided in running_config option parsed into structured data as per module argspec.

Sample:
This output will always be in the same format as the module argspec.
rendered
list
when state is rendered
The provided configuration in the task rendered in device-native format (offline).

Sample:
['set vpp acl mac tag-name MAC-FILTER rule 10 action deny']


Status ------ Authors ~~~~~~~ -- Evgeny Molotkov (@omnom62) +- VyOS community (@vyos) diff --git a/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py b/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py index 0d47a29e..780e6ae5 100644 --- a/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py +++ b/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py @@ -1,91 +1,97 @@ # -*- coding: utf-8 -*- # Copyright 2026 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type """ The vyos vpp_acl fact class It is in this file the configuration is collected from the device for a given resource, parsed, and the facts tree is populated based on the configuration. """ from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.vpp_acl.vpp_acl import ( Vpp_aclArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.vpp_acl import ( Vpp_aclTemplate, ) def _number(item, key): return int(item[key]) +def _name_key(item): + """Numeric names (parsed as int) first, in numeric order, then the others.""" + name = item["name"] + return (1, str(name)) if isinstance(name, str) else (0, name) + + class Vpp_aclFacts(object): """The vyos vpp_acl facts class""" def __init__(self, module, subspec="config", options="options"): self._module = module self.argument_spec = Vpp_aclArgs.argument_spec def get_device_data(self, connection): return connection.get("show configuration commands | grep 'vpp acl'") def populate_facts(self, connection, ansible_facts, data=None): """Populate the facts for Vpp_acl network resource :param connection: the device connection :param ansible_facts: Facts dictionary :param data: previously collected conf :rtype: dictionary :returns: facts """ if not data: data = self.get_device_data(connection) lines = [line.replace("'", "") for line in data.splitlines()] objs = Vpp_aclTemplate(lines=lines, module=self._module).parse() for family in ("ip", "mac"): section = objs.get(family) if not section: continue acls = [] for acl in (section.get("acls") or {}).values(): rules = [] for rule in (acl.get("rules") or {}).values(): flags = rule.get("tcp_flags") or {} for key in list(flags): flags[key] = sorted(flags[key].values()) rules.append(rule) if rules: acl["rules"] = sorted(rules, key=lambda r: _number(r, "number")) acls.append(acl) if acls: - section["acls"] = sorted(acls, key=lambda a: a["name"]) + section["acls"] = sorted(acls, key=_name_key) interfaces = [] for iface in (section.get("interfaces") or {}).values(): for direction in ("input", "output"): if direction in iface: iface[direction] = sorted( iface[direction].values(), key=lambda b: _number(b, "acl_tag"), ) interfaces.append(iface) if interfaces: - section["interfaces"] = sorted(interfaces, key=lambda i: i["name"]) + section["interfaces"] = sorted(interfaces, key=_name_key) facts = {"vpp_acl": {}} if objs: params = utils.validate_config(self.argument_spec, {"config": objs}) facts["vpp_acl"] = utils.remove_empties(params["config"]) ansible_facts["ansible_network_resources"].update(facts) return ansible_facts diff --git a/plugins/modules/vyos_vpp_acl.py b/plugins/modules/vyos_vpp_acl.py index 3a176795..6ec025fb 100644 --- a/plugins/modules/vyos_vpp_acl.py +++ b/plugins/modules/vyos_vpp_acl.py @@ -1,482 +1,486 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright 2026 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The module file for vyos_vpp_acl """ from __future__ import absolute_import, division, print_function __metaclass__ = type DOCUMENTATION = """ module: vyos_vpp_acl version_added: 7.0.0 short_description: VPP dataplane ACL resource module description: - This module manages the access control lists of the accelerated dataplane (VPP) on devices running VyOS - IP and MAC ACLs, and their bindings to dataplane interfaces. author: - - Evgeny Molotkov (@omnom62) + - VyOS community (@vyos) +extends_documentation_fragment: + - vyos.vyos.vyos notes: - Tested against VyOS 1.5.1. - This module works with connection C(network_cli). - ACLs can only be bound to interfaces that are part of the dataplane (C(vpp settings interface)). - IP and MAC ACLs are handled independently. With I(state=replaced), the listed ACLs and interface bindings are replaced as given; with I(state=overridden), ACLs and bindings that are not listed are also deleted, bindings before ACLs. - When not configured, VyOS uses the protocol C(all) for IP rules and the mask C(ff:ff:ff:ff:ff:ff) for MAC rules; the module reports only configured values. options: config: description: The VPP dataplane ACL configuration. type: dict suboptions: ip: description: IP ACLs and their bindings. type: dict suboptions: acls: description: IP ACLs (C(vpp acl ip tag-name)). type: list elements: dict suboptions: name: description: The ACL name. type: str required: true description: - description: The ACL description. + description: + - The ACL description. + - VyOS does not accept the single quote character in values. type: str rules: description: The ACL rules. type: list elements: dict suboptions: number: description: The rule number. type: int required: true action: description: The action for matching packets. type: str choices: ["permit", "deny", "permit-reflect"] description: description: The rule description. type: str protocol: description: The protocol to match, as a name or number, or C(all). type: str source: description: The source to match. type: dict suboptions: prefix: description: The IPv4 or IPv6 source prefix. type: str port: description: The source port or port range, for example C(1024-65535). type: str destination: description: The destination to match. type: dict suboptions: prefix: description: The IPv4 or IPv6 destination prefix. type: str port: description: The destination port or port range. type: str tcp_flags: description: The TCP flags to match. type: dict suboptions: is_set: description: The TCP flags that must be set. type: list elements: str choices: ["ack", "cwr", "ecn", "fin", "psh", "rst", "syn", "urg"] is_not_set: description: The TCP flags that must not be set. type: list elements: str choices: ["ack", "cwr", "ecn", "fin", "psh", "rst", "syn", "urg"] interfaces: description: The IP ACLs bound to dataplane interfaces. type: list elements: dict suboptions: name: description: The interface name, for example C(eth1). type: str required: true input: description: The ACLs applied to incoming traffic, in the order of I(acl_tag). type: list elements: dict suboptions: acl_tag: description: The position of the ACL in the list. type: int required: true tag_name: description: The name of the IP ACL. type: str required: true output: description: The ACLs applied to outgoing traffic, in the order of I(acl_tag). type: list elements: dict suboptions: acl_tag: description: The position of the ACL in the list. type: int required: true tag_name: description: The name of the IP ACL. type: str required: true mac: description: MAC ACLs and their bindings. type: dict suboptions: acls: description: MAC ACLs (C(vpp acl mac tag-name)). type: list elements: dict suboptions: name: description: The ACL name. type: str required: true description: description: The ACL description. type: str rules: description: The ACL rules. type: list elements: dict suboptions: number: description: The rule number. type: int required: true action: description: The action for matching packets. type: str choices: ["permit", "deny"] description: description: The rule description. type: str prefix: description: The IPv4 or IPv6 prefix to match. type: str mac_address: description: The MAC address to match. type: str mac_mask: description: The mask applied to I(mac_address). type: str interfaces: description: The MAC ACLs bound to dataplane interfaces, one per interface. type: list elements: dict suboptions: name: description: The interface name, for example C(eth1). type: str required: true tag_name: description: The name of the MAC ACL. type: str running_config: description: - This option is used only with state I(parsed). - The value of this option should be the output received from the VyOS device by executing the command B(show configuration commands | grep 'vpp acl'). - The state I(parsed) reads the configuration from C(running_config) option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the I(parsed) key within the result. type: str state: description: - The state the configuration should be left in. type: str choices: - merged - replaced - overridden - deleted - gathered - rendered - parsed default: merged """ EXAMPLES = """ # Using merged # Before state: # ------------- # # vyos@vyos:~$ show configuration commands | grep 'vpp acl' # - name: Permit HTTPS from partners into eth1 vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN description: Inbound web traffic rules: - number: 10 action: permit protocol: tcp source: prefix: 192.0.2.0/24 destination: prefix: 198.51.100.10/32 port: "443" - number: 20 action: deny interfaces: - name: eth1 input: - acl_tag: 1 tag_name: WEB-IN state: merged # Task output # ----------- # # commands: # - set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' # - set vpp acl ip tag-name WEB-IN rule 10 action permit # - set vpp acl ip tag-name WEB-IN rule 10 protocol tcp # - set vpp acl ip tag-name WEB-IN rule 10 source prefix 192.0.2.0/24 # - set vpp acl ip tag-name WEB-IN rule 10 destination prefix 198.51.100.10/32 # - set vpp acl ip tag-name WEB-IN rule 10 destination port 443 # - set vpp acl ip tag-name WEB-IN rule 20 action deny # - set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN # Using merged, MAC ACL - name: Permit one vendor's MAC addresses on eth1 vyos.vyos.vyos_vpp_acl: config: mac: acls: - name: MAC-FILTER rules: - number: 10 action: permit mac_address: 00:11:22:00:00:00 mac_mask: ff:ff:ff:00:00:00 interfaces: - name: eth1 tag_name: MAC-FILTER state: merged # Using replaced # Before state: # ------------- # # set vpp acl ip tag-name WEB-IN rule 10 action 'permit' # set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' # set vpp acl ip tag-name WEB-IN rule 20 action 'deny' - name: Replace the rules of WEB-IN vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN rules: - number: 10 action: permit state: replaced # Task output # ----------- # # commands: # - delete vpp acl ip tag-name WEB-IN rule 10 protocol # - delete vpp acl ip tag-name WEB-IN rule 20 # Using overridden - name: Keep only the WEB-IN ACL and its binding vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN rules: - number: 10 action: permit interfaces: - name: eth1 input: - acl_tag: 1 tag_name: WEB-IN state: overridden # Using deleted - name: Remove the ACL bindings of eth1 and the WEB-IN ACL vyos.vyos.vyos_vpp_acl: config: ip: acls: - name: WEB-IN interfaces: - name: eth1 state: deleted - name: Delete all dataplane ACL configuration vyos.vyos.vyos_vpp_acl: state: deleted # Task output (all ACL configuration) # ----------------------------------- # # commands: # - delete vpp acl # Using gathered - name: Gather the dataplane ACL configuration vyos.vyos.vyos_vpp_acl: state: gathered # Using rendered - name: Render the commands for a MAC ACL vyos.vyos.vyos_vpp_acl: config: mac: acls: - name: MAC-FILTER rules: - number: 10 action: deny mac_address: 00:11:22:33:44:55 state: rendered # Task output # ----------- # # rendered: # - set vpp acl mac tag-name MAC-FILTER rule 10 action deny # - set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55 # Using parsed - name: Parse dataplane ACL configuration from device output vyos.vyos.vyos_vpp_acl: running_config: | set vpp acl ip tag-name WEB-IN rule 10 action 'permit' set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' state: parsed # Task output # ----------- # # parsed: # ip: # acls: # - name: WEB-IN # rules: # - number: 10 # action: permit # tcp_flags: # is_set: # - syn """ RETURN = """ before: description: The configuration prior to the module execution. returned: when I(state) is C(merged), C(replaced), C(overridden) or C(deleted) type: dict sample: > This output will always be in the same format as the module argspec. after: description: The resulting configuration after module execution. returned: when changed type: dict sample: > This output will always be in the same format as the module argspec. commands: description: The set of commands pushed to the remote device. returned: when I(state) is C(merged), C(replaced), C(overridden) or C(deleted) type: list sample: - set vpp acl ip tag-name WEB-IN rule 10 action permit - set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN rendered: description: The provided configuration in the task rendered in device-native format (offline). returned: when I(state) is C(rendered) type: list sample: - set vpp acl mac tag-name MAC-FILTER rule 10 action deny gathered: description: Facts about the network resource gathered from the remote device as structured data. returned: when I(state) is C(gathered) type: dict sample: > This output will always be in the same format as the module argspec. parsed: description: The device native config provided in I(running_config) option parsed into structured data as per module argspec. returned: when I(state) is C(parsed) type: dict sample: > This output will always be in the same format as the module argspec. """ from ansible.module_utils.basic import AnsibleModule from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.vpp_acl.vpp_acl import ( Vpp_aclArgs, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.vpp_acl.vpp_acl import ( Vpp_acl, ) def main(): """ Main entry point for module execution :returns: the result form module invocation """ module = AnsibleModule( argument_spec=Vpp_aclArgs.argument_spec, mutually_exclusive=[["config", "running_config"]], required_if=[ ["state", "merged", ["config"]], ["state", "replaced", ["config"]], ["state", "overridden", ["config"]], ["state", "rendered", ["config"]], ["state", "parsed", ["running_config"]], ], supports_check_mode=True, ) result = Vpp_acl(module).execute_module() module.exit_json(**result) if __name__ == "__main__": main() diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml index aff72574..1510f309 100644 --- a/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml @@ -1,7 +1,6 @@ --- # removes all of vpp, so eth2 returns to the kernel - name: Remove the dataplane vyos.vyos.vyos_config: lines: - delete vpp - ignore_errors: true diff --git a/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py b/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py index c7a7b40a..4e20ff6b 100644 --- a/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py +++ b/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py @@ -1,347 +1,358 @@ # -*- coding: utf-8 -*- # Copyright 2026 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import absolute_import, division, print_function __metaclass__ = type from unittest.mock import patch from ansible_collections.vyos.vyos.plugins.modules import vyos_vpp_acl from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args from .vyos_module import TestVyosModule, load_fixture WEB_IN = dict( name="WEB-IN", description="Inbound web traffic", rules=[ dict( number=10, action="permit", description="Allow HTTPS from partners", protocol="tcp", source=dict(prefix="192.0.2.0/24", port="1024-65535"), destination=dict(prefix="198.51.100.10/32", port="443"), tcp_flags=dict(is_set=["syn"], is_not_set=["ack"]), ), dict(number=20, action="deny"), ], ) ACLS = dict( ip=dict( acls=[ dict(name="MGMT", rules=[dict(number=10, action="permit")]), WEB_IN, dict(name="WEB-OUT", rules=[dict(number=10, action="permit-reflect")]), ], interfaces=[ dict( name="eth1", input=[dict(acl_tag=1, tag_name="WEB-IN"), dict(acl_tag=2, tag_name="MGMT")], output=[dict(acl_tag=1, tag_name="WEB-OUT")], ), ], ), mac=dict( acls=[ dict( name="MAC-FILTER", rules=[ dict( number=10, action="permit", prefix="192.0.2.0/24", mac_address="00:11:22:33:44:55", mac_mask="ff:ff:ff:00:00:00", ), ], ), ], interfaces=[dict(name="eth1", tag_name="MAC-FILTER")], ), ) class TestVyosVppAclModule(TestVyosModule): module = vyos_vpp_acl def setUp(self): super(TestVyosVppAclModule, self).setUp() self.mock_get_resource_connection_config = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", ) self.get_resource_connection_config = self.mock_get_resource_connection_config.start() self.mock_get_resource_connection_facts = patch( "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", ) self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() self.mock_get_device_data = patch( "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpp_acl.vpp_acl.Vpp_aclFacts.get_device_data", ) self.get_device_data = self.mock_get_device_data.start() def tearDown(self): super(TestVyosVppAclModule, self).tearDown() self.mock_get_resource_connection_config.stop() self.mock_get_resource_connection_facts.stop() self.mock_get_device_data.stop() def load_fixtures(self, commands=None, filename=None): if filename is None: filename = "vyos_vpp_acl_config.cfg" def load_from_file(*args, **kwargs): return load_fixture(filename) self.get_device_data.side_effect = load_from_file def test_vyos_vpp_acl_merged_idempotent(self): set_module_args(dict(config=ACLS, state="merged")) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_merged_tcp_flag_order_does_not_matter(self): rule = dict(number=10, tcp_flags=dict(is_set=["syn"], is_not_set=["ack"])) set_module_args( dict(config=dict(ip=dict(acls=[dict(name="WEB-IN", rules=[rule])])), state="merged"), ) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_merged(self): set_module_args( dict( config=dict( ip=dict( acls=[ dict( name="WEB-IN", rules=[ dict(number=10, tcp_flags=dict(is_set=["syn", "fin"])), dict(number=30, action="deny", protocol="udp"), ], ), dict(name="NEW", description="New ACL with spaces"), ], interfaces=[ dict(name="eth1", input=[dict(acl_tag=3, tag_name="NEW")]), dict(name="eth2", output=[dict(acl_tag=1, tag_name="NEW")]), ], ), ), state="merged", ), ) commands = [ "set vpp acl ip tag-name NEW description 'New ACL with spaces'", "set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set fin", "set vpp acl ip tag-name WEB-IN rule 30 action deny", "set vpp acl ip tag-name WEB-IN rule 30 protocol udp", "set vpp acl ip interface eth1 input acl-tag 3 tag-name NEW", "set vpp acl ip interface eth2 output acl-tag 1 tag-name NEW", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_acl(self): set_module_args( dict( config=dict( ip=dict( acls=[ dict( name="WEB-IN", rules=[ dict( number=10, action="permit", protocol="tcp", tcp_flags=dict(is_set=["syn"]), ), ], ), ], ), ), state="replaced", ), ) commands = [ "delete vpp acl ip tag-name WEB-IN description", "delete vpp acl ip tag-name WEB-IN rule 10 description", "delete vpp acl ip tag-name WEB-IN rule 10 source prefix", "delete vpp acl ip tag-name WEB-IN rule 10 source port", "delete vpp acl ip tag-name WEB-IN rule 10 destination prefix", "delete vpp acl ip tag-name WEB-IN rule 10 destination port", "delete vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-not-set ack", "delete vpp acl ip tag-name WEB-IN rule 20", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_bindings(self): set_module_args( dict( config=dict( ip=dict( interfaces=[dict(name="eth1", input=[dict(acl_tag=1, tag_name="MGMT")])], ), ), state="replaced", ), ) commands = [ "set vpp acl ip interface eth1 input acl-tag 1 tag-name MGMT", "delete vpp acl ip interface eth1 input acl-tag 2", "delete vpp acl ip interface eth1 output acl-tag 1", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_mac(self): set_module_args( dict( config=dict( mac=dict( acls=[ dict( name="MAC-FILTER", rules=[ dict(number=10, action="deny", mac_address="00:11:22:33:44:55"), ], ), ], interfaces=[dict(name="eth1", tag_name="MAC-FILTER")], ), ), state="replaced", ), ) commands = [ "set vpp acl mac tag-name MAC-FILTER rule 10 action deny", "delete vpp acl mac tag-name MAC-FILTER rule 10 prefix", "delete vpp acl mac tag-name MAC-FILTER rule 10 mac-mask", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_replaced_idempotent(self): set_module_args(dict(config=ACLS, state="replaced")) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_overridden(self): set_module_args( dict( config=dict( ip=dict( acls=[dict(name="MGMT", rules=[dict(number=10, action="permit")])], interfaces=[dict(name="eth1", input=[dict(acl_tag=1, tag_name="MGMT")])], ), ), state="overridden", ), ) commands = [ "delete vpp acl ip tag-name WEB-IN", "delete vpp acl ip tag-name WEB-OUT", "set vpp acl ip interface eth1 input acl-tag 1 tag-name MGMT", "delete vpp acl ip interface eth1 input acl-tag 2", "delete vpp acl ip interface eth1 output acl-tag 1", "delete vpp acl mac", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_overridden_idempotent(self): set_module_args(dict(config=ACLS, state="overridden")) self.execute_module(changed=False, commands=[]) def test_vyos_vpp_acl_deleted_all(self): set_module_args(dict(state="deleted")) self.execute_module(changed=True, commands=["delete vpp acl"]) def test_vyos_vpp_acl_deleted_parts(self): set_module_args( dict( config=dict( ip=dict(acls=[dict(name="WEB-OUT")], interfaces=[dict(name="eth1")]), mac=dict(interfaces=[dict(name="eth1")]), ), state="deleted", ), ) commands = [ "delete vpp acl ip interface eth1", "delete vpp acl ip tag-name WEB-OUT", "delete vpp acl mac interface eth1", ] self.execute_module(changed=True, commands=commands) def test_vyos_vpp_acl_rendered(self): set_module_args( dict( config=dict( mac=dict( acls=[ dict( name="MAC-FILTER", description="Vendor filter", rules=[ dict(number=10, action="deny", mac_address="00:11:22:33:44:55"), ], ), ], interfaces=[dict(name="eth2", tag_name="MAC-FILTER")], ), ), state="rendered", ), ) commands = [ "set vpp acl mac tag-name MAC-FILTER description 'Vendor filter'", "set vpp acl mac tag-name MAC-FILTER rule 10 action deny", "set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55", "set vpp acl mac interface eth2 tag-name MAC-FILTER", ] result = self.execute_module(changed=False) self.assertEqual(sorted(result["rendered"]), sorted(commands)) def test_vyos_vpp_acl_gathered(self): set_module_args(dict(state="gathered")) result = self.execute_module(changed=False) self.assertEqual(result["gathered"], ACLS) def test_vyos_vpp_acl_parsed(self): set_module_args( dict(running_config=load_fixture("vyos_vpp_acl_config.cfg"), state="parsed"), ) result = self.execute_module(changed=False) self.assertEqual(result["parsed"], ACLS) def test_vyos_vpp_acl_deleted_whole_family(self): # all MAC ACLs and bindings listed: the family node goes as a whole set_module_args( dict( config=dict( mac=dict(acls=[dict(name="MAC-FILTER")], interfaces=[dict(name="eth1")]), ), state="deleted", ), ) self.execute_module(changed=True, commands=["delete vpp acl mac"]) def test_vyos_vpp_acl_deleted_both_families(self): set_module_args( dict( config=dict( ip=dict( acls=[dict(name="MGMT"), dict(name="WEB-IN"), dict(name="WEB-OUT")], interfaces=[dict(name="eth1")], ), mac=dict(acls=[dict(name="MAC-FILTER")], interfaces=[dict(name="eth1")]), ), state="deleted", ), ) self.execute_module(changed=True, commands=["delete vpp acl"]) + + def test_vyos_vpp_acl_parsed_numeric_and_text_names(self): + # numeric and text ACL names together, numeric ones first in numeric order + set_module_args( + dict( + running_config=load_fixture("vyos_vpp_acl_config_names.cfg"), + state="parsed", + ), + ) + acls = self.execute_module(changed=False)["parsed"]["ip"]["acls"] + self.assertEqual([acl["name"] for acl in acls], ["20", "100", "WEB"])