2016-11-15 22:59:08 +00:00
|
|
|
// +build linux
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2016-08-10 23:48:17 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-05-16 15:50:55 +00:00
|
|
|
"fmt"
|
2016-10-04 19:01:19 +00:00
|
|
|
"io"
|
2016-08-10 23:48:17 +00:00
|
|
|
"io/ioutil"
|
2016-05-16 15:50:55 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-11-24 01:29:21 +00:00
|
|
|
"reflect"
|
2016-11-24 04:04:44 +00:00
|
|
|
"regexp"
|
2016-05-16 15:50:55 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2016-10-04 19:01:19 +00:00
|
|
|
"github.com/docker/docker/pkg/chrootarchive"
|
2016-05-16 15:50:55 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/plugin/distribution"
|
2016-08-26 17:02:38 +00:00
|
|
|
"github.com/docker/docker/plugin/v2"
|
2016-11-28 19:08:39 +00:00
|
|
|
"github.com/docker/docker/reference"
|
2016-12-13 02:18:17 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-10-04 19:01:19 +00:00
|
|
|
"golang.org/x/net/context"
|
2016-05-16 15:50:55 +00:00
|
|
|
)
|
|
|
|
|
2016-11-24 04:04:44 +00:00
|
|
|
var (
|
|
|
|
validFullID = regexp.MustCompile(`^([a-f0-9]{64})$`)
|
|
|
|
validPartialID = regexp.MustCompile(`^([a-f0-9]{1,64})$`)
|
|
|
|
)
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
// Disable deactivates a plugin, which implies that they cannot be used by containers.
|
|
|
|
func (pm *Manager) Disable(name string) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
p, err := pm.pluginStore.GetByName(name)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
|
|
|
if err := pm.disable(p, c); err != nil {
|
2016-07-18 15:02:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
pm.pluginEventLogger(p.GetID(), name, "disable")
|
2016-07-18 15:02:12 +00:00
|
|
|
return nil
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enable activates a plugin, which implies that they are ready to be used by containers.
|
2016-11-21 17:24:01 +00:00
|
|
|
func (pm *Manager) Enable(name string, config *types.PluginEnableConfig) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
p, err := pm.pluginStore.GetByName(name)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-21 17:24:01 +00:00
|
|
|
|
2016-12-01 19:36:56 +00:00
|
|
|
c := &controller{timeoutInSecs: config.Timeout}
|
|
|
|
if err := pm.enable(p, c, false); err != nil {
|
2016-07-18 15:02:12 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
pm.pluginEventLogger(p.GetID(), name, "enable")
|
2016-07-18 15:02:12 +00:00
|
|
|
return nil
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-08 02:51:47 +00:00
|
|
|
// Inspect examines a plugin config
|
2016-11-24 04:04:44 +00:00
|
|
|
func (pm *Manager) Inspect(refOrID string) (tp types.Plugin, err error) {
|
|
|
|
// Match on full ID
|
|
|
|
if validFullID.MatchString(refOrID) {
|
|
|
|
p, err := pm.pluginStore.GetByID(refOrID)
|
|
|
|
if err == nil {
|
|
|
|
return p.PluginObj, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match on full name
|
|
|
|
if pluginName, err := getPluginName(refOrID); err == nil {
|
|
|
|
if p, err := pm.pluginStore.GetByName(pluginName); err == nil {
|
|
|
|
return p.PluginObj, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match on partial ID
|
|
|
|
if validPartialID.MatchString(refOrID) {
|
|
|
|
p, err := pm.pluginStore.Search(refOrID)
|
|
|
|
if err == nil {
|
|
|
|
return p.PluginObj, nil
|
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
return tp, err
|
|
|
|
}
|
2016-11-24 04:04:44 +00:00
|
|
|
|
2016-11-30 01:31:29 +00:00
|
|
|
return tp, fmt.Errorf("no such plugin name or ID associated with %q", refOrID)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
func (pm *Manager) pull(name string, metaHeader http.Header, authConfig *types.AuthConfig) (reference.Named, distribution.PullData, error) {
|
|
|
|
ref, err := distribution.GetRef(name)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error in distribution.GetRef: %v", err)
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
name = ref.String()
|
|
|
|
|
|
|
|
if p, _ := pm.pluginStore.GetByName(name); p != nil {
|
|
|
|
logrus.Debug("plugin already exists")
|
|
|
|
return nil, nil, fmt.Errorf("%s exists", name)
|
|
|
|
}
|
|
|
|
|
2016-11-28 19:08:39 +00:00
|
|
|
pd, err := distribution.Pull(ref, pm.registryService, metaHeader, authConfig)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error in distribution.Pull(): %v", err)
|
2016-11-24 01:29:21 +00:00
|
|
|
return nil, nil, err
|
2016-11-28 19:08:39 +00:00
|
|
|
}
|
2016-11-24 01:29:21 +00:00
|
|
|
return ref, pd, nil
|
|
|
|
}
|
2016-11-28 19:08:39 +00:00
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
func computePrivileges(pd distribution.PullData) (types.PluginPrivileges, error) {
|
|
|
|
config, err := pd.Config()
|
|
|
|
if err != nil {
|
2016-11-28 19:08:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
var c types.PluginConfig
|
|
|
|
if err := json.Unmarshal(config, &c); err != nil {
|
2016-11-28 19:08:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
var privileges types.PluginPrivileges
|
2016-12-14 01:46:01 +00:00
|
|
|
if c.Network.Type != "null" && c.Network.Type != "bridge" && c.Network.Type != "" {
|
2016-11-24 01:29:21 +00:00
|
|
|
privileges = append(privileges, types.PluginPrivilege{
|
|
|
|
Name: "network",
|
|
|
|
Description: "permissions to access a network",
|
|
|
|
Value: []string{c.Network.Type},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, mount := range c.Mounts {
|
|
|
|
if mount.Source != nil {
|
|
|
|
privileges = append(privileges, types.PluginPrivilege{
|
|
|
|
Name: "mount",
|
|
|
|
Description: "host path to mount",
|
|
|
|
Value: []string{*mount.Source},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, device := range c.Linux.Devices {
|
|
|
|
if device.Path != nil {
|
|
|
|
privileges = append(privileges, types.PluginPrivilege{
|
|
|
|
Name: "device",
|
|
|
|
Description: "host device to access",
|
|
|
|
Value: []string{*device.Path},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.Linux.DeviceCreation {
|
|
|
|
privileges = append(privileges, types.PluginPrivilege{
|
|
|
|
Name: "device-creation",
|
|
|
|
Description: "allow creating devices inside plugin",
|
|
|
|
Value: []string{"true"},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(c.Linux.Capabilities) > 0 {
|
|
|
|
privileges = append(privileges, types.PluginPrivilege{
|
|
|
|
Name: "capabilities",
|
|
|
|
Description: "list of additional capabilities required",
|
|
|
|
Value: c.Linux.Capabilities,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return privileges, nil
|
2016-11-28 19:08:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
// Privileges pulls a plugin config and computes the privileges required to install it.
|
|
|
|
func (pm *Manager) Privileges(name string, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
|
|
|
|
_, pd, err := pm.pull(name, metaHeader, authConfig)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-24 01:29:21 +00:00
|
|
|
return computePrivileges(pd)
|
|
|
|
}
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
// Pull pulls a plugin, check if the correct privileges are provided and install the plugin.
|
|
|
|
func (pm *Manager) Pull(name string, metaHeader http.Header, authConfig *types.AuthConfig, privileges types.PluginPrivileges) (err error) {
|
|
|
|
ref, pd, err := pm.pull(name, metaHeader, authConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredPrivileges, err := computePrivileges(pd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(privileges, requiredPrivileges) {
|
|
|
|
return errors.New("incorrect privileges")
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pluginID := stringid.GenerateNonCryptoID()
|
2016-11-28 19:08:39 +00:00
|
|
|
pluginDir := filepath.Join(pm.libRoot, pluginID)
|
|
|
|
if err := os.MkdirAll(pluginDir, 0755); err != nil {
|
2016-05-16 15:50:55 +00:00
|
|
|
logrus.Debugf("error in MkdirAll: %v", err)
|
2016-11-24 01:29:21 +00:00
|
|
|
return err
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if delErr := os.RemoveAll(pluginDir); delErr != nil {
|
|
|
|
logrus.Warnf("unable to remove %q from failed plugin pull: %v", pluginDir, delErr)
|
|
|
|
}
|
2016-11-28 19:08:39 +00:00
|
|
|
}
|
2016-11-24 01:29:21 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
err = distribution.WritePullData(pd, filepath.Join(pm.libRoot, pluginID), true)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error in distribution.WritePullData(): %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tag := distribution.GetTag(ref)
|
|
|
|
p := v2.NewPlugin(ref.Name(), pluginID, pm.runRoot, pm.libRoot, tag)
|
|
|
|
err = p.InitPlugin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-11-24 01:29:21 +00:00
|
|
|
pm.pluginStore.Add(p)
|
2016-05-16 15:50:55 +00:00
|
|
|
|
2016-11-24 01:29:21 +00:00
|
|
|
pm.pluginEventLogger(pluginID, ref.String(), "pull")
|
|
|
|
|
|
|
|
return nil
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List displays the list of plugins and associated metadata.
|
|
|
|
func (pm *Manager) List() ([]types.Plugin, error) {
|
2016-08-26 17:02:38 +00:00
|
|
|
plugins := pm.pluginStore.GetAll()
|
|
|
|
out := make([]types.Plugin, 0, len(plugins))
|
|
|
|
for _, p := range plugins {
|
2016-07-18 15:02:12 +00:00
|
|
|
out = append(out, p.PluginObj)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes a plugin to the store.
|
|
|
|
func (pm *Manager) Push(name string, metaHeader http.Header, authConfig *types.AuthConfig) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
p, err := pm.pluginStore.GetByName(name)
|
2016-06-27 15:41:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
dest := filepath.Join(pm.libRoot, p.GetID())
|
2016-11-08 02:51:47 +00:00
|
|
|
config, err := ioutil.ReadFile(filepath.Join(dest, "config.json"))
|
2016-08-10 23:48:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var dummy types.Plugin
|
|
|
|
err = json.Unmarshal(config, &dummy)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-27 15:41:53 +00:00
|
|
|
|
2016-11-22 19:21:34 +00:00
|
|
|
rootfs, err := archive.Tar(p.Rootfs, archive.Gzip)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-25 03:57:21 +00:00
|
|
|
defer rootfs.Close()
|
|
|
|
|
2016-08-10 23:48:17 +00:00
|
|
|
_, err = distribution.Push(name, pm.registryService, metaHeader, authConfig, ioutil.NopCloser(bytes.NewReader(config)), rootfs)
|
2016-05-16 15:50:55 +00:00
|
|
|
// XXX: Ignore returning digest for now.
|
|
|
|
// Since digest needs to be written to the ProgressWriter.
|
2016-06-22 17:40:32 +00:00
|
|
|
return err
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes plugin's root directory.
|
2016-12-13 02:18:17 +00:00
|
|
|
func (pm *Manager) Remove(name string, config *types.PluginRmConfig) (err error) {
|
2016-08-26 17:02:38 +00:00
|
|
|
p, err := pm.pluginStore.GetByName(name)
|
2016-12-01 19:36:56 +00:00
|
|
|
pm.mu.RLock()
|
|
|
|
c := pm.cMap[p]
|
|
|
|
pm.mu.RUnlock()
|
|
|
|
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-07 13:59:15 +00:00
|
|
|
|
|
|
|
if !config.ForceRemove {
|
2016-12-01 19:36:56 +00:00
|
|
|
if p.GetRefCount() > 0 {
|
2016-09-07 13:59:15 +00:00
|
|
|
return fmt.Errorf("plugin %s is in use", p.Name())
|
|
|
|
}
|
|
|
|
if p.IsEnabled() {
|
2016-08-26 17:02:38 +00:00
|
|
|
return fmt.Errorf("plugin %s is enabled", p.Name())
|
|
|
|
}
|
2016-09-07 13:59:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.IsEnabled() {
|
2016-12-01 19:36:56 +00:00
|
|
|
if err := pm.disable(p, c); err != nil {
|
2016-08-26 17:02:38 +00:00
|
|
|
logrus.Errorf("failed to disable plugin '%s': %s", p.Name(), err)
|
|
|
|
}
|
2016-07-18 15:02:12 +00:00
|
|
|
}
|
2016-09-07 13:59:15 +00:00
|
|
|
|
2016-11-22 19:21:34 +00:00
|
|
|
id := p.GetID()
|
|
|
|
pluginDir := filepath.Join(pm.libRoot, id)
|
2016-12-13 02:18:17 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == nil || config.ForceRemove {
|
|
|
|
pm.pluginStore.Remove(p)
|
|
|
|
pm.pluginEventLogger(id, name, "remove")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err = os.RemoveAll(pluginDir); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to remove plugin directory")
|
2016-11-22 19:21:34 +00:00
|
|
|
}
|
2016-07-18 15:02:12 +00:00
|
|
|
return nil
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets plugin args
|
|
|
|
func (pm *Manager) Set(name string, args []string) error {
|
2016-08-26 17:02:38 +00:00
|
|
|
p, err := pm.pluginStore.GetByName(name)
|
2016-05-16 15:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-26 17:02:38 +00:00
|
|
|
return p.Set(args)
|
2016-05-16 15:50:55 +00:00
|
|
|
}
|
2016-10-04 19:01:19 +00:00
|
|
|
|
|
|
|
// CreateFromContext creates a plugin from the given pluginDir which contains
|
2016-11-08 02:51:47 +00:00
|
|
|
// both the rootfs and the config.json and a repoName with optional tag.
|
2016-10-04 19:01:19 +00:00
|
|
|
func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.Reader, options *types.PluginCreateOptions) error {
|
2016-11-29 20:55:41 +00:00
|
|
|
repoName := options.RepoName
|
|
|
|
ref, err := distribution.GetRef(repoName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
name := ref.Name()
|
|
|
|
tag := distribution.GetTag(ref)
|
2016-10-04 19:01:19 +00:00
|
|
|
pluginID := stringid.GenerateNonCryptoID()
|
|
|
|
|
2016-11-29 20:55:41 +00:00
|
|
|
p := v2.NewPlugin(name, pluginID, pm.runRoot, pm.libRoot, tag)
|
|
|
|
|
|
|
|
if v, _ := pm.pluginStore.GetByName(p.Name()); v != nil {
|
|
|
|
return fmt.Errorf("plugin %q already exists", p.Name())
|
|
|
|
}
|
|
|
|
|
2016-10-04 19:01:19 +00:00
|
|
|
pluginDir := filepath.Join(pm.libRoot, pluginID)
|
|
|
|
if err := os.MkdirAll(pluginDir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-22 17:42:58 +00:00
|
|
|
// In case an error happens, remove the created directory.
|
2016-11-29 20:55:41 +00:00
|
|
|
if err := pm.createFromContext(ctx, tarCtx, pluginDir, repoName, p); err != nil {
|
2016-11-22 17:42:58 +00:00
|
|
|
if err := os.RemoveAll(pluginDir); err != nil {
|
|
|
|
logrus.Warnf("unable to remove %q from failed plugin creation: %v", pluginDir, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-29 20:55:41 +00:00
|
|
|
func (pm *Manager) createFromContext(ctx context.Context, tarCtx io.Reader, pluginDir, repoName string, p *v2.Plugin) error {
|
2016-10-04 19:01:19 +00:00
|
|
|
if err := chrootarchive.Untar(tarCtx, pluginDir, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.InitPlugin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-22 17:42:58 +00:00
|
|
|
if err := pm.pluginStore.Add(p); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-10-04 19:01:19 +00:00
|
|
|
|
|
|
|
pm.pluginEventLogger(p.GetID(), repoName, "create")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-24 04:04:44 +00:00
|
|
|
|
|
|
|
func getPluginName(name string) (string, error) {
|
|
|
|
named, err := reference.ParseNamed(name) // FIXME: validate
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if reference.IsNameOnly(named) {
|
|
|
|
named = reference.WithDefaultTag(named)
|
|
|
|
}
|
|
|
|
ref, ok := named.(reference.NamedTagged)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("invalid name: %s", named.String())
|
|
|
|
}
|
|
|
|
return ref.String(), nil
|
|
|
|
}
|