install.go 5.4 KB

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