@@ -703,30 +703,28 @@ def process(self):
703703 self .add_routing_rules ()
704704 return
705705
706- desired_firewall_ips = self ._get_desired_vpc_firewall_ips ()
706+ desired_firewall_ips = set ()
707+ if self .config .is_vpc () and self .config .is_vpc_firewall_enabled ():
708+ desired_firewall_ips = self ._get_desired_vpc_firewall_ips ()
709+
707710 fw_chains_created = set ()
711+
708712 for item in self .dbag :
709713 if item == "id" :
710714 continue
711715 if self .config .is_vpc () and not ("purpose" in self .dbag [item ] and self .dbag [item ]["purpose" ] == "Firewall" ):
712716 self .AclDevice (self .dbag [item ], self .config ).create ()
713717 else :
718+ if self .config .is_vpc () and self .dbag [item ].get ("purpose" ) == "Firewall" and not self .config .is_vpc_firewall_enabled ():
719+ continue
714720 # For VPC firewall rules, create the PREROUTING jump and chain skeleton
715721 # once per public IP before adding the individual rule
716- if self .config .is_vpc () and self .dbag [item ].get ("purpose" ) == "Firewall" :
722+ if self .config .is_vpc () and self .config . is_vpc_firewall_enabled () and self . dbag [item ].get ("purpose" ) == "Firewall" :
717723 src_ip = self .dbag [item ].get ("src_ip" )
718- if src_ip and src_ip not in fw_chains_created :
719- fw = self .config .get_fw ()
720- fw .append (["mangle" , "front" ,
721- "-A PREROUTING -d %s/32 -j FIREWALL_%s" % (src_ip , src_ip )])
722- fw .append (["mangle" , "front" ,
723- "-A FIREWALL_%s -m state --state RELATED,ESTABLISHED -j RETURN" % src_ip ])
724- fw .append (["mangle" , "" ,
725- "-A FIREWALL_%s -j DROP" % src_ip ])
726- fw_chains_created .add (src_ip )
724+ self ._ensure_vpc_firewall_chains ([src_ip ], fw_chains_created )
727725 self .AclIP (self .dbag [item ], self .config ).create ()
728726
729- if self .config .is_vpc ():
727+ if self .config .is_vpc () and self . config . is_vpc_firewall_enabled () :
730728 self ._cleanup_removed_vpc_firewall_chains (desired_firewall_ips )
731729
732730 def _get_desired_vpc_firewall_ips (self ):
@@ -744,8 +742,20 @@ def _get_desired_vpc_firewall_ips(self):
744742 desired_firewall_ips .add (src_ip )
745743 return desired_firewall_ips
746744
745+ def _ensure_vpc_firewall_chains (self , source_ips , fw_chains_created ):
746+ fw = self .config .get_fw ()
747+ for src_ip in source_ips :
748+ if not src_ip or src_ip in fw_chains_created :
749+ continue
750+ fw .append (["mangle" , "front" ,
751+ "-A PREROUTING -d %s/32 -j FIREWALL_%s" % (src_ip , src_ip )])
752+ fw .append (["mangle" , "front" ,
753+ "-A FIREWALL_%s -m state --state RELATED,ESTABLISHED -j RETURN" % src_ip ])
754+ fw .append (["mangle" , "" ,
755+ "-A FIREWALL_%s -j DROP" % src_ip ])
756+ fw_chains_created .add (src_ip )
757+
747758 def _cleanup_removed_vpc_firewall_chains (self , desired_firewall_ips ):
748- """Delete FIREWALL_<ip> chain only when no firewall rule remains for that VPC public IP."""
749759 try :
750760 mangle_save = CsHelper .execute ("iptables-save -t mangle" )
751761 existing_firewall_ips = []
0 commit comments