Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Unwrap adapter modules to get underlying modules #5624

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions caddyconfig/configadapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,11 @@ func (am adapterModule) CaddyModule() caddy.ModuleInfo {
}
}

// UnwrapAdapter return the original Adapter, this method must be exported
// to be type-assertable 🤷
// hack, https://github.com/caddyserver/caddy/issues/5621
func (am adapterModule) UnwrapAdapter() any {
return am.Adapter
}

var configAdapters = make(map[string]Adapter)
18 changes: 15 additions & 3 deletions cmd/packagesfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func upgradeBuild(pluginPkgs map[string]struct{}, fl Flags) (int, error) {
return caddy.ExitCodeSuccess, nil
}

func getModPkgPath(iface any) string {
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
}
return reflect.TypeOf(iface).PkgPath()
}

func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
bi, ok := debug.ReadBuildInfo()
if !ok {
Expand All @@ -195,10 +202,15 @@ func getModules() (standard, nonstandard, unknown []moduleInfo, err error) {
// not sure why), and since New() should return a pointer
// value, we need to dereference it first
iface := any(modInfo.New())
if rv := reflect.ValueOf(iface); rv.Kind() == reflect.Ptr {
iface = reflect.New(reflect.TypeOf(iface).Elem()).Elem().Interface()
modPkgPath := getModPkgPath(iface)

// Unwrap config adapters to get the underlying adapter modules, as config adapter modules are hacks anyway. https://github.com/caddyserver/caddy/issues/5621
// this method will only be called if it's from the built-in module to prevent abuse
if strings.HasPrefix(modPkgPath, caddy.ImportPath) {
if unwrapper, ok := iface.(interface{ UnwrapAdapter() any }); ok {
modPkgPath = getModPkgPath(unwrapper.UnwrapAdapter())
}
}
modPkgPath := reflect.TypeOf(iface).PkgPath()

// now we find the Go module that the Caddy module's package
// belongs to; we assume the Caddy module package path will
Expand Down
Loading