Skip to content

Commit

Permalink
IP version check with respect to assigned type
Browse files Browse the repository at this point in the history
When is assigned an IP address in the source or destination IP, there is
no validation of these match with the version type, so it is possible
assign an ipv4 address with a type ipv6.
These changes allow verifying before apply the change the congruence
between the version type and the version of the source and destination
IP. If both are different an error is raised.

CR 251552

Change-Id: I2a0518a775459632ba807c5cbdf2641cf0e30dad
(cherry picked from commit 2d5a014d25cfc892fc15de28fa9fad17acb92344)
  • Loading branch information
Joss-AG committed Feb 1, 2023
1 parent 070aba6 commit 225d937
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
51 changes: 50 additions & 1 deletion pyaoscx/acl_entry.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Copyright 2019-2022 Hewlett Packard Enterprise Development LP.
# (C) Copyright 2019-2023 Hewlett Packard Enterprise Development LP.
# Apache License 2.0

import json
Expand All @@ -7,6 +7,7 @@

from pyaoscx.exceptions.generic_op_error import GenericOperationError
from pyaoscx.exceptions.response_error import ResponseError
from pyaoscx.exceptions.verification_error import VerificationError

from pyaoscx.utils import util as utils

Expand Down Expand Up @@ -105,6 +106,10 @@ def __init__(
utils.set_creation_attrs(self, **kwargs)
# Attribute used to know if object was changed recently
self.__modified = False
if "src_ip" in kwargs:
self.src_ip = kwargs["src_ip"]
if "dst_ip" in kwargs:
self.dst_ip = kwargs["dst_ip"]

def __set_acl(self, parent_acl):
"""
Expand Down Expand Up @@ -350,6 +355,8 @@ def create(self):
"""
acl_entry_data = utils.get_attrs(self, self.config_attrs)
acl_entry_data["sequence_number"] = self.sequence_number
acl_entry_data["src_ip"] = self.src_ip
acl_entry_data["dst_ip"] = self.dst_ip

# Try to get protocol number
try:
Expand Down Expand Up @@ -590,3 +597,45 @@ def modify(

# Apply changes
return self.apply()

@property
def src_ip(self):
"""
Getter method for source ip attribute.
:return: String value for src_ip.
"""
return self._src_ip

@src_ip.setter
def src_ip(self, new_src_ip):
"""
Setter method for the src_ip attribute.
"""
version = utils.get_ip_version(new_src_ip)
if version != self.__parent_acl.list_type:
raise VerificationError(
"Version does not match the IP"
"version type in {}".format(self.__parent_acl.name)
)
self._src_ip = new_src_ip

@property
def dst_ip(self):
"""
Getter method for destination ip attribute.
:return: String value for dst_ip.
"""
return self._dst_ip

@dst_ip.setter
def dst_ip(self, new_dst_ip):
"""
Setter method for the dst_ip attribute.
"""
version = utils.get_ip_version(new_dst_ip)
if version != self.__parent_acl.list_type:
raise VerificationError(
"Version does not match the IP"
"version type in {}".format(self.__parent_acl.name)
)
self._dst_ip = new_dst_ip
11 changes: 5 additions & 6 deletions pyaoscx/utils/util.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# (C) Copyright 2019-2022 Hewlett Packard Enterprise Development LP.
# (C) Copyright 2019-2023 Hewlett Packard Enterprise Development LP.
# Apache License 2.0

from netaddr import IPNetwork
import os

from ipaddress import ip_interface
from requests_toolbelt.multipart.encoder import MultipartEncoder

from pyaoscx.exceptions.generic_op_error import GenericOperationError
Expand Down Expand Up @@ -323,8 +322,8 @@ def get_ip_version(ip):
:return: String with the IP version. Can be either ipv4 or ipv6.
"""
try:
ip_net = IPNetwork(ip)
ip_net = ip_interface(ip)
return "ipv{0}".format(ip_net.version)
except ValueError:
msg = "Invalid IP Address: {0}".format(ip)
except ValueError as intr:
msg = "Invalid IP: {0}".format(intr)
raise ParameterError(msg)

0 comments on commit 225d937

Please sign in to comment.