From d9d24b6605218b34b0e3c4051625e1e1e0cda472 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 17 Jun 2016 00:33:21 +0200 Subject: [PATCH] =?UTF-8?q?Update=20docker=20plugin=20install=20code?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … for more consistency (api side). Signed-off-by: Vincent Demeester (cherry picked from commit 2c82337b0490dc0a5b8329d1f23c6bad00d8551b) --- api/client/plugin/install.go | 39 ++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/api/client/plugin/install.go b/api/client/plugin/install.go index ef890b1f90..486dc4f622 100644 --- a/api/client/plugin/install.go +++ b/api/client/plugin/install.go @@ -3,12 +3,15 @@ package plugin import ( + "bufio" "fmt" + "strings" "github.com/docker/docker/api/client" "github.com/docker/docker/cli" "github.com/docker/docker/reference" "github.com/docker/docker/registry" + "github.com/docker/engine-api/types" "github.com/spf13/cobra" "golang.org/x/net/context" ) @@ -36,8 +39,8 @@ func newInstallCommand(dockerCli *client.DockerCli) *cobra.Command { return cmd } -func runInstall(dockerCli *client.DockerCli, options pluginOptions) error { - named, err := reference.ParseNamed(options.name) // FIXME: validate +func runInstall(dockerCli *client.DockerCli, opts pluginOptions) error { + named, err := reference.ParseNamed(opts.name) // FIXME: validate if err != nil { return err } @@ -56,6 +59,34 @@ func runInstall(dockerCli *client.DockerCli, options pluginOptions) error { if err != nil { return err } - // TODO: pass noEnable flag - return dockerCli.Client().PluginInstall(ctx, ref.String(), encodedAuth, options.grantPerms, false, dockerCli.In(), dockerCli.Out()) + + requestPrivilege := dockerCli.RegistryAuthenticationPrivilegedFunc(repoInfo.Index, "plugin install") + + // TODO: pass acceptAllPermissions and noEnable flag + options := types.PluginInstallOptions{ + RegistryAuth: encodedAuth, + Disabled: false, + AcceptAllPermissions: opts.grantPerms, + AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.name), + PrivilegeFunc: requestPrivilege, + } + + return dockerCli.Client().PluginInstall(ctx, ref.String(), options) +} + +func acceptPrivileges(dockerCli *client.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) { + return func(privileges types.PluginPrivileges) (bool, error) { + fmt.Fprintf(dockerCli.Out(), "Plugin %q requested the following privileges:\n", name) + for _, privilege := range privileges { + fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value) + } + + fmt.Fprint(dockerCli.Out(), "Do you grant the above permissions? [y/N] ") + reader := bufio.NewReader(dockerCli.In()) + line, _, err := reader.ReadLine() + if err != nil { + return false, err + } + return strings.ToLower(string(line)) == "y", nil + } }