Bläddra i källkod

Ensure security group rule ports are integers

Enis Afgan 10 år sedan
förälder
incheckning
1b4f5255fb

+ 15 - 0
cloudbridge/cloud/interfaces/resources.py

@@ -1736,6 +1736,9 @@ class SecurityGroupRule(CloudResource):
 
         Note that this may be a Cloudbridge-specific ID if the underlying
         provider does not support rule IDs.
+
+        :rtype: str
+        :return: Role ID.
         """
         pass
 
@@ -1743,6 +1746,9 @@ class SecurityGroupRule(CloudResource):
     def ip_protocol(self):
         """
         IP protocol used. Either ``tcp`` | ``udp`` | ``icmp``.
+
+        :rtype: str
+        :return: Active protocol.
         """
         pass
 
@@ -1750,6 +1756,9 @@ class SecurityGroupRule(CloudResource):
     def from_port(self):
         """
         Lowest port number opened as part of this rule.
+
+        :rtype: int
+        :return: Lowest port number or 0 if not set.
         """
         pass
 
@@ -1757,6 +1766,9 @@ class SecurityGroupRule(CloudResource):
     def to_port(self):
         """
         Highest port number opened as part of this rule.
+
+        :rtype: int
+        :return: Highest port number or 0 if not set.
         """
         pass
 
@@ -1764,6 +1776,9 @@ class SecurityGroupRule(CloudResource):
     def cidr_ip(self):
         """
         CIDR block this security group is providing access to.
+
+        :rtype: str
+        :return: CIDR block.
         """
         pass
 

+ 8 - 4
cloudbridge/cloud/providers/aws/resources.py

@@ -648,8 +648,8 @@ class AWSSecurityGroup(BaseSecurityGroup):
                  cidr_ip=None, src_group=None):
         for rule in self._security_group.rules:
             if (rule.ip_protocol == ip_protocol and
-               str(rule.from_port) == str(from_port) and
-               str(rule.to_port) == str(to_port) and
+               rule.from_port == from_port and
+               rule.to_port == to_port and
                rule.grants[0].cidr_ip == cidr_ip) or \
                (rule.grants[0].name == src_group.name if src_group and
                hasattr(rule.grants[0], 'name') else False):
@@ -686,11 +686,15 @@ class AWSSecurityGroupRule(BaseSecurityGroupRule):
 
     @property
     def from_port(self):
-        return self._rule.from_port
+        if str(self._rule.from_port).isdigit():
+            return int(self._rule.from_port)
+        return 0
 
     @property
     def to_port(self):
-        return self._rule.to_port
+        if str(self._rule.to_port).isdigit():
+            return int(self._rule.to_port)
+        return 0
 
     @property
     def cidr_ip(self):

+ 4 - 4
cloudbridge/cloud/providers/openstack/resources.py

@@ -816,8 +816,8 @@ class OpenStackSecurityGroup(BaseSecurityGroup):
             self._security_group)
         for rule in self._security_group.rules:
             if (rule['ip_protocol'] == ip_protocol and
-               str(rule['from_port']) == str(from_port) and
-               str(rule['to_port']) == str(to_port) and
+               rule['from_port'] == from_port and
+               rule['to_port'] == to_port and
                rule['ip_range'].get('cidr') == cidr_ip) or \
                (rule['group'].get('name') == src_group.name if src_group
                else False):
@@ -848,11 +848,11 @@ class OpenStackSecurityGroupRule(BaseSecurityGroupRule):
 
     @property
     def from_port(self):
-        return self._rule.get('from_port')
+        return int(self._rule.get('from_port', 0))
 
     @property
     def to_port(self):
-        return self._rule.get('to_port')
+        return int(self._rule.get('to_port', 0))
 
     @property
     def cidr_ip(self):