Skip to content

Commit a6a45ff

Browse files
francislavoiemholt
andauthored
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 <[email protected]>
1 parent 73e094e commit a6a45ff

File tree

4 files changed

+25
-25
lines changed

4 files changed

+25
-25
lines changed

context.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -453,23 +453,20 @@ func (ctx Context) App(name string) (any, error) {
453453
return modVal, nil
454454
}
455455

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)
471468
}
472-
return ctx.cfg.apps[name]
469+
return ctx.App(name)
473470
}
474471

475472
// Storage returns the configured Caddy storage implementation.

modules/caddypki/adminapi.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ func (a *adminAPI) Provision(ctx caddy.Context) error {
5050
a.ctx = ctx
5151
a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032)
5252

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 {
5558
a.pkiApp = pkiApp.(*PKI)
5659
}
5760

modules/caddytls/capools.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ func (PKIRootCAPool) CaddyModule() caddy.ModuleInfo {
187187

188188
// Loads the PKI app and load the root certificates into the certificate pool
189189
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)
193193
}
194194
pki := pkiApp.(*caddypki.PKI)
195195
for _, caID := range p.Authority {
@@ -259,9 +259,9 @@ func (PKIIntermediateCAPool) CaddyModule() caddy.ModuleInfo {
259259

260260
// Loads the PKI app and load the intermediate certificates into the certificate pool
261261
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)
265265
}
266266
pki := pkiApp.(*caddypki.PKI)
267267
for _, caID := range p.Authority {

modules/caddytls/tls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func (t *TLS) Cleanup() error {
353353
// if a new TLS app was loaded, remove certificates from the cache that are no longer
354354
// being managed or loaded by the new config; if there is no more TLS app running,
355355
// 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 {
357357
nextTLSApp := nextTLS.(*TLS)
358358

359359
// compute which certificates were managed or loaded into the cert cache by this

0 commit comments

Comments
 (0)