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, 12:00 UTC
context: AppIfConfigured returns error; consider not-yet-provisioned modules (#6292)
* context: Add new `AppStrict()` method to avoid instantiating empty apps * Rename AppStrict -> AppIfConfigured --------- Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
Francis Lavoie committed
May 20, 2024 at 13:14 UTC
a6a45ff6c56d2d0df1ac86f22d38997da3ba3b39
4 files changed
+25
-25
context.go
+13
-16
@@ -453,23 +453,20 @@ func (ctx Context) App(name string) (any, error) {
453
return modVal, nil
454
}
455
456
-// AppIfConfigured returns an app by its name if it has been
457
-// configured. Can be called instead of App() to avoid
458
-// instantiating an empty app when that's not desirable. If
459
-// the app has not been loaded, nil is returned.
460
-//
461
-// We return any type instead of the App type because it is not
462
-// intended for the caller of this method to be the one to start
463
-// or stop App modules. The caller is expected to assert to the
464
-// concrete type.
465
-func (ctx Context) AppIfConfigured(name string) any {
466
- if ctx.cfg == nil {
467
- // this can happen if the currently-active context
468
- // is being accessed, but no config has successfully
469
- // been loaded yet
470
- return nil
456
+// AppIfConfigured is like App, but it returns an error if the
457
+// app has not been configured. This is useful when the app is
458
+// required and its absence is a configuration error, or when
459
+// the app is optional and you don't want to instantiate a
460
+// new one that hasn't been explicitly configured.
461
+func (ctx Context) AppIfConfigured(name string) (any, error) {
462
+ if app, ok := ctx.cfg.apps[name]; ok {
463
+ return app, nil
464
+ }
465
+ appRaw := ctx.cfg.AppsRaw[name]
466
+ if appRaw == nil {
467
+ return nil, fmt.Errorf("app module %s is not configured", name)
468
}
472
- return ctx.cfg.apps[name]
469
+ return ctx.App(name)
470
}
471
472
// Storage returns the configured Caddy storage implementation.
modules/caddypki/adminapi.go
+5
-2
@@ -50,8 +50,11 @@ func (a *adminAPI) Provision(ctx caddy.Context) error {
50
a.ctx = ctx
51
a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032)
52
53
- // Avoid initializing PKI if it wasn't configured
54
- if pkiApp := a.ctx.AppIfConfigured("pki"); pkiApp != nil {
53
+ // Avoid initializing PKI if it wasn't configured.
54
+ // We intentionally ignore the error since it's not
55
+ // fatal if the PKI app is not explicitly configured.
56
+ pkiApp, err := ctx.AppIfConfigured("pki")
57
+ if err == nil {
58
a.pkiApp = pkiApp.(*PKI)
59
}
60
modules/caddytls/capools.go
+6
-6
@@ -187,9 +187,9 @@ func (PKIRootCAPool) CaddyModule() caddy.ModuleInfo {
187
188
// Loads the PKI app and load the root certificates into the certificate pool
189
func (p *PKIRootCAPool) Provision(ctx caddy.Context) error {
190
- pkiApp := ctx.AppIfConfigured("pki")
191
- if pkiApp == nil {
192
- return fmt.Errorf("PKI app not configured")
190
+ pkiApp, err := ctx.AppIfConfigured("pki")
191
+ if err != nil {
192
+ return fmt.Errorf("pki_root CA pool requires that a PKI app is configured: %v", err)
193
}
194
pki := pkiApp.(*caddypki.PKI)
195
for _, caID := range p.Authority {
@@ -259,9 +259,9 @@ func (PKIIntermediateCAPool) CaddyModule() caddy.ModuleInfo {
259
260
// Loads the PKI app and load the intermediate certificates into the certificate pool
261
func (p *PKIIntermediateCAPool) Provision(ctx caddy.Context) error {
262
- pkiApp := ctx.AppIfConfigured("pki")
263
- if pkiApp == nil {
264
- return fmt.Errorf("PKI app not configured")
262
+ pkiApp, err := ctx.AppIfConfigured("pki")
263
+ if err != nil {
264
+ return fmt.Errorf("pki_intermediate CA pool requires that a PKI app is configured: %v", err)
265
}
266
pki := pkiApp.(*caddypki.PKI)
267
for _, caID := range p.Authority {
modules/caddytls/tls.go
+1
-1
@@ -353,7 +353,7 @@ func (t *TLS) Cleanup() error {
353
// if a new TLS app was loaded, remove certificates from the cache that are no longer
354
// being managed or loaded by the new config; if there is no more TLS app running,
355
// then stop cert maintenance and let the cert cache be GC'ed
356
- if nextTLS := caddy.ActiveContext().AppIfConfigured("tls"); nextTLS != nil {
356
+ if nextTLS, err := caddy.ActiveContext().AppIfConfigured("tls"); err == nil && nextTLS != nil {
357
nextTLSApp := nextTLS.(*TLS)
358
359
// compute which certificates were managed or loaded into the cert cache by this