summaryrefslogtreecommitdiff
path: root/modules/caddytls
diff options
context:
space:
mode:
authorFrancis Lavoie <lavofr@gmail.com>2022-08-31 17:01:30 -0400
committerGitHub <noreply@github.com>2022-08-31 15:01:30 -0600
commitd4d8bbcfc64d1194079cae35697709f6d267d02f (patch)
tree8617401e3560f1eb5986878747180862822d4d4c /modules/caddytls
parent68d8ac9802c5e7bf5bf55d1a3c1db634edc93999 (diff)
events: Implement event system (#4912)
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
Diffstat (limited to 'modules/caddytls')
-rw-r--r--modules/caddytls/automation.go1
-rw-r--r--modules/caddytls/tls.go15
2 files changed, 16 insertions, 0 deletions
diff --git a/modules/caddytls/automation.go b/modules/caddytls/automation.go
index 0a732b8..e80d355 100644
--- a/modules/caddytls/automation.go
+++ b/modules/caddytls/automation.go
@@ -256,6 +256,7 @@ func (ap *AutomationPolicy) Provision(tlsApp *TLS) error {
MustStaple: ap.MustStaple,
RenewalWindowRatio: ap.RenewalWindowRatio,
KeySource: keySource,
+ OnEvent: tlsApp.onEvent,
OnDemand: ond,
OCSP: certmagic.OCSPConfig{
DisableStapling: ap.DisableOCSPStapling,
diff --git a/modules/caddytls/tls.go b/modules/caddytls/tls.go
index f129489..fc5f2ac 100644
--- a/modules/caddytls/tls.go
+++ b/modules/caddytls/tls.go
@@ -15,6 +15,7 @@
package caddytls
import (
+ "context"
"crypto/tls"
"encoding/json"
"fmt"
@@ -25,6 +26,7 @@ import (
"time"
"github.com/caddyserver/caddy/v2"
+ "github.com/caddyserver/caddy/v2/modules/caddyevents"
"github.com/caddyserver/certmagic"
"go.uber.org/zap"
)
@@ -73,6 +75,7 @@ type TLS struct {
storageCleanTicker *time.Ticker
storageCleanStop chan struct{}
logger *zap.Logger
+ events *caddyevents.App
}
// CaddyModule returns the Caddy module information.
@@ -85,6 +88,11 @@ func (TLS) CaddyModule() caddy.ModuleInfo {
// Provision sets up the configuration for the TLS app.
func (t *TLS) Provision(ctx caddy.Context) error {
+ eventsAppIface, err := ctx.App("events")
+ if err != nil {
+ return fmt.Errorf("getting events app: %v", err)
+ }
+ t.events = eventsAppIface.(*caddyevents.App)
t.ctx = ctx
t.logger = ctx.Logger(t)
repl := caddy.NewReplacer()
@@ -189,6 +197,7 @@ func (t *TLS) Provision(ctx caddy.Context) error {
magic := certmagic.New(t.certCache, certmagic.Config{
Storage: ctx.Storage(),
Logger: t.logger,
+ OnEvent: t.onEvent,
OCSP: certmagic.OCSPConfig{
DisableStapling: t.DisableOCSPStapling,
},
@@ -514,6 +523,12 @@ func (t *TLS) storageCleanInterval() time.Duration {
return defaultStorageCleanInterval
}
+// onEvent translates CertMagic events into Caddy events then dispatches them.
+func (t *TLS) onEvent(ctx context.Context, eventName string, data map[string]any) error {
+ evt := t.events.Emit(t.ctx, eventName, data)
+ return evt.Aborted
+}
+
// CertificateLoader is a type that can load certificates.
// Certificates can optionally be associated with tags.
type CertificateLoader interface {