discovery.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package plugins // import "github.com/docker/docker/pkg/plugins"
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/fs"
  6. "net/url"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "sync"
  11. "github.com/pkg/errors"
  12. )
  13. // ErrNotFound plugin not found
  14. var ErrNotFound = errors.New("plugin not found")
  15. const defaultSocketsPath = "/run/docker/plugins"
  16. // LocalRegistry defines a registry that is local (using unix socket).
  17. type LocalRegistry struct {
  18. socketsPath string
  19. specsPaths []string
  20. }
  21. func NewLocalRegistry() LocalRegistry {
  22. return LocalRegistry{
  23. socketsPath: defaultSocketsPath,
  24. specsPaths: specsPaths(),
  25. }
  26. }
  27. // Scan scans all the plugin paths and returns all the names it found
  28. func (l *LocalRegistry) Scan() ([]string, error) {
  29. var names []string
  30. dirEntries, err := os.ReadDir(l.socketsPath)
  31. if err != nil && !os.IsNotExist(err) {
  32. return nil, errors.Wrap(err, "error reading dir entries")
  33. }
  34. for _, entry := range dirEntries {
  35. if entry.IsDir() {
  36. fi, err := os.Stat(filepath.Join(l.socketsPath, entry.Name(), entry.Name()+".sock"))
  37. if err != nil {
  38. continue
  39. }
  40. entry = fs.FileInfoToDirEntry(fi)
  41. }
  42. if entry.Type()&os.ModeSocket != 0 {
  43. names = append(names, strings.TrimSuffix(filepath.Base(entry.Name()), filepath.Ext(entry.Name())))
  44. }
  45. }
  46. for _, p := range l.specsPaths {
  47. dirEntries, err = os.ReadDir(p)
  48. if err != nil && !os.IsNotExist(err) {
  49. return nil, errors.Wrap(err, "error reading dir entries")
  50. }
  51. for _, entry := range dirEntries {
  52. if entry.IsDir() {
  53. infos, err := os.ReadDir(filepath.Join(p, entry.Name()))
  54. if err != nil {
  55. continue
  56. }
  57. for _, info := range infos {
  58. if strings.TrimSuffix(info.Name(), filepath.Ext(info.Name())) == entry.Name() {
  59. entry = info
  60. break
  61. }
  62. }
  63. }
  64. switch ext := filepath.Ext(entry.Name()); ext {
  65. case ".spec", ".json":
  66. plugin := strings.TrimSuffix(entry.Name(), ext)
  67. names = append(names, plugin)
  68. default:
  69. }
  70. }
  71. }
  72. return names, nil
  73. }
  74. // Plugin returns the plugin registered with the given name (or returns an error).
  75. func (l *LocalRegistry) Plugin(name string) (*Plugin, error) {
  76. socketPaths := pluginPaths(l.socketsPath, name, ".sock")
  77. for _, p := range socketPaths {
  78. if fi, err := os.Stat(p); err == nil && fi.Mode()&os.ModeSocket != 0 {
  79. return NewLocalPlugin(name, "unix://"+p), nil
  80. }
  81. }
  82. var txtSpecPaths []string
  83. for _, p := range l.specsPaths {
  84. txtSpecPaths = append(txtSpecPaths, pluginPaths(p, name, ".spec")...)
  85. txtSpecPaths = append(txtSpecPaths, pluginPaths(p, name, ".json")...)
  86. }
  87. for _, p := range txtSpecPaths {
  88. if _, err := os.Stat(p); err == nil {
  89. if strings.HasSuffix(p, ".json") {
  90. return readPluginJSONInfo(name, p)
  91. }
  92. return readPluginInfo(name, p)
  93. }
  94. }
  95. return nil, errors.Wrapf(ErrNotFound, "could not find plugin %s in v1 plugin registry", name)
  96. }
  97. // SpecsPaths returns paths in which to look for plugins, in order of priority.
  98. //
  99. // On Windows:
  100. //
  101. // - "%programdata%\docker\plugins"
  102. //
  103. // On Unix in non-rootless mode:
  104. //
  105. // - "/etc/docker/plugins"
  106. // - "/usr/lib/docker/plugins"
  107. //
  108. // On Unix in rootless-mode:
  109. //
  110. // - "$XDG_CONFIG_HOME/docker/plugins" (or "/etc/docker/plugins" if $XDG_CONFIG_HOME is not set)
  111. // - "$HOME/.local/lib/docker/plugins" (pr "/usr/lib/docker/plugins" if $HOME is set)
  112. func SpecsPaths() []string {
  113. return specsPaths()
  114. }
  115. func readPluginInfo(name, path string) (*Plugin, error) {
  116. content, err := os.ReadFile(path)
  117. if err != nil {
  118. return nil, err
  119. }
  120. addr := strings.TrimSpace(string(content))
  121. u, err := url.Parse(addr)
  122. if err != nil {
  123. return nil, err
  124. }
  125. if len(u.Scheme) == 0 {
  126. return nil, fmt.Errorf("Unknown protocol")
  127. }
  128. return NewLocalPlugin(name, addr), nil
  129. }
  130. func readPluginJSONInfo(name, path string) (*Plugin, error) {
  131. f, err := os.Open(path)
  132. if err != nil {
  133. return nil, err
  134. }
  135. defer f.Close()
  136. var p Plugin
  137. if err := json.NewDecoder(f).Decode(&p); err != nil {
  138. return nil, err
  139. }
  140. p.name = name
  141. if p.TLSConfig != nil && len(p.TLSConfig.CAFile) == 0 {
  142. p.TLSConfig.InsecureSkipVerify = true
  143. }
  144. p.activateWait = sync.NewCond(&sync.Mutex{})
  145. return &p, nil
  146. }
  147. func pluginPaths(base, name, ext string) []string {
  148. return []string{
  149. filepath.Join(base, name+ext),
  150. filepath.Join(base, name, name+ext),
  151. }
  152. }