traefik/pkg/plugins/plugins.go

193 lines
5 KiB
Go
Raw Normal View History

2020-04-20 16:36:34 +00:00
package plugins
import (
"context"
"errors"
"fmt"
2022-03-28 13:24:08 +00:00
"reflect"
2020-04-20 16:36:34 +00:00
"strings"
"github.com/hashicorp/go-multierror"
"github.com/traefik/traefik/v2/pkg/log"
2020-04-20 16:36:34 +00:00
)
const localGoPath = "./plugins-local/"
// SetupRemotePlugins setup remote plugins environment.
func SetupRemotePlugins(client *Client, plugins map[string]Descriptor) error {
err := checkRemotePluginsConfiguration(plugins)
2020-04-20 16:36:34 +00:00
if err != nil {
return fmt.Errorf("invalid configuration: %w", err)
}
err = client.CleanArchives(plugins)
if err != nil {
return fmt.Errorf("failed to clean archives: %w", err)
}
ctx := context.Background()
for pAlias, desc := range plugins {
log.FromContext(ctx).Debugf("loading of plugin: %s: %s@%s", pAlias, desc.ModuleName, desc.Version)
hash, err := client.Download(ctx, desc.ModuleName, desc.Version)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to download plugin %s: %w", desc.ModuleName, err)
}
err = client.Check(ctx, desc.ModuleName, desc.Version, hash)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to check archive integrity of the plugin %s: %w", desc.ModuleName, err)
}
}
err = client.WriteState(plugins)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to write plugins state: %w", err)
}
for _, desc := range plugins {
err = client.Unzip(desc.ModuleName, desc.Version)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("failed to unzip archive: %w", err)
}
}
return nil
}
func checkRemotePluginsConfiguration(plugins map[string]Descriptor) error {
if plugins == nil {
2020-04-20 16:36:34 +00:00
return nil
}
uniq := make(map[string]struct{})
2020-04-20 16:36:34 +00:00
var errs []string
for pAlias, descriptor := range plugins {
if descriptor.ModuleName == "" {
errs = append(errs, fmt.Sprintf("%s: plugin name is missing", pAlias))
}
2020-04-20 16:36:34 +00:00
if descriptor.Version == "" {
errs = append(errs, fmt.Sprintf("%s: plugin version is missing", pAlias))
}
2020-04-20 16:36:34 +00:00
if strings.HasPrefix(descriptor.ModuleName, "/") || strings.HasSuffix(descriptor.ModuleName, "/") {
errs = append(errs, fmt.Sprintf("%s: plugin name should not start or end with a /", pAlias))
continue
}
2020-04-20 16:36:34 +00:00
if _, ok := uniq[descriptor.ModuleName]; ok {
errs = append(errs, fmt.Sprintf("only one version of a plugin is allowed, there is a duplicate of %s", descriptor.ModuleName))
continue
}
2020-04-20 16:36:34 +00:00
uniq[descriptor.ModuleName] = struct{}{}
2020-04-20 16:36:34 +00:00
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, ": "))
2020-04-20 16:36:34 +00:00
}
return nil
}
// SetupLocalPlugins setup local plugins environment.
func SetupLocalPlugins(plugins map[string]LocalDescriptor) error {
2020-04-20 16:36:34 +00:00
if plugins == nil {
return nil
}
uniq := make(map[string]struct{})
var errs *multierror.Error
2020-04-20 16:36:34 +00:00
for pAlias, descriptor := range plugins {
if descriptor.ModuleName == "" {
errs = multierror.Append(errs, fmt.Errorf("%s: plugin name is missing", pAlias))
2020-04-20 16:36:34 +00:00
}
if strings.HasPrefix(descriptor.ModuleName, "/") || strings.HasSuffix(descriptor.ModuleName, "/") {
errs = multierror.Append(errs, fmt.Errorf("%s: plugin name should not start or end with a /", pAlias))
2020-04-20 16:36:34 +00:00
continue
}
if _, ok := uniq[descriptor.ModuleName]; ok {
errs = multierror.Append(errs, fmt.Errorf("only one version of a plugin is allowed, there is a duplicate of %s", descriptor.ModuleName))
2020-04-20 16:36:34 +00:00
continue
}
uniq[descriptor.ModuleName] = struct{}{}
err := checkLocalPluginManifest(descriptor)
errs = multierror.Append(errs, err)
2020-04-20 16:36:34 +00:00
}
return errs.ErrorOrNil()
}
func checkLocalPluginManifest(descriptor LocalDescriptor) error {
m, err := ReadManifest(localGoPath, descriptor.ModuleName)
if err != nil {
return err
2020-04-20 16:36:34 +00:00
}
var errs *multierror.Error
switch m.Type {
case "middleware", "provider":
// noop
default:
errs = multierror.Append(errs, fmt.Errorf("%s: unsupported type %q", descriptor.ModuleName, m.Type))
}
if m.Import == "" {
errs = multierror.Append(errs, fmt.Errorf("%s: missing import", descriptor.ModuleName))
}
if !strings.HasPrefix(m.Import, descriptor.ModuleName) {
errs = multierror.Append(errs, fmt.Errorf("the import %q must be related to the module name %q", m.Import, descriptor.ModuleName))
}
if m.DisplayName == "" {
errs = multierror.Append(errs, fmt.Errorf("%s: missing DisplayName", descriptor.ModuleName))
}
if m.Summary == "" {
errs = multierror.Append(errs, fmt.Errorf("%s: missing Summary", descriptor.ModuleName))
}
if m.TestData == nil {
errs = multierror.Append(errs, fmt.Errorf("%s: missing TestData", descriptor.ModuleName))
}
return errs.ErrorOrNil()
2020-04-20 16:36:34 +00:00
}
2022-03-28 13:24:08 +00:00
func stringToSliceHookFunc(f reflect.Kind, t reflect.Kind, data interface{}) (interface{}, error) {
if f != reflect.String || t != reflect.Slice {
return data, nil
}
raw := data.(string)
if raw == "" {
return []string{}, nil
}
if strings.Contains(raw, "║") {
values := strings.Split(raw, "║")
// Removes the first value if the slice has a length of 2 and a first value empty.
// It's a workaround to escape the parsing on `,`.
if len(values) == 2 && values[0] == "" {
return values[1:], nil
}
return values, nil
}
return strings.Split(raw, ","), nil
}