Files
archie/internal/app/archied/harness.go
T

255 lines
8.8 KiB
Go

package archied
import (
"context"
"errors"
"io"
"log/slog"
"net"
"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"
"github.com/samcharles93/archie-core/internal/container"
daemonpb "github.com/samcharles93/archie-core/internal/contracts/daemon/v1"
pb "github.com/samcharles93/archie-core/internal/contracts/harness/v1"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/presence"
"github.com/samcharles93/archie-core/internal/infrastructure/harnesssession"
"github.com/samcharles93/archie-core/internal/infrastructure/kitrun"
"github.com/samcharles93/archie-core/internal/infrastructure/rpcidentity"
)
// 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
// runs serves the run target; nil answers Unavailable.
runs *runView
log *slog.Logger
}
// Open dispatches on the first frame's target. Setup sessions open a PTY; a run
// target is a read-only view of the task's running sandbox.
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")
}
if target.GetOrg() != string(org.OrgFromContext(stream.Context())) {
return status.Error(codes.PermissionDenied, "harness session belongs to another 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(), first.GetSize())
case *pb.OpenTarget_Run:
if t.Run.GetTaskId() <= 0 {
return status.Error(codes.InvalidArgument, "the run target names no task")
}
return h.openRun(stream, target.GetOrg(), t.Run)
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, size *pb.ConsoleSize) error {
session, err := h.sessions.Open(stream.Context(), org, profile, int(size.GetRows()), int(size.GetCols()))
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
}
}
}
}
// setupDaemonRPC serves repository checks and optional terminal sessions.
// Without certificates it binds only loopback and does not serve terminals.
func (b *boot) setupDaemonRPC(ctx context.Context) {
conn := b.cfg.Services.Get(config.ServiceNameDaemon)
listen, err := servicekit.ResolveListen(config.ServiceNameDaemon, "", conn.Listen)
if err != nil {
b.log.WarnContext(ctx, "harness sessions disabled", "err", err)
return
}
id, err := servicekit.Identity(b.cfg.TLS, presence.Daemon)
if err != nil {
b.log.WarnContext(ctx, "harness sessions disabled", "err", err)
return
}
creds, err := id.ServerOption(listen, false)
if err != nil {
b.log.WarnContext(ctx, "harness sessions disabled", "err", err)
return
}
ln, err := (&net.ListenConfig{}).Listen(ctx, "tcp", listen)
if err != nil {
b.log.WarnContext(ctx, "harness sessions disabled: listen", "err", err)
return
}
workspaces, _ := b.stateStore.(access.WorkspaceLister)
callers := rpcidentity.Callers{Principals: b.d.Principals, Workspaces: workspaces}
srv := grpc.NewServer(append(servicekit.ServerLimits(), creds, grpc.ChainUnaryInterceptor(callers.Unary()), grpc.ChainStreamInterceptor(callers.Stream()))...)
daemonpb.RegisterDaemonServiceServer(srv, &repositoryServer{boot: b})
// Terminal sessions still require certificates and a container launcher.
if id != nil && b.kitLauncher != nil && b.containerPool != nil {
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}
pb.RegisterHarnessServiceServer(srv, &harnessServer{sessions: manager, runs: b.runView(), log: b.log})
}
go func() {
if err := srv.Serve(ln); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
b.log.ErrorContext(ctx, "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.InfoContext(ctx, "harness sessions enabled", "addr", ln.Addr().String())
}
var _ kitrun.GrantRegistry = (*sessionGrants)(nil)
// runView wires the run target to the task store and the container pool. The
// store is the daemon's own contract client, so a task it cannot read answers
// Unavailable rather than guessing.
func (b *boot) runView() *runView {
if b.stateStore == nil || b.containerPool == nil {
return nil
}
cli := b.containerPool.Client()
return &runView{
tasks: b.stateStore,
follow: func(ctx context.Context, taskID, attempt int64) (io.ReadCloser, error) {
return container.FollowLogs(ctx, cli, kitrun.TaskContainer(b.containerPool.Instance(), taskID, attempt), runTail)
},
}
}