Skip to content

Commit 8d3ec34

Browse files
Check a new rule's timeouts before the rule is created
createLoadBalancerRule stored the per rule settings only after the rule had been created, so a refused keepalivetimeout or idletimeout left the rule behind in state Add. It held its port, and the next apply on the network pushed it to the router. Check the timeouts first, so a refused create leaves nothing. Also refuse a timeout above 2147483647 ms, which HAProxy rejects as a timer overflow; it used to reach the router and fail the whole apply. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent 3f69e75 commit 8d3ec34

5 files changed

Lines changed: 90 additions & 4 deletions

File tree

‎api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ LoadBalancer createPublicLoadBalancerRule(String xId, String name, String descri
7878
*/
7979
boolean updateLoadBalancerConnectionSettings(long lbRuleId, Boolean keepAlive, Long idleTimeout, Long keepAliveTimeout, boolean cleanup);
8080

81+
/**
82+
* Checks the per rule timeouts without storing them, so a create can refuse a bad value
83+
* before the rule exists. A null is not checked.
84+
*/
85+
void validateLoadBalancerConnectionSettings(Long idleTimeout, Long keepAliveTimeout);
86+
8187
boolean deleteLoadBalancerRule(long lbRuleId, boolean apply);
8288

8389
/**

‎api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ public void execute() throws ResourceAllocationException, ResourceUnavailableExc
329329
@Override
330330
public void create() {
331331
try {
332+
// Check the settings before the rule exists: a refused create must leave no rule behind
333+
_lbService.validateLoadBalancerConnectionSettings(getIdleTimeout(), getKeepAliveTimeout());
332334
LoadBalancer result =
333335
_lbService.createPublicLoadBalancerRule(getXid(), getName(), getDescription(), getSourcePortStart(), getSourcePortEnd(), getDefaultPortStart(),
334336
getDefaultPortEnd(), getSourceIpAddressId(), getProtocol(), getAlgorithm(), getNetworkId(), getEntityOwnerId(), getOpenFirewall(), getLbProtocol(), isDisplay(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.user.loadbalancer;
18+
19+
import org.apache.cloudstack.api.ApiErrorCode;
20+
import org.apache.cloudstack.api.ServerApiException;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
import org.mockito.Mockito;
24+
import org.springframework.test.util.ReflectionTestUtils;
25+
26+
import com.cloud.exception.InvalidParameterValueException;
27+
import com.cloud.network.lb.LoadBalancingRulesService;
28+
29+
public class CreateLoadBalancerRuleCmdTest {
30+
31+
@Test
32+
public void testCreateRefusesBadTimeoutBeforeTheRuleExists() throws Exception {
33+
LoadBalancingRulesService lbService = Mockito.mock(LoadBalancingRulesService.class);
34+
Mockito.doThrow(new InvalidParameterValueException("keepalivetimeout must be 0 or greater"))
35+
.when(lbService).validateLoadBalancerConnectionSettings(null, -1L);
36+
37+
CreateLoadBalancerRuleCmd cmd = new CreateLoadBalancerRuleCmd();
38+
cmd._lbService = lbService;
39+
ReflectionTestUtils.setField(cmd, "keepAliveTimeout", -1L);
40+
41+
try {
42+
cmd.create();
43+
Assert.fail("A negative keepalivetimeout should be refused");
44+
} catch (ServerApiException e) {
45+
Assert.assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode());
46+
}
47+
Mockito.verify(lbService).validateLoadBalancerConnectionSettings(null, -1L);
48+
Mockito.verifyNoMoreInteractions(lbService);
49+
}
50+
}

‎server/src/main/java/com/cloud/network/lb/LoadBalancingRulesManagerImpl.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2302,19 +2302,32 @@ public List<LbDestination> getExistingDestinations(long lbId) {
23022302
}
23032303

23042304
/**
2305-
* Haproxy rejects a negative timeout, and a rejected file leaves every rule on the router
2306-
* running its previous config. Refuse the value here rather than let it reach the VR.
2305+
* Haproxy rejects a negative timeout, and one above 2147483647 ms ("timer overflow"). A
2306+
* rejected file leaves every rule on the router running its previous config, so refuse the
2307+
* value here rather than let it reach the VR.
23072308
*/
23082309
protected void validateConnectionTimeout(String name, Long value) {
2309-
if (value != null && value < 0) {
2310+
if (value == null) {
2311+
return;
2312+
}
2313+
if (value < 0) {
23102314
throw new InvalidParameterValueException(String.format("%s must be 0 or greater, got [%s]. 0 means no timeout.", name, value));
23112315
}
2316+
if (value > Integer.MAX_VALUE) {
2317+
throw new InvalidParameterValueException(String.format("%s must be at most %d milliseconds, the largest timeout HAProxy accepts, got [%s].",
2318+
name, Integer.MAX_VALUE, value));
2319+
}
23122320
}
23132321

23142322
@Override
2315-
public boolean updateLoadBalancerConnectionSettings(long lbRuleId, Boolean keepAlive, Long idleTimeout, Long keepAliveTimeout, boolean cleanup) {
2323+
public void validateLoadBalancerConnectionSettings(Long idleTimeout, Long keepAliveTimeout) {
23162324
validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, idleTimeout);
23172325
validateConnectionTimeout(ApiConstants.KEEPALIVE_TIMEOUT, keepAliveTimeout);
2326+
}
2327+
2328+
@Override
2329+
public boolean updateLoadBalancerConnectionSettings(long lbRuleId, Boolean keepAlive, Long idleTimeout, Long keepAliveTimeout, boolean cleanup) {
2330+
validateLoadBalancerConnectionSettings(idleTimeout, keepAliveTimeout);
23182331

23192332
Map<String, String> current = getConnectionSettings(lbRuleId);
23202333
Map<String, String> wanted = cleanup ? new HashMap<>() : new HashMap<>(current);

‎server/src/test/java/com/cloud/network/lb/LoadBalancingRulesManagerImplTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,21 @@ public void testValidateConnectionTimeoutRejectsNegative() {
415415
lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, -1L);
416416
}
417417

418+
@Test
419+
public void testValidateConnectionTimeoutAcceptsHaproxyMaximum() {
420+
lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, (long) Integer.MAX_VALUE);
421+
}
422+
423+
@Test(expected = InvalidParameterValueException.class)
424+
public void testValidateConnectionTimeoutRejectsAboveHaproxyMaximum() {
425+
lbr.validateConnectionTimeout(ApiConstants.IDLE_TIMEOUT, 99999999999L);
426+
}
427+
428+
@Test(expected = InvalidParameterValueException.class)
429+
public void testValidateLoadBalancerConnectionSettingsChecksKeepAliveTimeout() {
430+
lbr.validateLoadBalancerConnectionSettings(600000L, -1L);
431+
}
432+
418433
private void stubConnectionSettings(String keepAlive, String idleTimeout, String keepAliveTimeout) {
419434
stubConnectionSetting(LoadBalancer.KEEPALIVE, keepAlive);
420435
stubConnectionSetting(LoadBalancer.IDLE_TIMEOUT, idleTimeout);

0 commit comments

Comments
 (0)