Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<NetworkOfferingVO> captor = ArgumentCaptor.forClass(NetworkOfferingVO.class);
Mockito.verify(_networkOfferingDao, Mockito.atLeastOnce()).persistDefaultNetworkOffering(captor.capture());

Map<String, NetworkOfferingVO> 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());
}
}
2 changes: 1 addition & 1 deletion ui/src/views/offering/AddNetworkOffering.vue
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ export default {
isolation: 'dedicated',
conservemode: true,
availability: 'optional',
egressdefaultpolicy: 'deny',
egressdefaultpolicy: 'allow',
ispublic: this.isPublic,
nsxsupportlb: true,
routingmode: 'static'
Expand Down
Loading