The dashboard's setup terminal was a 503: it had a WebSocket route, an authorizer and an xterm.js surface, but nothing owned a container. The daemon owns containers, so the session contract lands there and the dashboard consumes it over a new gRPC contract. One session concept, two targets. A setup session is addressed by Kit profile and is ephemeral; a run session (archie-core-qi91) is addressed by task and attempt and is bounded by the task. This slice serves setup and answers Unimplemented for run. The contract, the authorisation, the lifetime and the egress phase are settled in docs/prds/external-agent-harness.md, "Session contract". The short version: the daemon serves harness.v1.HarnessService; the dashboard authorizes the operator as an update on the secret and then dials the daemon with the harness service token, required even on loopback because a session is a shell holding an org's credential; the setup container runs no worker, mounts no worktree, holds a keepalive, and carries an empty container environment. Install hooks run at the install phase and the session enters runtime before the shell, so a runtime-phase credential's token endpoint is the one the proxy intercepts. Capture is gated by the egress proxy on a granted service, and a setup session has no task, so the daemon registers a session grant: the session's proxy token resolves to the org's bindings for the Kit's declared services. The real token never enters the container: the token endpoint response is sentinelized and stored as the org's harness secret. Every teardown path releases the pool slot, revokes the egress session and the grant, removes the sandbox network, and removes the session's volumes. The listener requires HARNESS_TOKEN and stops with a 10s grace before Stop, so an open session cannot hold shutdown to the watchdog leash.
225 lines
7.3 KiB
Go
225 lines
7.3 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"net"
|
|
"os"
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/app/servicekit"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/harnessrpc"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/harnesssession"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
|
|
)
|
|
|
|
// setupSessionTTL bounds a setup container's life independently of the pool's
|
|
// max-uptime reaper: a login that never finishes must not hold a container
|
|
// until the reaper's (longer) cap.
|
|
const setupSessionTTL = 30 * time.Minute
|
|
|
|
// sessionGrants records the credential services each live setup session is
|
|
// bound for. A session's proxy token names no task, so the egress proxy's run
|
|
// resolver consults this registry: without it, the token endpoint is never
|
|
// intercepted and no OAuth token is ever captured.
|
|
type sessionGrants struct {
|
|
mu sync.RWMutex
|
|
m map[string]sessionGrant
|
|
}
|
|
|
|
type sessionGrant struct {
|
|
org string
|
|
services []string
|
|
}
|
|
|
|
func (g *sessionGrants) Grant(token, org string, services []string) {
|
|
g.mu.Lock()
|
|
defer g.mu.Unlock()
|
|
if g.m == nil {
|
|
g.m = map[string]sessionGrant{}
|
|
}
|
|
g.m[token] = sessionGrant{org: org, services: slices.Clone(services)}
|
|
}
|
|
|
|
func (g *sessionGrants) Revoke(token string) {
|
|
g.mu.Lock()
|
|
defer g.mu.Unlock()
|
|
delete(g.m, token)
|
|
}
|
|
|
|
// GrantedFor reports the org and granted services a session token carries.
|
|
func (g *sessionGrants) GrantedFor(token string) (string, []string, bool) {
|
|
g.mu.RLock()
|
|
defer g.mu.RUnlock()
|
|
grant, ok := g.m[token]
|
|
return grant.org, grant.services, ok
|
|
}
|
|
|
|
// harnessServer serves the daemon's harness session contract.
|
|
type harnessServer struct {
|
|
pb.UnimplementedHarnessServiceServer
|
|
sessions *harnesssession.Manager
|
|
log *slog.Logger
|
|
}
|
|
|
|
// Open dispatches on the first frame's target. Setup sessions are served; the
|
|
// run-scoped target is reserved for archie-core-qi91.
|
|
func (h *harnessServer) Open(stream pb.HarnessService_OpenServer) error {
|
|
first, err := stream.Recv()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
target := first.GetTarget()
|
|
if target == nil || target.GetOrg() == "" {
|
|
return status.Error(codes.InvalidArgument, "the first frame must name a session target with an org")
|
|
}
|
|
switch t := target.GetTarget().(type) {
|
|
case *pb.OpenTarget_Setup:
|
|
if t.Setup.GetProfile() == "" {
|
|
return status.Error(codes.InvalidArgument, "the setup target names no profile")
|
|
}
|
|
return h.openSetup(stream, target.GetOrg(), t.Setup.GetProfile())
|
|
case *pb.OpenTarget_Run:
|
|
return status.Error(codes.Unimplemented, "run-scoped sessions are not served yet")
|
|
default:
|
|
return status.Error(codes.InvalidArgument, "the target names no session")
|
|
}
|
|
}
|
|
|
|
// openSetup opens one setup session and pumps frames until the shell exits or
|
|
// either side closes. Teardown runs on every return.
|
|
func (h *harnessServer) openSetup(stream pb.HarnessService_OpenServer, org, profile string) error {
|
|
session, err := h.sessions.Open(stream.Context(), org, profile)
|
|
if err != nil {
|
|
h.log.Warn("setup session failed to open", "org", org, "profile", profile, "err", err)
|
|
// The failure rides the stream as a frame, not only as a status: the
|
|
// dashboard's terminal shows the frame's text, which is the operator's
|
|
// only honest account of why the session did not open.
|
|
return stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_Error{Error: err.Error()}})
|
|
}
|
|
defer func() {
|
|
_ = session.Close()
|
|
h.log.Info("setup session closed", "org", org, "profile", profile)
|
|
}()
|
|
h.log.Info("setup session opened", "org", org, "profile", profile)
|
|
|
|
output := make(chan error, 1)
|
|
go func() { output <- h.pumpOutput(stream, session) }()
|
|
|
|
input := make(chan error, 1)
|
|
go func() { input <- h.pumpInput(stream, session) }()
|
|
|
|
select {
|
|
case err := <-output:
|
|
if err != nil {
|
|
return status.Error(codes.Internal, err.Error())
|
|
}
|
|
code, codeErr := session.ExitCode(context.WithoutCancel(stream.Context()))
|
|
if codeErr != nil {
|
|
return nil
|
|
}
|
|
return stream.Send(&pb.OpenResponse{Frame: &pb.OpenResponse_ExitCode{ExitCode: int32(code)}})
|
|
case err := <-input:
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
|
|
// pumpOutput copies terminal output to the stream until the shell ends.
|
|
func (h *harnessServer) pumpOutput(stream pb.HarnessService_OpenServer, session *harnesssession.Session) error {
|
|
buf := make([]byte, 32<<10)
|
|
for {
|
|
n, err := session.Read(buf)
|
|
if n > 0 {
|
|
frame := &pb.OpenResponse{Frame: &pb.OpenResponse_Stdout{Stdout: slices.Clone(buf[:n])}}
|
|
if sendErr := stream.Send(frame); sendErr != nil {
|
|
return sendErr
|
|
}
|
|
}
|
|
if err != nil {
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// pumpInput copies client frames to the session until the client closes.
|
|
func (h *harnessServer) pumpInput(stream pb.HarnessService_OpenServer, session *harnesssession.Session) error {
|
|
for {
|
|
req, err := stream.Recv()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if stdin, ok := req.GetFrame().(*pb.OpenRequest_Stdin); ok {
|
|
if _, err := session.Write(stdin.Stdin); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// setupHarness serves the harness session contract on the configured listen
|
|
// address. It is skipped, with the reason logged, when no service token is
|
|
// configured: a session is a shell in a container that holds an org's
|
|
// credential, so the token is required even on loopback.
|
|
func (b *boot) setupHarness(ctx context.Context) {
|
|
conn := b.cfg.Services.Get(config.ServiceNameHarness)
|
|
listen, err := servicekit.ResolveListen(config.ServiceNameHarness, "", conn.Listen)
|
|
if err != nil {
|
|
b.log.Warn("harness sessions disabled", "err", err)
|
|
return
|
|
}
|
|
token := b.cfg.Services.ResolvedToken(config.ServiceNameHarness, os.Getenv)
|
|
if token == "" {
|
|
b.log.Warn("harness sessions disabled: [services.harness].target_token or HARNESS_TOKEN is required, even on loopback")
|
|
return
|
|
}
|
|
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen)
|
|
if err != nil {
|
|
b.log.Warn("harness sessions disabled: listen", "err", err)
|
|
return
|
|
}
|
|
manager := &harnesssession.Manager{
|
|
Launcher: b.kitLauncher,
|
|
Client: b.containerPool.Client(),
|
|
Profile: func(name string) (config.AgentProfile, error) { return b.cfgHolder.Get().Containers.Profile(name) },
|
|
TTL: setupSessionTTL,
|
|
}
|
|
srv := grpc.NewServer(
|
|
//nolint:contextcheck // grpc.StreamServerInterceptor has no context.Context parameter; the interceptor derives its context from stream.Context()
|
|
grpc.StreamInterceptor(harnessrpc.StreamServerInterceptor(token)),
|
|
)
|
|
pb.RegisterHarnessServiceServer(srv, &harnessServer{sessions: manager, log: b.log})
|
|
go func() {
|
|
if err := srv.Serve(ln); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
|
|
b.log.Error("harness listener stopped", "err", err)
|
|
}
|
|
}()
|
|
b.addCleanup(func() {
|
|
// An open session stream would hold GracefulStop until its TTL; give
|
|
// in-flight frames a moment, then stop. Mirrors the State Store's
|
|
// shutdown so a session cannot hold shutdown to the watchdog leash.
|
|
timer := time.AfterFunc(10*time.Second, srv.Stop)
|
|
srv.GracefulStop()
|
|
timer.Stop()
|
|
_ = ln.Close()
|
|
})
|
|
b.log.Info("harness sessions enabled", "addr", ln.Addr().String())
|
|
}
|
|
|
|
var _ kitrun.GrantRegistry = (*sessionGrants)(nil)
|