Skip to content

Commit cc858f3

Browse files
committed
handle wildcards
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent fdb4eba commit cc858f3

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

framework/cluster/src/main/java/com/cloud/cluster/ClusterServiceServletImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,13 @@ private String executePostMethod(final CloseableHttpClient client, final HttpPos
169169
protected InetAddress getBindAddressIfAvailable() {
170170
String bindAddressStr = ServerPropertiesUtil.getProperty("bind.interface");
171171
InetAddress bindAddress = null;
172+
if (StringUtils.isBlank(bindAddressStr) ||
173+
"0.0.0.0".equalsIgnoreCase(bindAddressStr) ||
174+
"::".equals(bindAddressStr)) {
175+
return bindAddress;
176+
}
172177
try {
173-
if (StringUtils.isNotBlank(bindAddressStr)) {
174-
bindAddress = InetAddress.getByName(bindAddressStr);
175-
}
178+
bindAddress = InetAddress.getByName(bindAddressStr);
176179
} catch (UnknownHostException e) {
177180
logger.error("Unable to resolve bind address: {}", bindAddressStr, e);
178181
throw new RuntimeException(e);

framework/cluster/src/test/java/com/cloud/cluster/ClusterServiceServletImplTest.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.cloud.cluster;
1919

2020
import java.net.InetAddress;
21+
import java.net.UnknownHostException;
2122
import java.util.List;
2223
import java.util.Optional;
2324

@@ -69,9 +70,7 @@ public void testPingPostParameters() {
6970
public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsValid() {
7071
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
7172
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn("127.0.0.1");
72-
7373
InetAddress result = clusterServiceServlet.getBindAddressIfAvailable();
74-
7574
Assert.assertNotNull(result);
7675
Assert.assertEquals("127.0.0.1", result.getHostAddress());
7776
} catch (RuntimeException e) {
@@ -80,23 +79,53 @@ public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsValid(
8079
}
8180

8281
@Test
83-
public void getBindAddressIfAvailable_returnsNull_whenBindAddressIsBlank() {
82+
public void getBindAddressIfAvailable_returnsInetAddress_whenBindAddressIsHostname() {
83+
String hostname;
84+
try {
85+
hostname = InetAddress.getLocalHost().getHostName();
86+
} catch (UnknownHostException e) {
87+
hostname = "localhost";
88+
}
8489
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
85-
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn("");
86-
90+
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn(hostname);
8791
InetAddress result = clusterServiceServlet.getBindAddressIfAvailable();
92+
Assert.assertNotNull(result);
93+
InetAddress address = InetAddress.getByName(hostname);
94+
Assert.assertEquals(address, result);
95+
} catch (UnknownHostException | RuntimeException e) {
96+
Assert.fail("Unexpected RuntimeException: " + e.getMessage());
97+
}
98+
}
8899

100+
private void runBindAddressIfAvailableForNullResult(String bindAddress) {
101+
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
102+
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn(bindAddress);
103+
InetAddress result = clusterServiceServlet.getBindAddressIfAvailable();
89104
Assert.assertNull(result);
90105
} catch (RuntimeException e) {
91106
Assert.fail("Unexpected RuntimeException: " + e.getMessage());
92107
}
93108
}
94109

110+
@Test
111+
public void getBindAddressIfAvailable_returnsInetAddress_whenWildcard() {
112+
runBindAddressIfAvailableForNullResult("0.0.0.0");
113+
}
114+
115+
@Test
116+
public void getBindAddressIfAvailable_returnsInetAddress_whenIp6Wildcard() {
117+
runBindAddressIfAvailableForNullResult("::");
118+
}
119+
120+
@Test
121+
public void getBindAddressIfAvailable_returnsNull_whenBindAddressIsBlank() {
122+
runBindAddressIfAvailableForNullResult("");
123+
}
124+
95125
@Test(expected = RuntimeException.class)
96126
public void getBindAddressIfAvailable_throwsRuntimeException_whenBindAddressIsInvalid() throws RuntimeException {
97127
try (MockedStatic<ServerPropertiesUtil> ignored = Mockito.mockStatic(ServerPropertiesUtil.class)) {
98128
Mockito.when(ServerPropertiesUtil.getProperty("bind.interface")).thenReturn("invalid-address");
99-
100129
clusterServiceServlet.getBindAddressIfAvailable();
101130
}
102131
}

0 commit comments

Comments
 (0)