Search in sources :

Example 16 with Notification

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

the class AWSZTSHealthNotificationTaskTest method testGetEmailBody.

@Test
public void testGetEmailBody() {
    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");
    Map<String, String> details = new HashMap<>();
    details.put(NOTIFICATION_DETAILS_AFFECTED_ZTS, "affected zts");
    details.put(NOTIFICATION_DETAILS_AWS_ZTS_HEALTH, "zts.url;domain0;role0;Sun Mar 15 15:08:07 IST 2020;Error message");
    Notification notification = new Notification();
    notification.setDetails(details);
    AWSZTSHealthNotificationTask.AWSZTSHealthNotificationToEmailConverter converter = new AWSZTSHealthNotificationTask.AWSZTSHealthNotificationToEmailConverter(new NotificationToEmailConverterCommon(null));
    NotificationEmail notificationAsEmail = converter.getNotificationAsEmail(notification);
    String body = notificationAsEmail.getBody();
    assertNotNull(body);
    assertTrue(body.contains("zts.url"));
    assertTrue(body.contains("domain0"));
    assertTrue(body.contains("role0"));
    assertTrue(body.contains("Sun Mar 15 15:08:07 IST 2020"));
    assertTrue(body.contains("Error message"));
    // Make sure support text and url do appear
    assertTrue(body.contains("slack"));
    assertTrue(body.contains("link.to.athenz.channel.com"));
    System.clearProperty("athenz.notification_workflow_url");
    System.clearProperty("notification_support_text");
    System.clearProperty("notification_support_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) ZTSClientNotification(com.yahoo.athenz.zts.ZTSClientNotification) Test(org.testng.annotations.Test)

Example 17 with Notification

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

the class AWSZTSHealthNotificationTaskTest method testGetNotifications.

@Test
public void testGetNotifications() {
    System.setProperty(ZTSConsts.ZTS_PROP_NOTIFICATION_AWS_HEALTH_DOMAIN, "testDomain");
    ZTSClientNotification clientNotification = new ZTSClientNotification("zts.url", "role", "AWS", 1592346376, false, "testDomain");
    RoleMember roleMember1 = new RoleMember();
    roleMember1.setMemberName("user.test1");
    RoleMember roleMember2 = new RoleMember();
    roleMember2.setMemberName("user.test2");
    List<RoleMember> roleMembers = new ArrayList<>();
    roleMembers.add(roleMember1);
    roleMembers.add(roleMember2);
    Role adminRole = new Role();
    adminRole.setRoleMembers(roleMembers);
    adminRole.setName("testDomain:role.admin");
    List<Role> roles = new ArrayList<>();
    roles.add(adminRole);
    when(dataStore.getRolesByDomain(eq("testDomain"))).thenReturn(roles);
    AWSZTSHealthNotificationTask awsztsHealthNotificationTask = new AWSZTSHealthNotificationTask(clientNotification, dataStore, userDomainPrefix, serverName, notificationToEmailConverterCommon);
    List<Notification> notifications = awsztsHealthNotificationTask.getNotifications();
    assertEquals(1, notifications.size());
    assertTrue(notifications.get(0).getRecipients().contains("user.test1"));
    assertTrue(notifications.get(0).getRecipients().contains("user.test2"));
    Timestamp expiration = Timestamp.fromMillis(clientNotification.getExpiration() * 1000);
    assertEquals("zts.url;testDomain;role;" + expiration + ";Fail to get token of type AWS. ", notifications.get(0).getDetails().get("awsZtsHealth"));
    assertEquals("testServer", notifications.get(0).getDetails().get("affectedZts"));
    System.clearProperty(ZTSConsts.ZTS_PROP_NOTIFICATION_AWS_HEALTH_DOMAIN);
}
Also used : Role(com.yahoo.athenz.zms.Role) ZTSClientNotification(com.yahoo.athenz.zts.ZTSClientNotification) Timestamp(com.yahoo.rdl.Timestamp) RoleMember(com.yahoo.athenz.zms.RoleMember) Notification(com.yahoo.athenz.common.server.notification.Notification) ZTSClientNotification(com.yahoo.athenz.zts.ZTSClientNotification) Test(org.testng.annotations.Test)

Example 18 with Notification

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

the class MetricNotificationServiceTest method testNotify.

@Test
public void testNotify() {
    Metric metric = Mockito.mock(Metric.class);
    MetricNotificationService metricNotificationService = new MetricNotificationService(metric);
    String[] attributesList1 = new String[] { "key1", "attribute11", "key2", "attribute12", "key3", "attribute13" };
    String[] attributesList2 = new String[] { "key1", "attribute21", "key2", "attribute22", "key3", "attribute23" };
    List<String[]> attributes = new ArrayList<>();
    attributes.add(attributesList1);
    attributes.add(attributesList2);
    NotificationToMetricConverter notificationToMetricConverter = Mockito.mock(NotificationToMetricConverter.class);
    Mockito.when(notificationToMetricConverter.getNotificationAsMetrics(Mockito.any(), Mockito.any())).thenReturn(new NotificationMetric(attributes));
    Notification notification = new Notification();
    notification.setNotificationToMetricConverter(notificationToMetricConverter);
    boolean notify = metricNotificationService.notify(notification);
    assertTrue(notify);
    ArgumentCaptor<String> captorMetric = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String[]> captorAttributes = ArgumentCaptor.forClass(String[].class);
    Mockito.verify(metric, Mockito.times(2)).increment(captorMetric.capture(), captorAttributes.capture());
    assertEquals(2, captorMetric.getAllValues().size());
    assertEquals("athenz_notification", captorMetric.getAllValues().get(0));
    assertEquals("athenz_notification", captorMetric.getAllValues().get(1));
    // Mockito captures all varargs arguments in a single array
    assertEquals(12, captorAttributes.getAllValues().size());
    List<String> expectedAttributes = new ArrayList<String>(Arrays.asList("key1", "attribute11", "key2", "attribute12", "key3", "attribute13", "key1", "attribute21", "key2", "attribute22", "key3", "attribute23"));
    assertEquals(expectedAttributes, captorAttributes.getAllValues());
}
Also used : NotificationMetric(com.yahoo.athenz.common.server.notification.NotificationMetric) Notification(com.yahoo.athenz.common.server.notification.Notification) NotificationToMetricConverter(com.yahoo.athenz.common.server.notification.NotificationToMetricConverter) NotificationMetric(com.yahoo.athenz.common.server.notification.NotificationMetric) Metric(com.yahoo.athenz.common.metrics.Metric) Test(org.testng.annotations.Test)

Example 19 with Notification

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

the class RoleMemberNotificationCommonTest method testExpiryPrincipalGetNotificationDetails.

@Test
public void testExpiryPrincipalGetNotificationDetails() {
    DBService dbsvc = Mockito.mock(DBService.class);
    RoleMemberNotificationCommon roleMemberNotificationCommon = new RoleMemberNotificationCommon(dbsvc, USER_DOMAIN_PREFIX);
    NotificationToEmailConverterCommon notificationToEmailConverterCommon = new NotificationToEmailConverterCommon(null);
    // Verify no notification for member without member roles
    DomainRoleMember roleMember = new DomainRoleMember();
    roleMember.setMemberName("user.joe");
    Map<String, DomainRoleMember> members = new HashMap<>();
    members.put("user.joe", roleMember);
    List<Notification> notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
    assertEquals(0, notification.size());
    // Verify the same result when setting the memberRoles to an empty collection
    roleMember.setMemberRoles(Collections.emptyList());
    notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
    assertEquals(0, notification.size());
    final Timestamp expirationTs = Timestamp.fromMillis(100);
    final Timestamp reviewTs = Timestamp.fromMillis(50);
    List<MemberRole> memberRoles = new ArrayList<>();
    memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz1").setMemberName("user.joe").setExpiration(expirationTs).setReviewReminder(reviewTs));
    roleMember.setMemberRoles(memberRoles);
    notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
    assertEquals(1, notification.size());
    assertEquals(2, notification.get(0).getDetails().size());
    assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs);
    assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe");
    memberRoles.add(new MemberRole().setRoleName("role1").setDomainName("athenz2").setMemberName("user.joe").setExpiration(expirationTs).setReviewReminder(reviewTs));
    memberRoles.add(new MemberRole().setRoleName("role2").setDomainName("athenz2").setMemberName("user.joe").setExpiration(expirationTs).setReviewReminder(reviewTs));
    notification = roleMemberNotificationCommon.getNotificationDetails(members, new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToEmailConverter(notificationToEmailConverterCommon), new RoleMemberExpiryNotificationTask.ExpiryRoleMemberDetailStringer(), new RoleMemberExpiryNotificationTask.RoleExpiryPrincipalNotificationToMetricConverter(), new RoleMemberExpiryNotificationTask.RoleExpiryDomainNotificationToMetricConverter(), memberRole -> DisableNotificationEnum.getEnumSet(0));
    assertEquals(1, notification.size());
    assertEquals(2, notification.get(0).getDetails().size());
    assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs + "|athenz2;role1;" + expirationTs + "|athenz2;role2;" + expirationTs);
    assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_MEMBER), "user.joe");
    assertEquals(notification.get(0).getDetails().get(NOTIFICATION_DETAILS_ROLES_LIST), "athenz1;role1;" + expirationTs + "|athenz2;role1;" + expirationTs + "|athenz2;role2;" + expirationTs);
}
Also used : Notification(com.yahoo.athenz.common.server.notification.Notification) Mockito(org.mockito.Mockito) java.util(java.util) Timestamp(com.yahoo.rdl.Timestamp) NotificationServiceConstants(com.yahoo.athenz.common.server.notification.NotificationServiceConstants) com.yahoo.athenz.zms(com.yahoo.athenz.zms) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Test(org.testng.annotations.Test) NotificationToEmailConverterCommon(com.yahoo.athenz.common.server.notification.NotificationToEmailConverterCommon) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) USER_DOMAIN_PREFIX(com.yahoo.athenz.common.ServerCommonConsts.USER_DOMAIN_PREFIX) Timestamp(com.yahoo.rdl.Timestamp) Notification(com.yahoo.athenz.common.server.notification.Notification) NotificationToEmailConverterCommon(com.yahoo.athenz.common.server.notification.NotificationToEmailConverterCommon) Test(org.testng.annotations.Test)

Example 20 with Notification

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

the class ZMSNotificationsTest method testGroupExpiryNotification.

@Test
public void testGroupExpiryNotification() {
    TopLevelDomain dom1 = zmsTestInitializer.createTopLevelDomainObject("test-domain1", "Test Domain1", "testOrg", zmsTestInitializer.getAdminUser());
    zmsTestInitializer.getZms().postTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), zmsTestInitializer.getAuditRef(), dom1);
    try {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp currentTimestamp = Timestamp.fromMillis(currentTimeMillis);
        List<GroupMember> groupMembers = generateGroupMembers(currentTimeMillis);
        Group group1 = zmsTestInitializer.createGroupObject("test-domain1", "Group1", groupMembers);
        zmsTestInitializer.getZms().putGroup(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", "Group1", zmsTestInitializer.getAuditRef(), group1);
        GroupMemberExpiryNotificationTask groupMemberExpiryNotificationTask = new GroupMemberExpiryNotificationTask(zmsTestInitializer.getZms().dbService, zmsTestInitializer.getZms().userDomainPrefix, zmsTestInitializer.getZms().notificationToEmailConverterCommon);
        List<Notification> notifications = groupMemberExpiryNotificationTask.getNotifications();
        // Email notifications should be sent every 7 days
        Set<String> emailNotificationMembers = new HashSet<>(Arrays.asList(new String[] { "user.expireddays0", "user.expireddays1", "user.expireddays7", "user.expireddays14", "user.expireddays21", "user.expireddays28" }));
        assertEquals(notifications.size(), 7, "notificationRecipients: " + notificationsToRecipientString(notifications));
        for (Notification notification : notifications) {
            String recipient = notification.getRecipients().stream().findFirst().get();
            if (recipient.equals("user.testadminuser")) {
                verifyAdminNotifications(emailNotificationMembers, notification);
            } else {
                if (emailNotificationMembers.contains(recipient)) {
                    assertNotNull(notification.getNotificationAsEmail());
                } else {
                    assertNull(notification.getNotificationAsEmail());
                }
                assertNotNull(notification.getNotificationAsMetrics(currentTimestamp));
            }
        }
    } finally {
        zmsTestInitializer.getZms().deleteTopLevelDomain(zmsTestInitializer.getMockDomRsrcCtx(), "test-domain1", zmsTestInitializer.getAuditRef());
    }
}
Also used : GroupMemberExpiryNotificationTask(com.yahoo.athenz.zms.notification.GroupMemberExpiryNotificationTask) Timestamp(com.yahoo.rdl.Timestamp) Notification(com.yahoo.athenz.common.server.notification.Notification)

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