完善证书管理与邮件通知流程
This commit is contained in:
1 parent
c74372284a
commit
b2833b761d
60 files changed
+7340
-254
No files matched your search
@@ -0,0 +1,534 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
// CredentialReader 读取加密凭据存储中的通知认证信息。
|
||||
type CredentialReader interface {
|
||||
Get(context.Context, string) (map[string]string, error)
|
||||
}
|
||||
|
||||
// HTTPDoer 抽象 HTTP 客户端,便于测试重试、认证和响应处理。
|
||||
type HTTPDoer interface {
|
||||
Do(*http.Request) (*http.Response, error)
|
||||
}
|
||||
|
||||
// Notifier 发送证书任务通知。
|
||||
type Notifier interface {
|
||||
Send(context.Context, Event) error
|
||||
}
|
||||
|
||||
// Event 描述一次证书申请、续期、导入或部署任务。
|
||||
type Event struct {
|
||||
Type string
|
||||
Status string
|
||||
DomainID string
|
||||
Domain string
|
||||
Domains []string
|
||||
ExpiresAt *time.Time
|
||||
CertificateSerial string
|
||||
Deployment deployment.Report
|
||||
ManagementURL string
|
||||
Error string
|
||||
}
|
||||
|
||||
type address struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type mailPayload struct {
|
||||
From address `json:"from"`
|
||||
To []address `json:"to"`
|
||||
Subject string `json:"subject"`
|
||||
Text string `json:"text"`
|
||||
HTML string `json:"html"`
|
||||
CC []address `json:"cc"`
|
||||
BCC []address `json:"bcc"`
|
||||
Attrs []any `json:"attrs"`
|
||||
}
|
||||
|
||||
type sessionAuth struct {
|
||||
cookie string
|
||||
csrf string
|
||||
origin string
|
||||
}
|
||||
|
||||
type sessionExpiredError struct {
|
||||
statusCode int
|
||||
errorCode int
|
||||
}
|
||||
|
||||
func (e *sessionExpiredError) Error() string {
|
||||
if e.statusCode != 0 {
|
||||
return fmt.Sprintf("邮件服务登录会话已失效,HTTP 状态码 %d", e.statusCode)
|
||||
}
|
||||
return fmt.Sprintf("邮件服务登录会话已失效,业务错误码 %d", e.errorCode)
|
||||
}
|
||||
|
||||
// Service 通过 HTTP 邮件 API 发送通知。
|
||||
type Service struct {
|
||||
config config.NotificationConfig
|
||||
credentials CredentialReader
|
||||
client HTTPDoer
|
||||
now func() time.Time
|
||||
requestID func() string
|
||||
sleep func(context.Context, time.Duration) error
|
||||
}
|
||||
|
||||
// New 创建 HTTP 通知服务。SMTP 配置暂保留为兼容字段,当前发送器只执行 HTTP 通知。
|
||||
func New(cfg config.NotificationConfig, credentials CredentialReader) *Service {
|
||||
return &Service{
|
||||
config: cfg,
|
||||
credentials: credentials,
|
||||
client: &http.Client{},
|
||||
now: time.Now,
|
||||
requestID: newRequestID,
|
||||
sleep: sleepContext,
|
||||
}
|
||||
}
|
||||
|
||||
// Send 按配置发送通知;未启用任何通知通道时直接返回成功。
|
||||
func (s *Service) Send(ctx context.Context, event Event) error {
|
||||
if s == nil {
|
||||
return errors.New("通知服务未初始化")
|
||||
}
|
||||
if !s.config.HTTPEnabled && !s.config.SMTPEnabled {
|
||||
return nil
|
||||
}
|
||||
if !s.config.HTTPEnabled {
|
||||
return errors.New("当前仅支持 HTTP 邮件通知,SMTP 通知发送器尚未接入")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
payload, err := s.buildPayload(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return fmt.Errorf("编码邮件通知失败:%w", err)
|
||||
}
|
||||
endpoint, err := notificationURL(s.config.Endpoint, s.config.HTTPPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
credentials, err := s.loadCredentials(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var session sessionAuth
|
||||
if s.config.HTTPAuthMode == "session" {
|
||||
session, err = s.loginSession(ctx, credentials)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
requestID := s.requestID()
|
||||
if requestID == "" {
|
||||
return errors.New("生成通知请求 ID 失败")
|
||||
}
|
||||
var lastErr error
|
||||
refreshSession := false
|
||||
for attempt := 0; attempt <= s.config.RetryCount; attempt++ {
|
||||
if attempt > 0 {
|
||||
interval := time.Duration(0)
|
||||
if attempt-1 < len(s.config.RetryIntervalsSecs) {
|
||||
interval = time.Duration(s.config.RetryIntervalsSecs[attempt-1]) * time.Second
|
||||
}
|
||||
if err := s.sleep(ctx, interval); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if attempt > 0 && s.config.HTTPAuthMode == "session" && refreshSession {
|
||||
session, err = s.loginSession(ctx, credentials)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
refreshSession = false
|
||||
}
|
||||
err := s.sendOnce(ctx, endpoint, body, credentials, requestID, session)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
lastErr = err
|
||||
var expired *sessionExpiredError
|
||||
refreshSession = errors.As(err, &expired)
|
||||
}
|
||||
return fmt.Errorf("发送邮件通知失败,已重试 %d 次:%w", s.config.RetryCount, lastErr)
|
||||
}
|
||||
|
||||
func (s *Service) sendOnce(ctx context.Context, endpoint string, body []byte, credentials map[string]string, requestID string, session sessionAuth) error {
|
||||
timeout := time.Duration(s.config.TimeoutSecs) * time.Second
|
||||
if timeout < time.Second {
|
||||
timeout = time.Second
|
||||
}
|
||||
requestCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
request, err := http.NewRequestWithContext(requestCtx, http.MethodPost, endpoint, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建邮件通知请求失败:%w", err)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
request.Header.Set("Accept", "application/json")
|
||||
request.Header.Set("X-Scert-Request-Id", requestID)
|
||||
timestamp := strconv.FormatInt(s.now().UTC().Unix(), 10)
|
||||
request.Header.Set("X-Scert-Timestamp", timestamp)
|
||||
if s.config.HTTPAuthMode == "session" {
|
||||
if session.cookie == "" {
|
||||
return errors.New("HTTP 通知登录会话为空")
|
||||
}
|
||||
request.Header.Set("Cookie", session.cookie)
|
||||
if session.csrf != "" {
|
||||
request.Header.Set("X-CSRF-Token", session.csrf)
|
||||
}
|
||||
request.Header.Set("Origin", session.origin)
|
||||
request.Header.Set("Referer", session.origin+"/")
|
||||
}
|
||||
for key, value := range s.config.HTTPHeaders {
|
||||
if strings.TrimSpace(key) == "" || strings.ContainsAny(value, "\r\n") {
|
||||
return fmt.Errorf("HTTP 自定义请求头无效:%s", key)
|
||||
}
|
||||
if isProtectedHeader(key) {
|
||||
return fmt.Errorf("HTTP 自定义请求头不允许覆盖安全头:%s", key)
|
||||
}
|
||||
request.Header.Set(key, value)
|
||||
}
|
||||
switch s.config.HTTPAuthMode {
|
||||
case "none", "":
|
||||
case "bearer":
|
||||
token := credentialValue(credentials, "token", "access_token")
|
||||
if token == "" {
|
||||
return errors.New("HTTP Bearer 凭据中缺少 Token")
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
case "hmac_sha256":
|
||||
secret := credentialValue(credentials, "secret", "hmac_secret", "key", "token")
|
||||
if secret == "" {
|
||||
return errors.New("HTTP HMAC 凭据中缺少签名密钥")
|
||||
}
|
||||
signature := sign(secret, timestamp, requestID, body)
|
||||
request.Header.Set("X-Scert-Signature", "sha256="+signature)
|
||||
case "session":
|
||||
default:
|
||||
return fmt.Errorf("HTTP 通知认证方式无效:%s", s.config.HTTPAuthMode)
|
||||
}
|
||||
client := s.client
|
||||
if client == nil {
|
||||
client = &http.Client{}
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("请求邮件服务失败:%w", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
responseBody, err := io.ReadAll(io.LimitReader(response.Body, (64<<10)+1))
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取邮件服务响应失败:%w", err)
|
||||
}
|
||||
if len(responseBody) > 64<<10 {
|
||||
return errors.New("邮件服务响应超过允许大小")
|
||||
}
|
||||
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
|
||||
if response.StatusCode == http.StatusUnauthorized || response.StatusCode == http.StatusForbidden {
|
||||
return &sessionExpiredError{statusCode: response.StatusCode}
|
||||
}
|
||||
return fmt.Errorf("邮件服务返回 HTTP %d", response.StatusCode)
|
||||
}
|
||||
var envelope struct {
|
||||
ErrorNo *int `json:"errorNo"`
|
||||
}
|
||||
if len(responseBody) > 0 && json.Unmarshal(responseBody, &envelope) == nil && envelope.ErrorNo != nil && *envelope.ErrorNo != 0 {
|
||||
if *envelope.ErrorNo == http.StatusUnauthorized || *envelope.ErrorNo == http.StatusForbidden {
|
||||
return &sessionExpiredError{errorCode: *envelope.ErrorNo}
|
||||
}
|
||||
return fmt.Errorf("邮件服务业务失败,错误码 %d", *envelope.ErrorNo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) loginSession(ctx context.Context, credentials map[string]string) (sessionAuth, error) {
|
||||
account := credentialValue(credentials, "account", "username", "identifier")
|
||||
password := credentials["password"]
|
||||
if account == "" || strings.TrimSpace(password) == "" {
|
||||
return sessionAuth{}, errors.New("HTTP 通知会话凭据必须包含账号和密码")
|
||||
}
|
||||
loginURL, err := notificationURL(s.config.Endpoint, s.config.HTTPLoginPath)
|
||||
if err != nil {
|
||||
return sessionAuth{}, err
|
||||
}
|
||||
payload, err := json.Marshal(map[string]string{"account": account, "password": password})
|
||||
if err != nil {
|
||||
return sessionAuth{}, fmt.Errorf("编码 HTTP 通知登录请求失败:%w", err)
|
||||
}
|
||||
timeout := time.Duration(s.config.TimeoutSecs) * time.Second
|
||||
if timeout < time.Second {
|
||||
timeout = time.Second
|
||||
}
|
||||
loginCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
request, err := http.NewRequestWithContext(loginCtx, http.MethodPost, loginURL, bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return sessionAuth{}, fmt.Errorf("创建 HTTP 通知登录请求失败:%w", err)
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
request.Header.Set("Accept", "application/json")
|
||||
request.Header.Set("Origin", notificationOrigin(loginURL))
|
||||
request.Header.Set("Referer", notificationOrigin(loginURL)+"/")
|
||||
client := s.client
|
||||
if client == nil {
|
||||
client = &http.Client{}
|
||||
}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return sessionAuth{}, fmt.Errorf("请求 HTTP 通知登录接口失败:%w", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
responseBody, err := io.ReadAll(io.LimitReader(response.Body, 64<<10))
|
||||
if err != nil {
|
||||
return sessionAuth{}, fmt.Errorf("读取 HTTP 通知登录响应失败:%w", err)
|
||||
}
|
||||
if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices {
|
||||
return sessionAuth{}, fmt.Errorf("HTTP 通知登录接口返回 HTTP %d", response.StatusCode)
|
||||
}
|
||||
var envelope struct {
|
||||
ErrorNo *int `json:"errorNo"`
|
||||
ErrorMsg string `json:"errorMsg"`
|
||||
}
|
||||
if len(responseBody) > 0 && json.Unmarshal(responseBody, &envelope) == nil && envelope.ErrorNo != nil && *envelope.ErrorNo != 0 {
|
||||
if strings.TrimSpace(envelope.ErrorMsg) == "" {
|
||||
return sessionAuth{}, fmt.Errorf("HTTP 通知登录失败,错误码 %d", *envelope.ErrorNo)
|
||||
}
|
||||
return sessionAuth{}, fmt.Errorf("HTTP 通知登录失败,错误码 %d", *envelope.ErrorNo)
|
||||
}
|
||||
cookies := make([]string, 0, len(response.Cookies()))
|
||||
csrf := ""
|
||||
for _, cookie := range response.Cookies() {
|
||||
cookies = append(cookies, cookie.Name+"="+cookie.Value)
|
||||
if cookie.Name == "smail_csrf" {
|
||||
csrf = cookie.Value
|
||||
}
|
||||
}
|
||||
if len(cookies) == 0 {
|
||||
return sessionAuth{}, errors.New("HTTP 通知登录成功但未返回会话 Cookie")
|
||||
}
|
||||
return sessionAuth{cookie: strings.Join(cookies, "; "), csrf: csrf, origin: notificationOrigin(loginURL)}, nil
|
||||
}
|
||||
|
||||
func notificationOrigin(value string) string {
|
||||
parsed, err := url.Parse(value)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return (&url.URL{Scheme: parsed.Scheme, Host: parsed.Host}).String()
|
||||
}
|
||||
|
||||
func (s *Service) loadCredentials(ctx context.Context) (map[string]string, error) {
|
||||
if s.config.HTTPAuthMode == "none" || s.config.HTTPAuthMode == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if s.credentials == nil {
|
||||
return nil, errors.New("HTTP 通知凭据存储未配置")
|
||||
}
|
||||
ref := strings.TrimSpace(s.config.HTTPCredentialRef)
|
||||
if ref == "" {
|
||||
return nil, errors.New("HTTP 通知凭据引用不能为空")
|
||||
}
|
||||
values, err := s.credentials.Get(ctx, ref)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取 HTTP 通知凭据失败:%w", err)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func (s *Service) buildPayload(event Event) (mailPayload, error) {
|
||||
fromEmail := strings.TrimSpace(s.config.SenderEmail)
|
||||
if fromEmail == "" {
|
||||
return mailPayload{}, errors.New("通知发件人地址不能为空")
|
||||
}
|
||||
parsedSender, err := mail.ParseAddress(fromEmail)
|
||||
if err != nil || parsedSender.Address != fromEmail {
|
||||
return mailPayload{}, errors.New("通知发件人地址格式无效")
|
||||
}
|
||||
recipients := make([]address, 0, len(s.config.Recipients))
|
||||
for _, value := range s.config.Recipients {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
parsed, err := mail.ParseAddress(value)
|
||||
if err != nil || parsed.Address != value {
|
||||
return mailPayload{}, fmt.Errorf("通知收件人地址格式无效:%s", value)
|
||||
}
|
||||
recipients = append(recipients, address{Name: parsed.Name, Email: parsed.Address})
|
||||
}
|
||||
if len(recipients) == 0 {
|
||||
return mailPayload{}, errors.New("通知收件人不能为空")
|
||||
}
|
||||
status := eventStatus(event.Status)
|
||||
domainName := strings.TrimSpace(event.Domain)
|
||||
if domainName == "" {
|
||||
domainName = strings.TrimSpace(event.DomainID)
|
||||
}
|
||||
if domainName == "" {
|
||||
domainName = "未指定域名"
|
||||
}
|
||||
title := "证书任务"
|
||||
if s.config.EventMode == "unified" {
|
||||
title = "证书任务通知"
|
||||
} else if event.Type != "" {
|
||||
title = eventTypeName(event.Type)
|
||||
}
|
||||
text := buildText(event, title, status, domainName)
|
||||
return mailPayload{
|
||||
From: address{Name: s.config.SenderName, Email: fromEmail},
|
||||
To: recipients,
|
||||
Subject: fmt.Sprintf("[Scert] %s:%s(%s)", title, domainName, status),
|
||||
Text: text,
|
||||
HTML: "<pre>" + html.EscapeString(text) + "</pre>",
|
||||
CC: []address{},
|
||||
BCC: []address{},
|
||||
Attrs: []any{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func buildText(event Event, title, status, domainName string) string {
|
||||
lines := []string{
|
||||
"Scert 证书管理服务通知",
|
||||
"任务类型:" + title,
|
||||
"任务状态:" + status,
|
||||
"域名:" + domainName,
|
||||
}
|
||||
if len(event.Domains) > 0 {
|
||||
lines = append(lines, "证书域名:"+strings.Join(event.Domains, "、"))
|
||||
}
|
||||
if event.ExpiresAt != nil && !event.ExpiresAt.IsZero() {
|
||||
lines = append(lines, "证书到期时间:"+event.ExpiresAt.Local().Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
if event.CertificateSerial != "" {
|
||||
lines = append(lines, "证书序列号:"+event.CertificateSerial)
|
||||
}
|
||||
if event.ManagementURL != "" {
|
||||
lines = append(lines, "管理后台:"+event.ManagementURL)
|
||||
}
|
||||
if event.Deployment.Total > 0 {
|
||||
lines = append(lines, fmt.Sprintf("部署结果:总计 %d 台,成功 %d 台,失败 %d 台,状态 %s", event.Deployment.Total, event.Deployment.Success, event.Deployment.Failed, event.Deployment.Status))
|
||||
for _, result := range event.Deployment.Results {
|
||||
lines = append(lines, fmt.Sprintf("- %s:%s(%s)", result.Server, result.Status, result.Message))
|
||||
}
|
||||
}
|
||||
if event.Error != "" {
|
||||
lines = append(lines, "错误信息:"+event.Error)
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func eventStatus(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "success":
|
||||
return "成功"
|
||||
case "failed", "failure":
|
||||
return "失败"
|
||||
case "error":
|
||||
return "错误"
|
||||
default:
|
||||
return "未知"
|
||||
}
|
||||
}
|
||||
|
||||
func eventTypeName(value string) string {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "certificate_issue":
|
||||
return "证书申请"
|
||||
case "certificate_renewal":
|
||||
return "证书续期"
|
||||
case "certificate_import":
|
||||
return "证书导入"
|
||||
default:
|
||||
return "证书任务"
|
||||
}
|
||||
}
|
||||
|
||||
func notificationURL(endpoint, path string) (string, error) {
|
||||
base, err := url.Parse(strings.TrimSpace(endpoint))
|
||||
if err != nil || (base.Scheme != "http" && base.Scheme != "https") || base.Host == "" {
|
||||
return "", errors.New("第三方邮箱 API 地址格式无效")
|
||||
}
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" || !strings.HasPrefix(path, "/") {
|
||||
return "", errors.New("HTTP 通知请求路径必须以 / 开头")
|
||||
}
|
||||
base.Path = strings.TrimRight(base.Path, "/") + path
|
||||
base.RawQuery = ""
|
||||
base.Fragment = ""
|
||||
return base.String(), nil
|
||||
}
|
||||
|
||||
func isProtectedHeader(value string) bool {
|
||||
switch strings.ToLower(strings.TrimSpace(value)) {
|
||||
case "authorization", "cookie", "origin", "referer", "content-type", "accept", "x-csrf-token", "x-scert-signature", "x-scert-request-id", "x-scert-timestamp":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func credentialValue(values map[string]string, keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if value := strings.TrimSpace(values[key]); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func sign(secret, timestamp, requestID string, body []byte) string {
|
||||
hasher := hmac.New(sha256.New, []byte(secret))
|
||||
_, _ = hasher.Write([]byte(timestamp + "." + requestID + "."))
|
||||
_, _ = hasher.Write(body)
|
||||
return hex.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
var value [16]byte
|
||||
if _, err := rand.Read(value[:]); err != nil {
|
||||
return ""
|
||||
}
|
||||
return hex.EncodeToString(value[:])
|
||||
}
|
||||
|
||||
func sleepContext(ctx context.Context, duration time.Duration) error {
|
||||
if duration <= 0 {
|
||||
return nil
|
||||
}
|
||||
timer := time.NewTimer(duration)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
type credentialReaderStub struct {
|
||||
values map[string]string
|
||||
}
|
||||
|
||||
func (s credentialReaderStub) Get(context.Context, string) (map[string]string, error) {
|
||||
return s.values, nil
|
||||
}
|
||||
|
||||
func newTestConfig(endpoint string) config.NotificationConfig {
|
||||
return config.NotificationConfig{
|
||||
HTTPEnabled: true,
|
||||
Endpoint: endpoint,
|
||||
HTTPPath: "/api/email/send",
|
||||
HTTPLoginPath: "/api/login",
|
||||
HTTPAuthMode: "none",
|
||||
EventMode: "detailed",
|
||||
SenderName: "Scert",
|
||||
SenderEmail: "admin@example.com",
|
||||
Recipients: []string{"receiver@example.com"},
|
||||
TimeoutSecs: 2,
|
||||
RetryCount: 0,
|
||||
RetryIntervalsSecs: []int{},
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendBuildsSmailPayload(t *testing.T) {
|
||||
var payload map[string]any
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
if request.Method != http.MethodPost || request.URL.Path != "/api/email/send" {
|
||||
t.Fatalf("请求方法或路径错误:%s %s", request.Method, request.URL.Path)
|
||||
}
|
||||
if request.Header.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||
t.Fatalf("通知请求编码声明错误:%q", request.Header.Get("Content-Type"))
|
||||
}
|
||||
if request.Header.Get("X-Scert-Request-Id") == "" || request.Header.Get("X-Scert-Timestamp") == "" {
|
||||
t.Fatal("缺少通知安全请求头")
|
||||
}
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(body), "证书续期") {
|
||||
t.Fatalf("通知请求未使用 UTF-8 编码:%q", string(body))
|
||||
}
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
service := New(newTestConfig(server.URL), nil)
|
||||
service.requestID = func() string { return "request-id" }
|
||||
service.now = func() time.Time { return time.Unix(1700000000, 0).UTC() }
|
||||
when := time.Date(2026, 8, 30, 12, 0, 0, 0, time.UTC)
|
||||
err := service.Send(context.Background(), Event{
|
||||
Type: "certificate_renewal",
|
||||
Status: "success",
|
||||
Domain: "example.com",
|
||||
Domains: []string{"example.com", "www.example.com"},
|
||||
ExpiresAt: &when,
|
||||
ManagementURL: "https://scert.example.com",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("发送通知失败:%v", err)
|
||||
}
|
||||
if payload["subject"] == "" || !strings.Contains(payload["text"].(string), "证书续期") {
|
||||
t.Fatalf("通知正文未正确生成:%v", payload)
|
||||
}
|
||||
to := payload["to"].([]any)
|
||||
if len(to) != 1 || to[0].(map[string]any)["email"] != "receiver@example.com" {
|
||||
t.Fatalf("收件人未正确生成:%v", payload["to"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendSupportsBearerAndRetries(t *testing.T) {
|
||||
attempts := 0
|
||||
var authorization string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
attempts++
|
||||
authorization = request.Header.Get("Authorization")
|
||||
if attempts < 3 {
|
||||
writer.WriteHeader(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := newTestConfig(server.URL)
|
||||
cfg.HTTPAuthMode = "bearer"
|
||||
cfg.HTTPCredentialRef = "secret://mail-token"
|
||||
cfg.RetryCount = 2
|
||||
cfg.RetryIntervalsSecs = []int{1, 1}
|
||||
service := New(cfg, credentialReaderStub{values: map[string]string{"token": "test-token"}})
|
||||
service.sleep = func(context.Context, time.Duration) error { return nil }
|
||||
if err := service.Send(context.Background(), Event{Type: "certificate_issue", Status: "success", Domain: "example.com"}); err != nil {
|
||||
t.Fatalf("重试后发送通知失败:%v", err)
|
||||
}
|
||||
if attempts != 3 || authorization != "Bearer test-token" {
|
||||
t.Fatalf("Bearer 或重试次数错误:attempts=%d authorization=%q", attempts, authorization)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendSupportsHMACAndProtectsHeaders(t *testing.T) {
|
||||
var receivedSignature string
|
||||
var receivedTimestamp string
|
||||
var receivedRequestID string
|
||||
var receivedBody []byte
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
receivedSignature = request.Header.Get("X-Scert-Signature")
|
||||
receivedTimestamp = request.Header.Get("X-Scert-Timestamp")
|
||||
receivedRequestID = request.Header.Get("X-Scert-Request-Id")
|
||||
receivedBody, _ = io.ReadAll(request.Body)
|
||||
writer.WriteHeader(http.StatusCreated)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := newTestConfig(server.URL)
|
||||
cfg.HTTPAuthMode = "hmac_sha256"
|
||||
cfg.HTTPCredentialRef = "secret://mail-hmac"
|
||||
service := New(cfg, credentialReaderStub{values: map[string]string{"secret": "hmac-secret"}})
|
||||
service.requestID = func() string { return "fixed-request" }
|
||||
service.now = func() time.Time { return time.Unix(1700000000, 0).UTC() }
|
||||
if err := service.Send(context.Background(), Event{Type: "certificate_import", Status: "unknown", Domain: "example.com"}); err != nil {
|
||||
t.Fatalf("HMAC 通知发送失败:%v", err)
|
||||
}
|
||||
expected := "sha256=" + sign("hmac-secret", receivedTimestamp, receivedRequestID, receivedBody)
|
||||
if receivedSignature != expected {
|
||||
t.Fatalf("HMAC 签名错误:got=%q want=%q", receivedSignature, expected)
|
||||
}
|
||||
|
||||
cfg.HTTPAuthMode = "none"
|
||||
cfg.HTTPHeaders = map[string]string{"Content-Type": "text/plain"}
|
||||
service = New(cfg, nil)
|
||||
service.sleep = func(context.Context, time.Duration) error { return nil }
|
||||
if err := service.Send(context.Background(), Event{Status: "success", Domain: "example.com"}); err == nil {
|
||||
t.Fatal("覆盖安全请求头时应失败")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendSupportsSessionLogin(t *testing.T) {
|
||||
var loginPayload map[string]string
|
||||
var sendCookie string
|
||||
var csrfToken string
|
||||
var origin string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
switch request.URL.Path {
|
||||
case "/api/login":
|
||||
if request.Method != http.MethodPost {
|
||||
t.Fatalf("登录请求方法错误:%s", request.Method)
|
||||
}
|
||||
body, err := io.ReadAll(request.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(body, &loginPayload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if request.Header.Get("Content-Type") != "application/json; charset=utf-8" {
|
||||
t.Fatalf("登录请求编码声明错误:%q", request.Header.Get("Content-Type"))
|
||||
}
|
||||
writer.Header().Add("Set-Cookie", "session=test-session; Path=/; HttpOnly")
|
||||
writer.Header().Add("Set-Cookie", "smail_csrf=test-csrf; Path=/")
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
_, _ = writer.Write([]byte(`{"errorNo":0,"errorMsg":"","data":{"account":"xuans"}}`))
|
||||
case "/api/email/send":
|
||||
sendCookie = request.Header.Get("Cookie")
|
||||
csrfToken = request.Header.Get("X-CSRF-Token")
|
||||
origin = request.Header.Get("Origin")
|
||||
writer.WriteHeader(http.StatusAccepted)
|
||||
default:
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := newTestConfig(server.URL)
|
||||
cfg.HTTPAuthMode = "session"
|
||||
cfg.HTTPCredentialRef = "secret://mail-session"
|
||||
service := New(cfg, credentialReaderStub{values: map[string]string{
|
||||
"account": "xuans",
|
||||
"password": " test-password ",
|
||||
}})
|
||||
if err := service.Send(context.Background(), Event{Type: "certificate_issue", Status: "success", Domain: "example.com"}); err != nil {
|
||||
t.Fatalf("会话认证通知发送失败:%v", err)
|
||||
}
|
||||
if loginPayload["account"] != "xuans" || loginPayload["password"] != " test-password " {
|
||||
t.Fatalf("登录请求字段错误:%v", loginPayload)
|
||||
}
|
||||
if !strings.Contains(sendCookie, "session=test-session") || csrfToken != "test-csrf" {
|
||||
t.Fatalf("发送请求未携带登录会话:cookie=%q csrf=%q", sendCookie, csrfToken)
|
||||
}
|
||||
if origin != server.URL {
|
||||
t.Fatalf("发送请求来源错误:%q", origin)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendRejectsBusinessFailure(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, _ = writer.Write([]byte(`{"errorNo":100,"errorMsg":"参数错误"}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
service := New(newTestConfig(server.URL), nil)
|
||||
err := service.Send(context.Background(), Event{Status: "success", Domain: "example.com"})
|
||||
if err == nil || !strings.Contains(err.Error(), "业务失败") {
|
||||
t.Fatalf("业务错误响应应被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendRefreshesExpiredSession(t *testing.T) {
|
||||
loginCount := 0
|
||||
sendCount := 0
|
||||
var cookies []string
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
switch request.URL.Path {
|
||||
case "/api/login":
|
||||
loginCount++
|
||||
writer.Header().Add("Set-Cookie", "session=session-"+strconv.Itoa(loginCount)+"; Path=/; HttpOnly")
|
||||
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
_, _ = writer.Write([]byte(`{"errorNo":0,"errorMsg":""}`))
|
||||
case "/api/email/send":
|
||||
sendCount++
|
||||
cookies = append(cookies, request.Header.Get("Cookie"))
|
||||
if sendCount == 1 {
|
||||
writer.WriteHeader(http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
writer.WriteHeader(http.StatusNoContent)
|
||||
default:
|
||||
writer.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
cfg := newTestConfig(server.URL)
|
||||
cfg.HTTPAuthMode = "session"
|
||||
cfg.HTTPCredentialRef = "secret://mail-session"
|
||||
cfg.RetryCount = 1
|
||||
cfg.RetryIntervalsSecs = []int{1}
|
||||
service := New(cfg, credentialReaderStub{values: map[string]string{
|
||||
"account": "xuans",
|
||||
"password": "test-password",
|
||||
}})
|
||||
service.sleep = func(context.Context, time.Duration) error { return nil }
|
||||
if err := service.Send(context.Background(), Event{Status: "success", Domain: "example.com"}); err != nil {
|
||||
t.Fatalf("会话失效后重新登录仍发送失败:%v", err)
|
||||
}
|
||||
if loginCount != 2 || sendCount != 2 || len(cookies) != 2 {
|
||||
t.Fatalf("会话刷新次数错误:login=%d send=%d cookies=%v", loginCount, sendCount, cookies)
|
||||
}
|
||||
if cookies[0] != "session=session-1" || cookies[1] != "session=session-2" {
|
||||
t.Fatalf("未使用刷新后的会话:%v", cookies)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user