|
@@ -161,43 +161,26 @@ func (daemon *Daemon) setupIpcDirs(c *container.Container) error {
|
|
|
}
|
|
|
|
|
|
func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
|
|
|
- if len(c.SecretReferences) == 0 {
|
|
|
+ if len(c.SecretReferences) == 0 && len(c.ConfigReferences) == 0 {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- localMountPath, err := c.SecretMountPath()
|
|
|
- if err != nil {
|
|
|
- return errors.Wrap(err, "error getting secrets mount dir")
|
|
|
- }
|
|
|
- logrus.Debugf("secrets: setting up secret dir: %s", localMountPath)
|
|
|
-
|
|
|
- // retrieve possible remapped range start for root UID, GID
|
|
|
- rootIDs := daemon.idMappings.RootPair()
|
|
|
- // create tmpfs
|
|
|
- if err := idtools.MkdirAllAndChown(localMountPath, 0700, rootIDs); err != nil {
|
|
|
- return errors.Wrap(err, "error creating secret local mount path")
|
|
|
+ if err := daemon.createSecretsDir(c); err != nil {
|
|
|
+ return err
|
|
|
}
|
|
|
-
|
|
|
defer func() {
|
|
|
if setupErr != nil {
|
|
|
- // cleanup
|
|
|
- _ = detachMounted(localMountPath)
|
|
|
-
|
|
|
- if err := os.RemoveAll(localMountPath); err != nil {
|
|
|
- logrus.Errorf("error cleaning up secret mount: %s", err)
|
|
|
- }
|
|
|
+ daemon.cleanupSecretDir(c)
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
|
|
|
- if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
|
|
|
- return errors.Wrap(err, "unable to setup secret mount")
|
|
|
- }
|
|
|
-
|
|
|
if c.DependencyStore == nil {
|
|
|
return fmt.Errorf("secret store is not initialized")
|
|
|
}
|
|
|
|
|
|
+ // retrieve possible remapped range start for root UID, GID
|
|
|
+ rootIDs := daemon.idMappings.RootPair()
|
|
|
+
|
|
|
for _, s := range c.SecretReferences {
|
|
|
// TODO (ehazlett): use type switch when more are supported
|
|
|
if s.File == nil {
|
|
@@ -244,78 +227,38 @@ func (daemon *Daemon) setupSecretDir(c *container.Container) (setupErr error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- label.Relabel(localMountPath, c.MountLabel, false)
|
|
|
-
|
|
|
- // remount secrets ro
|
|
|
- if err := mount.Mount("tmpfs", localMountPath, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
|
|
|
- return errors.Wrap(err, "unable to remount secret dir as readonly")
|
|
|
- }
|
|
|
-
|
|
|
- return nil
|
|
|
-}
|
|
|
-
|
|
|
-func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
|
|
|
- if len(c.ConfigReferences) == 0 {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- localPath, err := c.ConfigsDirPath()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- logrus.Debugf("configs: setting up config dir: %s", localPath)
|
|
|
-
|
|
|
- // retrieve possible remapped range start for root UID, GID
|
|
|
- rootIDs := daemon.idMappings.RootPair()
|
|
|
- // create tmpfs
|
|
|
- if err := idtools.MkdirAllAndChown(localPath, 0700, rootIDs); err != nil {
|
|
|
- return errors.Wrap(err, "error creating config dir")
|
|
|
- }
|
|
|
-
|
|
|
- defer func() {
|
|
|
- if setupErr != nil {
|
|
|
- if err := os.RemoveAll(localPath); err != nil {
|
|
|
- logrus.Errorf("error cleaning up config dir: %s", err)
|
|
|
- }
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- if c.DependencyStore == nil {
|
|
|
- return fmt.Errorf("config store is not initialized")
|
|
|
- }
|
|
|
-
|
|
|
- for _, configRef := range c.ConfigReferences {
|
|
|
+ for _, ref := range c.ConfigReferences {
|
|
|
// TODO (ehazlett): use type switch when more are supported
|
|
|
- if configRef.File == nil {
|
|
|
+ if ref.File == nil {
|
|
|
logrus.Error("config target type is not a file target")
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
- fPath, err := c.ConfigFilePath(*configRef)
|
|
|
+ fPath, err := c.ConfigFilePath(*ref)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return errors.Wrap(err, "error getting config file path for container")
|
|
|
}
|
|
|
-
|
|
|
- log := logrus.WithFields(logrus.Fields{"name": configRef.File.Name, "path": fPath})
|
|
|
-
|
|
|
if err := idtools.MkdirAllAndChown(filepath.Dir(fPath), 0700, rootIDs); err != nil {
|
|
|
- return errors.Wrap(err, "error creating config path")
|
|
|
+ return errors.Wrap(err, "error creating config mount path")
|
|
|
}
|
|
|
|
|
|
- log.Debug("injecting config")
|
|
|
- config, err := c.DependencyStore.Configs().Get(configRef.ConfigID)
|
|
|
+ logrus.WithFields(logrus.Fields{
|
|
|
+ "name": ref.File.Name,
|
|
|
+ "path": fPath,
|
|
|
+ }).Debug("injecting config")
|
|
|
+ config, err := c.DependencyStore.Configs().Get(ref.ConfigID)
|
|
|
if err != nil {
|
|
|
return errors.Wrap(err, "unable to get config from config store")
|
|
|
}
|
|
|
- if err := ioutil.WriteFile(fPath, config.Spec.Data, configRef.File.Mode); err != nil {
|
|
|
+ if err := ioutil.WriteFile(fPath, config.Spec.Data, ref.File.Mode); err != nil {
|
|
|
return errors.Wrap(err, "error injecting config")
|
|
|
}
|
|
|
|
|
|
- uid, err := strconv.Atoi(configRef.File.UID)
|
|
|
+ uid, err := strconv.Atoi(ref.File.UID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
- gid, err := strconv.Atoi(configRef.File.GID)
|
|
|
+ gid, err := strconv.Atoi(ref.File.GID)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
@@ -323,16 +266,69 @@ func (daemon *Daemon) setupConfigDir(c *container.Container) (setupErr error) {
|
|
|
if err := os.Chown(fPath, rootIDs.UID+uid, rootIDs.GID+gid); err != nil {
|
|
|
return errors.Wrap(err, "error setting ownership for config")
|
|
|
}
|
|
|
- if err := os.Chmod(fPath, configRef.File.Mode); err != nil {
|
|
|
+ if err := os.Chmod(fPath, ref.File.Mode); err != nil {
|
|
|
return errors.Wrap(err, "error setting file mode for config")
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- label.Relabel(fPath, c.MountLabel, false)
|
|
|
+ return daemon.remountSecretDir(c)
|
|
|
+}
|
|
|
+
|
|
|
+// createSecretsDir is used to create a dir suitable for storing container secrets.
|
|
|
+// In practice this is using a tmpfs mount and is used for both "configs" and "secrets"
|
|
|
+func (daemon *Daemon) createSecretsDir(c *container.Container) error {
|
|
|
+ // retrieve possible remapped range start for root UID, GID
|
|
|
+ rootIDs := daemon.idMappings.RootPair()
|
|
|
+ dir, err := c.SecretMountPath()
|
|
|
+ if err != nil {
|
|
|
+ return errors.Wrap(err, "error getting container secrets dir")
|
|
|
+ }
|
|
|
+
|
|
|
+ // create tmpfs
|
|
|
+ if err := idtools.MkdirAllAndChown(dir, 0700, rootIDs); err != nil {
|
|
|
+ return errors.Wrap(err, "error creating secret local mount path")
|
|
|
+ }
|
|
|
+
|
|
|
+ tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
|
|
|
+ if err := mount.Mount("tmpfs", dir, "tmpfs", "nodev,nosuid,noexec,"+tmpfsOwnership); err != nil {
|
|
|
+ return errors.Wrap(err, "unable to setup secret mount")
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (daemon *Daemon) remountSecretDir(c *container.Container) error {
|
|
|
+ dir, err := c.SecretMountPath()
|
|
|
+ if err != nil {
|
|
|
+ return errors.Wrap(err, "error getting container secrets path")
|
|
|
+ }
|
|
|
+ if err := label.Relabel(dir, c.MountLabel, false); err != nil {
|
|
|
+ logrus.WithError(err).WithField("dir", dir).Warn("Error while attempting to set selinux label")
|
|
|
+ }
|
|
|
+ rootIDs := daemon.idMappings.RootPair()
|
|
|
+ tmpfsOwnership := fmt.Sprintf("uid=%d,gid=%d", rootIDs.UID, rootIDs.GID)
|
|
|
+
|
|
|
+ // remount secrets ro
|
|
|
+ if err := mount.Mount("tmpfs", dir, "tmpfs", "remount,ro,"+tmpfsOwnership); err != nil {
|
|
|
+ return errors.Wrap(err, "unable to remount dir as readonly")
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
+func (daemon *Daemon) cleanupSecretDir(c *container.Container) {
|
|
|
+ dir, err := c.SecretMountPath()
|
|
|
+ if err != nil {
|
|
|
+ logrus.WithError(err).WithField("container", c.ID).Warn("error getting secrets mount path for container")
|
|
|
+ }
|
|
|
+ if err := mount.RecursiveUnmount(dir); err != nil {
|
|
|
+ logrus.WithField("dir", dir).WithError(err).Warn("Error while attmepting to unmount dir, this may prevent removal of container.")
|
|
|
+ }
|
|
|
+ if err := os.RemoveAll(dir); err != nil && !os.IsNotExist(err) {
|
|
|
+ logrus.WithField("dir", dir).WithError(err).Error("Error removing dir.")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func killProcessDirectly(cntr *container.Container) error {
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
defer cancel()
|