From 6038e89f262d863cddefc7c242c496a58360b154 Mon Sep 17 00:00:00 2001 From: Alexander Verhaar Date: Wed, 19 Aug 2020 19:29:44 +0200 Subject: [PATCH] Add algorithm checks in lb api calls --- .../src/main/java/com/cloud/utils/net/NetUtils.java | 2 +- .../user/loadbalancer/CreateLoadBalancerRuleCmd.java | 8 ++++++-- .../user/loadbalancer/UpdateLoadBalancerRuleCmd.java | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cosmic-common/src/main/java/com/cloud/utils/net/NetUtils.java b/cosmic-common/src/main/java/com/cloud/utils/net/NetUtils.java index c601a0a0df..cf2d94ed4c 100644 --- a/cosmic-common/src/main/java/com/cloud/utils/net/NetUtils.java +++ b/cosmic-common/src/main/java/com/cloud/utils/net/NetUtils.java @@ -791,7 +791,7 @@ public static boolean isValidPort(final String p) { } public static boolean isValidAlgorithm(final String p) { - final String algo = p.toLowerCase(); + final String algo = p; return algo.equals("roundrobin") || algo.equals("leastconn") || algo.equals("source"); } diff --git a/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java b/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java index b2ec8aa90a..b16437342e 100644 --- a/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java +++ b/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java @@ -122,7 +122,7 @@ public class CreateLoadBalancerRuleCmd extends BaseAsyncCreateCmd /*implements L ///////////////////////////////////////////////////// public String getAlgorithm() { - return algorithm; + return algorithm.toLowerCase(); } public String getDescription() { @@ -284,10 +284,14 @@ public void create() { throw new InvalidParameterValueException( "Only TCP protocol is supported because HAProxy can only do TCP."); } + if (getAlgorithm() != null && !NetUtils.isValidAlgorithm(getAlgorithm())) { + throw new InvalidParameterValueException("Only source/roundrobin/leastconn are supported loadbalance algorithms."); + } try { final LoadBalancer result = _lbService.createPublicLoadBalancerRule(getXid(), getName(), getDescription(), getSourcePortStart(), getSourcePortEnd(), getDefaultPortStart(), - getDefaultPortEnd(), getSourceIpAddressId(), getProtocol(), getAlgorithm(), getNetworkId(), getEntityOwnerId(), getOpenFirewall(), getLbProtocol(), + getDefaultPortEnd(), getSourceIpAddressId(), getProtocol(), getAlgorithm(), getNetworkId(), getEntityOwnerId(), getOpenFirewall(), + getLbProtocol(), isDisplay(), getClientTimeout(), getServerTimeout()); this.setEntityId(result.getId()); this.setEntityUuid(result.getUuid()); diff --git a/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java b/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java index ad5962d49f..6f0af218d1 100644 --- a/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java +++ b/cosmic-core/api/src/main/java/com/cloud/api/command/user/loadbalancer/UpdateLoadBalancerRuleCmd.java @@ -17,6 +17,7 @@ import com.cloud.legacymodel.network.FirewallRule; import com.cloud.legacymodel.network.LoadBalancer; import com.cloud.legacymodel.user.Account; +import com.cloud.utils.net.NetUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -131,6 +132,9 @@ public Long getId() { @Override public void execute() { + if (algorithm != null && !NetUtils.isValidAlgorithm(algorithm)) { + throw new InvalidParameterValueException("Only source/roundrobin/leastconn are supported loadbalance algorithms."); + } CallContext.current().setEventDetails("Load balancer ID: " + getId()); final LoadBalancer result = _lbService.updateLoadBalancerRule(this); if (result != null) {