This commit is contained in:
Abhishek Kumar 2026-03-09 13:14:41 +00:00 committed by GitHub
commit 1fb000a063
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 99 additions and 0 deletions

View File

@ -18,6 +18,8 @@ package com.cloud.cluster;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
@ -26,6 +28,7 @@ import java.util.List;
import javax.net.ssl.SSLContext;
import org.apache.cloudstack.framework.ca.CAService;
import org.apache.cloudstack.utils.server.ServerPropertiesUtil;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
@ -41,6 +44,7 @@ import org.apache.logging.log4j.Logger;
import com.cloud.utils.HttpUtils;
import com.cloud.utils.Profiler;
import com.cloud.utils.StringUtils;
import com.cloud.utils.nio.Link;
import com.google.gson.Gson;
@ -162,6 +166,23 @@ public class ClusterServiceServletImpl implements ClusterService {
return result;
}
protected InetAddress getBindAddressIfAvailable() {
String bindAddressStr = ServerPropertiesUtil.getProperty("bind.interface");
InetAddress bindAddress = null;
if (StringUtils.isBlank(bindAddressStr) ||
"0.0.0.0".equalsIgnoreCase(bindAddressStr) ||
"::".equals(bindAddressStr)) {
return bindAddress;
}
try {
bindAddress = InetAddress.getByName(bindAddressStr);
} catch (UnknownHostException e) {
logger.error("Unable to resolve bind address: {}", bindAddressStr, e);
throw new RuntimeException(e);
}
return bindAddress;
}
private CloseableHttpClient getHttpClient() {
if (s_client == null) {
SSLContext sslContext = null;
@ -172,7 +193,9 @@ public class ClusterServiceServletImpl implements ClusterService {
}
int timeout = ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000;
InetAddress bindAddress = getBindAddressIfAvailable();
RequestConfig config = RequestConfig.custom()
.setLocalAddress(bindAddress)
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout).build();

View File

@ -17,15 +17,19 @@
package com.cloud.cluster;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Optional;
import org.apache.cloudstack.utils.server.ServerPropertiesUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.NameValuePair;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
@ -61,4 +65,68 @@ public class ClusterServiceServletImplTest {
val = opt.get();
Assert.assertEquals(peer, val.getValue());
}
@Test
public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsValid() {
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn("127.0.0.1");
InetAddress result = clusterServiceServlet.getBindAddressIfAvailable();
Assert.assertNotNull(result);
Assert.assertEquals("127.0.0.1", result.getHostAddress());
} catch (RuntimeException e) {
Assert.fail("Unexpected RuntimeException: " + e.getMessage());
}
}
@Test
public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsHostname() {
String hostname;
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostname = "localhost";
}
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn(hostname);
InetAddress result = clusterServiceServlet.getBindAddressIfAvailable();
Assert.assertNotNull(result);
InetAddress address = InetAddress.getByName(hostname);
Assert.assertEquals(address, result);
} catch (UnknownHostException | RuntimeException e) {
Assert.fail("Unexpected RuntimeException: " + e.getMessage());
}
}
private void runBindAddressIfAvailableForNullResult(String bindAddress) {
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn(bindAddress);
InetAddress result = clusterServiceServlet.getBindAddressIfAvailable();
Assert.assertNull(result);
} catch (RuntimeException e) {
Assert.fail("Unexpected RuntimeException: " + e.getMessage());
}
}
@Test
public void getBindAddressIfAvailable_returnsInetAddress_whenWildcard() {
runBindAddressIfAvailableForNullResult("0.0.0.0");
}
@Test
public void getBindAddressIfAvailable_returnsInetAddress_whenIp6Wildcard() {
runBindAddressIfAvailableForNullResult("::");
}
@Test
public void getBindAddressIfAvailable_returnsNull_whenBindAddressIsBlank() {
runBindAddressIfAvailableForNullResult("");
}
@Test(expected = RuntimeException.class)
public void getBindAddressIfAvailable_throwsRuntimeException_whenBindAddressIsInvalid() throws RuntimeException {
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn("invalid-address");
clusterServiceServlet.getBindAddressIfAvailable();
}
}
}

View File

@ -55,4 +55,12 @@ public class ServerPropertiesUtil {
}
return tempProps.getProperty(name);
}
public static String getProperty(String name, String defaultValue) {
String value = getProperty(name);
if (value == null) {
value = defaultValue;
}
return value;
}
}