From 7b881517b716e605b8436b3db964d7f83ad0d714 Mon Sep 17 00:00:00 2001 From: Spaceman1984 <49917670+Spaceman1984@users.noreply.github.com> Date: Wed, 15 Jul 2020 12:28:07 +0200 Subject: [PATCH] kvm: Fixed removal of hosts from certsmap when running certificate auto-renewal (#4156) When a host connects to a management server, the host IP address and the certificate are stored in memory on the management server. This mapping is checked periodically to determine if any certificates are due to expire. Before a certificate is renewed, a few checks are done to determine if the host is connected to the management server by fetching the host record from the database. The problem here is if the wrong record is fetched, the host is not checked for renewal. This PR improves the host record fetch from the database by looking only at hosts that are not removed. Fixes: #4129 --- .../main/java/com/cloud/host/dao/HostDao.java | 6 ++++++ .../java/com/cloud/host/dao/HostDaoImpl.java | 20 ++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/engine/schema/src/main/java/com/cloud/host/dao/HostDao.java b/engine/schema/src/main/java/com/cloud/host/dao/HostDao.java index ced19cecf79..88e8eaca95e 100644 --- a/engine/schema/src/main/java/com/cloud/host/dao/HostDao.java +++ b/engine/schema/src/main/java/com/cloud/host/dao/HostDao.java @@ -97,6 +97,12 @@ public interface HostDao extends GenericDao, StateDao listByType(Type type); + /** + * Finds a host by ip address, excludes removed hosts. + * + * @param ip The ip address to match on + * @return One matched host + */ HostVO findByIp(String ip); /** diff --git a/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java b/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java index ec4573faf22..d28357d39e7 100644 --- a/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java +++ b/engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java @@ -90,7 +90,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao protected SearchBuilder DcPrivateIpAddressSearch; protected SearchBuilder DcStorageIpAddressSearch; protected SearchBuilder PublicIpAddressSearch; - protected SearchBuilder AnyIpAddressSearch; + protected SearchBuilder UnremovedIpAddressSearch; protected SearchBuilder GuidSearch; protected SearchBuilder DcSearch; @@ -227,10 +227,12 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao PublicIpAddressSearch.and("publicIpAddress", PublicIpAddressSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ); PublicIpAddressSearch.done(); - AnyIpAddressSearch = createSearchBuilder(); - AnyIpAddressSearch.or("publicIpAddress", AnyIpAddressSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ); - AnyIpAddressSearch.or("privateIpAddress", AnyIpAddressSearch.entity().getPrivateIpAddress(), SearchCriteria.Op.EQ); - AnyIpAddressSearch.done(); + UnremovedIpAddressSearch = createSearchBuilder(); + UnremovedIpAddressSearch.and("removed", UnremovedIpAddressSearch.entity().getRemoved(), Op.NULL); // We don't want any removed hosts + UnremovedIpAddressSearch.and().op("publicIpAddress", UnremovedIpAddressSearch.entity().getPublicIpAddress(), SearchCriteria.Op.EQ); + UnremovedIpAddressSearch.or("privateIpAddress", UnremovedIpAddressSearch.entity().getPrivateIpAddress(), SearchCriteria.Op.EQ); + UnremovedIpAddressSearch.cp(); + UnremovedIpAddressSearch.done(); GuidSearch = createSearchBuilder(); GuidSearch.and("guid", GuidSearch.entity().getGuid(), SearchCriteria.Op.EQ); @@ -308,12 +310,6 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao UnmanagedDirectConnectSearch.and("lastPinged", UnmanagedDirectConnectSearch.entity().getLastPinged(), SearchCriteria.Op.LTEQ); UnmanagedDirectConnectSearch.and("resourceStates", UnmanagedDirectConnectSearch.entity().getResourceState(), SearchCriteria.Op.NIN); UnmanagedDirectConnectSearch.and("clusterIn", UnmanagedDirectConnectSearch.entity().getClusterId(), SearchCriteria.Op.IN); - /* - * UnmanagedDirectConnectSearch.op(SearchCriteria.Op.OR, "managementServerId", - * UnmanagedDirectConnectSearch.entity().getManagementServerId(), SearchCriteria.Op.EQ); - * UnmanagedDirectConnectSearch.and("lastPinged", UnmanagedDirectConnectSearch.entity().getLastPinged(), - * SearchCriteria.Op.LTEQ); UnmanagedDirectConnectSearch.cp(); UnmanagedDirectConnectSearch.cp(); - */ try { HostTransferSearch = _hostTransferDao.createSearchBuilder(); } catch (Throwable e) { @@ -1116,7 +1112,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao @Override public HostVO findByIp(final String ipAddress) { - SearchCriteria sc = AnyIpAddressSearch.create(); + SearchCriteria sc = UnremovedIpAddressSearch.create(); sc.setParameters("publicIpAddress", ipAddress); sc.setParameters("privateIpAddress", ipAddress); return findOneBy(sc);