Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAl
Properties props = sender.getJavaMailProperties();
props.put("mail.smtp.ssl.enable", emailNoticeSenderConfig.isEmailSsl());
props.put("mail.smtp.starttls.enable", emailNoticeSenderConfig.isEmailStarttls());
applySslCertVerify(props, emailNoticeSenderConfig.isEmailSslCertVerify());
fromUsername = emailNoticeSenderConfig.getEmailUsername();
useDatabase = true;
}
Expand All @@ -111,6 +112,7 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAl
Properties props = sender.getJavaMailProperties();
props.put("mail.smtp.ssl.enable", sslEnable);
props.put("mail.smtp.starttls.enable", starttlsEnable);
applySslCertVerify(props, true);
}
} catch (Exception e) {
log.error("Type not found {}", e.getMessage());
Expand All @@ -133,6 +135,17 @@ public void send(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAl
}
}

// the sender is a singleton, so both branches must set the props to avoid stale state
private void applySslCertVerify(Properties props, boolean verify) {
if (verify) {
props.remove("mail.smtp.ssl.trust");
props.remove("mail.smtp.ssl.checkserveridentity");
} else {
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.ssl.checkserveridentity", "false");
}
}

@Override
public byte type() {
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.apache.hertzbeat.alert.notice.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
Expand Down Expand Up @@ -124,6 +126,40 @@ public void testNotifyAlertSuccess() throws Exception {
verify(mailSender).send(any(MimeMessage.class));
}

@Test
public void testSkipSslCertVerifyTrustsAllHosts() throws Exception {
Properties props = stubMailConfig(false);
emailAlertNotifyHandler.send(receiver, template, groupAlert);
assertEquals("*", props.get("mail.smtp.ssl.trust"));
assertEquals("false", props.get("mail.smtp.ssl.checkserveridentity"));
}

@Test
public void testEnableSslCertVerifyClearsStaleTrustProps() throws Exception {
Properties props = stubMailConfig(true);
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.ssl.checkserveridentity", "false");
emailAlertNotifyHandler.send(receiver, template, groupAlert);
assertNull(props.get("mail.smtp.ssl.trust"));
assertNull(props.get("mail.smtp.ssl.checkserveridentity"));
}

private Properties stubMailConfig(boolean sslCertVerify) {
MailServerConfig config = new MailServerConfig();
config.setEmailHost("smtp.example.com");
config.setEmailPort(465);
config.setEmailUsername("sender@example.com");
config.setEmailPassword("password");
config.setEnable(true);
config.setEmailSslCertVerify(sslCertVerify);
when(generalConfigDao.findByType(any()))
.thenReturn(GeneralConfig.builder().content(JsonUtil.toJson(config)).build());
Properties props = new Properties();
when(mailSender.getJavaMailProperties()).thenReturn(props);
when(mailSender.createMimeMessage()).thenReturn(mimeMessage);
return props;
}

@Test
public void testNotifyAlertFailure() {
when(mailSender.createMimeMessage()).thenThrow(new RuntimeException("Test Error"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ public class MailServerConfig {

private boolean emailStarttls = false;

private boolean emailSslCertVerify = true;

private boolean enable = true;
}
1 change: 1 addition & 0 deletions web-app/src/app/pojo/EmailNoticeSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class EmailNoticeSender {
emailPassword!: string;
emailSsl: boolean = true;
emailStarttls: boolean = false;
emailSslCertVerify: boolean = true;
enable!: boolean;
creator!: string;
modifier!: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@
<nz-switch [(ngModel)]="emailSender.emailStarttls" required name="emailStarttls" id="emailStarttls"></nz-switch>
</nz-form-control>
</nz-form-item>
<nz-form-item *ngIf="emailSender.emailSsl || emailSender.emailStarttls">
<nz-form-label nzSpan="7" nzFor="emailSslCertVerify" nzRequired="true">{{ 'alert.notice.sender.mail.ssl-cert-verify' | i18n }}</nz-form-label>
<nz-form-control nzSpan="12">
<nz-switch [(ngModel)]="emailSender.emailSslCertVerify" required name="emailSslCertVerify" id="emailSslCertVerify"></nz-switch>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label nzSpan="7" nzFor="emailEnable" nzRequired="true">{{ 'common.enable' | i18n }}</nz-form-label>
<nz-form-control nzSpan="12">
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"alert.notice.sender.mail.port": "Email Port",
"alert.notice.sender.mail.ssl": "Enable SSL",
"alert.notice.sender.mail.starttls": "Enable STARTTLS",
"alert.notice.sender.mail.ssl-cert-verify": "Verify SSL Certificate",
"alert.notice.sender.mail.username": "Email Account",
"alert.notice.sender.sms.tencent.appId": "Tencent Sms AppId",
"alert.notice.sender.sms.tencent.secretId": "Tencent Sms SecretId",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"alert.notice.sender.mail.port": "メールポート",
"alert.notice.sender.mail.ssl": "SSLを有効化",
"alert.notice.sender.mail.starttls": "STARTTLSを有効化",
"alert.notice.sender.mail.ssl-cert-verify": "SSL証明書を検証",
"alert.notice.sender.mail.username": "メールアカウント",
"alert.notice.sender.sms.tencent.appId": "Tencent Sms AppId",
"alert.notice.sender.sms.tencent.secretId": "Tencent Sms SecretId",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/ko-KR.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"alert.notice.sender.mail.port": "이메일 포트",
"alert.notice.sender.mail.ssl": "SSL 활성화",
"alert.notice.sender.mail.starttls": "STARTTLS 활성화",
"alert.notice.sender.mail.ssl-cert-verify": "SSL 인증서 검증",
"alert.notice.sender.mail.username": "이메일 계정",
"alert.notice.sender.sms.tencent.appId": "Tencent SMS AppId",
"alert.notice.sender.sms.tencent.secretId": "Tencent SMS SecretId",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
"alert.notice.sender.mail.port": "Porta do Email",
"alert.notice.sender.mail.ssl": "Habilitar SSL",
"alert.notice.sender.mail.starttls": "Habilitar STARTTLS",
"alert.notice.sender.mail.ssl-cert-verify": "Verificar Certificado SSL",
"alert.notice.sender.mail.enable": "Habilitar Configuração de Email",
"alert.notice.sender.sms.type": "Tipo de SMS",
"alert.notice.sender.sms.type.tencent": "SMS Tencent",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"alert.notice.sender.mail.port": "邮箱端口",
"alert.notice.sender.mail.ssl": "是否启用SSL",
"alert.notice.sender.mail.starttls": "是否启用STARTTLS",
"alert.notice.sender.mail.ssl-cert-verify": "是否校验SSL证书",
"alert.notice.sender.mail.username": "邮箱账号",
"alert.notice.sender.sms.tencent.appId": "腾讯短信AppId",
"alert.notice.sender.sms.tencent.secretId": "腾讯短信SecretId",
Expand Down
1 change: 1 addition & 0 deletions web-app/src/assets/i18n/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"alert.notice.sender.mail.port": "郵件端口",
"alert.notice.sender.mail.ssl": "是否啟用SSL",
"alert.notice.sender.mail.starttls": "是否啟用STARTTLS",
"alert.notice.sender.mail.ssl-cert-verify": "是否校驗SSL證書",
"alert.notice.sender.mail.username": "郵件帳號",
"alert.notice.sender.sms.tencent.appId": "騰訊短訊AppId",
"alert.notice.sender.sms.tencent.secretId": "騰訊短訊SecretId",
Expand Down
Loading