CertFailedRefreshNotificationTask implements NotificationTask { @Override public List<Notification> getNotifications() { if (providers == null || providers.isEmpty()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No configured providers"); } return new ArrayList<>(); } List<X509CertRecord> unrefreshedCerts = new ArrayList<>(); for (String provider : providers) { unrefreshedCerts.addAll(instanceCertManager.getUnrefreshedCertsNotifications(serverName, provider)); } if (unrefreshedCerts.isEmpty()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No unrefreshed certificates available to send notifications"); } return new ArrayList<>(); } List<X509CertRecord> unrefreshedCertsValidServices = getRecordsWithValidServices(unrefreshedCerts); if (unrefreshedCertsValidServices.isEmpty()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No unrefreshed certificates with configured services available to send notifications"); } return new ArrayList<>(); } List<X509CertRecord> unrefreshedCertsValidHosts = getRecordsWithValidHosts(unrefreshedCertsValidServices); if (unrefreshedCertsValidHosts.isEmpty()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("No unrefreshed certificates with valid hosts available to send notifications"); } return new ArrayList<>(); } Map<String, List<X509CertRecord>> domainToCertRecordsMap = getDomainToCertRecordsMap(unrefreshedCertsValidHosts); return generateNotificationsForAdmins(domainToCertRecordsMap); } CertFailedRefreshNotificationTask(InstanceCertManager instanceCertManager,
                                             DataStore dataStore,
                                             HostnameResolver hostnameResolver,
                                             String userDomainPrefix,
                                             String serverName,
                                             int httpsPort); @Override List<Notification> getNotifications(); @Override String getDescription();  }
@Test public void testNoProviders() { Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); for (int i = 0; i < 6; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); } Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(0, notifications.size()); }
@Test public void testSeveralProviders() { Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); Mockito.when(hostnameResolver.isValidHostname(anyString())).thenReturn(true); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider1, provider2, provider3"); for (int i = 0; i < 7; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); } records.get(0).setProvider("provider1"); records.get(1).setProvider("provider2"); records.get(2).setProvider("provider2"); records.get(3).setProvider("provider3"); records.get(4).setProvider("provider3"); records.get(5).setProvider("provider3"); records.get(6).setProvider("providerNotInProperty"); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("provider1"))) .thenReturn(records.subList(0, 1)); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("provider2"))) .thenReturn(records.subList(1, 3)); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("provider3"))) .thenReturn(records.subList(3, 6)); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), eq("providerNotInProperty"))) .thenReturn(records.subList(6, 7)); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(6, notifications.size()); notifications.sort(Comparator.comparing(notif -> notif.getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS))); String expectedDetail = "service0;provider1;instanceID0;" + new Timestamp(currentDate.getTime()) + ";;hostName0"; assertEquals(expectedDetail, notifications.get(0).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); expectedDetail = "service1;provider2;instanceID1;" + new Timestamp(currentDate.getTime()) + ";;hostName1"; assertEquals(expectedDetail, notifications.get(1).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); expectedDetail = "service2;provider2;instanceID2;" + new Timestamp(currentDate.getTime()) + ";;hostName2"; assertEquals(expectedDetail, notifications.get(2).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); expectedDetail = "service3;provider3;instanceID3;" + new Timestamp(currentDate.getTime()) + ";;hostName3"; assertEquals(expectedDetail, notifications.get(3).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); expectedDetail = "service4;provider3;instanceID4;" + new Timestamp(currentDate.getTime()) + ";;hostName4"; assertEquals(expectedDetail, notifications.get(4).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); expectedDetail = "service5;provider3;instanceID5;" + new Timestamp(currentDate.getTime()) + ";;hostName5"; assertEquals(expectedDetail, notifications.get(5).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); }
@Test public void testValidHosts() { Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider"); boolean isValidHost = true; for (int i = 0; i < 6; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); Mockito.when(hostnameResolver.isValidHostname(eq("hostName" + i))).thenReturn(isValidHost); isValidHost = !isValidHost; } X509CertRecord record = getMockX509CertRecord(currentDate, 7); record.setHostName(null); records.add(record); NotificationTestsCommon.mockDomainData(7, dataStore); Mockito.when(hostnameResolver.isValidHostname(eq(null))).thenReturn(true); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(4, notifications.size()); assertEquals("domain7", notifications.get(0).getDetails().get("domain")); assertEquals("domain4", notifications.get(1).getDetails().get("domain")); assertEquals("domain2", notifications.get(2).getDetails().get("domain")); assertEquals("domain0", notifications.get(3).getDetails().get("domain")); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); }
@Test public void testNoValidHosts() { Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider"); boolean isValidHost = true; for (int i = 0; i < 6; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); Mockito.when(hostnameResolver.isValidHostname(eq("hostName" + i))).thenReturn(false); } Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(new ArrayList<>(), notifications); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); }
@Test public void testNoValidRecipient() { Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider"); X509CertRecord record = getMockX509CertRecord(currentDate, 1); records.add(record); String domainName = "domain1"; Mockito.when(dataStore.getRolesByDomain(eq(domainName))).thenReturn(new ArrayList<>()); Mockito.when(hostnameResolver.isValidHostname(eq("hostName1"))).thenReturn(true); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(new ArrayList<>(), notifications); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); }
@Test public void testValidServices() { String globStrings = "domain0.service0, " + "???????.service1, " + "domain4.????????, "; System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_IGNORED_SERVICES_LIST, globStrings); Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider"); for (int i = 0; i < 6; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); Mockito.when(hostnameResolver.isValidHostname(eq("hostName" + i))).thenReturn(true); } Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(3, notifications.size()); assertEquals("domain5", notifications.get(0).getDetails().get("domain")); assertEquals("domain2", notifications.get(1).getDetails().get("domain")); assertEquals("domain3", notifications.get(2).getDetails().get("domain")); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_IGNORED_SERVICES_LIST); }
@Test public void testNoValidServices() { String globStrings = "*"; System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_IGNORED_SERVICES_LIST, globStrings); Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider"); for (int i = 0; i < 6; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); Mockito.when(hostnameResolver.isValidHostname(eq("hostName" + i))).thenReturn(true); } Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(new ArrayList<>(), notifications); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_IGNORED_SERVICES_LIST); }
@Test public void testValidProvidersNoUnrefreshedCerts() { System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider1, provider2, provider3"); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(new ArrayList<>()); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(new ArrayList<>(), notifications); }
@Test public void testNotificationsByDomain() { Date currentDate = new Date(); List<X509CertRecord> records = new ArrayList<>(); System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider"); for (int i = 0; i < 6; ++i) { X509CertRecord record = getMockX509CertRecord(currentDate, i); records.add(record); NotificationTestsCommon.mockDomainData(i, dataStore); } Mockito.when(hostnameResolver.isValidHostname(any())).thenReturn(true); X509CertRecord recordDomain0 = getMockX509CertRecord(currentDate, 0); recordDomain0.setHostName("secondHostName0"); X509CertRecord recordDomain5 = getMockX509CertRecord(currentDate, 5); recordDomain5.setHostName("secondHostName5"); records.add(recordDomain0); records.add(recordDomain5); Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records); CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask( instanceCertManager, dataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort); List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications(); assertEquals(6, notifications.size()); String twoRecordsDomain5 = "service5;provider;instanceID5;" + new Timestamp(currentDate.getTime()) + ";;hostName5|" + "service5;provider;instanceID5;" + new Timestamp(currentDate.getTime()) + ";;secondHostName5"; assertEquals(twoRecordsDomain5, notifications.get(1).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); String twoRecordsDomain0 = "service0;provider;instanceID0;" + new Timestamp(currentDate.getTime()) + ";;hostName0|" + "service0;provider;instanceID0;" + new Timestamp(currentDate.getTime()) + ";;secondHostName0"; assertEquals(twoRecordsDomain0, notifications.get(4).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); String oneRecordDomain1 = "service1;provider;instanceID1;" + new Timestamp(currentDate.getTime()) + ";;hostName1"; assertEquals(oneRecordDomain1, notifications.get(5).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS)); System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST); }