From 79de6df93d0404790c3bfecfefa9e1458ffcff75 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Wed, 22 Feb 2023 13:39:40 -0500 Subject: cmd: Strict unmarshal for validate (#5383) --- context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'context.go') diff --git a/context.go b/context.go index 330bfa6..c585aa5 100644 --- a/context.go +++ b/context.go @@ -326,7 +326,7 @@ func (ctx Context) LoadModuleByID(id string, rawMsg json.RawMessage) (any, error // fill in its config only if there is a config to fill in if len(rawMsg) > 0 { - err := strictUnmarshalJSON(rawMsg, &val) + err := StrictUnmarshalJSON(rawMsg, &val) if err != nil { return nil, fmt.Errorf("decoding module config: %s: %v", modInfo, err) } -- cgit v1.2.3 From f6bab8ba85b231ea0930282e684c0040001059e6 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 27 Feb 2023 13:58:27 -0500 Subject: context: Rename func to `AppIfConfigured` (#5397) --- context.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'context.go') diff --git a/context.go b/context.go index c585aa5..fcefad1 100644 --- a/context.go +++ b/context.go @@ -426,15 +426,20 @@ func (ctx Context) App(name string) (any, error) { return modVal, nil } -// AppIsConfigured returns whether an app named name has been -// configured. Can be called before calling App() to avoid +// AppIfConfigured returns an app by its name if it has been +// configured. Can be called instead of App() to avoid // instantiating an empty app when that's not desirable. -func (ctx Context) AppIsConfigured(name string) bool { - if _, ok := ctx.cfg.apps[name]; ok { - return true +func (ctx Context) AppIfConfigured(name string) (any, error) { + app, ok := ctx.cfg.apps[name] + if !ok || app == nil { + return nil, nil } - appRaw := ctx.cfg.AppsRaw[name] - return appRaw != nil + + appModule, err := ctx.App(name) + if err != nil { + return nil, err + } + return appModule, nil } // Storage returns the configured Caddy storage implementation. -- cgit v1.2.3 From c6ac350a3b53feb1dfcc24d82421cd20da61a163 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 20 Apr 2023 10:27:40 -0600 Subject: core: Return default logger if no modules loaded Fix report from: https://caddy.community/t/remote-caddyfile-invalid-memory-address-or-nil-pointer-dereference/19700/3 --- context.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'context.go') diff --git a/context.go b/context.go index fcefad1..615192c 100644 --- a/context.go +++ b/context.go @@ -480,6 +480,9 @@ func (ctx Context) Logger(module ...Module) *zap.Logger { if len(module) > 0 { mod = module[0] } + if mod == nil { + return Log() + } return ctx.cfg.Logging.Logger(mod) } -- cgit v1.2.3 From 0e2c7e1d35b287fc0e56d6db2951f791e09b5a37 Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Tue, 11 Jul 2023 13:10:58 -0600 Subject: caddytls: Reuse certificate cache through reloads (#5623) * caddytls: Don't purge cert cache on config reload * Update CertMagic This actually avoids reloading managed certs from storage when already in the cache, d'oh. * Fix bug; re-implement HasCertificateForSubject * Update go.mod: CertMagic tag --- context.go | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'context.go') diff --git a/context.go b/context.go index 615192c..004dee6 100644 --- a/context.go +++ b/context.go @@ -410,6 +410,11 @@ func (ctx Context) loadModuleInline(moduleNameKey, moduleScope string, raw json. // called during the Provision/Validate phase to reference a // module's own host app (since the parent app module is still // in the process of being provisioned, it is not yet ready). +// +// We return any type instead of the App type because it is NOT +// intended for the caller of this method to be the one to start +// or stop App modules. The caller is expected to assert to the +// concrete type. func (ctx Context) App(name string) (any, error) { if app, ok := ctx.cfg.apps[name]; ok { return app, nil @@ -428,18 +433,15 @@ func (ctx Context) App(name string) (any, error) { // AppIfConfigured returns an app by its name if it has been // configured. Can be called instead of App() to avoid -// instantiating an empty app when that's not desirable. -func (ctx Context) AppIfConfigured(name string) (any, error) { - app, ok := ctx.cfg.apps[name] - if !ok || app == nil { - return nil, nil - } - - appModule, err := ctx.App(name) - if err != nil { - return nil, err - } - return appModule, nil +// instantiating an empty app when that's not desirable. If +// the app has not been loaded, nil is returned. +// +// We return any type instead of the App type because it is not +// intended for the caller of this method to be the one to start +// or stop App modules. The caller is expected to assert to the +// concrete type. +func (ctx Context) AppIfConfigured(name string) any { + return ctx.cfg.apps[name] } // Storage returns the configured Caddy storage implementation. -- cgit v1.2.3 From b51dc5d5d0b8764165170af1f54b77d6de8cb5a1 Mon Sep 17 00:00:00 2001 From: Matt Holt Date: Fri, 21 Jul 2023 15:32:20 -0600 Subject: core: Refine mutex during reloads (fix #5628) (#5645) Separate currentCtxMu to protect currentCtx, and a new rawCfgMu to protect rawCfg and synchronize loads. --- context.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'context.go') diff --git a/context.go b/context.go index 004dee6..85978d4 100644 --- a/context.go +++ b/context.go @@ -441,6 +441,12 @@ func (ctx Context) App(name string) (any, error) { // or stop App modules. The caller is expected to assert to the // 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] } -- cgit v1.2.3