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>
This commit is contained in:
Francis Lavoie 2024-05-20 13:14:58 -04:00 committed by GitHub
parent 73e094e1dd
commit a6a45ff6c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 25 deletions

View File

@ -453,23 +453,20 @@ func (ctx Context) App(name string) (any, error) {
return modVal, nil return modVal, nil
} }
// AppIfConfigured returns an app by its name if it has been // AppIfConfigured is like App, but it returns an error if the
// configured. Can be called instead of App() to avoid // app has not been configured. This is useful when the app is
// instantiating an empty app when that's not desirable. If // required and its absence is a configuration error, or when
// the app has not been loaded, nil is returned. // the app is optional and you don't want to instantiate a
// // new one that hasn't been explicitly configured.
// We return any type instead of the App type because it is not func (ctx Context) AppIfConfigured(name string) (any, error) {
// intended for the caller of this method to be the one to start if app, ok := ctx.cfg.apps[name]; ok {
// or stop App modules. The caller is expected to assert to the return app, nil
// concrete type.
func (ctx Context) AppIfConfigured(name string) any {
if ctx.cfg == nil {
// this can happen if the currently-active context
// is being accessed, but no config has successfully
// been loaded yet
return nil
} }
return ctx.cfg.apps[name] appRaw := ctx.cfg.AppsRaw[name]
if appRaw == nil {
return nil, fmt.Errorf("app module %s is not configured", name)
}
return ctx.App(name)
} }
// Storage returns the configured Caddy storage implementation. // Storage returns the configured Caddy storage implementation.

View File

@ -50,8 +50,11 @@ func (a *adminAPI) Provision(ctx caddy.Context) error {
a.ctx = ctx a.ctx = ctx
a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032) a.log = ctx.Logger(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032)
// Avoid initializing PKI if it wasn't configured // Avoid initializing PKI if it wasn't configured.
if pkiApp := a.ctx.AppIfConfigured("pki"); pkiApp != nil { // We intentionally ignore the error since it's not
// fatal if the PKI app is not explicitly configured.
pkiApp, err := ctx.AppIfConfigured("pki")
if err == nil {
a.pkiApp = pkiApp.(*PKI) a.pkiApp = pkiApp.(*PKI)
} }

View File

@ -187,9 +187,9 @@ func (PKIRootCAPool) CaddyModule() caddy.ModuleInfo {
// Loads the PKI app and load the root certificates into the certificate pool // Loads the PKI app and load the root certificates into the certificate pool
func (p *PKIRootCAPool) Provision(ctx caddy.Context) error { func (p *PKIRootCAPool) Provision(ctx caddy.Context) error {
pkiApp := ctx.AppIfConfigured("pki") pkiApp, err := ctx.AppIfConfigured("pki")
if pkiApp == nil { if err != nil {
return fmt.Errorf("PKI app not configured") return fmt.Errorf("pki_root CA pool requires that a PKI app is configured: %v", err)
} }
pki := pkiApp.(*caddypki.PKI) pki := pkiApp.(*caddypki.PKI)
for _, caID := range p.Authority { for _, caID := range p.Authority {
@ -259,9 +259,9 @@ func (PKIIntermediateCAPool) CaddyModule() caddy.ModuleInfo {
// Loads the PKI app and load the intermediate certificates into the certificate pool // Loads the PKI app and load the intermediate certificates into the certificate pool
func (p *PKIIntermediateCAPool) Provision(ctx caddy.Context) error { func (p *PKIIntermediateCAPool) Provision(ctx caddy.Context) error {
pkiApp := ctx.AppIfConfigured("pki") pkiApp, err := ctx.AppIfConfigured("pki")
if pkiApp == nil { if err != nil {
return fmt.Errorf("PKI app not configured") return fmt.Errorf("pki_intermediate CA pool requires that a PKI app is configured: %v", err)
} }
pki := pkiApp.(*caddypki.PKI) pki := pkiApp.(*caddypki.PKI)
for _, caID := range p.Authority { for _, caID := range p.Authority {

View File

@ -353,7 +353,7 @@ func (t *TLS) Cleanup() error {
// if a new TLS app was loaded, remove certificates from the cache that are no longer // if a new TLS app was loaded, remove certificates from the cache that are no longer
// being managed or loaded by the new config; if there is no more TLS app running, // being managed or loaded by the new config; if there is no more TLS app running,
// then stop cert maintenance and let the cert cache be GC'ed // then stop cert maintenance and let the cert cache be GC'ed
if nextTLS := caddy.ActiveContext().AppIfConfigured("tls"); nextTLS != nil { if nextTLS, err := caddy.ActiveContext().AppIfConfigured("tls"); err == nil && nextTLS != nil {
nextTLSApp := nextTLS.(*TLS) nextTLSApp := nextTLS.(*TLS)
// compute which certificates were managed or loaded into the cert cache by this // compute which certificates were managed or loaded into the cert cache by this