2121import java .io .StringReader ;
2222import java .math .BigInteger ;
2323import java .net .InetAddress ;
24+ import java .net .NetworkInterface ;
25+ import java .net .SocketException ;
2426import java .security .InvalidKeyException ;
2527import java .security .KeyManagementException ;
2628import java .security .KeyPair ;
3739import java .security .spec .InvalidKeySpecException ;
3840import java .util .ArrayList ;
3941import java .util .Collections ;
42+ import java .util .Enumeration ;
43+ import java .util .HashSet ;
4044import java .util .List ;
4145import java .util .Map ;
4246
4953import javax .net .ssl .TrustManagerFactory ;
5054import javax .xml .bind .DatatypeConverter ;
5155
56+ import com .cloud .configuration .Config ;
5257import org .apache .cloudstack .ca .CAManager ;
5358import org .apache .cloudstack .framework .ca .CAProvider ;
5459import 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