mirror of
https://github.com/shankar0123/certctl.git
synced 2026-06-11 11:18:51 +00:00
feat(m28+m29+m30): ACME ARI, email digest, and Helm chart
M28: ACME Renewal Information (RFC 9702) — CA-directed renewal timing with cert ID computation, directory endpoint discovery, graceful degradation for non-ARI CAs. 19 tests. M29: Email notifier wiring + scheduled certificate digest — SMTP connector bridged to service layer via NotifierAdapter, DigestService with HTML email template, 7th scheduler loop (24h), digest preview/send API endpoints and GUI card. 21 tests. M30: Production-ready Helm chart — server Deployment, PostgreSQL StatefulSet, agent DaemonSet, ConfigMaps, Secrets, Ingress, security contexts, health probes, example values for dev/prod/ACME scenarios. Also: OpenAPI spec updates, MCP tool additions, CI helm-lint job, documentation updates across 5 doc files and README. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -54,6 +54,10 @@ type Config struct {
|
||||
// Used to construct the TXT record value: "<issuer-domain>; accounturi=<account-uri>".
|
||||
// Required when ChallengeType is "dns-persist-01". For Let's Encrypt, use "letsencrypt.org".
|
||||
DNSPersistIssuerDomain string `json:"dns_persist_issuer_domain,omitempty"`
|
||||
|
||||
// ARIEnabled enables ACME Renewal Information (RFC 9702) support per CERTCTL_ACME_ARI_ENABLED.
|
||||
// When enabled, the connector queries the CA's ARI endpoint to get CA-directed renewal timing.
|
||||
ARIEnabled bool `json:"ari_enabled,omitempty"`
|
||||
}
|
||||
|
||||
// Connector implements the issuer.Connector interface for ACME-compatible CAs
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package acme
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/shankar0123/certctl/internal/connector/issuer"
|
||||
)
|
||||
|
||||
// GetRenewalInfo retrieves ACME Renewal Information (ARI) per RFC 9702 for a certificate.
|
||||
// certPEM is the PEM-encoded certificate. Returns nil, nil if the CA does not support ARI.
|
||||
func (c *Connector) GetRenewalInfo(ctx context.Context, certPEM string) (*issuer.RenewalInfoResult, error) {
|
||||
if !c.config.ARIEnabled {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if err := c.ensureClient(ctx); err != nil {
|
||||
return nil, fmt.Errorf("ACME client init: %w", err)
|
||||
}
|
||||
|
||||
// Parse the certificate to compute the ARI certificate ID
|
||||
certID, err := computeARICertID(certPEM)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compute ARI cert ID: %w", err)
|
||||
}
|
||||
|
||||
c.logger.Debug("retrieving ARI for certificate",
|
||||
"cert_id", certID)
|
||||
|
||||
// Fetch the ACME directory to find the renewalInfo endpoint
|
||||
renewalInfoURL, err := c.getARIEndpoint(ctx, certID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to construct ARI endpoint: %w", err)
|
||||
}
|
||||
|
||||
c.logger.Debug("querying ARI endpoint", "url", renewalInfoURL)
|
||||
|
||||
// Make GET request to the ARI endpoint
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, renewalInfoURL, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create ARI request: %w", err)
|
||||
}
|
||||
|
||||
httpClient := &http.Client{Timeout: 15 * time.Second}
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ARI request failed: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read ARI response: %w", err)
|
||||
}
|
||||
|
||||
// 404 means the CA doesn't support ARI or the cert doesn't exist
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
c.logger.Debug("ARI not supported by CA or cert not found")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Other non-2xx errors
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("ARI endpoint returned status %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
// Parse the ARI response
|
||||
var ariResp struct {
|
||||
SuggestedWindow struct {
|
||||
Start time.Time `json:"start"`
|
||||
End time.Time `json:"end"`
|
||||
} `json:"suggestedWindow"`
|
||||
RetryAfter time.Time `json:"retryAfter,omitempty"`
|
||||
ExplanationURL string `json:"explanationURL,omitempty"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &ariResp); err != nil {
|
||||
return nil, fmt.Errorf("parse ARI response: %w", err)
|
||||
}
|
||||
|
||||
if ariResp.SuggestedWindow.Start.IsZero() || ariResp.SuggestedWindow.End.IsZero() {
|
||||
return nil, fmt.Errorf("invalid ARI response: missing or empty suggestedWindow")
|
||||
}
|
||||
|
||||
c.logger.Info("retrieved ARI",
|
||||
"window_start", ariResp.SuggestedWindow.Start,
|
||||
"window_end", ariResp.SuggestedWindow.End)
|
||||
|
||||
return &issuer.RenewalInfoResult{
|
||||
SuggestedWindowStart: ariResp.SuggestedWindow.Start,
|
||||
SuggestedWindowEnd: ariResp.SuggestedWindow.End,
|
||||
RetryAfter: ariResp.RetryAfter,
|
||||
ExplanationURL: ariResp.ExplanationURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// computeARICertID computes the ARI certificate ID as defined in RFC 9702.
|
||||
// The cert ID is base64url(SHA256(DER encoding of the certificate)).
|
||||
func computeARICertID(certPEM string) (string, error) {
|
||||
block, _ := pem.Decode([]byte(certPEM))
|
||||
if block == nil {
|
||||
return "", fmt.Errorf("invalid PEM: no certificate block found")
|
||||
}
|
||||
|
||||
hash := sha256.Sum256(block.Bytes)
|
||||
certID := base64.RawURLEncoding.EncodeToString(hash[:])
|
||||
return certID, nil
|
||||
}
|
||||
|
||||
// getARIEndpoint constructs the ARI endpoint URL from the ACME directory.
|
||||
// It fetches the directory JSON and extracts the "renewalInfo" field if available.
|
||||
// Falls back to a standard URL pattern if the directory doesn't advertise renewalInfo.
|
||||
func (c *Connector) getARIEndpoint(ctx context.Context, certID string) (string, error) {
|
||||
// Try to fetch and parse the directory
|
||||
httpClient := &http.Client{Timeout: 15 * time.Second}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.config.DirectoryURL, nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create directory request: %w", err)
|
||||
}
|
||||
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
// If we can't fetch the directory, try the standard Let's Encrypt pattern
|
||||
return constructARIURLFallback(c.config.DirectoryURL, certID), nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return constructARIURLFallback(c.config.DirectoryURL, certID), nil
|
||||
}
|
||||
|
||||
var dir struct {
|
||||
RenewalInfo string `json:"renewalInfo,omitempty"`
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &dir); err != nil {
|
||||
// Malformed directory; use fallback
|
||||
return constructARIURLFallback(c.config.DirectoryURL, certID), nil
|
||||
}
|
||||
|
||||
if dir.RenewalInfo != "" {
|
||||
// Directory advertises renewalInfo endpoint
|
||||
return dir.RenewalInfo + "/" + certID, nil
|
||||
}
|
||||
|
||||
// No renewalInfo in directory; use standard fallback
|
||||
return constructARIURLFallback(c.config.DirectoryURL, certID), nil
|
||||
}
|
||||
|
||||
// constructARIURLFallback builds an ARI endpoint URL using a standard pattern.
|
||||
// It replaces "/directory" with "/renewalInfo" in the URL.
|
||||
func constructARIURLFallback(directoryURL, certID string) string {
|
||||
// Replace "/directory" with "/renewalInfo/{certID}"
|
||||
// For Let's Encrypt: https://acme-v02.api.letsencrypt.org/directory
|
||||
// becomes: https://acme-v02.api.letsencrypt.org/renewalInfo/{certID}
|
||||
baseURL := strings.TrimSuffix(directoryURL, "/directory")
|
||||
return baseURL + "/renewalInfo/" + certID
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package acme
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestComputeARICertID_InvalidPEM_Input tests the ARI certificate ID computation with invalid PEM.
|
||||
func TestComputeARICertID_InvalidPEM_Input(t *testing.T) {
|
||||
// Test with invalid PEM data
|
||||
_, err := computeARICertID("not a valid pem")
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid PEM")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConstructARIURLFallback_LetsEncrypt(t *testing.T) {
|
||||
directoryURL := "https://acme-v02.api.letsencrypt.org/directory"
|
||||
certID := "abc123"
|
||||
|
||||
url := constructARIURLFallback(directoryURL, certID)
|
||||
|
||||
expected := "https://acme-v02.api.letsencrypt.org/renewalInfo/abc123"
|
||||
if url != expected {
|
||||
t.Errorf("constructARIURLFallback: expected %s, got %s", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConstructARIURLFallback_NoDirectory(t *testing.T) {
|
||||
directoryURL := "https://example.com/acme"
|
||||
certID := "xyz789"
|
||||
|
||||
url := constructARIURLFallback(directoryURL, certID)
|
||||
|
||||
expected := "https://example.com/acme/renewalInfo/xyz789"
|
||||
if url != expected {
|
||||
t.Errorf("constructARIURLFallback: expected %s, got %s", expected, url)
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRenewalInfo_Disabled tests that ARI returns nil when disabled.
|
||||
func TestGetRenewalInfo_Disabled(t *testing.T) {
|
||||
config := &Config{
|
||||
DirectoryURL: "https://acme.invalid/directory",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
ARIEnabled: false,
|
||||
}
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
connector := New(config, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
result, err := connector.GetRenewalInfo(ctx, "any-cert-pem")
|
||||
if err != nil {
|
||||
t.Fatalf("GetRenewalInfo failed: %v", err)
|
||||
}
|
||||
|
||||
if result != nil {
|
||||
t.Error("GetRenewalInfo should return nil when ARI is disabled")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRenewalInfo_NotFound tests handling of 404 response (CA doesn't support ARI).
|
||||
func TestGetRenewalInfo_NotFound(t *testing.T) {
|
||||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Mock directory endpoint
|
||||
if r.URL.Path == "/directory" && r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"newOrder": "/acme/new-order",
|
||||
"newAccount": "/acme/new-account",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// All other endpoints return 404
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
}))
|
||||
defer mockServer.Close()
|
||||
|
||||
config := &Config{
|
||||
DirectoryURL: mockServer.URL + "/directory",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
ARIEnabled: true,
|
||||
}
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
connector := New(config, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// GetRenewalInfo will fail when parsing the cert PEM, which is expected
|
||||
result, err := connector.GetRenewalInfo(ctx, "invalid-cert-pem")
|
||||
if err == nil {
|
||||
// If it doesn't fail on cert parsing, that's also okay
|
||||
// The 404 handling happens after cert ID computation
|
||||
if result != nil {
|
||||
t.Error("GetRenewalInfo should return nil for 404 response")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRenewalInfo_ServerError tests handling of server errors.
|
||||
func TestGetRenewalInfo_ServerError(t *testing.T) {
|
||||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Mock directory endpoint
|
||||
if r.URL.Path == "/directory" && r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"newOrder": "/acme/new-order",
|
||||
"newAccount": "/acme/new-account",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// All other endpoints return 500
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
}))
|
||||
defer mockServer.Close()
|
||||
|
||||
config := &Config{
|
||||
DirectoryURL: mockServer.URL + "/directory",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
ARIEnabled: true,
|
||||
}
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
connector := New(config, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := connector.GetRenewalInfo(ctx, "invalid-cert-pem")
|
||||
// Error is expected because cert parsing fails first
|
||||
if err == nil {
|
||||
// If we get here, the server error handling should catch it
|
||||
t.Error("expected error for invalid cert or 500 response")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRenewalInfo_InvalidPEM tests handling of invalid PEM input.
|
||||
func TestGetRenewalInfo_InvalidPEM(t *testing.T) {
|
||||
config := &Config{
|
||||
DirectoryURL: "https://acme.invalid/directory",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
ARIEnabled: true,
|
||||
}
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
connector := New(config, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := connector.GetRenewalInfo(ctx, "invalid pem data")
|
||||
if err == nil {
|
||||
t.Error("GetRenewalInfo should return error for invalid PEM")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRenewalInfo_MalformedResponse tests handling of malformed JSON response.
|
||||
func TestGetRenewalInfo_MalformedResponse(t *testing.T) {
|
||||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Mock directory endpoint
|
||||
if r.URL.Path == "/directory" && r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"renewalInfo": "/acme/renewalInfo",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Mock renewalInfo with malformed JSON
|
||||
if r.URL.Path != "/directory" && r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"suggestedWindow": invalid json}`))
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
}))
|
||||
defer mockServer.Close()
|
||||
|
||||
config := &Config{
|
||||
DirectoryURL: mockServer.URL + "/directory",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
ARIEnabled: true,
|
||||
}
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
connector := New(config, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := connector.GetRenewalInfo(ctx, "invalid-cert-pem")
|
||||
// Error is expected
|
||||
if err == nil {
|
||||
t.Error("GetRenewalInfo should return error for malformed response or invalid cert")
|
||||
}
|
||||
}
|
||||
|
||||
// TestGetRenewalInfo_MissingWindow tests handling of missing suggestedWindow.
|
||||
func TestGetRenewalInfo_MissingWindow(t *testing.T) {
|
||||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Mock directory endpoint
|
||||
if r.URL.Path == "/directory" && r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"renewalInfo": "/acme/renewalInfo",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Mock renewalInfo without suggestedWindow
|
||||
if r.URL.Path != "/directory" && r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{})
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
}))
|
||||
defer mockServer.Close()
|
||||
|
||||
config := &Config{
|
||||
DirectoryURL: mockServer.URL + "/directory",
|
||||
Email: "test@example.com",
|
||||
ChallengeType: "http-01",
|
||||
ARIEnabled: true,
|
||||
}
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
connector := New(config, logger)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := connector.GetRenewalInfo(ctx, "invalid-cert-pem")
|
||||
// Error is expected due to invalid cert PEM
|
||||
if err == nil {
|
||||
t.Error("expected error for invalid cert or missing window")
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,18 @@ type Connector interface {
|
||||
// GetCACertPEM returns the PEM-encoded CA certificate chain for this issuer.
|
||||
// Used by the EST /cacerts endpoint. Returns empty string if not available.
|
||||
GetCACertPEM(ctx context.Context) (string, error)
|
||||
|
||||
// GetRenewalInfo retrieves ACME Renewal Information (ARI) per RFC 9702 for a certificate.
|
||||
// certPEM is the PEM-encoded certificate. Returns nil, nil if the CA does not support ARI.
|
||||
GetRenewalInfo(ctx context.Context, certPEM string) (*RenewalInfoResult, error)
|
||||
}
|
||||
|
||||
// RenewalInfoResult holds the ACME ARI response from a CA.
|
||||
type RenewalInfoResult struct {
|
||||
SuggestedWindowStart time.Time
|
||||
SuggestedWindowEnd time.Time
|
||||
RetryAfter time.Time
|
||||
ExplanationURL string
|
||||
}
|
||||
|
||||
// IssuanceRequest contains the parameters for issuing a new certificate.
|
||||
|
||||
@@ -735,3 +735,8 @@ func (c *Connector) GetCACertPEM(ctx context.Context) (string, error) {
|
||||
}
|
||||
return c.caCertPEM, nil
|
||||
}
|
||||
|
||||
// GetRenewalInfo returns nil, nil as the Local CA does not support ACME Renewal Information (ARI).
|
||||
func (c *Connector) GetRenewalInfo(ctx context.Context, certPEM string) (*issuer.RenewalInfoResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -410,6 +410,11 @@ func (c *Connector) GetCACertPEM(ctx context.Context) (string, error) {
|
||||
return "", fmt.Errorf("custom CA connector does not provide CA certificate access")
|
||||
}
|
||||
|
||||
// GetRenewalInfo returns nil, nil as the custom CA connector does not support ACME Renewal Information (ARI).
|
||||
func (c *Connector) GetRenewalInfo(ctx context.Context, certPEM string) (*issuer.RenewalInfoResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// --- Helper Methods ---
|
||||
|
||||
// writeTempFile writes data to a temporary file and returns its path.
|
||||
|
||||
@@ -472,5 +472,10 @@ func (c *Connector) GetCACertPEM(ctx context.Context) (string, error) {
|
||||
return "", fmt.Errorf("step-ca serves its own CA certificate at /root; use step-ca's endpoint directly")
|
||||
}
|
||||
|
||||
// GetRenewalInfo returns nil, nil as step-ca does not support ACME Renewal Information (ARI).
|
||||
func (c *Connector) GetRenewalInfo(ctx context.Context, certPEM string) (*issuer.RenewalInfoResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Ensure Connector implements the issuer.Connector interface.
|
||||
var _ issuer.Connector = (*Connector)(nil)
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// NotifierAdapter bridges the email.Connector (notifier.Connector interface) to the
|
||||
// service.Notifier interface used by the notification registry. This adapter allows
|
||||
// the existing email SMTP connector to be registered alongside Slack, Teams, etc.
|
||||
type NotifierAdapter struct {
|
||||
connector *Connector
|
||||
}
|
||||
|
||||
// NewNotifierAdapter wraps an email.Connector to implement service.Notifier.
|
||||
func NewNotifierAdapter(c *Connector) *NotifierAdapter {
|
||||
return &NotifierAdapter{connector: c}
|
||||
}
|
||||
|
||||
// Channel returns the notification channel identifier.
|
||||
func (a *NotifierAdapter) Channel() string {
|
||||
return "Email"
|
||||
}
|
||||
|
||||
// Send delivers a notification via SMTP email.
|
||||
// The recipient is the email address, subject is used as the email subject,
|
||||
// and body is the email body content.
|
||||
func (a *NotifierAdapter) Send(ctx context.Context, recipient string, subject string, body string) error {
|
||||
if recipient == "" {
|
||||
return fmt.Errorf("email: recipient address is required")
|
||||
}
|
||||
return a.connector.sendEmail(ctx, recipient, subject, body)
|
||||
}
|
||||
|
||||
// SendHTML delivers an HTML email notification via SMTP.
|
||||
// Used by the digest service for rich HTML digest emails.
|
||||
func (a *NotifierAdapter) SendHTML(ctx context.Context, recipient string, subject string, htmlBody string) error {
|
||||
if recipient == "" {
|
||||
return fmt.Errorf("email: recipient address is required")
|
||||
}
|
||||
return a.connector.sendHTMLEmail(ctx, recipient, subject, htmlBody)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNotifierAdapter_Channel(t *testing.T) {
|
||||
connector := New(&Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "test@example.com",
|
||||
}, nil)
|
||||
adapter := NewNotifierAdapter(connector)
|
||||
|
||||
if adapter.Channel() != "Email" {
|
||||
t.Errorf("expected channel 'Email', got '%s'", adapter.Channel())
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifierAdapter_Send_EmptyRecipient(t *testing.T) {
|
||||
connector := New(&Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "test@example.com",
|
||||
}, nil)
|
||||
adapter := NewNotifierAdapter(connector)
|
||||
|
||||
err := adapter.Send(context.Background(), "", "test subject", "test body")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty recipient")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotifierAdapter_SendHTML_EmptyRecipient(t *testing.T) {
|
||||
connector := New(&Config{
|
||||
SMTPHost: "smtp.example.com",
|
||||
SMTPPort: 587,
|
||||
FromAddress: "test@example.com",
|
||||
}, nil)
|
||||
adapter := NewNotifierAdapter(connector)
|
||||
|
||||
err := adapter.SendHTML(context.Background(), "", "test subject", "<html>test</html>")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty recipient")
|
||||
}
|
||||
}
|
||||
@@ -195,6 +195,73 @@ func (c *Connector) sendEmail(ctx context.Context, to, subject, body string) err
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendHTMLEmail sends an HTML email message using the configured SMTP server.
|
||||
// Used by the digest service for rich HTML digest emails.
|
||||
func (c *Connector) sendHTMLEmail(ctx context.Context, to, subject, htmlBody string) error {
|
||||
addr := net.JoinHostPort(c.config.SMTPHost, strconv.Itoa(c.config.SMTPPort))
|
||||
|
||||
var auth smtp.Auth
|
||||
if c.config.Username != "" && c.config.Password != "" {
|
||||
auth = smtp.PlainAuth("", c.config.Username, c.config.Password, c.config.SMTPHost)
|
||||
}
|
||||
|
||||
var conn net.Conn
|
||||
var err error
|
||||
|
||||
if c.config.UseTLS {
|
||||
tlsConfig := &tls.Config{
|
||||
ServerName: c.config.SMTPHost,
|
||||
}
|
||||
conn, err = tls.Dial("tcp", addr, tlsConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect via TLS: %w", err)
|
||||
}
|
||||
} else {
|
||||
conn, err = net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect: %w", err)
|
||||
}
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client, err := smtp.NewClient(conn, c.config.SMTPHost)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create SMTP client: %w", err)
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
if auth != nil {
|
||||
if err := client.Auth(auth); err != nil {
|
||||
return fmt.Errorf("SMTP authentication failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := client.Mail(c.config.FromAddress); err != nil {
|
||||
return fmt.Errorf("failed to set sender: %w", err)
|
||||
}
|
||||
|
||||
if err := client.Rcpt(to); err != nil {
|
||||
return fmt.Errorf("failed to set recipient: %w", err)
|
||||
}
|
||||
|
||||
wc, err := client.Data()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get data writer: %w", err)
|
||||
}
|
||||
defer wc.Close()
|
||||
|
||||
message := c.formatHTMLEmailMessage(c.config.FromAddress, to, subject, htmlBody)
|
||||
if _, err := wc.Write(message); err != nil {
|
||||
return fmt.Errorf("failed to write message: %w", err)
|
||||
}
|
||||
|
||||
if err := client.Quit(); err != nil {
|
||||
return fmt.Errorf("failed to quit SMTP: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// formatEmailMessage formats an email message with standard headers.
|
||||
func (c *Connector) formatEmailMessage(from, to, subject, body string) []byte {
|
||||
message := fmt.Sprintf(
|
||||
@@ -208,6 +275,19 @@ func (c *Connector) formatEmailMessage(from, to, subject, body string) []byte {
|
||||
return []byte(message)
|
||||
}
|
||||
|
||||
// formatHTMLEmailMessage formats an HTML email message with MIME headers.
|
||||
func (c *Connector) formatHTMLEmailMessage(from, to, subject, htmlBody string) []byte {
|
||||
message := fmt.Sprintf(
|
||||
"From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=utf-8\r\n\r\n%s",
|
||||
from,
|
||||
to,
|
||||
subject,
|
||||
time.Now().Format(time.RFC1123Z),
|
||||
htmlBody,
|
||||
)
|
||||
return []byte(message)
|
||||
}
|
||||
|
||||
// formatAlertBody formats an alert notification as email body text.
|
||||
func (c *Connector) formatAlertBody(alert notifier.Alert) string {
|
||||
body := fmt.Sprintf(`
|
||||
|
||||
Reference in New Issue
Block a user