4343import javax .net .ssl .SSLEngine ;
4444
4545import com .cloud .resource .ResourceState ;
46+ import com .cloud .utils .StringUtils ;
4647import org .apache .cloudstack .ca .CAManager ;
4748import org .apache .cloudstack .framework .config .ConfigDepot ;
4849import org .apache .cloudstack .framework .config .ConfigKey ;
@@ -541,14 +542,9 @@ public SocketChannel connectToPeer(final String peerName, final SocketChannel pr
541542 logger .info ("Unable to find peer: {}" , peerName );
542543 return null ;
543544 }
544- final String ip = ms .getServiceIP ();
545- InetAddress addr ;
546- int port = Port .value ();
547- try {
548- addr = InetAddress .getByName (ip );
549- } catch (final UnknownHostException e ) {
550- throw new CloudRuntimeException ("Unable to resolve " + ip );
551- }
545+ final String hostName = getHostIdentifier (ms );
546+ final int port = Port .value ();
547+ final InetAddress addr = resolveManagementServerAddress (ms );
552548 SocketChannel ch1 = null ;
553549 try {
554550 ch1 = SocketChannel .open (new InetSocketAddress (addr , port ));
@@ -557,20 +553,20 @@ public SocketChannel connectToPeer(final String peerName, final SocketChannel pr
557553 ch1 .socket ().setSoTimeout (60 * 1000 );
558554 try {
559555 SSLContext sslContext = Link .initManagementSSLContext (caService );
560- sslEngine = sslContext .createSSLEngine (ip , port );
556+ sslEngine = sslContext .createSSLEngine (hostName , port );
561557 sslEngine .setUseClientMode (true );
562558 sslEngine .setEnabledProtocols (SSLUtils .getSupportedProtocols (sslEngine .getEnabledProtocols ()));
563559 sslEngine .beginHandshake ();
564560 if (!Link .doHandshake (ch1 , sslEngine )) {
565561 ch1 .close ();
566- throw new IOException (String .format ("SSL: Handshake failed with peer management server '%s' on %s:%d " , peerName , ip , port ));
562+ throw new IOException (String .format ("SSL: Handshake failed with peer management server '%s' on %s:%d " , peerName , hostName , port ));
567563 }
568- logger .info ("SSL: Handshake done with peer management server '{}' on {}:{} " , peerName , ip , port );
564+ logger .info ("SSL: Handshake done with peer management server '{}' on {}:{} " , peerName , hostName , port );
569565 } catch (final Exception e ) {
570566 ch1 .close ();
571567 throw new IOException ("SSL: Fail to init SSL! " + e );
572568 }
573- logger .debug ("Connection to peer opened: {}, IP : {}" , peerName , ip );
569+ logger .debug ("Connection to peer opened: {}, host : {}" , peerName , hostName );
574570 _peers .put (peerName , ch1 );
575571 _sslEngines .put (peerName , sslEngine );
576572 return ch1 ;
@@ -582,7 +578,7 @@ public SocketChannel connectToPeer(final String peerName, final SocketChannel pr
582578 logger .error ("failed to close failed peer socket: {}" , ex );
583579 }
584580 }
585- logger .warn ("Unable to connect to peer management server: {}, IP {} due to {}" , peerName , ip , e .getMessage (), e );
581+ logger .warn ("Unable to connect to peer management server: {}, host {} due to {}" , peerName , hostName , e .getMessage (), e );
586582 return null ;
587583 }
588584 }
@@ -592,6 +588,38 @@ public SocketChannel connectToPeer(final String peerName, final SocketChannel pr
592588 }
593589 }
594590
591+ /**
592+ * Gets the hostname or IP address to use for connecting to a management server.
593+ * Prefers hostname (for CNAME support) but falls back to IP if hostname is null.
594+ *
595+ * @param host the management server host
596+ * @return hostname if available, otherwise service IP
597+ * @throws CloudRuntimeException if both hostname and IP are null
598+ */
599+ private String getHostIdentifier (final ManagementServerHost host ) {
600+ final String hostName = host .getName ();
601+ if (StringUtils .isNotBlank (hostName )) {
602+ return hostName ;
603+ }
604+
605+ final String serviceIP = host .getServiceIP ();
606+ if (StringUtils .isNotBlank (serviceIP )) {
607+ return serviceIP ;
608+ }
609+
610+ throw new CloudRuntimeException ("Management server host has neither hostname nor IP address: " + host );
611+ }
612+
613+ private InetAddress resolveManagementServerAddress (final ManagementServerHost host ) {
614+ final String addressToResolve = getHostIdentifier (host );
615+
616+ try {
617+ return InetAddress .getByName (addressToResolve );
618+ } catch (final UnknownHostException e ) {
619+ throw new CloudRuntimeException ("Unable to resolve " + addressToResolve , e );
620+ }
621+ }
622+
595623 public SocketChannel connectToPeer (final long hostId , final SocketChannel prevCh ) {
596624 final String peerName = getPeerName (hostId );
597625 if (peerName == null ) {
0 commit comments