2018-10-05 05:35:09 -04:00
|
|
|
package sshforward
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/session"
|
2019-09-20 20:42:32 -04:00
|
|
|
"github.com/pkg/errors"
|
2018-10-05 05:35:09 -04:00
|
|
|
context "golang.org/x/net/context"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultID is the default ssh ID
|
|
|
|
const DefaultID = "default"
|
|
|
|
|
|
|
|
const KeySSHID = "buildkit.ssh.id"
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
caller session.Caller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) run(ctx context.Context, l net.Listener, id string) error {
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
<-ctx.Done()
|
|
|
|
return ctx.Err()
|
|
|
|
})
|
|
|
|
|
|
|
|
eg.Go(func() error {
|
|
|
|
for {
|
|
|
|
conn, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := NewSSHClient(s.caller.Conn())
|
|
|
|
|
|
|
|
opts := make(map[string][]string)
|
|
|
|
opts[KeySSHID] = []string{id}
|
|
|
|
ctx = metadata.NewOutgoingContext(ctx, opts)
|
|
|
|
|
|
|
|
stream, err := client.ForwardAgent(ctx)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-20 20:42:32 -04:00
|
|
|
go Copy(ctx, conn, stream, stream.CloseSend)
|
2018-10-05 05:35:09 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return eg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
type SocketOpt struct {
|
|
|
|
ID string
|
|
|
|
UID int
|
|
|
|
GID int
|
|
|
|
Mode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func MountSSHSocket(ctx context.Context, c session.Caller, opt SocketOpt) (sockPath string, closer func() error, err error) {
|
|
|
|
dir, err := ioutil.TempDir("", ".buildkit-ssh-sock")
|
|
|
|
if err != nil {
|
2019-09-20 20:42:32 -04:00
|
|
|
return "", nil, errors.WithStack(err)
|
2018-10-05 05:35:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
if err := os.Chmod(dir, 0711); err != nil {
|
|
|
|
return "", nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2018-10-05 05:35:09 -04:00
|
|
|
sockPath = filepath.Join(dir, "ssh_auth_sock")
|
|
|
|
|
|
|
|
l, err := net.Listen("unix", sockPath)
|
|
|
|
if err != nil {
|
2019-09-20 20:42:32 -04:00
|
|
|
return "", nil, errors.WithStack(err)
|
2018-10-05 05:35:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Chown(sockPath, opt.UID, opt.GID); err != nil {
|
|
|
|
l.Close()
|
2019-09-20 20:42:32 -04:00
|
|
|
return "", nil, errors.WithStack(err)
|
2018-10-05 05:35:09 -04:00
|
|
|
}
|
|
|
|
if err := os.Chmod(sockPath, os.FileMode(opt.Mode)); err != nil {
|
|
|
|
l.Close()
|
2019-09-20 20:42:32 -04:00
|
|
|
return "", nil, errors.WithStack(err)
|
2018-10-05 05:35:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
s := &server{caller: c}
|
|
|
|
|
|
|
|
id := opt.ID
|
|
|
|
if id == "" {
|
|
|
|
id = DefaultID
|
|
|
|
}
|
|
|
|
|
|
|
|
go s.run(ctx, l, id) // erroring per connection allowed
|
|
|
|
|
|
|
|
return sockPath, func() error {
|
|
|
|
err := l.Close()
|
|
|
|
os.RemoveAll(sockPath)
|
2019-09-20 20:42:32 -04:00
|
|
|
return errors.WithStack(err)
|
2018-10-05 05:35:09 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckSSHID(ctx context.Context, c session.Caller, id string) error {
|
|
|
|
client := NewSSHClient(c.Conn())
|
|
|
|
_, err := client.CheckAgent(ctx, &CheckAgentRequest{ID: id})
|
2019-09-20 20:42:32 -04:00
|
|
|
return errors.WithStack(err)
|
2018-10-05 05:35:09 -04:00
|
|
|
}
|