This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/solver/socketprovider.go
Andrea Luzzardi 5b7b1cab79 Migrate dagger.#Secret and dagger.#Stream to new format
- Refactored to keep every transformation of built-in types (e.g. FS,
  Secret, etc) to/from CUE in the same place (plancontext)
- dagger.#Service and dagger.#Secret are now following the new FS-like format
  (e.g. `_service: id: string`)
- Backward compatibility
- dagger.#Stream is now an alias for dagger.#Service

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
2021-12-01 17:33:35 -08:00

52 lines
1.3 KiB
Go

package solver
import (
"context"
"fmt"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/sshforward"
"go.dagger.io/dagger/plancontext"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type SocketProvider struct {
pctx *plancontext.Context
}
func NewDockerSocketProvider(pctx *plancontext.Context) session.Attachable {
return &SocketProvider{pctx}
}
func (sp *SocketProvider) Register(server *grpc.Server) {
sshforward.RegisterSSHServer(server, sp)
}
func (sp *SocketProvider) CheckAgent(ctx context.Context, req *sshforward.CheckAgentRequest) (*sshforward.CheckAgentResponse, error) {
return &sshforward.CheckAgentResponse{}, nil
}
func (sp *SocketProvider) ForwardAgent(stream sshforward.SSH_ForwardAgentServer) error {
id := sshforward.DefaultID
opts, _ := metadata.FromIncomingContext(stream.Context()) // if no metadata continue with empty object
if v, ok := opts[sshforward.KeySSHID]; ok && len(v) > 0 && v[0] != "" {
id = v[0]
}
service := sp.pctx.Services.Get(id)
if service == nil {
return fmt.Errorf("invalid socket id %q", id)
}
conn, err := dialService(service)
if err != nil {
return fmt.Errorf("failed to connect to %s: %w", id, err)
}
defer conn.Close()
return sshforward.Copy(context.TODO(), conn, stream, nil)
}