From 0bd4dec852c7acf35735a67c59e95229cc8e9362 Mon Sep 17 00:00:00 2001 From: Andrija Panic Date: Tue, 4 Aug 2026 16:15:54 +0000 Subject: [PATCH] Network: default egress policy Allow for Isolated networks on fresh installations The built-in Isolated network offerings were seeded with egress_default_policy = false (Deny), while: - the createNetworkOffering API already defaults egressdefaultpolicy to true (Allow) when the parameter is omitted (NetworkOfferingBaseCmd), - the UI's Add Network Offering form contradicted that API default by preselecting Deny and sending an explicit egressdefaultpolicy=false, - the Kubernetes service rejects isolated offerings with egress Deny and creates its own default offering with egress Allow, - VPC tiers have no such baked-in Deny: allow-all (default_allow ACL) is a first-class choice. Seed both built-in Isolated offerings with egress Allow on fresh installations, and align the UI form default with the existing API default. For DefaultIsolatedNetworkOffering (no Firewall service) the flag is inert and set only so API responses do not advertise a misleading Deny policy. No global setting is introduced: per-offering configurability already exists via the egressdefaultpolicy parameter, and a setting consumed once at first-boot seeding but live for later createNetworkOffering calls would have inconsistent lifecycle semantics. Backward compatibility: createDefaultNetworkOfferings() only runs on first boot (guarded by the 'init' configuration flag) and persistDefaultNetworkOffering() is find-or-create by unique name - it never updates an existing row. No upgrade SQL is shipped, deliberately: egress enforcement reads the offering row live on every VR rule programming, so flipping existing rows would change the behavior of existing networks. Upgraded clouds keep Deny on the pre-existing built-in offering; an Allow-by-default offering for new networks on upgraded clouds is left as a follow-up (new/versioned offering). Unit tests cover the fresh-install seeding values and the API default for omitted/explicit egressdefaultpolicy. --- .../network/CreateNetworkOfferingCmdTest.java | 50 +++++++++++++++++++ .../cloud/offerings/NetworkOfferingVO.java | 4 ++ .../cloud/server/ConfigurationServerImpl.java | 9 ++++ .../server/ConfigurationServerImplTest.java | 39 +++++++++++++++ ui/src/views/offering/AddNetworkOffering.vue | 2 +- 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 api/src/test/java/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmdTest.java diff --git a/api/src/test/java/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmdTest.java b/api/src/test/java/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmdTest.java new file mode 100644 index 000000000000..9c22ecce1429 --- /dev/null +++ b/api/src/test/java/org/apache/cloudstack/api/command/admin/network/CreateNetworkOfferingCmdTest.java @@ -0,0 +1,50 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.cloudstack.api.command.admin.network; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.test.util.ReflectionTestUtils; + +import static org.junit.Assert.assertEquals; + +@RunWith(MockitoJUnitRunner.class) +public class CreateNetworkOfferingCmdTest { + + @Test + public void testEgressDefaultPolicyIsAllowWhenParameterOmitted() { + CreateNetworkOfferingCmd cmd = new CreateNetworkOfferingCmd(); + assertEquals("createNetworkOffering must default to egress Allow when egressdefaultpolicy is not specified", + Boolean.TRUE, cmd.getEgressDefaultPolicy()); + } + + @Test + public void testEgressDefaultPolicyExplicitDenyIsHonored() { + CreateNetworkOfferingCmd cmd = new CreateNetworkOfferingCmd(); + ReflectionTestUtils.setField(cmd, "egressDefaultPolicy", Boolean.FALSE); + assertEquals(Boolean.FALSE, cmd.getEgressDefaultPolicy()); + } + + @Test + public void testEgressDefaultPolicyExplicitAllowIsHonored() { + CreateNetworkOfferingCmd cmd = new CreateNetworkOfferingCmd(); + ReflectionTestUtils.setField(cmd, "egressDefaultPolicy", Boolean.TRUE); + assertEquals(Boolean.TRUE, cmd.getEgressDefaultPolicy()); + } +} diff --git a/engine/schema/src/main/java/com/cloud/offerings/NetworkOfferingVO.java b/engine/schema/src/main/java/com/cloud/offerings/NetworkOfferingVO.java index 904c8e646eb5..afaab2df580c 100644 --- a/engine/schema/src/main/java/com/cloud/offerings/NetworkOfferingVO.java +++ b/engine/schema/src/main/java/com/cloud/offerings/NetworkOfferingVO.java @@ -346,6 +346,10 @@ public boolean isEgressDefaultPolicy() { return egressdefaultpolicy; } + public void setEgressDefaultPolicy(boolean egressDefaultPolicy) { + this.egressdefaultpolicy = egressDefaultPolicy; + } + public NetworkOfferingVO(String name, String displayText, TrafficType trafficType, boolean systemOnly, boolean specifyVlan, Integer rateMbps, Integer multicastRateMbps, boolean isDefault, Availability availability, String tags, Network.GuestType guestType, boolean conserveMode, boolean specifyIpRanges, boolean isPersistent, boolean internalLb, boolean publicLb, boolean isForVpc) { diff --git a/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java b/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java index def564dfdc68..1901bcae3e0e 100644 --- a/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java +++ b/server/src/main/java/com/cloud/server/ConfigurationServerImpl.java @@ -1067,6 +1067,12 @@ public void doInTransactionWithoutResult(TransactionStatus status) { Network.GuestType.Isolated, true, false, false, false, true, false); defaultIsolatedSourceNatEnabledNetworkOffering.setState(NetworkOffering.State.Enabled); + // Default egress policy is Allow on fresh installations, consistent with the + // createNetworkOffering API default (egressdefaultpolicy=true when not specified). + // Existing installations are not affected: this method only runs on first boot + // (guarded by the "init" configuration flag) and persistDefaultNetworkOffering() + // never updates an already existing offering. + defaultIsolatedSourceNatEnabledNetworkOffering.setEgressDefaultPolicy(true); defaultIsolatedSourceNatEnabledNetworkOffering.setSupportsVmAutoScaling(true); defaultIsolatedSourceNatEnabledNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultIsolatedSourceNatEnabledNetworkOffering); @@ -1084,6 +1090,9 @@ public void doInTransactionWithoutResult(TransactionStatus status) { false, true, null, null, true, Availability.Optional, null, Network.GuestType.Isolated, true, true, false, false, false, false); defaultIsolatedEnabledNetworkOffering.setState(NetworkOffering.State.Enabled); + // This offering carries no Firewall service, so the flag is not enforced anywhere; + // it is set for consistency so API responses do not advertise a misleading Deny policy. + defaultIsolatedEnabledNetworkOffering.setEgressDefaultPolicy(true); defaultIsolatedEnabledNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(defaultIsolatedEnabledNetworkOffering); for (Service service : defaultIsolatedNetworkOfferingProviders.keySet()) { diff --git a/server/src/test/java/com/cloud/server/ConfigurationServerImplTest.java b/server/src/test/java/com/cloud/server/ConfigurationServerImplTest.java index fd1cb6a6313e..895caa36a4a3 100644 --- a/server/src/test/java/com/cloud/server/ConfigurationServerImplTest.java +++ b/server/src/test/java/com/cloud/server/ConfigurationServerImplTest.java @@ -23,6 +23,8 @@ import com.cloud.dc.dao.VlanDao; import com.cloud.domain.dao.DomainDao; import com.cloud.network.dao.NetworkDao; +import com.cloud.offering.NetworkOffering; +import com.cloud.offerings.NetworkOfferingVO; import com.cloud.offerings.dao.NetworkOfferingDao; import com.cloud.offerings.dao.NetworkOfferingServiceMapDao; import com.cloud.service.dao.ServiceOfferingDao; @@ -35,12 +37,16 @@ import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; +import java.util.HashMap; +import java.util.Map; + @RunWith(MockitoJUnitRunner.class) public class ConfigurationServerImplTest { @@ -122,4 +128,37 @@ public void testUpdateSystemvmPassword() { //teardown System.setProperty("user.name", realusername); } + + @Test + public void testCreateDefaultNetworkOfferingsSeedsIsolatedOfferingsWithEgressAllow() { + Mockito.when(_networkOfferingDao.persistDefaultNetworkOffering(Mockito.any(NetworkOfferingVO.class))) + .thenAnswer(invocation -> invocation.getArgument(0)); + + try (TransactionLegacy txn = TransactionLegacy.open("testCreateDefaultNetworkOfferings")) { + configurationServer.createDefaultNetworkOfferings(); + } + + ArgumentCaptor captor = ArgumentCaptor.forClass(NetworkOfferingVO.class); + Mockito.verify(_networkOfferingDao, Mockito.atLeastOnce()).persistDefaultNetworkOffering(captor.capture()); + + Map offeringsByName = new HashMap<>(); + for (NetworkOfferingVO offering : captor.getAllValues()) { + offeringsByName.putIfAbsent(offering.getUniqueName(), offering); + } + + NetworkOfferingVO isolatedSourceNatOffering = offeringsByName.get(NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService); + Assert.assertNotNull(isolatedSourceNatOffering); + Assert.assertTrue("Built-in Isolated source-NAT offering must be seeded with egress default policy Allow on fresh installations", + isolatedSourceNatOffering.isEgressDefaultPolicy()); + + NetworkOfferingVO isolatedOffering = offeringsByName.get(NetworkOffering.DefaultIsolatedNetworkOffering); + Assert.assertNotNull(isolatedOffering); + Assert.assertTrue("Built-in Isolated (no source-NAT) offering must be seeded with egress default policy Allow on fresh installations", + isolatedOffering.isEgressDefaultPolicy()); + + NetworkOfferingVO sharedOffering = offeringsByName.get(NetworkOffering.DefaultSharedNetworkOffering); + Assert.assertNotNull(sharedOffering); + Assert.assertFalse("Built-in Shared offering seeding must remain unchanged", + sharedOffering.isEgressDefaultPolicy()); + } } diff --git a/ui/src/views/offering/AddNetworkOffering.vue b/ui/src/views/offering/AddNetworkOffering.vue index 995b81ce68c6..81b602f6bfb0 100644 --- a/ui/src/views/offering/AddNetworkOffering.vue +++ b/ui/src/views/offering/AddNetworkOffering.vue @@ -704,7 +704,7 @@ export default { isolation: 'dedicated', conservemode: true, availability: 'optional', - egressdefaultpolicy: 'deny', + egressdefaultpolicy: 'allow', ispublic: this.isPublic, nsxsupportlb: true, routingmode: 'static'