Skip to content

Commit e1b6093

Browse files
authored
add 'management.network.cidr' ip to mgmt certificate (#7728)
1 parent f090c77 commit e1b6093

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

plugins/ca/root-ca/src/main/java/org/apache/cloudstack/ca/provider/RootCAProvider.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.io.StringReader;
2222
import java.math.BigInteger;
2323
import java.net.InetAddress;
24+
import java.net.NetworkInterface;
25+
import java.net.SocketException;
2426
import java.security.InvalidKeyException;
2527
import java.security.KeyManagementException;
2628
import java.security.KeyPair;
@@ -37,6 +39,8 @@
3739
import java.security.spec.InvalidKeySpecException;
3840
import java.util.ArrayList;
3941
import java.util.Collections;
42+
import java.util.Enumeration;
43+
import java.util.HashSet;
4044
import java.util.List;
4145
import java.util.Map;
4246

@@ -49,6 +53,7 @@
4953
import javax.net.ssl.TrustManagerFactory;
5054
import javax.xml.bind.DatatypeConverter;
5155

56+
import com.cloud.configuration.Config;
5257
import org.apache.cloudstack.ca.CAManager;
5358
import org.apache.cloudstack.framework.ca.CAProvider;
5459
import org.apache.cloudstack.framework.ca.Certificate;
@@ -365,8 +370,12 @@ private boolean loadManagementKeyStore() {
365370
if (managementKeyStore != null) {
366371
return true;
367372
}
368-
final Certificate serverCertificate = issueCertificate(Collections.singletonList(NetUtils.getHostName()),
369-
NetUtils.getAllDefaultNicIps(), getCaValidityDays());
373+
List<String> nicIps = NetUtils.getAllDefaultNicIps();
374+
addConfiguredManagementIp(nicIps);
375+
nicIps = new ArrayList<>(new HashSet<>(nicIps));
376+
377+
final Certificate serverCertificate = issueCertificate(Collections.singletonList(NetUtils.getHostName()), nicIps, getCaValidityDays());
378+
370379
if (serverCertificate == null || serverCertificate.getPrivateKey() == null) {
371380
throw new CloudRuntimeException("Failed to generate management server certificate and load management server keystore");
372381
}
@@ -384,6 +393,28 @@ private boolean loadManagementKeyStore() {
384393
return managementKeyStore != null;
385394
}
386395

396+
protected void addConfiguredManagementIp(List<String> ipList) {
397+
String msNetworkCidr = configDao.getValue(Config.ManagementNetwork.key());
398+
try {
399+
LOG.debug(String.format("Trying to find management IP in CIDR range [%s].", msNetworkCidr));
400+
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
401+
402+
networkInterfaces.asIterator().forEachRemaining(networkInterface -> {
403+
networkInterface.getInetAddresses().asIterator().forEachRemaining(inetAddress -> {
404+
if (NetUtils.isIpWithInCidrRange(inetAddress.getHostAddress(), msNetworkCidr)) {
405+
ipList.add(inetAddress.getHostAddress());
406+
LOG.debug(String.format("Added IP [%s] to the list of IPs in the management server's certificate.", inetAddress.getHostAddress()));
407+
}
408+
});
409+
});
410+
} catch (SocketException e) {
411+
String msg = "Exception while trying to gather the management server's network interfaces.";
412+
LOG.error(msg, e);
413+
throw new CloudRuntimeException(msg, e);
414+
}
415+
}
416+
417+
387418
private boolean setupCA() {
388419
if (!loadRootCAKeyPair() && !saveNewRootCAKeypair()) {
389420
LOG.error("Failed to save and load root CA keypair");

0 commit comments

Comments
 (0)