Skip to content

Commit

Permalink
[Imporvement][Notification] Improve dingtalk alert to avoid sending f…
Browse files Browse the repository at this point in the history
…ailure due to token expired (#456)
  • Loading branch information
leeoo authored Sep 10, 2024
1 parent 8698483 commit 67174d3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.HttpEntity;
Expand All @@ -33,7 +34,13 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -46,6 +53,7 @@ public class DingTalkSender {

private String msgType;
private String webHook;
private String secret;
private String keyWord;

private String mustNotNull = " must not be null";
Expand All @@ -59,8 +67,8 @@ public DingTalkSender(SlaSenderMessage senderMessage) {

webHook=config.get("webHook");
requireNonNull(webHook, "dingtalk webHook" + mustNotNull);
secret=config.get("secret");
keyWord=config.get("keyWord");
requireNonNull(keyWord, "dingtalk keyWord" + mustNotNull);

}

Expand All @@ -74,7 +82,8 @@ public SlaNotificationResultRecord sendCardMsg(Set<ReceiverConfig> receiverSet,
for(ReceiverConfig receiverConfig : receiverSet){
try {
String msg = generateMsgJson(subject, message, receiverConfig);
HttpPost httpPost = constructHttpPost(webHook, msg);
String url = constructUrl();
HttpPost httpPost = constructHttpPost(url, msg);
CloseableHttpClient httpClient = getDefaultClient();
try {
CloseableHttpResponse response = httpClient.execute(httpPost);
Expand Down Expand Up @@ -105,6 +114,22 @@ public SlaNotificationResultRecord sendCardMsg(Set<ReceiverConfig> receiverSet,
}
return result;
}

private String constructUrl() throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
if (org.apache.commons.lang3.StringUtils.isBlank(secret)) {
return webHook;
}
Long timestamp = System.currentTimeMillis();
String stringToSign = timestamp + "\n" + secret;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");

// sign字段和timestamp字段必须拼接到请求URL上,否则会出现 310000 的错误信息
return String.format(webHook + "&sign=%s&timestamp=%d", sign, timestamp);
}

private String generateMsgJson(String title, String content,ReceiverConfig receiverConfig) {

final String atMobiles = receiverConfig.getAtMobiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ public String getConfigSenderJson() {
InputParam webHook = InputParam.newBuilder("webHook", "webHook")
.addValidate(Validate.newBuilder().setRequired(true).build())
.build();
InputParam secret = InputParam.newBuilder("secret", "secret")
.addValidate(Validate.newBuilder().setRequired(false).build())
.build();
InputParam keyWord = InputParam.newBuilder("keyWord", "keyWord")
.addValidate(Validate.newBuilder().setRequired(true).build())
.addValidate(Validate.newBuilder().setRequired(false).build())
.build();

paramsList.add(webHook);
paramsList.add(secret);
paramsList.add(keyWord);

ObjectMapper mapper = new ObjectMapper();
Expand Down

0 comments on commit 67174d3

Please sign in to comment.