2020-04-20 16:36:34 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2023-07-20 13:02:07 +00:00
|
|
|
"context"
|
2020-04-20 16:36:34 +00:00
|
|
|
"errors"
|
2023-07-20 13:02:07 +00:00
|
|
|
"net/http"
|
2020-04-20 16:36:34 +00:00
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/plugins"
|
2024-01-08 08:10:06 +00:00
|
|
|
"go.opentelemetry.io/otel/trace"
|
2020-04-20 16:36:34 +00:00
|
|
|
)
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
const typeName = "Plugin"
|
|
|
|
|
2020-09-07 11:58:03 +00:00
|
|
|
// PluginsBuilder the plugin's builder interface.
|
|
|
|
type PluginsBuilder interface {
|
|
|
|
Build(pName string, config map[string]interface{}, middlewareName string) (plugins.Constructor, error)
|
|
|
|
}
|
|
|
|
|
2020-04-20 16:36:34 +00:00
|
|
|
func findPluginConfig(rawConfig map[string]dynamic.PluginConf) (string, map[string]interface{}, error) {
|
|
|
|
if len(rawConfig) != 1 {
|
2021-05-11 14:14:10 +00:00
|
|
|
return "", nil, errors.New("invalid configuration: no configuration or too many plugin definition")
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var pluginType string
|
|
|
|
var rawPluginConfig map[string]interface{}
|
|
|
|
|
|
|
|
for pType, pConfig := range rawConfig {
|
|
|
|
pluginType = pType
|
|
|
|
rawPluginConfig = pConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
if pluginType == "" {
|
2021-05-11 14:14:10 +00:00
|
|
|
return "", nil, errors.New("missing plugin type")
|
2020-04-20 16:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pluginType, rawPluginConfig, nil
|
|
|
|
}
|
2023-07-20 13:02:07 +00:00
|
|
|
|
|
|
|
type traceablePlugin struct {
|
|
|
|
name string
|
|
|
|
h http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTraceablePlugin(ctx context.Context, name string, plug plugins.Constructor, next http.Handler) (*traceablePlugin, error) {
|
|
|
|
h, err := plug(ctx, next)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &traceablePlugin{name: name, h: h}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *traceablePlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
s.h.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
|
2024-01-08 08:10:06 +00:00
|
|
|
func (s *traceablePlugin) GetTracingInformation() (string, string, trace.SpanKind) {
|
|
|
|
return s.name, typeName, trace.SpanKindInternal
|
2023-07-20 13:02:07 +00:00
|
|
|
}
|