Skip to content

Commit

Permalink
add athenz slack client impl
Browse files Browse the repository at this point in the history
Signed-off-by: craman <[email protected]>
  • Loading branch information
craman committed Jan 29, 2025
1 parent 3335c49 commit 3a79d5d
Show file tree
Hide file tree
Showing 19 changed files with 1,272 additions and 2 deletions.
1 change: 1 addition & 0 deletions actions/scripts/publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ else
deployProject "com.yahoo.athenz:athenz-syncer-common"
deployProject "com.yahoo.athenz:athenz-instance-provider"
deployProject "com.yahoo.athenz:athenz-server-msg-pulsar"
deployProject "com.yahoo.athenz:athenz-server-notification-slack"
fi
2 changes: 1 addition & 1 deletion libs/java/server_common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<packaging>jar</packaging>

<properties>
<code.coverage.min>0.9159</code.coverage.min>
<code.coverage.min>0.9155</code.coverage.min>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public enum Type {
GROUP_MEMBER_DECISION
}

public enum ChannelType {
EMAIL,
SLACK
}

// type of channel to send the notification, default is email
private ChannelType channelType = ChannelType.EMAIL;

// type of the notification
private final Type type;

Expand All @@ -48,6 +56,9 @@ public enum Type {
// Utility class to convert the notification into an email
private NotificationToEmailConverter notificationToEmailConverter;

// Utility class to convert the notification into a Slack message body
private NotificationToSlackMessageConverter notificationToSlackMessageConverter;

// Utility class to convert the notification into metric attributes
private NotificationToMetricConverter notificationToMetricConverter;

Expand All @@ -59,6 +70,15 @@ public Type getType() {
return type;
}

public ChannelType getChannelType() {
return channelType;
}

public Notification setChannelType(ChannelType channelType) {
this.channelType = channelType;
return this;
}

public Set<String> getRecipients() {
if (recipients == null) {
recipients = new HashSet<>();
Expand Down Expand Up @@ -108,6 +128,13 @@ public NotificationEmail getNotificationAsEmail() {
return null;
}

public NotificationSlackMessage getNotificationAsSlackMessage() {
if (notificationToSlackMessageConverter != null) {
return notificationToSlackMessageConverter.getNotificationAsSlackMessage(this);
}
return null;
}

public Notification setNotificationToMetricConverter(NotificationToMetricConverter notificationToMetricConverter) {
this.notificationToMetricConverter = notificationToMetricConverter;
return this;
Expand All @@ -120,6 +147,14 @@ public NotificationMetric getNotificationAsMetrics(Timestamp currentTime) {
return null;
}

public void setNotificationToSlackMessageConverter(NotificationToSlackMessageConverter notificationToSlackMessageConverter) {
this.notificationToSlackMessageConverter = notificationToSlackMessageConverter;
}

public NotificationToSlackMessageConverter getNotificationToSlackMessageConverter() {
return notificationToSlackMessageConverter;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -131,10 +166,12 @@ public boolean equals(Object o) {
Notification that = (Notification) o;
Timestamp currentTime = Timestamp.fromMillis(System.currentTimeMillis());
return getType() == that.getType() &&
getChannelType() == that.getChannelType() &&
Objects.equals(getRecipients(), that.getRecipients()) &&
Objects.equals(getDetails(), that.getDetails()) &&
Objects.equals(getNotificationAsMetrics(currentTime), that.getNotificationAsMetrics(currentTime)) &&
Objects.equals(getNotificationAsEmail(), that.getNotificationAsEmail());
Objects.equals(getNotificationAsEmail(), that.getNotificationAsEmail()) &&
Objects.equals(getNotificationAsSlackMessage(), that.getNotificationAsSlackMessage());
}

@Override
Expand All @@ -152,12 +189,19 @@ public String toString() {
if (notificationToMetricConverter != null) {
metricConverterClassName = notificationToMetricConverter.getClass().getName();
}

String slackConverterClassName = "";
if (notificationToSlackMessageConverter != null) {
slackConverterClassName = notificationToSlackMessageConverter.getClass().getName();
}
return "Notification{" +
"type=" + type +
"channelType=" + channelType +
", recipients=" + recipients +
", details=" + details +
", emailConverterClass=" + emailConverterClassName +
", metricConverterClass=" + metricConverterClassName +
", slackConverterClass=" + slackConverterClassName +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yahoo.athenz.common.server.notification;

import java.util.Objects;
import java.util.Set;

public class NotificationSlackMessage {

private final String message;
private final Set<String> recepients;

public NotificationSlackMessage(String message, Set<String> recepients) {
this.recepients = recepients;
this.message = message;
}

public String getMessage() {
return message;
}


public Set<String> getRecepients() {
return recepients;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
NotificationSlackMessage that = (NotificationSlackMessage) o;
return Objects.equals(getMessage(), that.getMessage()) &&
Objects.equals(getRecepients(), that.getRecepients());
}

@Override
public int hashCode() {
return Objects.hash(getMessage(), getRecepients());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The Athenz Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.yahoo.athenz.common.server.notification;

public interface NotificationToSlackMessageConverter {
/**
*
* @param notification object to be converted to slack message
* @return The slack block content for the given Notification
*/
NotificationSlackMessage getNotificationAsSlackMessage(Notification notification);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright The Athenz Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.yahoo.athenz.common.server.notification;

import org.testng.annotations.Test;

import java.util.*;

import static org.testng.Assert.*;

public class NotificationSlackMessageTest {

@Test
public void testNotificationSlackMessage() {

Set<String> recipients = new HashSet<>();
recipients.add("[email protected]");
NotificationSlackMessage slackMessage1 = new NotificationSlackMessage("message1", recipients);
NotificationSlackMessage slackMessage2 = new NotificationSlackMessage("message1", recipients);

assertEquals(slackMessage1.hashCode(), slackMessage2.hashCode());
assertEquals(slackMessage1.getMessage(), "message1");
assertEquals(slackMessage1.getRecepients(), recipients);

assertEquals(slackMessage1, slackMessage2);
assertEquals(slackMessage1, slackMessage1);

assertFalse(slackMessage1.equals(null));

slackMessage2 = new NotificationSlackMessage("message2", recipients);
assertNotEquals(slackMessage1, slackMessage2);

slackMessage2 = new NotificationSlackMessage("message1", recipients);
assertEquals(slackMessage1, slackMessage2);

Set<String> recipients2 = new HashSet<>();
recipients2.add("slack-channel");
slackMessage2 = new NotificationSlackMessage("message1", recipients2);
assertNotEquals(slackMessage1, slackMessage2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public void testNotificationMethods() {
detailsRes.put("domain", "dom1");
detailsRes.put("role", "role1");

obj.setChannelType(Notification.ChannelType.EMAIL);
assertTrue(obj.getChannelType().equals(Notification.ChannelType.EMAIL));

assertEquals(obj.getRecipients(), recipientsRes);
assertEquals(obj.getDetails(), detailsRes);

Expand All @@ -73,7 +76,10 @@ public void testNotificationMethods() {
assertTrue(obj.equals(obj3));

assertEquals(obj.hashCode(), obj3.hashCode());
obj3.setChannelType(Notification.ChannelType.SLACK);
assertTrue(obj3.getChannelType().equals(Notification.ChannelType.SLACK));

assertFalse(obj.equals(obj3));
Notification obj4 = new Notification(Notification.Type.ROLE_MEMBER_EXPIRY);
List<String> testlist = Arrays.asList("user.a", "user.a", "user.b");
obj4.getRecipients().addAll(testlist);
Expand Down
8 changes: 8 additions & 0 deletions libs/java/server_notification_slack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Athenz Server Notification Implementation using Slack
=============================================================

## License

Copyright The Athenz Authors

Licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)
92 changes: 92 additions & 0 deletions libs/java/server_notification_slack/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright The Athenz Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.yahoo.athenz</groupId>
<artifactId>athenz</artifactId>
<version>1.12.10-SNAPSHOT</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>athenz-server-notification-slack</artifactId>
<name>athenz-server-notification-slack</name>
<description>Athenz Server Notification Implementation using Slack</description>
<packaging>jar</packaging>

<properties>
<code.coverage.min>0.96</code.coverage.min>
</properties>

<dependencies>
<dependency>
<groupId>com.yahoo.athenz</groupId>
<artifactId>athenz-server-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.yahoo.athenz</groupId>
<artifactId>athenz-auth-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcutil-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.java.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.server.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.server.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>slack-api-client</artifactId>
<version>${slack.client.version}</version>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 3a79d5d

Please sign in to comment.