install.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package plugin
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "github.com/docker/distribution/reference"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/cli/command"
  10. "github.com/docker/docker/cli/command/image"
  11. "github.com/docker/docker/pkg/jsonmessage"
  12. "github.com/docker/docker/registry"
  13. "github.com/spf13/cobra"
  14. "github.com/spf13/pflag"
  15. "golang.org/x/net/context"
  16. )
  17. type pluginOptions struct {
  18. remote string
  19. localName string
  20. grantPerms bool
  21. disable bool
  22. args []string
  23. skipRemoteCheck bool
  24. }
  25. func loadPullFlags(opts *pluginOptions, flags *pflag.FlagSet) {
  26. flags.BoolVar(&opts.grantPerms, "grant-all-permissions", false, "Grant all permissions necessary to run the plugin")
  27. command.AddTrustVerificationFlags(flags)
  28. }
  29. func newInstallCommand(dockerCli *command.DockerCli) *cobra.Command {
  30. var options pluginOptions
  31. cmd := &cobra.Command{
  32. Use: "install [OPTIONS] PLUGIN [KEY=VALUE...]",
  33. Short: "Install a plugin",
  34. Args: cli.RequiresMinArgs(1),
  35. RunE: func(cmd *cobra.Command, args []string) error {
  36. options.remote = args[0]
  37. if len(args) > 1 {
  38. options.args = args[1:]
  39. }
  40. return runInstall(dockerCli, options)
  41. },
  42. }
  43. flags := cmd.Flags()
  44. loadPullFlags(&options, flags)
  45. flags.BoolVar(&options.disable, "disable", false, "Do not enable the plugin on install")
  46. flags.StringVar(&options.localName, "alias", "", "Local name for plugin")
  47. return cmd
  48. }
  49. type pluginRegistryService struct {
  50. registry.Service
  51. }
  52. func (s pluginRegistryService) ResolveRepository(name reference.Named) (repoInfo *registry.RepositoryInfo, err error) {
  53. repoInfo, err = s.Service.ResolveRepository(name)
  54. if repoInfo != nil {
  55. repoInfo.Class = "plugin"
  56. }
  57. return
  58. }
  59. func newRegistryService() registry.Service {
  60. return pluginRegistryService{
  61. Service: registry.NewService(registry.ServiceOptions{V2Only: true}),
  62. }
  63. }
  64. func buildPullConfig(ctx context.Context, dockerCli *command.DockerCli, opts pluginOptions, cmdName string) (types.PluginInstallOptions, error) {
  65. // Names with both tag and digest will be treated by the daemon
  66. // as a pull by digest with a local name for the tag
  67. // (if no local name is provided).
  68. ref, err := reference.ParseNormalizedNamed(opts.remote)
  69. if err != nil {
  70. return types.PluginInstallOptions{}, err
  71. }
  72. repoInfo, err := registry.ParseRepositoryInfo(ref)
  73. if err != nil {
  74. return types.PluginInstallOptions{}, err
  75. }
  76. remote := ref.String()
  77. _, isCanonical := ref.(reference.Canonical)
  78. if command.IsTrusted() && !isCanonical {
  79. ref = reference.TagNameOnly(ref)
  80. nt, ok := ref.(reference.NamedTagged)
  81. if !ok {
  82. return types.PluginInstallOptions{}, fmt.Errorf("invalid name: %s", ref.String())
  83. }
  84. ctx := context.Background()
  85. trusted, err := image.TrustedReference(ctx, dockerCli, nt, newRegistryService())
  86. if err != nil {
  87. return types.PluginInstallOptions{}, err
  88. }
  89. remote = reference.FamiliarString(trusted)
  90. }
  91. authConfig := command.ResolveAuthConfig(ctx, dockerCli, repoInfo.Index)
  92. encodedAuth, err := command.EncodeAuthToBase64(authConfig)
  93. if err != nil {
  94. return types.PluginInstallOptions{}, err
  95. }
  96. registryAuthFunc := command.RegistryAuthenticationPrivilegedFunc(dockerCli, repoInfo.Index, cmdName)
  97. options := types.PluginInstallOptions{
  98. RegistryAuth: encodedAuth,
  99. RemoteRef: remote,
  100. Disabled: opts.disable,
  101. AcceptAllPermissions: opts.grantPerms,
  102. AcceptPermissionsFunc: acceptPrivileges(dockerCli, opts.remote),
  103. // TODO: Rename PrivilegeFunc, it has nothing to do with privileges
  104. PrivilegeFunc: registryAuthFunc,
  105. Args: opts.args,
  106. }
  107. return options, nil
  108. }
  109. func runInstall(dockerCli *command.DockerCli, opts pluginOptions) error {
  110. var localName string
  111. if opts.localName != "" {
  112. aref, err := reference.ParseNormalizedNamed(opts.localName)
  113. if err != nil {
  114. return err
  115. }
  116. if _, ok := aref.(reference.Canonical); ok {
  117. return fmt.Errorf("invalid name: %s", opts.localName)
  118. }
  119. localName = reference.FamiliarString(reference.TagNameOnly(aref))
  120. }
  121. ctx := context.Background()
  122. options, err := buildPullConfig(ctx, dockerCli, opts, "plugin install")
  123. if err != nil {
  124. return err
  125. }
  126. responseBody, err := dockerCli.Client().PluginInstall(ctx, localName, options)
  127. if err != nil {
  128. if strings.Contains(err.Error(), "target is image") {
  129. return errors.New(err.Error() + " - Use `docker image pull`")
  130. }
  131. return err
  132. }
  133. defer responseBody.Close()
  134. if err := jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil); err != nil {
  135. return err
  136. }
  137. fmt.Fprintf(dockerCli.Out(), "Installed plugin %s\n", opts.remote) // todo: return proper values from the API for this result
  138. return nil
  139. }
  140. func acceptPrivileges(dockerCli *command.DockerCli, name string) func(privileges types.PluginPrivileges) (bool, error) {
  141. return func(privileges types.PluginPrivileges) (bool, error) {
  142. fmt.Fprintf(dockerCli.Out(), "Plugin %q is requesting the following privileges:\n", name)
  143. for _, privilege := range privileges {
  144. fmt.Fprintf(dockerCli.Out(), " - %s: %v\n", privilege.Name, privilege.Value)
  145. }
  146. return command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), "Do you grant the above permissions?"), nil
  147. }
  148. }