Replace the service bearer tokens with [tls] ca_file and cert_dir. Caller identity comes from the verified certificate, not metadata. Without TLS, services listen and dial on loopback only. Task containers get only the CA and keep their grant.
161 lines
4.8 KiB
Go
161 lines
4.8 KiB
Go
// Package harnessrpc adapts the daemon-served harness session contract
|
|
// (internal/contracts/harness/v1) to the dashboard's HarnessTerminal
|
|
// interface and the daemon's gRPC listener. The daemon owns the container, so
|
|
// this is the only contract a session crosses.
|
|
package harnessrpc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
|
)
|
|
|
|
// Client opens sessions against a daemon's HarnessService.
|
|
type Client struct {
|
|
svc pb.HarnessServiceClient
|
|
}
|
|
|
|
// Dial returns a harness client for target. A session is a shell in a
|
|
// container that holds an org's credential, so it needs a service
|
|
// certificate even on loopback. The cleanup closes the connection.
|
|
func Dial(target string, id *rpctls.Identity, options ...grpc.DialOption) (*Client, func(), error) {
|
|
target = strings.TrimSpace(target)
|
|
if target == "" {
|
|
return nil, nil, errors.New("harness target is required")
|
|
}
|
|
if id == nil {
|
|
return nil, nil, errors.New("harness sessions need [tls] ca_file and cert_dir")
|
|
}
|
|
creds, err := id.DialOption(target, presence.Daemon)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("harness: %w", err)
|
|
}
|
|
conn, err := grpc.NewClient(target, append([]grpc.DialOption{creds}, options...)...)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("create harness client: %w", err)
|
|
}
|
|
return &Client{svc: pb.NewHarnessServiceClient(conn)}, func() { _ = conn.Close() }, nil
|
|
}
|
|
|
|
// Open starts a setup session for profile at the requested terminal size and
|
|
// returns its duplex stream. The returned session satisfies the dashboard's
|
|
// HarnessTerminal interface. A failure the server reports before any output
|
|
// arrives is returned here; a failure after the session is open surfaces on
|
|
// Read.
|
|
func (c *Client) Open(ctx context.Context, orgID, profile string, rows, cols int) (io.ReadWriteCloser, error) {
|
|
streamCtx, cancel := context.WithCancel(ctx)
|
|
stream, err := c.svc.Open(streamCtx)
|
|
if err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
target := &pb.OpenRequest{
|
|
Frame: &pb.OpenRequest_Target{Target: &pb.OpenTarget{
|
|
Org: orgID,
|
|
Target: &pb.OpenTarget_Setup{Setup: &pb.SetupTarget{Profile: profile}},
|
|
}},
|
|
// The daemon clamps this; zero means "use your default".
|
|
Size: &pb.ConsoleSize{Rows: uint32(max(rows, 0)), Cols: uint32(max(cols, 0))},
|
|
}
|
|
if err := stream.Send(target); err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
return &session{stream: stream, cancel: cancel}, nil
|
|
}
|
|
|
|
// OpenRun starts a read-only view of a running task's sandbox and returns its
|
|
// output. The daemon refuses a finished or unknown task or attempt, and that
|
|
// reason is returned here, before any output, as a gRPC status the caller can
|
|
// map. Writes to the returned stream are not sent: a view has no stdin.
|
|
func (c *Client) OpenRun(ctx context.Context, orgID string, taskID, attempt int64) (io.ReadCloser, error) {
|
|
streamCtx, cancel := context.WithCancel(ctx)
|
|
stream, err := c.svc.Open(streamCtx)
|
|
if err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
target := &pb.OpenRequest{Frame: &pb.OpenRequest_Target{Target: &pb.OpenTarget{
|
|
Org: orgID,
|
|
Target: &pb.OpenTarget_Run{Run: &pb.RunTarget{TaskId: taskID, Attempt: attempt}},
|
|
}}}
|
|
if err := stream.Send(target); err != nil {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
s := &session{stream: stream, cancel: cancel}
|
|
// The first frame decides whether the view opened: a refusal arrives as
|
|
// the stream's status before any output.
|
|
s.mu.Lock()
|
|
err = s.recv()
|
|
s.mu.Unlock()
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
cancel()
|
|
return nil, err
|
|
}
|
|
return s, nil
|
|
}
|
|
|
|
// session adapts the bidi stream to io.ReadWriteCloser. Bytes are written as
|
|
// stdin frames and read from stdout frames; an error frame or an exit code
|
|
// ends the stream.
|
|
type session struct {
|
|
stream pb.HarnessService_OpenClient
|
|
cancel context.CancelFunc
|
|
|
|
mu sync.Mutex
|
|
pending []byte
|
|
}
|
|
|
|
func (s *session) Write(p []byte) (int, error) {
|
|
if err := s.stream.Send(&pb.OpenRequest{Frame: &pb.OpenRequest_Stdin{Stdin: p}}); err != nil {
|
|
return 0, err
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
func (s *session) Read(p []byte) (int, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
for len(s.pending) == 0 {
|
|
if err := s.recv(); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
n := copy(p, s.pending)
|
|
s.pending = s.pending[n:]
|
|
return n, nil
|
|
}
|
|
|
|
// recv reads one server frame into pending; an empty stdout frame leaves it
|
|
// empty.
|
|
func (s *session) recv() error {
|
|
resp, err := s.stream.Recv()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch frame := resp.GetFrame().(type) {
|
|
case *pb.OpenResponse_Stdout:
|
|
s.pending = frame.Stdout
|
|
case *pb.OpenResponse_ExitCode:
|
|
return io.EOF
|
|
case *pb.OpenResponse_Error:
|
|
return errors.New(frame.Error)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *session) Close() error {
|
|
s.cancel()
|
|
return nil
|
|
}
|