backend_linux.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. package plugin // import "github.com/docker/docker/plugin"
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "compress/gzip"
  6. "context"
  7. "encoding/json"
  8. "io"
  9. "net/http"
  10. "os"
  11. "path"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. "github.com/containerd/containerd/content"
  16. "github.com/containerd/containerd/images"
  17. "github.com/containerd/containerd/platforms"
  18. "github.com/containerd/containerd/remotes"
  19. "github.com/containerd/containerd/remotes/docker"
  20. "github.com/docker/distribution/manifest/schema2"
  21. "github.com/docker/distribution/reference"
  22. "github.com/docker/docker/api/types"
  23. "github.com/docker/docker/api/types/filters"
  24. "github.com/docker/docker/dockerversion"
  25. "github.com/docker/docker/errdefs"
  26. "github.com/docker/docker/pkg/authorization"
  27. "github.com/docker/docker/pkg/chrootarchive"
  28. "github.com/docker/docker/pkg/pools"
  29. "github.com/docker/docker/pkg/progress"
  30. "github.com/docker/docker/pkg/stringid"
  31. "github.com/docker/docker/pkg/system"
  32. v2 "github.com/docker/docker/plugin/v2"
  33. "github.com/moby/sys/mount"
  34. digest "github.com/opencontainers/go-digest"
  35. specs "github.com/opencontainers/image-spec/specs-go/v1"
  36. "github.com/pkg/errors"
  37. "github.com/sirupsen/logrus"
  38. )
  39. var acceptedPluginFilterTags = map[string]bool{
  40. "enabled": true,
  41. "capability": true,
  42. }
  43. // Disable deactivates a plugin. This means resources (volumes, networks) cant use them.
  44. func (pm *Manager) Disable(refOrID string, config *types.PluginDisableConfig) error {
  45. p, err := pm.config.Store.GetV2Plugin(refOrID)
  46. if err != nil {
  47. return err
  48. }
  49. pm.mu.RLock()
  50. c := pm.cMap[p]
  51. pm.mu.RUnlock()
  52. if !config.ForceDisable && p.GetRefCount() > 0 {
  53. return errors.WithStack(inUseError(p.Name()))
  54. }
  55. for _, typ := range p.GetTypes() {
  56. if typ.Capability == authorization.AuthZApiImplements {
  57. pm.config.AuthzMiddleware.RemovePlugin(p.Name())
  58. }
  59. }
  60. if err := pm.disable(p, c); err != nil {
  61. return err
  62. }
  63. pm.publisher.Publish(EventDisable{Plugin: p.PluginObj})
  64. pm.config.LogPluginEvent(p.GetID(), refOrID, "disable")
  65. return nil
  66. }
  67. // Enable activates a plugin, which implies that they are ready to be used by containers.
  68. func (pm *Manager) Enable(refOrID string, config *types.PluginEnableConfig) error {
  69. p, err := pm.config.Store.GetV2Plugin(refOrID)
  70. if err != nil {
  71. return err
  72. }
  73. c := &controller{timeoutInSecs: config.Timeout}
  74. if err := pm.enable(p, c, false); err != nil {
  75. return err
  76. }
  77. pm.publisher.Publish(EventEnable{Plugin: p.PluginObj})
  78. pm.config.LogPluginEvent(p.GetID(), refOrID, "enable")
  79. return nil
  80. }
  81. // Inspect examines a plugin config
  82. func (pm *Manager) Inspect(refOrID string) (tp *types.Plugin, err error) {
  83. p, err := pm.config.Store.GetV2Plugin(refOrID)
  84. if err != nil {
  85. return nil, err
  86. }
  87. return &p.PluginObj, nil
  88. }
  89. func computePrivileges(c types.PluginConfig) types.PluginPrivileges {
  90. var privileges types.PluginPrivileges
  91. if c.Network.Type != "null" && c.Network.Type != "bridge" && c.Network.Type != "" {
  92. privileges = append(privileges, types.PluginPrivilege{
  93. Name: "network",
  94. Description: "permissions to access a network",
  95. Value: []string{c.Network.Type},
  96. })
  97. }
  98. if c.IpcHost {
  99. privileges = append(privileges, types.PluginPrivilege{
  100. Name: "host ipc namespace",
  101. Description: "allow access to host ipc namespace",
  102. Value: []string{"true"},
  103. })
  104. }
  105. if c.PidHost {
  106. privileges = append(privileges, types.PluginPrivilege{
  107. Name: "host pid namespace",
  108. Description: "allow access to host pid namespace",
  109. Value: []string{"true"},
  110. })
  111. }
  112. for _, mount := range c.Mounts {
  113. if mount.Source != nil {
  114. privileges = append(privileges, types.PluginPrivilege{
  115. Name: "mount",
  116. Description: "host path to mount",
  117. Value: []string{*mount.Source},
  118. })
  119. }
  120. }
  121. for _, device := range c.Linux.Devices {
  122. if device.Path != nil {
  123. privileges = append(privileges, types.PluginPrivilege{
  124. Name: "device",
  125. Description: "host device to access",
  126. Value: []string{*device.Path},
  127. })
  128. }
  129. }
  130. if c.Linux.AllowAllDevices {
  131. privileges = append(privileges, types.PluginPrivilege{
  132. Name: "allow-all-devices",
  133. Description: "allow 'rwm' access to all devices",
  134. Value: []string{"true"},
  135. })
  136. }
  137. if len(c.Linux.Capabilities) > 0 {
  138. privileges = append(privileges, types.PluginPrivilege{
  139. Name: "capabilities",
  140. Description: "list of additional capabilities required",
  141. Value: c.Linux.Capabilities,
  142. })
  143. }
  144. return privileges
  145. }
  146. // Privileges pulls a plugin config and computes the privileges required to install it.
  147. func (pm *Manager) Privileges(ctx context.Context, ref reference.Named, metaHeader http.Header, authConfig *types.AuthConfig) (types.PluginPrivileges, error) {
  148. var (
  149. config types.PluginConfig
  150. configSeen bool
  151. )
  152. h := func(ctx context.Context, desc specs.Descriptor) ([]specs.Descriptor, error) {
  153. switch desc.MediaType {
  154. case schema2.MediaTypeManifest, specs.MediaTypeImageManifest:
  155. data, err := content.ReadBlob(ctx, pm.blobStore, desc)
  156. if err != nil {
  157. return nil, errors.Wrapf(err, "error reading image manifest from blob store for %s", ref)
  158. }
  159. var m specs.Manifest
  160. if err := json.Unmarshal(data, &m); err != nil {
  161. return nil, errors.Wrapf(err, "error unmarshaling image manifest for %s", ref)
  162. }
  163. return []specs.Descriptor{m.Config}, nil
  164. case schema2.MediaTypePluginConfig:
  165. configSeen = true
  166. data, err := content.ReadBlob(ctx, pm.blobStore, desc)
  167. if err != nil {
  168. return nil, errors.Wrapf(err, "error reading plugin config from blob store for %s", ref)
  169. }
  170. if err := json.Unmarshal(data, &config); err != nil {
  171. return nil, errors.Wrapf(err, "error unmarshaling plugin config for %s", ref)
  172. }
  173. }
  174. return nil, nil
  175. }
  176. if err := pm.fetch(ctx, ref, authConfig, progress.DiscardOutput(), metaHeader, images.HandlerFunc(h)); err != nil {
  177. return types.PluginPrivileges{}, nil
  178. }
  179. if !configSeen {
  180. return types.PluginPrivileges{}, errors.Errorf("did not find plugin config for specified reference %s", ref)
  181. }
  182. return computePrivileges(config), nil
  183. }
  184. // Upgrade upgrades a plugin
  185. //
  186. // TODO: replace reference package usage with simpler url.Parse semantics
  187. func (pm *Manager) Upgrade(ctx context.Context, ref reference.Named, name string, metaHeader http.Header, authConfig *types.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer) (err error) {
  188. p, err := pm.config.Store.GetV2Plugin(name)
  189. if err != nil {
  190. return err
  191. }
  192. if p.IsEnabled() {
  193. return errors.Wrap(enabledError(p.Name()), "plugin must be disabled before upgrading")
  194. }
  195. // revalidate because Pull is public
  196. if _, err := reference.ParseNormalizedNamed(name); err != nil {
  197. return errors.Wrapf(errdefs.InvalidParameter(err), "failed to parse %q", name)
  198. }
  199. pm.muGC.RLock()
  200. defer pm.muGC.RUnlock()
  201. tmpRootFSDir, err := os.MkdirTemp(pm.tmpDir(), ".rootfs")
  202. if err != nil {
  203. return errors.Wrap(err, "error creating tmp dir for plugin rootfs")
  204. }
  205. var md fetchMeta
  206. ctx, cancel := context.WithCancel(ctx)
  207. out, waitProgress := setupProgressOutput(outStream, cancel)
  208. defer waitProgress()
  209. if err := pm.fetch(ctx, ref, authConfig, out, metaHeader, storeFetchMetadata(&md), childrenHandler(pm.blobStore), applyLayer(pm.blobStore, tmpRootFSDir, out)); err != nil {
  210. return err
  211. }
  212. pm.config.LogPluginEvent(reference.FamiliarString(ref), name, "pull")
  213. if err := validateFetchedMetadata(md); err != nil {
  214. return err
  215. }
  216. if err := pm.upgradePlugin(p, md.config, md.manifest, md.blobs, tmpRootFSDir, &privileges); err != nil {
  217. return err
  218. }
  219. p.PluginObj.PluginReference = ref.String()
  220. return nil
  221. }
  222. // Pull pulls a plugin, check if the correct privileges are provided and install the plugin.
  223. //
  224. // TODO: replace reference package usage with simpler url.Parse semantics
  225. func (pm *Manager) Pull(ctx context.Context, ref reference.Named, name string, metaHeader http.Header, authConfig *types.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer, opts ...CreateOpt) (err error) {
  226. pm.muGC.RLock()
  227. defer pm.muGC.RUnlock()
  228. // revalidate because Pull is public
  229. nameref, err := reference.ParseNormalizedNamed(name)
  230. if err != nil {
  231. return errors.Wrapf(errdefs.InvalidParameter(err), "failed to parse %q", name)
  232. }
  233. name = reference.FamiliarString(reference.TagNameOnly(nameref))
  234. if err := pm.config.Store.validateName(name); err != nil {
  235. return errdefs.InvalidParameter(err)
  236. }
  237. tmpRootFSDir, err := os.MkdirTemp(pm.tmpDir(), ".rootfs")
  238. if err != nil {
  239. return errors.Wrap(errdefs.System(err), "error preparing upgrade")
  240. }
  241. defer os.RemoveAll(tmpRootFSDir)
  242. var md fetchMeta
  243. ctx, cancel := context.WithCancel(ctx)
  244. out, waitProgress := setupProgressOutput(outStream, cancel)
  245. defer waitProgress()
  246. if err := pm.fetch(ctx, ref, authConfig, out, metaHeader, storeFetchMetadata(&md), childrenHandler(pm.blobStore), applyLayer(pm.blobStore, tmpRootFSDir, out)); err != nil {
  247. return err
  248. }
  249. pm.config.LogPluginEvent(reference.FamiliarString(ref), name, "pull")
  250. if err := validateFetchedMetadata(md); err != nil {
  251. return err
  252. }
  253. refOpt := func(p *v2.Plugin) {
  254. p.PluginObj.PluginReference = ref.String()
  255. }
  256. optsList := make([]CreateOpt, 0, len(opts)+1)
  257. optsList = append(optsList, opts...)
  258. optsList = append(optsList, refOpt)
  259. // TODO: tmpRootFSDir is empty but should have layers in it
  260. p, err := pm.createPlugin(name, md.config, md.manifest, md.blobs, tmpRootFSDir, &privileges, optsList...)
  261. if err != nil {
  262. return err
  263. }
  264. pm.publisher.Publish(EventCreate{Plugin: p.PluginObj})
  265. return nil
  266. }
  267. // List displays the list of plugins and associated metadata.
  268. func (pm *Manager) List(pluginFilters filters.Args) ([]types.Plugin, error) {
  269. if err := pluginFilters.Validate(acceptedPluginFilterTags); err != nil {
  270. return nil, err
  271. }
  272. enabledOnly := false
  273. disabledOnly := false
  274. if pluginFilters.Contains("enabled") {
  275. if pluginFilters.ExactMatch("enabled", "true") {
  276. enabledOnly = true
  277. } else if pluginFilters.ExactMatch("enabled", "false") {
  278. disabledOnly = true
  279. } else {
  280. return nil, invalidFilter{"enabled", pluginFilters.Get("enabled")}
  281. }
  282. }
  283. plugins := pm.config.Store.GetAll()
  284. out := make([]types.Plugin, 0, len(plugins))
  285. next:
  286. for _, p := range plugins {
  287. if enabledOnly && !p.PluginObj.Enabled {
  288. continue
  289. }
  290. if disabledOnly && p.PluginObj.Enabled {
  291. continue
  292. }
  293. if pluginFilters.Contains("capability") {
  294. for _, f := range p.GetTypes() {
  295. if !pluginFilters.Match("capability", f.Capability) {
  296. continue next
  297. }
  298. }
  299. }
  300. out = append(out, p.PluginObj)
  301. }
  302. return out, nil
  303. }
  304. // Push pushes a plugin to the registry.
  305. func (pm *Manager) Push(ctx context.Context, name string, metaHeader http.Header, authConfig *types.AuthConfig, outStream io.Writer) error {
  306. p, err := pm.config.Store.GetV2Plugin(name)
  307. if err != nil {
  308. return err
  309. }
  310. ref, err := reference.ParseNormalizedNamed(p.Name())
  311. if err != nil {
  312. return errors.Wrapf(err, "plugin has invalid name %v for push", p.Name())
  313. }
  314. statusTracker := docker.NewInMemoryTracker()
  315. resolver, err := pm.newResolver(ctx, statusTracker, authConfig, metaHeader, false)
  316. if err != nil {
  317. return err
  318. }
  319. pusher, err := resolver.Pusher(ctx, ref.String())
  320. if err != nil {
  321. return errors.Wrap(err, "error creating plugin pusher")
  322. }
  323. pj := newPushJobs(statusTracker)
  324. ctx, cancel := context.WithCancel(ctx)
  325. out, waitProgress := setupProgressOutput(outStream, cancel)
  326. defer waitProgress()
  327. progressHandler := images.HandlerFunc(func(ctx context.Context, desc specs.Descriptor) ([]specs.Descriptor, error) {
  328. logrus.WithField("mediaType", desc.MediaType).WithField("digest", desc.Digest.String()).Debug("Preparing to push plugin layer")
  329. id := stringid.TruncateID(desc.Digest.String())
  330. pj.add(remotes.MakeRefKey(ctx, desc), id)
  331. progress.Update(out, id, "Preparing")
  332. return nil, nil
  333. })
  334. desc, err := pm.getManifestDescriptor(ctx, p)
  335. if err != nil {
  336. return errors.Wrap(err, "error reading plugin manifest")
  337. }
  338. progress.Messagef(out, "", "The push refers to repository [%s]", reference.FamiliarName(ref))
  339. // TODO: If a layer already exists on the registry, the progress output just says "Preparing"
  340. go func() {
  341. timer := time.NewTimer(100 * time.Millisecond)
  342. defer timer.Stop()
  343. if !timer.Stop() {
  344. <-timer.C
  345. }
  346. var statuses []contentStatus
  347. for {
  348. timer.Reset(100 * time.Millisecond)
  349. select {
  350. case <-ctx.Done():
  351. return
  352. case <-timer.C:
  353. statuses = pj.status()
  354. }
  355. for _, s := range statuses {
  356. out.WriteProgress(progress.Progress{ID: s.Ref, Current: s.Offset, Total: s.Total, Action: s.Status, LastUpdate: s.Offset == s.Total})
  357. }
  358. }
  359. }()
  360. // Make sure we can authenticate the request since the auth scope for plugin repos is different than a normal repo.
  361. ctx = docker.WithScope(ctx, scope(ref, true))
  362. if err := remotes.PushContent(ctx, pusher, desc, pm.blobStore, nil, nil, func(h images.Handler) images.Handler {
  363. return images.Handlers(progressHandler, h)
  364. }); err != nil {
  365. // Try fallback to http.
  366. // This is needed because the containerd pusher will only attempt the first registry config we pass, which would
  367. // typically be https.
  368. // If there are no http-only host configs found we'll error out anyway.
  369. resolver, _ := pm.newResolver(ctx, statusTracker, authConfig, metaHeader, true)
  370. if resolver != nil {
  371. pusher, _ := resolver.Pusher(ctx, ref.String())
  372. if pusher != nil {
  373. logrus.WithField("ref", ref).Debug("Re-attmpting push with http-fallback")
  374. err2 := remotes.PushContent(ctx, pusher, desc, pm.blobStore, nil, nil, func(h images.Handler) images.Handler {
  375. return images.Handlers(progressHandler, h)
  376. })
  377. if err2 == nil {
  378. err = nil
  379. } else {
  380. logrus.WithError(err2).WithField("ref", ref).Debug("Error while attempting push with http-fallback")
  381. }
  382. }
  383. }
  384. if err != nil {
  385. return errors.Wrap(err, "error pushing plugin")
  386. }
  387. }
  388. // For blobs that already exist in the registry we need to make sure to update the progress otherwise it will just say "pending"
  389. // TODO: How to check if the layer already exists? Is it worth it?
  390. for _, j := range pj.jobs {
  391. progress.Update(out, pj.names[j], "Upload complete")
  392. }
  393. // Signal the client for content trust verification
  394. progress.Aux(out, types.PushResult{Tag: ref.(reference.Tagged).Tag(), Digest: desc.Digest.String(), Size: int(desc.Size)})
  395. return nil
  396. }
  397. // manifest wraps an OCI manifest, because...
  398. // Historically the registry does not support plugins unless the media type on the manifest is specifically schema2.MediaTypeManifest
  399. // So the OCI manifest media type is not supported.
  400. // Additionally, there is extra validation for the docker schema2 manifest than there is a mediatype set on the manifest itself
  401. // even though this is set on the descriptor
  402. // The OCI types do not have this field.
  403. type manifest struct {
  404. specs.Manifest
  405. MediaType string `json:"mediaType,omitempty"`
  406. }
  407. func buildManifest(ctx context.Context, s content.Manager, config digest.Digest, layers []digest.Digest) (manifest, error) {
  408. var m manifest
  409. m.MediaType = images.MediaTypeDockerSchema2Manifest
  410. m.SchemaVersion = 2
  411. configInfo, err := s.Info(ctx, config)
  412. if err != nil {
  413. return m, errors.Wrapf(err, "error reading plugin config content for digest %s", config)
  414. }
  415. m.Config = specs.Descriptor{
  416. MediaType: mediaTypePluginConfig,
  417. Size: configInfo.Size,
  418. Digest: configInfo.Digest,
  419. }
  420. for _, l := range layers {
  421. info, err := s.Info(ctx, l)
  422. if err != nil {
  423. return m, errors.Wrapf(err, "error fetching info for content digest %s", l)
  424. }
  425. m.Layers = append(m.Layers, specs.Descriptor{
  426. MediaType: images.MediaTypeDockerSchema2LayerGzip, // TODO: This is assuming everything is a gzip compressed layer, but that may not be true.
  427. Digest: l,
  428. Size: info.Size,
  429. })
  430. }
  431. return m, nil
  432. }
  433. // getManifestDescriptor gets the OCI descriptor for a manifest
  434. // It will generate a manifest if one does not exist
  435. func (pm *Manager) getManifestDescriptor(ctx context.Context, p *v2.Plugin) (specs.Descriptor, error) {
  436. logger := logrus.WithField("plugin", p.Name()).WithField("digest", p.Manifest)
  437. if p.Manifest != "" {
  438. info, err := pm.blobStore.Info(ctx, p.Manifest)
  439. if err == nil {
  440. desc := specs.Descriptor{
  441. Size: info.Size,
  442. Digest: info.Digest,
  443. MediaType: images.MediaTypeDockerSchema2Manifest,
  444. }
  445. return desc, nil
  446. }
  447. logger.WithError(err).Debug("Could not find plugin manifest in content store")
  448. } else {
  449. logger.Info("Plugin does not have manifest digest")
  450. }
  451. logger.Info("Building a new plugin manifest")
  452. manifest, err := buildManifest(ctx, pm.blobStore, p.Config, p.Blobsums)
  453. if err != nil {
  454. return specs.Descriptor{}, err
  455. }
  456. desc, err := writeManifest(ctx, pm.blobStore, &manifest)
  457. if err != nil {
  458. return desc, err
  459. }
  460. if err := pm.save(p); err != nil {
  461. logger.WithError(err).Error("Could not save plugin with manifest digest")
  462. }
  463. return desc, nil
  464. }
  465. func writeManifest(ctx context.Context, cs content.Store, m *manifest) (specs.Descriptor, error) {
  466. platform := platforms.DefaultSpec()
  467. desc := specs.Descriptor{
  468. MediaType: images.MediaTypeDockerSchema2Manifest,
  469. Platform: &platform,
  470. }
  471. data, err := json.Marshal(m)
  472. if err != nil {
  473. return desc, errors.Wrap(err, "error encoding manifest")
  474. }
  475. desc.Digest = digest.FromBytes(data)
  476. desc.Size = int64(len(data))
  477. if err := content.WriteBlob(ctx, cs, remotes.MakeRefKey(ctx, desc), bytes.NewReader(data), desc); err != nil {
  478. return desc, errors.Wrap(err, "error writing plugin manifest")
  479. }
  480. return desc, nil
  481. }
  482. // Remove deletes plugin's root directory.
  483. func (pm *Manager) Remove(name string, config *types.PluginRmConfig) error {
  484. p, err := pm.config.Store.GetV2Plugin(name)
  485. pm.mu.RLock()
  486. c := pm.cMap[p]
  487. pm.mu.RUnlock()
  488. if err != nil {
  489. return err
  490. }
  491. if !config.ForceRemove {
  492. if p.GetRefCount() > 0 {
  493. return inUseError(p.Name())
  494. }
  495. if p.IsEnabled() {
  496. return enabledError(p.Name())
  497. }
  498. }
  499. if p.IsEnabled() {
  500. if err := pm.disable(p, c); err != nil {
  501. logrus.Errorf("failed to disable plugin '%s': %s", p.Name(), err)
  502. }
  503. }
  504. defer func() {
  505. go pm.GC()
  506. }()
  507. id := p.GetID()
  508. pluginDir := filepath.Join(pm.config.Root, id)
  509. if err := mount.RecursiveUnmount(pluginDir); err != nil {
  510. return errors.Wrap(err, "error unmounting plugin data")
  511. }
  512. if err := atomicRemoveAll(pluginDir); err != nil {
  513. return err
  514. }
  515. pm.config.Store.Remove(p)
  516. pm.config.LogPluginEvent(id, name, "remove")
  517. pm.publisher.Publish(EventRemove{Plugin: p.PluginObj})
  518. return nil
  519. }
  520. // Set sets plugin args
  521. func (pm *Manager) Set(name string, args []string) error {
  522. p, err := pm.config.Store.GetV2Plugin(name)
  523. if err != nil {
  524. return err
  525. }
  526. if err := p.Set(args); err != nil {
  527. return err
  528. }
  529. return pm.save(p)
  530. }
  531. // CreateFromContext creates a plugin from the given pluginDir which contains
  532. // both the rootfs and the config.json and a repoName with optional tag.
  533. func (pm *Manager) CreateFromContext(ctx context.Context, tarCtx io.ReadCloser, options *types.PluginCreateOptions) (err error) {
  534. pm.muGC.RLock()
  535. defer pm.muGC.RUnlock()
  536. ref, err := reference.ParseNormalizedNamed(options.RepoName)
  537. if err != nil {
  538. return errors.Wrapf(err, "failed to parse reference %v", options.RepoName)
  539. }
  540. if _, ok := ref.(reference.Canonical); ok {
  541. return errors.Errorf("canonical references are not permitted")
  542. }
  543. name := reference.FamiliarString(reference.TagNameOnly(ref))
  544. if err := pm.config.Store.validateName(name); err != nil { // fast check, real check is in createPlugin()
  545. return err
  546. }
  547. tmpRootFSDir, err := os.MkdirTemp(pm.tmpDir(), ".rootfs")
  548. if err != nil {
  549. return errors.Wrap(err, "failed to create temp directory")
  550. }
  551. defer os.RemoveAll(tmpRootFSDir)
  552. var configJSON []byte
  553. rootFS := splitConfigRootFSFromTar(tarCtx, &configJSON)
  554. rootFSBlob, err := pm.blobStore.Writer(ctx, content.WithRef(name))
  555. if err != nil {
  556. return err
  557. }
  558. defer rootFSBlob.Close()
  559. gzw := gzip.NewWriter(rootFSBlob)
  560. rootFSReader := io.TeeReader(rootFS, gzw)
  561. if err := chrootarchive.Untar(rootFSReader, tmpRootFSDir, nil); err != nil {
  562. return err
  563. }
  564. if err := rootFS.Close(); err != nil {
  565. return err
  566. }
  567. if configJSON == nil {
  568. return errors.New("config not found")
  569. }
  570. if err := gzw.Close(); err != nil {
  571. return errors.Wrap(err, "error closing gzip writer")
  572. }
  573. var config types.PluginConfig
  574. if err := json.Unmarshal(configJSON, &config); err != nil {
  575. return errors.Wrap(err, "failed to parse config")
  576. }
  577. if err := pm.validateConfig(config); err != nil {
  578. return err
  579. }
  580. pm.mu.Lock()
  581. defer pm.mu.Unlock()
  582. if err := rootFSBlob.Commit(ctx, 0, ""); err != nil {
  583. return err
  584. }
  585. defer func() {
  586. if err != nil {
  587. go pm.GC()
  588. }
  589. }()
  590. config.Rootfs = &types.PluginConfigRootfs{
  591. Type: "layers",
  592. DiffIds: []string{rootFSBlob.Digest().String()},
  593. }
  594. config.DockerVersion = dockerversion.Version
  595. configBlob, err := pm.blobStore.Writer(ctx, content.WithRef(name+"-config.json"))
  596. if err != nil {
  597. return err
  598. }
  599. defer configBlob.Close()
  600. if err := json.NewEncoder(configBlob).Encode(config); err != nil {
  601. return errors.Wrap(err, "error encoding json config")
  602. }
  603. if err := configBlob.Commit(ctx, 0, ""); err != nil {
  604. return err
  605. }
  606. configDigest := configBlob.Digest()
  607. layers := []digest.Digest{rootFSBlob.Digest()}
  608. manifest, err := buildManifest(ctx, pm.blobStore, configDigest, layers)
  609. if err != nil {
  610. return err
  611. }
  612. desc, err := writeManifest(ctx, pm.blobStore, &manifest)
  613. if err != nil {
  614. return
  615. }
  616. p, err := pm.createPlugin(name, configDigest, desc.Digest, layers, tmpRootFSDir, nil)
  617. if err != nil {
  618. return err
  619. }
  620. p.PluginObj.PluginReference = name
  621. pm.publisher.Publish(EventCreate{Plugin: p.PluginObj})
  622. pm.config.LogPluginEvent(p.PluginObj.ID, name, "create")
  623. return nil
  624. }
  625. func (pm *Manager) validateConfig(config types.PluginConfig) error {
  626. return nil // TODO:
  627. }
  628. func splitConfigRootFSFromTar(in io.ReadCloser, config *[]byte) io.ReadCloser {
  629. pr, pw := io.Pipe()
  630. go func() {
  631. tarReader := tar.NewReader(in)
  632. tarWriter := tar.NewWriter(pw)
  633. defer in.Close()
  634. hasRootFS := false
  635. for {
  636. hdr, err := tarReader.Next()
  637. if err == io.EOF {
  638. if !hasRootFS {
  639. pw.CloseWithError(errors.Wrap(err, "no rootfs found"))
  640. return
  641. }
  642. // Signals end of archive.
  643. tarWriter.Close()
  644. pw.Close()
  645. return
  646. }
  647. if err != nil {
  648. pw.CloseWithError(errors.Wrap(err, "failed to read from tar"))
  649. return
  650. }
  651. content := io.Reader(tarReader)
  652. name := path.Clean(hdr.Name)
  653. if path.IsAbs(name) {
  654. name = name[1:]
  655. }
  656. if name == configFileName {
  657. dt, err := io.ReadAll(content)
  658. if err != nil {
  659. pw.CloseWithError(errors.Wrapf(err, "failed to read %s", configFileName))
  660. return
  661. }
  662. *config = dt
  663. }
  664. if parts := strings.Split(name, "/"); len(parts) != 0 && parts[0] == rootFSFileName {
  665. hdr.Name = path.Clean(path.Join(parts[1:]...))
  666. if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(strings.ToLower(hdr.Linkname), rootFSFileName+"/") {
  667. hdr.Linkname = hdr.Linkname[len(rootFSFileName)+1:]
  668. }
  669. if err := tarWriter.WriteHeader(hdr); err != nil {
  670. pw.CloseWithError(errors.Wrap(err, "error writing tar header"))
  671. return
  672. }
  673. if _, err := pools.Copy(tarWriter, content); err != nil {
  674. pw.CloseWithError(errors.Wrap(err, "error copying tar data"))
  675. return
  676. }
  677. hasRootFS = true
  678. } else {
  679. io.Copy(io.Discard, content)
  680. }
  681. }
  682. }()
  683. return pr
  684. }
  685. func atomicRemoveAll(dir string) error {
  686. renamed := dir + "-removing"
  687. err := os.Rename(dir, renamed)
  688. switch {
  689. case os.IsNotExist(err), err == nil:
  690. // even if `dir` doesn't exist, we can still try and remove `renamed`
  691. case os.IsExist(err):
  692. // Some previous remove failed, check if the origin dir exists
  693. if e := system.EnsureRemoveAll(renamed); e != nil {
  694. return errors.Wrap(err, "rename target already exists and could not be removed")
  695. }
  696. if _, err := os.Stat(dir); os.IsNotExist(err) {
  697. // origin doesn't exist, nothing left to do
  698. return nil
  699. }
  700. // attempt to rename again
  701. if err := os.Rename(dir, renamed); err != nil {
  702. return errors.Wrap(err, "failed to rename dir for atomic removal")
  703. }
  704. default:
  705. return errors.Wrap(err, "failed to rename dir for atomic removal")
  706. }
  707. if err := system.EnsureRemoveAll(renamed); err != nil {
  708. os.Rename(renamed, dir)
  709. return err
  710. }
  711. return nil
  712. }