@@ -122,15 +122,13 @@ type TLS struct {
122
122
DNSRaw json.RawMessage `json:"dns,omitempty" caddy:"namespace=dns.providers inline_key=name"`
123
123
dns any // technically, it should be any/all of the libdns interfaces (RecordSetter, RecordAppender, etc.)
124
124
125
- magic * certmagic.Config
126
- certificateLoaders []CertificateLoader
127
- unmanagedCertsTicker * time.Ticker
128
- automateNames []string
129
- ctx caddy.Context
130
- storageCleanTicker * time.Ticker
131
- storageCleanStop chan struct {}
132
- logger * zap.Logger
133
- events * caddyevents.App
125
+ certificateLoaders []CertificateLoader
126
+ automateNames []string
127
+ ctx caddy.Context
128
+ storageCleanTicker * time.Ticker
129
+ storageCleanStop chan struct {}
130
+ logger * zap.Logger
131
+ events * caddyevents.App
134
132
135
133
serverNames map [string ]struct {}
136
134
serverNamesMu * sync.Mutex
@@ -240,7 +238,7 @@ func (t *TLS) Provision(ctx caddy.Context) error {
240
238
// certificates have been manually loaded, and also so that
241
239
// commands like validate can be a better test
242
240
certCacheMu .RLock ()
243
- t . magic = certmagic .New (certCache , certmagic.Config {
241
+ magic : = certmagic .New (certCache , certmagic.Config {
244
242
Storage : ctx .Storage (),
245
243
Logger : t .logger ,
246
244
OnEvent : t .onEvent ,
@@ -251,13 +249,13 @@ func (t *TLS) Provision(ctx caddy.Context) error {
251
249
})
252
250
certCacheMu .RUnlock ()
253
251
254
- unmanaged , err := t . loadUnmanagedCertificates ( ctx )
255
- if err != nil {
256
- return err
257
- }
258
-
259
- if unmanaged > 0 {
260
- t . regularlyReloadUnmanagedCertificates ()
252
+ for _ , loader := range t . certificateLoaders {
253
+ err := loader . Initialize ( func ( add [] Certificate , remove [] string ) error {
254
+ return t . updateCertificates ( ctx , magic , add , remove )
255
+ })
256
+ if err != nil {
257
+ return fmt . Errorf ( "loading certificates: %v" , err )
258
+ }
261
259
}
262
260
263
261
// on-demand permission module
@@ -718,58 +716,17 @@ func (t *TLS) HasCertificateForSubject(subject string) bool {
718
716
return false
719
717
}
720
718
721
- func (t * TLS ) loadUnmanagedCertificates (ctx caddy.Context ) (int , error ) {
722
- cached := 0
723
- for _ , loader := range t .certificateLoaders {
724
- certs , err := loader .LoadCertificates ()
725
- if err != nil {
726
- return 0 , fmt .Errorf ("loading certificates: %v" , err )
727
- }
728
- for _ , cert := range certs {
729
- hash , err := t .magic .CacheUnmanagedTLSCertificate (ctx , cert .Certificate , cert .Tags )
730
- if err != nil {
731
- return 0 , fmt .Errorf ("caching unmanaged certificate: %v" , err )
732
- }
733
- t .loaded [hash ] = ""
734
- }
735
- }
736
- return cached , nil
737
- }
738
-
739
- func (t * TLS ) regularlyReloadUnmanagedCertificates () {
740
- t .unmanagedCertsTicker = time .NewTicker (2 * time .Hour )
741
- go func () {
742
- defer func () {
743
- if err := recover (); err != nil {
744
- log .Printf ("[PANIC] unmanaged certificates reloader: %v\n %s" , err , debug .Stack ())
745
- }
746
- }()
747
- t .reloadUnmanagedCertificates ()
748
- for {
749
- select {
750
- case <- t .storageCleanStop :
751
- return
752
- case <- t .storageCleanTicker .C :
753
- t .cleanStorageUnits ()
754
- }
755
- }
756
- }()
757
- }
758
-
759
- func (t * TLS ) reloadUnmanagedCertificates () error {
760
- for _ , loader := range t .certificateLoaders {
761
- certs , err := loader .LoadCertificates ()
719
+ func (t * TLS ) updateCertificates (ctx caddy.Context , magic * certmagic.Config , add []Certificate , remove []string ) error {
720
+ for _ , cert := range add {
721
+ hash , err := magic .CacheUnmanagedTLSCertificate (ctx , cert .Certificate , cert .Tags )
762
722
if err != nil {
763
- return fmt .Errorf ("loading certificates: %v" , err )
764
- }
765
- for _ , cert := range certs {
766
- hash , err := t .magic .CacheUnmanagedTLSCertificate (t .ctx , cert .Certificate , cert .Tags )
767
- if err != nil {
768
- return fmt .Errorf ("caching unmanaged certificate: %v" , err )
769
- }
770
- t .loaded [hash ] = ""
723
+ return fmt .Errorf ("caching unmanaged certificate: %v" , err )
771
724
}
725
+ t .loaded [hash ] = ""
772
726
}
727
+ certCacheMu .Lock ()
728
+ certCache .Remove (remove )
729
+ certCacheMu .Unlock ()
773
730
return nil
774
731
}
775
732
@@ -878,7 +835,7 @@ func (t *TLS) onEvent(ctx context.Context, eventName string, data map[string]any
878
835
// CertificateLoader is a type that can load certificates.
879
836
// Certificates can optionally be associated with tags.
880
837
type CertificateLoader interface {
881
- LoadCertificates () ( []Certificate , error )
838
+ Initialize ( updateCertificates func ( add []Certificate , remove [] string ) error ) error
882
839
}
883
840
884
841
// Certificate is a TLS certificate, optionally
0 commit comments