install.go 5.4 KB

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