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
Expand Up @@ -18,6 +18,8 @@

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;
Expand All @@ -26,6 +28,7 @@
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;
Expand All @@ -41,6 +44,7 @@

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;

Expand Down Expand Up @@ -162,6 +166,23 @@ private String executePostMethod(final CloseableHttpClient client, final HttpPos
return result;
}

protected InetAddress getBindAddressIfAvailable() {
String bindAddressStr = ServerPropertiesUtil.getProperty("bind.interface");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bind.interface is a bit misleading, maybe bind.address ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I see
let's keep the name

can you update the description in server.properties ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the javadoc

https://javadoc.jetty.org/jetty-12/org/eclipse/jetty/server/AbstractNetworkConnector.html#setHost(java.lang.String)

image

so IP/hostname/null/0.0.0.0 are valid values. maybe we need to consider them ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IP/null/hostname should have been okay. I've added check for wildcards

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;
Expand All @@ -172,7 +193,9 @@ private CloseableHttpClient getHttpClient() {
}

int timeout = ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000;
InetAddress bindAddress = getBindAddressIfAvailable();
RequestConfig config = RequestConfig.custom()
.setLocalAddress(bindAddress)
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,4 +65,68 @@ public void testPingPostParameters() {
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public static String getProperty(String name) {
}
return tempProps.getProperty(name);
}

public static String getProperty(String name, String defaultValue) {
String value = getProperty(name);
if (value == null) {
value = defaultValue;
}
return value;
}
}
Loading