diff --git a/server/src/main/java/com/cloud/network/router/NetworkHelperImpl.java b/server/src/main/java/com/cloud/network/router/NetworkHelperImpl.java index 4baa19f8f845..92253a6d76e7 100644 --- a/server/src/main/java/com/cloud/network/router/NetworkHelperImpl.java +++ b/server/src/main/java/com/cloud/network/router/NetworkHelperImpl.java @@ -945,26 +945,8 @@ public boolean validateHAProxyLBRule(final LoadBalancingRule rule) { } } else if (LbStickinessMethod.StickinessMethodType.AppCookieBased.getName().equalsIgnoreCase(stickinessPolicy.getMethodName())) { - String length = null; // optional - String holdTime = null; // optional - - for (final Pair paramKV : paramsList) { - final String key = paramKV.first(); - final String value = paramKV.second(); - if ("length".equalsIgnoreCase(key)) { - length = value; - } - if ("holdtime".equalsIgnoreCase(key)) { - holdTime = value; - } - } - - if (length != null && !containsOnlyNumbers(length, null)) { - throw new InvalidParameterValueException(String.format("Failed LB in validation rule id: %s Cause: length is not a number: %s", rule.getLb(), length)); - } - if (holdTime != null && !containsOnlyNumbers(holdTime, timeEndChar) && !containsOnlyNumbers(holdTime, null)) { - throw new InvalidParameterValueException(String.format("Failed LB in validation rule id: %s Cause: holdtime is not in timeformat: %s", rule.getLb(), holdTime)); - } + throw new InvalidParameterValueException(String.format("Failed LB in validation rule: %s Cause: AppCookie based stickiness " + + "(HAProxy 'appsession') is not supported on a Virtual Router, as this directive was removed in HAProxy 1.6", rule.getLb())); } } return true; diff --git a/server/src/test/java/com/cloud/network/router/NetworkHelperImplTest.java b/server/src/test/java/com/cloud/network/router/NetworkHelperImplTest.java index 4237ef7f6f65..9680b3cebe22 100644 --- a/server/src/test/java/com/cloud/network/router/NetworkHelperImplTest.java +++ b/server/src/test/java/com/cloud/network/router/NetworkHelperImplTest.java @@ -29,7 +29,15 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.cloud.configuration.Config; +import com.cloud.exception.InvalidParameterValueException; +import com.cloud.network.lb.LoadBalancingRule; +import com.cloud.network.rules.LbStickinessMethod; +import com.cloud.network.rules.LoadBalancer; +import com.cloud.utils.Pair; +import com.cloud.utils.net.Ip; import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; import org.apache.cloudstack.network.router.deployment.RouterDeploymentDefinition; import org.junit.Before; import org.junit.Test; @@ -57,6 +65,10 @@ import com.cloud.vm.dao.DomainRouterDao; import com.cloud.vm.dao.NicDao; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + @RunWith(MockitoJUnitRunner.class) public class NetworkHelperImplTest { @@ -91,10 +103,15 @@ public class NetworkHelperImplTest { @Mock private VMTemplateVO template; + @Mock + private ConfigurationDao configDao; + @Before public void setUp() { nwHelper._networkDao = networkDao; nwHelper._networkModel = networkModel; + nwHelper._configDao = configDao; + lenient().when(configDao.getValue(Config.NetworkLBHaproxyStatsPort.key())).thenReturn("8081"); when(template.getId()).thenReturn(1L); when(template.isDynamicallyScalable()).thenReturn(true); when(virtualProvider.getId()).thenReturn(1L); @@ -268,4 +285,33 @@ public void testUpdateDomainRouter() { assertEquals(Hypervisor.HypervisorType.KVM, result.getHypervisorType()); assertTrue(result.isDynamicallyScalable()); } + + @Test(expected = InvalidParameterValueException.class) + public void testValidateHAProxyLBRuleRejectsAppCookieStickiness() { + LoadBalancer lb = mock(LoadBalancer.class); + when(lb.getSourcePortStart()).thenReturn(80); + + List> params = new ArrayList<>(); + params.add(new Pair<>("cookie-name", "JSESSIONID")); + LoadBalancingRule.LbStickinessPolicy stickinessPolicy = + new LoadBalancingRule.LbStickinessPolicy(LbStickinessMethod.StickinessMethodType.AppCookieBased.getName(), params); + LoadBalancingRule rule = new LoadBalancingRule(lb, new ArrayList<>(), Collections.singletonList(stickinessPolicy), new ArrayList<>(), mock(Ip.class)); + + nwHelper.validateHAProxyLBRule(rule); + } + + @Test + public void testValidateHAProxyLBRuleAllowsSourceBasedStickiness() { + LoadBalancer lb = mock(LoadBalancer.class); + when(lb.getSourcePortStart()).thenReturn(80); + + List> params = new ArrayList<>(); + params.add(new Pair<>("tablesize", "200k")); + params.add(new Pair<>("expire", "30m")); + LoadBalancingRule.LbStickinessPolicy stickinessPolicy = + new LoadBalancingRule.LbStickinessPolicy(LbStickinessMethod.StickinessMethodType.SourceBased.getName(), params); + LoadBalancingRule rule = new LoadBalancingRule(lb, new ArrayList<>(), Collections.singletonList(stickinessPolicy), new ArrayList<>(), mock(Ip.class)); + + assertTrue(nwHelper.validateHAProxyLBRule(rule)); + } }