@marcel91200 / caddy Mirror · read-only / commits / 5c8dc344
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 07:00 UTC

caddytls: Allow disabling storage cleaning, avoids writing two files (#6593)

Francis Lavoie committed Nov 5, 2024 at 12:47 UTC 5c8dc344181c49bc7feade4a293bb4eed882b838
4 files changed +67 -3
caddyconfig/httpcaddyfile/options.go
+36 -1
@@ -39,7 +39,8 @@ func init() {
39 RegisterGlobalOption("fallback_sni", parseOptSingleString)
40 RegisterGlobalOption("order", parseOptOrder)
41 RegisterGlobalOption("storage", parseOptStorage)
42 - RegisterGlobalOption("storage_clean_interval", parseOptDuration)
42 + RegisterGlobalOption("storage_check", parseStorageCheck)
43 + RegisterGlobalOption("storage_clean_interval", parseStorageCleanInterval)
44 RegisterGlobalOption("renew_interval", parseOptDuration)
45 RegisterGlobalOption("ocsp_interval", parseOptDuration)
46 RegisterGlobalOption("acme_ca", parseOptSingleString)
@@ -189,6 +190,40 @@ func parseOptStorage(d *caddyfile.Dispenser, _ any) (any, error) {
190 return storage, nil
191 }
192
193 +func parseStorageCheck(d *caddyfile.Dispenser, _ any) (any, error) {
194 + d.Next() // consume option name
195 + if !d.Next() {
196 + return "", d.ArgErr()
197 + }
198 + val := d.Val()
199 + if d.Next() {
200 + return "", d.ArgErr()
201 + }
202 + if val != "off" {
203 + return "", d.Errf("storage_check must be 'off'")
204 + }
205 + return val, nil
206 +}
207 +
208 +func parseStorageCleanInterval(d *caddyfile.Dispenser, _ any) (any, error) {
209 + d.Next() // consume option name
210 + if !d.Next() {
211 + return "", d.ArgErr()
212 + }
213 + val := d.Val()
214 + if d.Next() {
215 + return "", d.ArgErr()
216 + }
217 + if val == "off" {
218 + return false, nil
219 + }
220 + dur, err := caddy.ParseDuration(d.Val())
221 + if err != nil {
222 + return nil, d.Errf("failed to parse storage_clean_interval, must be a duration or 'off' %w", err)
223 + }
224 + return caddy.Duration(dur), nil
225 +}
226 +
227 func parseOptDuration(d *caddyfile.Dispenser, _ any) (any, error) {
228 if !d.Next() { // consume option name
229 return nil, d.ArgErr()
caddyconfig/httpcaddyfile/tlsapp.go
+10
@@ -349,6 +349,16 @@ func (st ServerType) buildTLSApp(
349 tlsApp.Automation.OnDemand = onDemand
350 }
351
352 + // if the storage clean interval is a boolean, then it's "off" to disable cleaning
353 + if sc, ok := options["storage_check"].(string); ok && sc == "off" {
354 + tlsApp.DisableStorageCheck = true
355 + }
356 +
357 + // if the storage clean interval is a boolean, then it's "off" to disable cleaning
358 + if sci, ok := options["storage_clean_interval"].(bool); ok && !sci {
359 + tlsApp.DisableStorageClean = true
360 + }
361 +
362 // set the storage clean interval if configured
363 if storageCleanInterval, ok := options["storage_clean_interval"].(caddy.Duration); ok {
364 if tlsApp.Automation == nil {
caddytest/integration/caddyfile_adapt/global_options.caddyfiletest
+5 -1
@@ -9,6 +9,8 @@
9 storage file_system {
10 root /data
11 }
12 + storage_check off
13 + storage_clean_interval off
14 acme_ca https://example.com
15 acme_ca_root /path/to/ca.crt
16 ocsp_stapling off
@@ -73,7 +75,9 @@
75 }
76 }
77 },
76 - "disable_ocsp_stapling": true
78 + "disable_ocsp_stapling": true,
79 + "disable_storage_check": true,
80 + "disable_storage_clean": true
81 }
82 }
83 }
modules/caddytls/tls.go
+16 -1
@@ -92,6 +92,17 @@ type TLS struct {
92 // EXPERIMENTAL. Subject to change.
93 DisableStorageCheck bool `json:"disable_storage_check,omitempty"`
94
95 + // Disables the automatic cleanup of the storage backend.
96 + // This is useful when TLS is not being used to store certificates
97 + // and the user wants run their server in a read-only mode.
98 + //
99 + // Storage cleaning creates two files: instance.uuid and last_clean.json.
100 + // The instance.uuid file is used to identify the instance of Caddy
101 + // in a cluster. The last_clean.json file is used to store the last
102 + // time the storage was cleaned.
103 + // EXPERIMENTAL. Subject to change.
104 + DisableStorageClean bool `json:"disable_storage_clean,omitempty"`
105 +
106 certificateLoaders []CertificateLoader
107 automateNames []string
108 ctx caddy.Context
@@ -328,7 +339,11 @@ func (t *TLS) Start() error {
339 return fmt.Errorf("automate: managing %v: %v", t.automateNames, err)
340 }
341
331 - t.keepStorageClean()
342 + if !t.DisableStorageClean {
343 + // start the storage cleaner goroutine and ticker,
344 + // which cleans out expired certificates and more
345 + t.keepStorageClean()
346 + }
347
348 return nil
349 }