Skip to content

Commit 5c8dc34

Browse files
caddytls: Allow disabling storage cleaning, avoids writing two files (#6593)
1 parent 5823ecc commit 5c8dc34

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

caddyconfig/httpcaddyfile/options.go

+36-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ func init() {
3939
RegisterGlobalOption("fallback_sni", parseOptSingleString)
4040
RegisterGlobalOption("order", parseOptOrder)
4141
RegisterGlobalOption("storage", parseOptStorage)
42-
RegisterGlobalOption("storage_clean_interval", parseOptDuration)
42+
RegisterGlobalOption("storage_check", parseStorageCheck)
43+
RegisterGlobalOption("storage_clean_interval", parseStorageCleanInterval)
4344
RegisterGlobalOption("renew_interval", parseOptDuration)
4445
RegisterGlobalOption("ocsp_interval", parseOptDuration)
4546
RegisterGlobalOption("acme_ca", parseOptSingleString)
@@ -189,6 +190,40 @@ func parseOptStorage(d *caddyfile.Dispenser, _ any) (any, error) {
189190
return storage, nil
190191
}
191192

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+
192227
func parseOptDuration(d *caddyfile.Dispenser, _ any) (any, error) {
193228
if !d.Next() { // consume option name
194229
return nil, d.ArgErr()

caddyconfig/httpcaddyfile/tlsapp.go

+10
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ func (st ServerType) buildTLSApp(
349349
tlsApp.Automation.OnDemand = onDemand
350350
}
351351

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+
352362
// set the storage clean interval if configured
353363
if storageCleanInterval, ok := options["storage_clean_interval"].(caddy.Duration); ok {
354364
if tlsApp.Automation == nil {

caddytest/integration/caddyfile_adapt/global_options.caddyfiletest

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
storage file_system {
1010
root /data
1111
}
12+
storage_check off
13+
storage_clean_interval off
1214
acme_ca https://example.com
1315
acme_ca_root /path/to/ca.crt
1416
ocsp_stapling off
@@ -73,7 +75,9 @@
7375
}
7476
}
7577
},
76-
"disable_ocsp_stapling": true
78+
"disable_ocsp_stapling": true,
79+
"disable_storage_check": true,
80+
"disable_storage_clean": true
7781
}
7882
}
7983
}

modules/caddytls/tls.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ type TLS struct {
9292
// EXPERIMENTAL. Subject to change.
9393
DisableStorageCheck bool `json:"disable_storage_check,omitempty"`
9494

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+
95106
certificateLoaders []CertificateLoader
96107
automateNames []string
97108
ctx caddy.Context
@@ -328,7 +339,11 @@ func (t *TLS) Start() error {
328339
return fmt.Errorf("automate: managing %v: %v", t.automateNames, err)
329340
}
330341

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+
}
332347

333348
return nil
334349
}

0 commit comments

Comments
 (0)