Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add private plugins support #7515

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 14 additions & 7 deletions cmd/traefik/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,33 @@ import (
const outputDir = "./plugins-storage/"

func initPlugins(staticCfg *static.Configuration) (*plugins.Client, map[string]plugins.Descriptor, *plugins.DevPlugin, error) {
if !isPilotEnabled(staticCfg) || !hasPlugins(staticCfg) {
return nil, map[string]plugins.Descriptor{}, nil, nil

if (!staticCfg.Pilot.Private && !(isPilotEnabled(staticCfg) || hasPlugins(staticCfg))) {
return nil, map[string]plugins.Descriptor{}, nil, nil
}


opts := plugins.ClientOptions{
Output: outputDir,
Token: staticCfg.Pilot.Token,
RepoUrl: staticCfg.Pilot.RepoUrl,
Private: staticCfg.Pilot.Private,
}

client, err := plugins.NewClient(opts)
if err != nil {
return nil, nil, nil, err
}

err = plugins.Setup(client, staticCfg.Experimental.Plugins, staticCfg.Experimental.DevPlugin)
if err != nil {

if hasPlugins(staticCfg) {
err = plugins.Setup(client, staticCfg.Experimental.Plugins, staticCfg.Experimental.DevPlugin)
if err != nil {
return nil, nil, nil, err
}
}
return client, staticCfg.Experimental.Plugins, staticCfg.Experimental.DevPlugin, nil
}

return client, staticCfg.Experimental.Plugins, staticCfg.Experimental.DevPlugin, nil
return client, nil, nil, nil
}

func isPilotEnabled(staticCfg *static.Configuration) bool {
Expand Down
8 changes: 8 additions & 0 deletions pkg/config/static/pilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ package static
// Pilot Configuration related to Traefik Pilot.
type Pilot struct {
Token string `description:"Traefik Pilot token." json:"token,omitempty" toml:"token,omitempty" yaml:"token,omitempty"`
Private bool `description:"Traefik Pilot private enabled." json:"private,omitempty" toml:"private,omitempty" yaml:"private,omitempty"`
RepoUrl string `description:"Traefik Pilot private repository url." json:"repo,omitempty" toml:"repo,omitempty" yaml:"repo,omitempty"`
}

// SetDefaults sets the default values.
func (a *Pilot) SetDefaults() {
a.RepoUrl = "https://plugin.pilot.traefik.io/public/"
}

10 changes: 8 additions & 2 deletions pkg/plugins/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path"
"reflect"
"strings"
"path/filepath"

"github.com/mitchellh/mapstructure"
"github.com/traefik/yaegi/interp"
Expand Down Expand Up @@ -52,8 +53,13 @@ func NewBuilder(client *Client, plugins map[string]Descriptor, devPlugin *DevPlu

i := interp.New(interp.Options{GoPath: client.GoPath()})
i.Use(stdlib.Symbols)

_, err = i.Eval(fmt.Sprintf(`import "%s"`, manifest.Import))
var importPath string
if client.private == true {
importPath = filepath.Base(manifest.Import)
} else {
importPath = manifest.Import
}
_, err = i.Eval(fmt.Sprintf(`import "%s"`, importPath))
if err != nil {
return nil, fmt.Errorf("%s: failed to import plugin code %q: %w", desc.ModuleName, manifest.Import, err)
}
Expand Down
18 changes: 15 additions & 3 deletions pkg/plugins/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
type ClientOptions struct {
Output string
Token string
RepoUrl string
Private bool
}

// Client a Traefik Pilot client.
Expand All @@ -53,11 +55,12 @@ type Client struct {
stateFile string
goPath string
sources string
private bool
}

// NewClient creates a new Traefik Pilot client.
func NewClient(opts ClientOptions) (*Client, error) {
baseURL, err := url.Parse(pilotURL)
baseURL, err := url.Parse(opts.RepoUrl)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -90,6 +93,7 @@ func NewClient(opts ClientOptions) (*Client, error) {
sources: filepath.Join(goPath, goPathSrc),

token: opts.Token,
private: opts.Private,
}, nil
}

Expand Down Expand Up @@ -139,8 +143,13 @@ func (c *Client) Download(ctx context.Context, pName, pVersion string) (string,
return "", fmt.Errorf("failed to compute hash: %w", err)
}
}

endpoint, err := c.baseURL.Parse(path.Join(c.baseURL.Path, "download", pName, pVersion))
var pluginUrl string
if c.private == true {
pluginUrl = path.Join(c.baseURL.Path, pName, pVersion)
} else {
pluginUrl = path.Join(c.baseURL.Path, "download", pName, pVersion)
}
endpoint, err := c.baseURL.Parse(pluginUrl)
if err != nil {
return "", fmt.Errorf("failed to parse endpoint URL: %w", err)
}
Expand Down Expand Up @@ -203,6 +212,9 @@ func (c *Client) Download(ctx context.Context, pName, pVersion string) (string,

// Check checks the plugin archive integrity.
func (c *Client) Check(ctx context.Context, pName, pVersion, hash string) error {
if c.private == true {
return nil
}
endpoint, err := c.baseURL.Parse(path.Join(c.baseURL.Path, "validate", pName, pVersion))
if err != nil {
return fmt.Errorf("failed to parse endpoint URL: %w", err)
Expand Down