install.go 5.0 KB

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