2019-02-21 23:10:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-03-17 14:41:24 +00:00
|
|
|
"path"
|
2019-02-21 23:10:43 +00:00
|
|
|
"path/filepath"
|
|
|
|
"plugin"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/Luzifer/nginx-sso/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
func loadPlugins(pluginDir string) error {
|
|
|
|
logger := log.WithField("plugin_dir", pluginDir)
|
|
|
|
|
|
|
|
d, err := os.Stat(pluginDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
logger.Warn("Plugin directory not found, skipping")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.Wrap(err, "Could not stat plugin dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !d.IsDir() {
|
|
|
|
return errors.New("Plugin directory is not a directory")
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:41:24 +00:00
|
|
|
return errors.Wrap(filepath.Walk(pluginDir, func(currentPath string, info os.FileInfo, err error) error {
|
2019-02-21 23:10:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:41:24 +00:00
|
|
|
if !strings.HasSuffix(currentPath, ".so") {
|
2019-02-21 23:10:43 +00:00
|
|
|
// Ignore that file, is not a plugin
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-17 14:41:24 +00:00
|
|
|
logger := log.WithField("plugin", path.Base(currentPath))
|
|
|
|
|
|
|
|
p, err := plugin.Open(currentPath)
|
2019-02-21 23:10:43 +00:00
|
|
|
if err != nil {
|
2019-03-17 14:41:24 +00:00
|
|
|
logger.WithError(err).Error("Unable to open plugin")
|
|
|
|
return nil
|
2019-02-21 23:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := p.Lookup("Register")
|
|
|
|
if err != nil {
|
2019-03-17 14:41:24 +00:00
|
|
|
logger.WithError(err).Error("Unable to find register function")
|
|
|
|
return nil
|
2019-02-21 23:10:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.(func(plugins.RegisterAuthenticatorFunc, plugins.RegisterMFAProviderFunc))(
|
|
|
|
registerAuthenticator,
|
|
|
|
registerMFAProvider,
|
|
|
|
)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}), "Unable to load plugins")
|
|
|
|
}
|