Skip to content

Commit

Permalink
feat: nice errors when failing to load plugins
Browse files Browse the repository at this point in the history
partially fixes #7305
  • Loading branch information
Stebalien committed Jun 5, 2020
1 parent 0cdde03 commit 5239bd5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 41 deletions.
17 changes: 17 additions & 0 deletions plugin/loader/load_nocgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build linux,!cgo,!noplugin darwin,!cgo,!noplugin

package loader

import (
"errors"

iplugin "github.com/ipfs/go-ipfs/plugin"
)

func init() {
loadPluginFunc = nocgoLoadPlugin
}

func nocgoLoadPlugin(fi string) ([]iplugin.Plugin, error) {
return nil, errors.New("not built with cgo support")
}
17 changes: 17 additions & 0 deletions plugin/loader/load_noplugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build noplugin

package loader

import (
"errors"

iplugin "github.com/ipfs/go-ipfs/plugin"
)

func init() {
loadPluginFunc = nopluginLoadPlugin
}

func nopluginLoadPlugin(string) ([]iplugin.Plugin, error) {
return nil, errors.New("not built with plugin support")
}
40 changes: 2 additions & 38 deletions plugin/loader/load_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,16 @@ package loader

import (
"errors"
"fmt"
"os"
"path/filepath"
"plugin"

iplugin "github.com/ipfs/go-ipfs/plugin"
)

func init() {
loadPluginsFunc = linuxLoadFunc
loadPluginFunc = unixLoadPlugin
}

func linuxLoadFunc(pluginDir string) ([]iplugin.Plugin, error) {
var plugins []iplugin.Plugin

err := filepath.Walk(pluginDir, func(fi string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if fi != pluginDir {
log.Warnf("found directory inside plugins directory: %s", fi)
}
return nil
}

if info.Mode().Perm()&0111 == 0 {
// file is not executable let's not load it
// this is to prevent loading plugins from for example non-executable
// mounts, some /tmp mounts are marked as such for security
log.Errorf("non-executable file in plugins directory: %s", fi)
return nil
}

if newPlugins, err := loadPlugin(fi); err == nil {
plugins = append(plugins, newPlugins...)
} else {
return fmt.Errorf("loading plugin %s: %s", fi, err)
}
return nil
})

return plugins, err
}

func loadPlugin(fi string) ([]iplugin.Plugin, error) {
func unixLoadPlugin(fi string) ([]iplugin.Plugin, error) {
pl, err := plugin.Open(fi)
if err != nil {
return nil, err
Expand Down
36 changes: 33 additions & 3 deletions plugin/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"

config "github.com/ipfs/go-ipfs-config"
Expand All @@ -30,8 +31,8 @@ func Preload(plugins ...plugin.Plugin) {

var log = logging.Logger("plugin/loader")

var loadPluginsFunc = func(string) ([]plugin.Plugin, error) {
return nil, nil
var loadPluginFunc = func(string) ([]plugin.Plugin, error) {
return nil, fmt.Errorf("unsupported platform %s", runtime.GOOS)
}

type loaderState int
Expand Down Expand Up @@ -182,7 +183,36 @@ func loadDynamicPlugins(pluginDir string) ([]plugin.Plugin, error) {
return nil, err
}

return loadPluginsFunc(pluginDir)
var plugins []plugin.Plugin

err = filepath.Walk(pluginDir, func(fi string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
if fi != pluginDir {
log.Warnf("found directory inside plugins directory: %s", fi)
}
return nil
}

if info.Mode().Perm()&0111 == 0 {
// file is not executable let's not load it
// this is to prevent loading plugins from for example non-executable
// mounts, some /tmp mounts are marked as such for security
log.Errorf("non-executable file in plugins directory: %s", fi)
return nil
}

if newPlugins, err := loadPluginFunc(fi); err == nil {
plugins = append(plugins, newPlugins...)
} else {
return fmt.Errorf("loading plugin %s: %s", fi, err)
}
return nil
})

return plugins, err
}

// Initialize initializes all loaded plugins
Expand Down
10 changes: 10 additions & 0 deletions test/sharness/t0280-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ test_expect_success "configure the plugin" '

test_plugin true "$IPFS_PATH" "foobar"

test_expect_success "noplugin flag works" '
test_must_fail go run -tags=noplugin github.com/ipfs/go-ipfs/cmd/ipfs id > output 2>&1
test_should_contain "not built with plugin support" output
'

test_expect_success "noplugin flag works" '
CGO_ENABLED=0 test_must_fail go run github.com/ipfs/go-ipfs/cmd/ipfs id > output 2>&1
test_should_contain "not built with cgo support" output
'

test_done

0 comments on commit 5239bd5

Please sign in to comment.