2020-04-20 16:36:34 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-11-09 13:30:09 +00:00
|
|
|
"os"
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/logs"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/yaegi/interp"
|
|
|
|
"github.com/traefik/yaegi/stdlib"
|
2020-04-20 16:36:34 +00:00
|
|
|
)
|
|
|
|
|
2020-09-07 11:58:03 +00:00
|
|
|
// Constructor creates a plugin handler.
|
|
|
|
type Constructor func(context.Context, http.Handler) (http.Handler, error)
|
|
|
|
|
2020-04-20 16:36:34 +00:00
|
|
|
// Builder is a plugin builder.
|
|
|
|
type Builder struct {
|
2022-01-14 11:22:06 +00:00
|
|
|
middlewareBuilders map[string]*middlewareBuilder
|
|
|
|
providerBuilders map[string]providerBuilder
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBuilder creates a new Builder.
|
2021-06-25 13:50:09 +00:00
|
|
|
func NewBuilder(client *Client, plugins map[string]Descriptor, localPlugins map[string]LocalDescriptor) (*Builder, error) {
|
2020-04-20 16:36:34 +00:00
|
|
|
pb := &Builder{
|
2022-01-14 11:22:06 +00:00
|
|
|
middlewareBuilders: map[string]*middlewareBuilder{},
|
|
|
|
providerBuilders: map[string]providerBuilder{},
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for pName, desc := range plugins {
|
|
|
|
manifest, err := client.ReadManifest(desc.ModuleName)
|
|
|
|
if err != nil {
|
|
|
|
_ = client.ResetAll()
|
|
|
|
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
|
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.With().Str("plugin", "plugin-"+pName).Str("module", desc.ModuleName).Logger()
|
|
|
|
|
2021-12-08 16:08:05 +00:00
|
|
|
i := interp.New(interp.Options{
|
|
|
|
GoPath: client.GoPath(),
|
|
|
|
Env: os.Environ(),
|
2022-11-21 17:36:05 +00:00
|
|
|
Stdout: logs.NoLevel(logger, zerolog.DebugLevel),
|
|
|
|
Stderr: logs.NoLevel(logger, zerolog.ErrorLevel),
|
2021-12-08 16:08:05 +00:00
|
|
|
})
|
2021-06-25 13:50:09 +00:00
|
|
|
|
|
|
|
err = i.Use(stdlib.Symbols)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to load symbols: %w", desc.ModuleName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.Use(ppSymbols())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to load provider symbols: %w", desc.ModuleName, err)
|
|
|
|
}
|
2020-04-20 16:36:34 +00:00
|
|
|
|
|
|
|
_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifest.Import))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to import plugin code %q: %w", desc.ModuleName, manifest.Import, err)
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:14:10 +00:00
|
|
|
switch manifest.Type {
|
|
|
|
case "middleware":
|
2022-01-14 11:22:06 +00:00
|
|
|
middleware, err := newMiddlewareBuilder(i, manifest.BasePkg, manifest.Import)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
2022-01-14 11:22:06 +00:00
|
|
|
|
|
|
|
pb.middlewareBuilders[pName] = middleware
|
2021-05-11 14:14:10 +00:00
|
|
|
case "provider":
|
2022-01-14 11:22:06 +00:00
|
|
|
pb.providerBuilders[pName] = providerBuilder{
|
2021-05-11 14:14:10 +00:00
|
|
|
interpreter: i,
|
|
|
|
Import: manifest.Import,
|
|
|
|
BasePkg: manifest.BasePkg,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:50:09 +00:00
|
|
|
for pName, desc := range localPlugins {
|
|
|
|
manifest, err := ReadManifest(localGoPath, desc.ModuleName)
|
2020-04-20 16:36:34 +00:00
|
|
|
if err != nil {
|
2021-06-25 13:50:09 +00:00
|
|
|
return nil, fmt.Errorf("%s: failed to read manifest: %w", desc.ModuleName, err)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:36:05 +00:00
|
|
|
logger := log.With().Str("plugin", "plugin-"+pName).Str("module", desc.ModuleName).Logger()
|
|
|
|
|
2021-12-08 16:08:05 +00:00
|
|
|
i := interp.New(interp.Options{
|
|
|
|
GoPath: localGoPath,
|
|
|
|
Env: os.Environ(),
|
2022-11-21 17:36:05 +00:00
|
|
|
Stdout: logs.NoLevel(logger, zerolog.DebugLevel),
|
|
|
|
Stderr: logs.NoLevel(logger, zerolog.ErrorLevel),
|
2021-12-08 16:08:05 +00:00
|
|
|
})
|
2021-06-25 13:50:09 +00:00
|
|
|
|
|
|
|
err = i.Use(stdlib.Symbols)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to load symbols: %w", desc.ModuleName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = i.Use(ppSymbols())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: failed to load provider symbols: %w", desc.ModuleName, err)
|
|
|
|
}
|
2020-04-20 16:36:34 +00:00
|
|
|
|
|
|
|
_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifest.Import))
|
|
|
|
if err != nil {
|
2021-06-25 13:50:09 +00:00
|
|
|
return nil, fmt.Errorf("%s: failed to import plugin code %q: %w", desc.ModuleName, manifest.Import, err)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:14:10 +00:00
|
|
|
switch manifest.Type {
|
|
|
|
case "middleware":
|
2022-01-14 11:22:06 +00:00
|
|
|
middleware, err := newMiddlewareBuilder(i, manifest.BasePkg, manifest.Import)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-11 14:14:10 +00:00
|
|
|
}
|
2022-01-14 11:22:06 +00:00
|
|
|
|
|
|
|
pb.middlewareBuilders[pName] = middleware
|
2021-05-11 14:14:10 +00:00
|
|
|
case "provider":
|
2022-01-14 11:22:06 +00:00
|
|
|
pb.providerBuilders[pName] = providerBuilder{
|
2021-05-11 14:14:10 +00:00
|
|
|
interpreter: i,
|
|
|
|
Import: manifest.Import,
|
|
|
|
BasePkg: manifest.BasePkg,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknow plugin type: %s", manifest.Type)
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pb, nil
|
|
|
|
}
|