traefik/pkg/plugins/plugins.go

169 lines
4.4 KiB
Go
Raw Normal View History

2020-04-20 16:36:34 +00:00
package plugins
import (
"context"
"errors"
"fmt"
"strings"
"github.com/hashicorp/go-multierror"
2022-11-21 17:36:05 +00:00
"github.com/rs/zerolog/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("unable to clean archives: %w", err)
2020-04-20 16:36:34 +00:00
}
ctx := context.Background()
for pAlias, desc := range plugins {
2022-11-21 17:36:05 +00:00
log.Ctx(ctx).Debug().Msgf("Loading of plugin: %s: %s@%s", pAlias, desc.ModuleName, desc.Version)
2020-04-20 16:36:34 +00:00
hash, err := client.Download(ctx, desc.ModuleName, desc.Version)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("unable to download plugin %s: %w", desc.ModuleName, err)
2020-04-20 16:36:34 +00:00
}
err = client.Check(ctx, desc.ModuleName, desc.Version, hash)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("unable to check archive integrity of the plugin %s: %w", desc.ModuleName, err)
2020-04-20 16:36:34 +00:00
}
}
err = client.WriteState(plugins)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("unable to write plugins state: %w", err)
2020-04-20 16:36:34 +00:00
}
for _, desc := range plugins {
err = client.Unzip(desc.ModuleName, desc.Version)
if err != nil {
_ = client.ResetAll()
return fmt.Errorf("unable to unzip archive: %w", err)
2020-04-20 16:36:34 +00:00
}
}
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
}