Skip to content

Commit 8fec093

Browse files
Fix delete firewall flow
1 parent 483e2ff commit 8fec093

3 files changed

Lines changed: 61 additions & 6 deletions

File tree

server/src/main/java/com/cloud/network/firewall/FirewallManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ public boolean applyRules(List<? extends FirewallRule> rules, boolean continueOn
894894
success = false;
895895
} else {
896896
removeRule(rule);
897-
if (rule.getSourceIpAddressId() != null) {
897+
if (rule.getSourceIpAddressId() != null && rule.getVpcId() == null) {
898898
//if the rule is the last one for the ip address assigned to VPC, unassign it from the network
899899
_vpcMgr.unassignIPFromVpcNetwork(rule.getSourceIpAddressId(), rule.getNetworkId());
900900
}

systemvm/debian/opt/cloud/bin/configure.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,7 @@ def process(self):
703703
self.add_routing_rules()
704704
return
705705

706+
desired_firewall_ips = self._get_desired_vpc_firewall_ips()
706707
fw_chains_created = set()
707708
for item in self.dbag:
708709
if item == "id":
@@ -725,6 +726,56 @@ def process(self):
725726
fw_chains_created.add(src_ip)
726727
self.AclIP(self.dbag[item], self.config).create()
727728

729+
if self.config.is_vpc():
730+
self._cleanup_removed_vpc_firewall_chains(desired_firewall_ips)
731+
732+
def _get_desired_vpc_firewall_ips(self):
733+
desired_firewall_ips = set()
734+
if not self.config.is_vpc():
735+
return desired_firewall_ips
736+
737+
for item in self.dbag:
738+
if item == "id":
739+
continue
740+
rule = self.dbag[item]
741+
if rule.get("purpose") == "Firewall":
742+
src_ip = rule.get("src_ip")
743+
if src_ip:
744+
desired_firewall_ips.add(src_ip)
745+
return desired_firewall_ips
746+
747+
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."""
749+
try:
750+
mangle_save = CsHelper.execute("iptables-save -t mangle")
751+
existing_firewall_ips = []
752+
for line in mangle_save:
753+
if line.startswith(":FIREWALL_"):
754+
chain = line.split(" ")[0][1:]
755+
existing_firewall_ips.append(chain.replace("FIREWALL_", "", 1))
756+
757+
for src_ip in existing_firewall_ips:
758+
if src_ip in desired_firewall_ips:
759+
continue
760+
self._delete_vpc_firewall_chain(src_ip)
761+
except Exception as e:
762+
logging.debug("Failed VPC firewall chain cleanup: %s", e)
763+
764+
def _delete_vpc_firewall_chain(self, src_ip):
765+
chain = "FIREWALL_%s" % src_ip
766+
try:
767+
prerouting_rules = CsHelper.execute("iptables -t mangle -S PREROUTING")
768+
for rule in prerouting_rules:
769+
if ("-d %s/32" % src_ip) in rule and ("-j %s" % chain) in rule:
770+
delete_rule = rule.replace("-A PREROUTING", "-D PREROUTING", 1)
771+
CsHelper.execute2("iptables -t mangle %s" % delete_rule, False)
772+
773+
CsHelper.execute2("iptables -t mangle -F %s" % chain, False)
774+
CsHelper.execute2("iptables -t mangle -X %s" % chain, False)
775+
logging.info("Deleted VPC firewall chain %s as last firewall rule was removed", chain)
776+
except Exception as e:
777+
logging.debug("Failed deleting VPC firewall chain %s: %s", chain, e)
778+
728779
class CsIpv6Firewall(CsDataBag):
729780
"""
730781
Deal with IPv6 Firewall

ui/src/views/network/PublicIpResource.vue

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,15 @@ export default {
147147
148148
// VPC IPs with static nat keep existing VPN behavior and always show firewall
149149
if (this.resource.isstaticnat) {
150-
let tabs = this.defaultTabs
151-
if (this.resource.virtualmachinetype === 'DomainRouter') {
152-
tabs = this.defaultTabs.concat(this.$route.meta.tabs.filter(tab => tab.name === 'vpn'))
153-
}
154-
this.tabs = hasFirewallCapability ? this.addFirewallTab(tabs) : tabs
150+
const tabs = this.addFirewallTab(this.$route.meta.tabs).map(tab => {
151+
if (tab.name !== 'firewall') {
152+
return tab
153+
}
154+
const staticNatFirewallTab = { ...tab }
155+
delete staticNatFirewallTab.networkServiceFilter
156+
return staticNatFirewallTab
157+
})
158+
this.tabs = tabs
155159
return
156160
}
157161

0 commit comments

Comments
 (0)