-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[TT-15379] Introduce certificate expiry monitor #7271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[TT-15379] Introduce certificate expiry monitor #7271
Conversation
Let's make that PR title a 💯 shall we? 💪 Your PR title and story title look slightly different. Just checking in to know if it was intentional!
Check out this guide to learn more about PR best-practices. |
This PR is too huge for one to review 💔
Consider breaking it down into multiple small PRs. Check out this guide to learn more about PR best-practices. |
PR Reviewer Guide 🔍(Review updated until commit 6cb53ec)Here are some key observations to aid the review process:
|
PR Code Suggestions ✨Latest suggestions up to 6cb53ec
Previous suggestionsSuggestions
|
API Changes --- prev.txt 2025-08-06 09:54:03.641034656 +0000
+++ current.txt 2025-08-06 09:53:54.366000721 +0000
@@ -5524,6 +5524,18 @@
CONSTANTS
const (
+ // DefaultWarningThresholdDays is the default number of days before certificate expiration to start sending notifications
+ DefaultWarningThresholdDays = 30
+
+ // DefaultCheckCooldownSeconds is the default minimum time in seconds between certificate expiration checks
+ DefaultCheckCooldownSeconds = 3600 // 1 hour
+
+ // DefaultEventCooldownSeconds is the default minimum time in seconds between firing the same certificate expiration event
+ DefaultEventCooldownSeconds = 86400 // 24 hours
+)
+ Certificate monitor constants
+
+const (
PickFirstStrategy IPsHandleStrategy = "pick_first"
RandomStrategy IPsHandleStrategy = "random"
NoCacheStrategy IPsHandleStrategy = "no_cache"
@@ -5573,6 +5585,13 @@
AllowUnsafe: []string{},
},
PIDFileLocation: "/var/run/tyk/tyk-gateway.pid",
+ Security: SecurityConfig{
+ CertificateExpiryMonitor: CertificateExpiryMonitorConfig{
+ WarningThresholdDays: DefaultWarningThresholdDays,
+ CheckCooldownSeconds: DefaultCheckCooldownSeconds,
+ EventCooldownSeconds: DefaultEventCooldownSeconds,
+ },
+ },
}
)
var Global func() Config
@@ -5695,6 +5714,22 @@
KeyFile string `json:"key_file"`
}
+type CertificateExpiryMonitorConfig struct {
+ // WarningThresholdDays specifies the number of days before certificate expiration to start sending notifications
+ // Default: DefaultWarningThresholdDays (30 days)
+ WarningThresholdDays int `json:"warning_threshold_days"`
+
+ // CheckCooldownSeconds specifies the minimum time in seconds between certificate expiration checks
+ // Default: DefaultCheckCooldownSeconds (3600 seconds = 1 hour)
+ CheckCooldownSeconds int `json:"check_cooldown_seconds"`
+
+ // EventCooldownSeconds specifies the minimum time in seconds between firing the same certificate expiration event
+ // Default: DefaultEventCooldownSeconds (86400 seconds = 24 hours)
+ EventCooldownSeconds int `json:"event_cooldown_seconds"`
+}
+ CertificateExpiryMonitorConfig configures the certificate expiration
+ notification feature
+
type CertificatesConfig struct {
API []string `json:"apis"`
// Upstream is used to specify the certificates to be used in mutual TLS connections to upstream services. These are set at gateway level as a map of domain -> certificate id or path.
@@ -6720,6 +6755,9 @@
PinnedPublicKeys map[string]string `json:"pinned_public_keys"`
Certificates CertificatesConfig `json:"certificates"`
+
+ // CertificateExpiryMonitor configures the certificate expiration notification feature
+ CertificateExpiryMonitor CertificateExpiryMonitorConfig `json:"certificate_expiry_monitor"`
}
type ServiceDiscoveryConf struct {
@@ -8457,6 +8495,8 @@
EventTokenUpdated = event.TokenUpdated
// EventTokenDeleted is an alias maintained for backwards compatibility.
EventTokenDeleted = event.TokenDeleted
+ // EventCertificateExpiringSoon is an alias maintained for backwards compatibility.
+ EventCertificateExpiringSoon = event.CertificateExpiringSoon
)
const (
MsgAuthFieldMissing = "Authorization field missing"
@@ -9115,6 +9155,7 @@
type CertificateCheckMW struct {
*BaseMiddleware
+ // Has unexported fields.
}
CertificateCheckMW is used if domain was not detected or multiple APIs bind
on the same domain. In this case authentification check happens not on TLS
@@ -9350,6 +9391,18 @@
HandleError is the actual error handler and will store the error details in
analytics if analytics processing is enabled.
+type EventCertificateExpiringSoonMeta struct {
+ EventMetaDefault
+ CertID string `json:"cert_id"`
+ CertName string `json:"cert_name"`
+ ExpiresAt time.Time `json:"expires_at"`
+ DaysRemaining int `json:"days_remaining"`
+ APIID string `json:"api_id"`
+ OrgID string `json:"org_id"`
+}
+ EventCertificateExpiringSoonMeta is the metadata structure for certificate
+ expiration events
+
type EventCurcuitBreakerMeta struct {
EventMetaDefault
Path string |
Persistent review updated to latest commit c1ba970 |
Persistent review updated to latest commit c1ba970 |
PR Code Suggestions ✨No code suggestions found for the PR. |
Persistent review updated to latest commit 6cb53ec |
📦 Impact Review Snapshot
## Impact AssessmentThis PR introduces certificate expiry monitoring functionality to Tyk Gateway, which checks TLS certificates for upcoming expiration and sends webhook notifications. The implementation is largely self-contained within the gateway codebase, with minimal impact on downstream repositories. The feature adds a new event type ( ## Required Updates
## Compatibility ConcernsNo backward compatibility issues identified. The feature:
The implementation includes proper handling of default values, so existing deployments will work without explicit configuration. ## Summary & Recommendations
Tip: Mention me again using |
🛡️ Security Snapshot
## Security Impact AnalysisThe PR introduces certificate expiry monitoring to detect and notify when TLS certificates are approaching expiration. The implementation enhances security posture by providing early warning for certificate renewals, preventing potential service disruptions and security vulnerabilities from expired certificates. The feature uses Redis for cooldown state storage and integrates with Tyk's event system for notifications. The implementation is generally secure, with proper validation of certificates and appropriate logging. However, there are two race conditions in the cooldown logic that could lead to duplicate events or checks in high-concurrency scenarios. ## Identified VulnerabilitiesMedium Risk:
Low Risk:
## Security Recommendations
## OWASP ComplianceThe implementation aligns well with OWASP best practices:
The code does not introduce any new OWASP Top 10 vulnerabilities and actually helps mitigate potential issues related to certificate management. ## Summary
Tip: Mention me again using |
🚦 Connectivity Review Snapshot
## Connectivity Assessment
## Test Coverage Validation
## Security & Performance Impact
## Summary & Recommendations
Tip: Mention me again using |
🚀 Performance Snapshot
## Performance Impact AnalysisThis PR introduces certificate expiry monitoring to the Tyk Gateway, which adds additional processing during certificate validation. The implementation performs expiry checks synchronously in the request path, which could impact request latency, especially with many certificates. The cooldown mechanism using Redis helps mitigate this by preventing redundant checks, but the check-then-set pattern introduces race conditions that could lead to duplicate processing under high concurrency. ## Critical Areas
## Optimization Recommendations
## Summary
Tip: Mention me again using |
|
User description
TT-15379
Description
This PR implements certificate expiry monitoring functionality for Tyk Gateway. The feature monitors TLS certificates for expiration and sends webhook notifications when certificates are approaching their expiry date.
Key Changes:
Certificate Check Middleware Implementation:
gateway/mw_certificate_check.go
with certificate expiry monitoring logicConfiguration System:
CertificateExpiryMonitorConfig
struct toconfig/config.go
Comprehensive Test Suite:
gateway/mw_certificate_check_test.go
- Unit testsgateway/mw_certificate_check_integration_test.go
- Integration testsgateway/mw_certificate_check_benchmark_test.go
- Performance benchmarksTest Data & Configuration:
config/testdata/
config/config_test.go
Webhook Template:
templates/certificate_check_webhook.json
for certificate expiry event formattingRelated Issue
https://tyktech.atlassian.net/browse/TT-15379
Motivation and Context
How This Has Been Tested
Unit Testing:
Integration Testing:
Performance Testing:
Configuration Testing:
Benchmarks
BenchmarkCerstructtificateCheckMW_ProcessRequest
With expiry certificate monitor disabled
With expiry certificate monitor enabled
Screenshots (if appropriate)
Types of changes
Checklist
PR Type
Enhancement, Tests
Description
Introduce certificate expiry monitoring and notification system
Add configuration for expiry thresholds and cooldowns
Implement Redis-backed cooldown for event suppression
Provide comprehensive unit, integration, and benchmark tests
Diagram Walkthrough
File Walkthrough