Production hardening: security, resilience, observability, and compliance

Password complexity: custom validator requiring uppercase, lowercase, digit (min 8 chars)
Token expiry: 90-day token lifetime with refresh endpoint (60-90 day renewal window)
Health check: /api/health/ now pings Postgres + Redis, returns 503 on failure
Audit logging: async audit_log table for auth events (login, register, delete, etc.)
Circuit breaker: APNs/FCM push sends wrapped with 5-failure threshold, 30s recovery
FK indexes: 27 missing foreign key indexes across all tables (migration 017)
CSP header: default-src 'none'; frame-ancestors 'none'
Gzip compression: level 5 with media endpoint skipper
Prometheus metrics: /metrics endpoint using existing monitoring service
External timeouts: 15s push, 30s SMTP, context timeouts on all external calls

Migrations: 016 (token created_at), 017 (FK indexes), 018 (audit_log)
Tests: circuit breaker (15), audit service (8), token refresh (7), health (4),
       middleware expiry (5), validator (new)
This commit is contained in:
Trey T
2026-03-26 14:05:28 -05:00
parent 4abc57535e
commit b679f28e55
30 changed files with 2077 additions and 47 deletions

View File

@@ -2,6 +2,7 @@ package push
import (
"context"
"time"
"github.com/rs/zerolog/log"
@@ -14,16 +15,25 @@ const (
PlatformAndroid = "android"
)
// Timeout for individual push notification send operations.
const pushSendTimeout = 15 * time.Second
// Client provides a unified interface for sending push notifications
type Client struct {
apns *APNsClient
fcm *FCMClient
enabled bool
apns *APNsClient
fcm *FCMClient
enabled bool
apnsBreaker *CircuitBreaker
fcmBreaker *CircuitBreaker
}
// NewClient creates a new unified push notification client
func NewClient(cfg *config.PushConfig, enabled bool) (*Client, error) {
client := &Client{enabled: enabled}
client := &Client{
enabled: enabled,
apnsBreaker: NewCircuitBreaker("apns"),
fcmBreaker: NewCircuitBreaker("fcm"),
}
// Initialize APNs client (iOS)
if cfg.APNSKeyPath != "" && cfg.APNSKeyID != "" && cfg.APNSTeamID != "" {
@@ -54,7 +64,8 @@ func NewClient(cfg *config.PushConfig, enabled bool) (*Client, error) {
return client, nil
}
// SendToIOS sends a push notification to iOS devices
// SendToIOS sends a push notification to iOS devices.
// The call is guarded by a circuit breaker and uses a context timeout.
func (c *Client) SendToIOS(ctx context.Context, tokens []string, title, message string, data map[string]string) error {
if !c.enabled {
log.Debug().Msg("Push notifications disabled by feature flag")
@@ -64,10 +75,26 @@ func (c *Client) SendToIOS(ctx context.Context, tokens []string, title, message
log.Warn().Msg("APNs client not initialized, skipping iOS push")
return nil
}
return c.apns.Send(ctx, tokens, title, message, data)
if !c.apnsBreaker.Allow() {
log.Warn().Str("breaker", c.apnsBreaker.Name()).Msg("APNs circuit breaker is open, skipping iOS push")
return ErrCircuitOpen
}
sendCtx, cancel := context.WithTimeout(ctx, pushSendTimeout)
defer cancel()
err := c.apns.Send(sendCtx, tokens, title, message, data)
if err != nil {
c.apnsBreaker.RecordFailure()
log.Warn().Err(err).Str("breaker_state", c.apnsBreaker.State()).Msg("APNs send failed, recorded circuit breaker failure")
return err
}
c.apnsBreaker.RecordSuccess()
return nil
}
// SendToAndroid sends a push notification to Android devices
// SendToAndroid sends a push notification to Android devices.
// The call is guarded by a circuit breaker and uses a context timeout.
func (c *Client) SendToAndroid(ctx context.Context, tokens []string, title, message string, data map[string]string) error {
if !c.enabled {
log.Debug().Msg("Push notifications disabled by feature flag")
@@ -77,7 +104,22 @@ func (c *Client) SendToAndroid(ctx context.Context, tokens []string, title, mess
log.Warn().Msg("FCM client not initialized, skipping Android push")
return nil
}
return c.fcm.Send(ctx, tokens, title, message, data)
if !c.fcmBreaker.Allow() {
log.Warn().Str("breaker", c.fcmBreaker.Name()).Msg("FCM circuit breaker is open, skipping Android push")
return ErrCircuitOpen
}
sendCtx, cancel := context.WithTimeout(ctx, pushSendTimeout)
defer cancel()
err := c.fcm.Send(sendCtx, tokens, title, message, data)
if err != nil {
c.fcmBreaker.RecordFailure()
log.Warn().Err(err).Str("breaker_state", c.fcmBreaker.State()).Msg("FCM send failed, recorded circuit breaker failure")
return err
}
c.fcmBreaker.RecordSuccess()
return nil
}
// SendToAll sends a push notification to both iOS and Android devices
@@ -115,8 +157,9 @@ func (c *Client) IsAndroidEnabled() bool {
return c.fcm != nil
}
// SendActionableNotification sends notifications with action button support
// iOS receives a category for actionable notifications, Android handles actions via data payload
// SendActionableNotification sends notifications with action button support.
// iOS receives a category for actionable notifications, Android handles actions via data payload.
// Both platforms are guarded by their respective circuit breakers.
func (c *Client) SendActionableNotification(ctx context.Context, iosTokens, androidTokens []string, title, message string, data map[string]string, iosCategoryID string) error {
if !c.enabled {
log.Debug().Msg("Push notifications disabled by feature flag")
@@ -127,10 +170,19 @@ func (c *Client) SendActionableNotification(ctx context.Context, iosTokens, andr
if len(iosTokens) > 0 {
if c.apns == nil {
log.Warn().Msg("APNs client not initialized, skipping iOS actionable push")
} else if !c.apnsBreaker.Allow() {
log.Warn().Str("breaker", c.apnsBreaker.Name()).Msg("APNs circuit breaker is open, skipping iOS actionable push")
lastErr = ErrCircuitOpen
} else {
if err := c.apns.SendWithCategory(ctx, iosTokens, title, message, data, iosCategoryID); err != nil {
sendCtx, cancel := context.WithTimeout(ctx, pushSendTimeout)
err := c.apns.SendWithCategory(sendCtx, iosTokens, title, message, data, iosCategoryID)
cancel()
if err != nil {
c.apnsBreaker.RecordFailure()
log.Error().Err(err).Msg("Failed to send iOS actionable notifications")
lastErr = err
} else {
c.apnsBreaker.RecordSuccess()
}
}
}