|
@@ -349,12 +349,11 @@ func (sm *sshMount) Mount(ctx context.Context, readonly bool) (snapshot.Mountabl
|
|
|
}
|
|
|
|
|
|
type sshMountInstance struct {
|
|
|
- sm *sshMount
|
|
|
- cleanup func() error
|
|
|
- idmap *idtools.IdentityMapping
|
|
|
+ sm *sshMount
|
|
|
+ idmap *idtools.IdentityMapping
|
|
|
}
|
|
|
|
|
|
-func (sm *sshMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
+func (sm *sshMountInstance) Mount() ([]mount.Mount, func() error, error) {
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
|
|
uid := int(sm.sm.mount.SSHOpt.Uid)
|
|
@@ -366,7 +365,7 @@ func (sm *sshMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
GID: gid,
|
|
|
})
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
uid = identity.UID
|
|
|
gid = identity.GID
|
|
@@ -380,9 +379,9 @@ func (sm *sshMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
})
|
|
|
if err != nil {
|
|
|
cancel()
|
|
|
- return nil, err
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
- sm.cleanup = func() error {
|
|
|
+ release := func() error {
|
|
|
var err error
|
|
|
if cleanup != nil {
|
|
|
err = cleanup()
|
|
@@ -395,16 +394,7 @@ func (sm *sshMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
Type: "bind",
|
|
|
Source: sock,
|
|
|
Options: []string{"rbind"},
|
|
|
- }}, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (sm *sshMountInstance) Release() error {
|
|
|
- if sm.cleanup != nil {
|
|
|
- if err := sm.cleanup(); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- }
|
|
|
- return nil
|
|
|
+ }}, release, nil
|
|
|
}
|
|
|
|
|
|
func (sm *sshMountInstance) IdentityMapping() *idtools.IdentityMapping {
|
|
@@ -462,14 +452,18 @@ type secretMountInstance struct {
|
|
|
idmap *idtools.IdentityMapping
|
|
|
}
|
|
|
|
|
|
-func (sm *secretMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
+func (sm *secretMountInstance) Mount() ([]mount.Mount, func() error, error) {
|
|
|
dir, err := ioutil.TempDir("", "buildkit-secrets")
|
|
|
if err != nil {
|
|
|
- return nil, errors.Wrap(err, "failed to create temp dir")
|
|
|
+ return nil, nil, errors.Wrap(err, "failed to create temp dir")
|
|
|
+ }
|
|
|
+ cleanupDir := func() error {
|
|
|
+ return os.RemoveAll(dir)
|
|
|
}
|
|
|
|
|
|
if err := os.Chmod(dir, 0711); err != nil {
|
|
|
- return nil, err
|
|
|
+ cleanupDir()
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
|
|
|
tmpMount := mount.Mount{
|
|
@@ -483,15 +477,23 @@ func (sm *secretMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
}
|
|
|
|
|
|
if err := mount.All([]mount.Mount{tmpMount}, dir); err != nil {
|
|
|
- return nil, errors.Wrap(err, "unable to setup secret mount")
|
|
|
+ cleanupDir()
|
|
|
+ return nil, nil, errors.Wrap(err, "unable to setup secret mount")
|
|
|
}
|
|
|
sm.root = dir
|
|
|
|
|
|
+ cleanup := func() error {
|
|
|
+ if err := mount.Unmount(dir, 0); err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return cleanupDir()
|
|
|
+ }
|
|
|
+
|
|
|
randID := identity.NewID()
|
|
|
fp := filepath.Join(dir, randID)
|
|
|
if err := ioutil.WriteFile(fp, sm.sm.data, 0600); err != nil {
|
|
|
- sm.Release()
|
|
|
- return nil, err
|
|
|
+ cleanup()
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
|
|
|
uid := int(sm.sm.mount.SecretOpt.Uid)
|
|
@@ -503,35 +505,28 @@ func (sm *secretMountInstance) Mount() ([]mount.Mount, error) {
|
|
|
GID: gid,
|
|
|
})
|
|
|
if err != nil {
|
|
|
- return nil, err
|
|
|
+ cleanup()
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
uid = identity.UID
|
|
|
gid = identity.GID
|
|
|
}
|
|
|
|
|
|
if err := os.Chown(fp, uid, gid); err != nil {
|
|
|
- return nil, err
|
|
|
+ cleanup()
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
|
|
|
if err := os.Chmod(fp, os.FileMode(sm.sm.mount.SecretOpt.Mode&0777)); err != nil {
|
|
|
- return nil, err
|
|
|
+ cleanup()
|
|
|
+ return nil, nil, err
|
|
|
}
|
|
|
|
|
|
return []mount.Mount{{
|
|
|
Type: "bind",
|
|
|
Source: fp,
|
|
|
Options: []string{"ro", "rbind"},
|
|
|
- }}, nil
|
|
|
-}
|
|
|
-
|
|
|
-func (sm *secretMountInstance) Release() error {
|
|
|
- if sm.root != "" {
|
|
|
- if err := mount.Unmount(sm.root, 0); err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- return os.RemoveAll(sm.root)
|
|
|
- }
|
|
|
- return nil
|
|
|
+ }}, cleanup, nil
|
|
|
}
|
|
|
|
|
|
func (sm *secretMountInstance) IdentityMapping() *idtools.IdentityMapping {
|
|
@@ -767,7 +762,7 @@ type tmpfsMount struct {
|
|
|
idmap *idtools.IdentityMapping
|
|
|
}
|
|
|
|
|
|
-func (m *tmpfsMount) Mount() ([]mount.Mount, error) {
|
|
|
+func (m *tmpfsMount) Mount() ([]mount.Mount, func() error, error) {
|
|
|
opt := []string{"nosuid"}
|
|
|
if m.readonly {
|
|
|
opt = append(opt, "ro")
|
|
@@ -776,10 +771,7 @@ func (m *tmpfsMount) Mount() ([]mount.Mount, error) {
|
|
|
Type: "tmpfs",
|
|
|
Source: "tmpfs",
|
|
|
Options: opt,
|
|
|
- }}, nil
|
|
|
-}
|
|
|
-func (m *tmpfsMount) Release() error {
|
|
|
- return nil
|
|
|
+ }}, func() error { return nil }, nil
|
|
|
}
|
|
|
|
|
|
func (m *tmpfsMount) IdentityMapping() *idtools.IdentityMapping {
|