install.go 5.6 KB

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