Search in sources :

Example 11 with Notification

use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.

the class CertFailedRefreshNotificationTaskTest method testNoValidRecipient.

@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, notificationToEmailConverterCommon);
    List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications();
    assertEquals(new ArrayList<>(), notifications);
    System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST);
}
Also used : X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Notification(com.yahoo.athenz.common.server.notification.Notification) Test(org.testng.annotations.Test)

Example 12 with Notification

use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.

the class CertFailedRefreshNotificationTaskTest method testAllRecordsSnoozed.

@Test
public void testAllRecordsSnoozed() {
    Date currentDate = new Date();
    List<X509CertRecord> records = new ArrayList<>();
    System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider");
    DataStore snoozedDataStore = Mockito.mock(DataStore.class);
    for (int i = 0; i < 8; ++i) {
        X509CertRecord record = getMockX509CertRecord(currentDate, i);
        records.add(record);
        NotificationTestsCommon.mockDomainData(i, snoozedDataStore);
        DomainData domainData = new DomainData();
        // Make all domains snooze
        Map<String, TagValueList> tags = new HashMap<>();
        tags.put("zts.DisableCertRefreshNotifications", new TagValueList().setList(Arrays.asList("true")));
        domainData.setTags(tags);
        Mockito.when(snoozedDataStore.getDomainData("domain" + i)).thenReturn(domainData);
        Mockito.when(hostnameResolver.isValidHostname(eq("hostName" + i))).thenReturn(true);
    }
    Mockito.when(instanceCertManager.getUnrefreshedCertsNotifications(eq(serverName), anyString())).thenReturn(records);
    CertFailedRefreshNotificationTask certFailedRefreshNotificationTask = new CertFailedRefreshNotificationTask(instanceCertManager, snoozedDataStore, hostnameResolver, userDomainPrefix, serverName, httpsPort, notificationToEmailConverterCommon);
    List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications();
    assertEquals(new ArrayList<>(), notifications);
    System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST);
}
Also used : DomainData(com.yahoo.athenz.zms.DomainData) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Notification(com.yahoo.athenz.common.server.notification.Notification) DataStore(com.yahoo.athenz.zts.store.DataStore) TagValueList(com.yahoo.athenz.zms.TagValueList) Test(org.testng.annotations.Test)

Example 13 with Notification

use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.

the class CertFailedRefreshNotificationTaskTest method testNotificationsByDomain.

@Test
public void testNotificationsByDomain() {
    Date currentDate = new Date();
    List<X509CertRecord> records = new ArrayList<>();
    System.setProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST, "provider");
    // Create 6 records, each in it's own domain (domain0, domain1... domain5)
    for (int i = 0; i < 6; ++i) {
        X509CertRecord record = getMockX509CertRecord(currentDate, i);
        records.add(record);
        NotificationTestsCommon.mockDomainData(i, dataStore);
    }
    // Now add one record to domain 0 and one record to domain 5
    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, notificationToEmailConverterCommon);
    List<Notification> notifications = certFailedRefreshNotificationTask.getNotifications();
    assertEquals(6, notifications.size());
    // Assert 2 records for domain5 and domain0:
    String twoRecordsDomain5 = "service5;provider;instanceID5;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName5|" + "service5;provider;instanceID5;" + Timestamp.fromMillis(currentDate.getTime()) + ";;secondHostName5";
    assertEquals(twoRecordsDomain5, notifications.get(1).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
    String twoRecordsDomain0 = "service0;provider;instanceID0;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName0|" + "service0;provider;instanceID0;" + Timestamp.fromMillis(currentDate.getTime()) + ";;secondHostName0";
    assertEquals(twoRecordsDomain0, notifications.get(4).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
    // Assert other domains only have 1 record:
    String oneRecordDomain1 = "service1;provider;instanceID1;" + Timestamp.fromMillis(currentDate.getTime()) + ";;hostName1";
    assertEquals(oneRecordDomain1, notifications.get(5).getDetails().get(NOTIFICATION_DETAILS_UNREFRESHED_CERTS));
    System.clearProperty(ZTS_PROP_NOTIFICATION_CERT_FAIL_PROVIDER_LIST);
}
Also used : X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Notification(com.yahoo.athenz.common.server.notification.Notification) Test(org.testng.annotations.Test)

Example 14 with Notification

use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.

the class CertFailedRefreshNotificationTaskTest method testGetEmailBodyMultipleRecords.

@Test
public void testGetEmailBodyMultipleRecords() {
    System.setProperty("athenz.notification_workflow_url", "https://athenz.example.com/workflow");
    System.setProperty("athenz.notification_support_text", "#Athenz slack channel");
    System.setProperty("athenz.notification_support_url", "https://link.to.athenz.channel.com");
    System.setProperty("athenz.notification_athenz_ui_url", "https://ui-athenz.example.com/");
    Map<String, String> details = new HashMap<>();
    details.put("domain", "dom1");
    details.put(NOTIFICATION_DETAILS_UNREFRESHED_CERTS, "service0;provider;instanceID0;Sun Mar 15 15:08:07 IST 2020;;hostName0|" + // bad entry with missing provider
    "bad;instanceID0;Sun Mar 15 15:08:07 IST 2020;;hostBad|" + "service0;provider;instanceID0;Sun Mar 15 15:08:07 IST 2020;;secondHostName0");
    Notification notification = new Notification();
    notification.setDetails(details);
    CertFailedRefreshNotificationTask.CertFailedRefreshNotificationToEmailConverter converter = new CertFailedRefreshNotificationTask.CertFailedRefreshNotificationToEmailConverter(serverName, httpsPort, new NotificationToEmailConverterCommon(null));
    NotificationEmail notificationAsEmail = converter.getNotificationAsEmail(notification);
    String body = notificationAsEmail.getBody();
    assertNotNull(body);
    assertTrue(body.contains(htmlSeveralRecords));
    // make sure the bad entries are not included
    assertFalse(body.contains("bad"));
    assertFalse(body.contains("hostBad"));
    System.clearProperty("athenz.notification_workflow_url");
    System.clearProperty("notification_support_text");
    System.clearProperty("notification_support_url");
    System.clearProperty("athenz.notification_athenz_ui_url");
}
Also used : NotificationEmail(com.yahoo.athenz.common.server.notification.NotificationEmail) NotificationToEmailConverterCommon(com.yahoo.athenz.common.server.notification.NotificationToEmailConverterCommon) Notification(com.yahoo.athenz.common.server.notification.Notification) Test(org.testng.annotations.Test)

Example 15 with Notification

use of com.yahoo.athenz.common.server.notification.Notification in project athenz by yahoo.

the class CertFailedRefreshNotificationTaskTest method testGetNotificationAsMetric.

@Test
public void testGetNotificationAsMetric() {
    Timestamp currentTimeStamp = Timestamp.fromCurrentTime();
    Timestamp fiveDaysAgo = ZTSTestUtils.addDays(currentTimeStamp, -5);
    Timestamp twentyFiveDaysFromNow = ZTSTestUtils.addDays(currentTimeStamp, 25);
    Map<String, String> details = new HashMap<>();
    details.put("domain", "dom1");
    details.put(NOTIFICATION_DETAILS_UNREFRESHED_CERTS, "service0;provider0;instanceID0;" + fiveDaysAgo.toString() + ";" + twentyFiveDaysFromNow + ";hostName1|" + "service1;provider1;instanceID1;" + fiveDaysAgo.toString() + ";" + twentyFiveDaysFromNow + ";hostName2");
    Notification notification = new Notification();
    notification.setDetails(details);
    CertFailedRefreshNotificationTask.CertFailedRefreshNotificationToMetricConverter converter = new CertFailedRefreshNotificationTask.CertFailedRefreshNotificationToMetricConverter();
    NotificationMetric notificationAsMetrics = converter.getNotificationAsMetrics(notification, currentTimeStamp);
    String[] expectedRecord1 = new String[] { METRIC_NOTIFICATION_TYPE_KEY, "cert_fail_refresh", METRIC_NOTIFICATION_DOMAIN_KEY, "dom1", METRIC_NOTIFICATION_SERVICE_KEY, "service0", METRIC_NOTIFICATION_PROVIDER_KEY, "provider0", METRIC_NOTIFICATION_INSTANCE_ID_KEY, "instanceID0", METRIC_NOTIFICATION_UPDATE_DAYS_KEY, "-5", METRIC_NOTIFICATION_EXPIRY_DAYS_KEY, "25" };
    String[] expectedRecord2 = new String[] { METRIC_NOTIFICATION_TYPE_KEY, "cert_fail_refresh", METRIC_NOTIFICATION_DOMAIN_KEY, "dom1", METRIC_NOTIFICATION_SERVICE_KEY, "service1", METRIC_NOTIFICATION_PROVIDER_KEY, "provider1", METRIC_NOTIFICATION_INSTANCE_ID_KEY, "instanceID1", METRIC_NOTIFICATION_UPDATE_DAYS_KEY, "-5", METRIC_NOTIFICATION_EXPIRY_DAYS_KEY, "25" };
    List<String[]> expectedAttributes = new ArrayList<>();
    expectedAttributes.add(expectedRecord1);
    expectedAttributes.add(expectedRecord2);
    assertEquals(new NotificationMetric(expectedAttributes), notificationAsMetrics);
}
Also used : NotificationMetric(com.yahoo.athenz.common.server.notification.NotificationMetric) Timestamp(com.yahoo.rdl.Timestamp) Notification(com.yahoo.athenz.common.server.notification.Notification) Test(org.testng.annotations.Test)

Aggregations

Notification (com.yahoo.athenz.common.server.notification.Notification)30 Test (org.testng.annotations.Test)22 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)10 Timestamp (com.yahoo.rdl.Timestamp)9 NotificationToEmailConverterCommon (com.yahoo.athenz.common.server.notification.NotificationToEmailConverterCommon)8 NotificationEmail (com.yahoo.athenz.common.server.notification.NotificationEmail)6 NotificationMetric (com.yahoo.athenz.common.server.notification.NotificationMetric)4 NotificationServiceConstants (com.yahoo.athenz.common.server.notification.NotificationServiceConstants)4 GroupMemberExpiryNotificationTask (com.yahoo.athenz.zms.notification.GroupMemberExpiryNotificationTask)4 ZTSClientNotification (com.yahoo.athenz.zts.ZTSClientNotification)4 java.util (java.util)4 Mockito (org.mockito.Mockito)4 AssertJUnit.assertEquals (org.testng.AssertJUnit.assertEquals)4 USER_DOMAIN_PREFIX (com.yahoo.athenz.common.ServerCommonConsts.USER_DOMAIN_PREFIX)3 com.yahoo.athenz.zms (com.yahoo.athenz.zms)3 DomainData (com.yahoo.athenz.zms.DomainData)3 TagValueList (com.yahoo.athenz.zms.TagValueList)3 DataStore (com.yahoo.athenz.zts.store.DataStore)3 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)3 AssertJUnit.assertTrue (org.testng.AssertJUnit.assertTrue)3