Agent review pass.
This commit is contained in:
1 parent
e9b2e9508b
commit
64ee749a8e
105 files changed
+2098
-320
No files matched your search
@@ -10,6 +10,7 @@ package remoteinput
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/input"
|
||||
)
|
||||
@@ -23,6 +24,11 @@ const (
|
||||
Down byte = 2
|
||||
Up byte = 3
|
||||
Close byte = 4
|
||||
// Hello is the first fixed control frame. It carries the lease epoch
|
||||
// over the private pipe itself, never in argv (world-readable via
|
||||
// /proc/<pid>/cmdline). Possession of the pipe is the authentication;
|
||||
// the epoch only binds subsequent frames to one approved lease.
|
||||
Hello byte = 5
|
||||
|
||||
Ready byte = 1
|
||||
Accepted byte = 2
|
||||
@@ -46,16 +52,22 @@ type Operation struct {
|
||||
}
|
||||
|
||||
func EncodeOperation(operation Operation) ([]byte, error) {
|
||||
if operation.Epoch == 0 || operation.Sequence == 0 || len(operation.Name) > 32 {
|
||||
if operation.Epoch == 0 || len(operation.Name) > 32 {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
switch operation.Kind {
|
||||
case Hello:
|
||||
// Sequence zero is reserved for the opening epoch frame, mirroring
|
||||
// Ready; regular operations still start at sequence one.
|
||||
if operation.Sequence != 0 || operation.Name != "" {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
case Down, Up:
|
||||
if !input.ValidRemoteKeyName(operation.Name) {
|
||||
if operation.Sequence == 0 || !input.ValidRemoteKeyName(operation.Name) {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
case Heartbeat, Close:
|
||||
if operation.Name != "" {
|
||||
if operation.Sequence == 0 || operation.Name != "" {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
default:
|
||||
@@ -69,6 +81,27 @@ func EncodeOperation(operation Operation) ([]byte, error) {
|
||||
return frame, nil
|
||||
}
|
||||
|
||||
// ReadEpoch consumes the mandatory first fixed frame from a control pipe
|
||||
// and returns its lease epoch. A helper must call it before Run (and
|
||||
// before opening any native target): Ready is only sent by Run for the
|
||||
// epoch obtained here. Anything other than one well-formed Hello frame
|
||||
// is a protocol failure. Run itself rejects a later Hello, whose
|
||||
// sequence zero can never follow the first regular operation.
|
||||
func ReadEpoch(control io.Reader) (uint64, error) {
|
||||
if control == nil {
|
||||
return 0, ErrProtocol
|
||||
}
|
||||
frame := make([]byte, OperationSize)
|
||||
if _, err := io.ReadFull(control, frame); err != nil {
|
||||
return 0, ErrProtocol
|
||||
}
|
||||
operation, err := DecodeOperation(frame)
|
||||
if err != nil || operation.Kind != Hello {
|
||||
return 0, ErrProtocol
|
||||
}
|
||||
return operation.Epoch, nil
|
||||
}
|
||||
|
||||
func DecodeOperation(frame []byte) (Operation, error) {
|
||||
if len(frame) != OperationSize || frame[0] != Version || frame[3] != 0 || frame[2] > 32 {
|
||||
return Operation{}, ErrProtocol
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
package remoteinput
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"testing"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
|
||||
func TestHelperOperationCodec(t *testing.T) {
|
||||
for _, operation := range []Operation{
|
||||
{Kind: Hello, Epoch: 7},
|
||||
{Kind: Down, Epoch: 7, Sequence: 1, Name: "Control_R"},
|
||||
{Kind: Up, Epoch: 7, Sequence: 2, Name: "KP_Enter"},
|
||||
{Kind: Heartbeat, Epoch: 7, Sequence: ^uint64(0)},
|
||||
@@ -28,9 +30,12 @@ func TestHelperOperationCodec(t *testing.T) {
|
||||
}
|
||||
for _, operation := range []Operation{
|
||||
{Kind: 0, Epoch: 7, Sequence: 1}, {Kind: Heartbeat, Sequence: 1},
|
||||
{Kind: Hello}, {Kind: Hello, Epoch: 7, Sequence: 1},
|
||||
{Kind: Hello, Epoch: 7, Name: "a"},
|
||||
{Kind: Heartbeat, Epoch: 7}, {Kind: Down, Epoch: 7, Sequence: 1},
|
||||
{Kind: Up, Epoch: 7, Sequence: 1, Name: "private unknown"},
|
||||
{Kind: Down, Epoch: 7, Sequence: 1, Name: "a\x00"},
|
||||
{Kind: Down, Epoch: 7, Name: "a"},
|
||||
{Kind: Heartbeat, Epoch: 7, Sequence: 1, Name: "a"},
|
||||
{Kind: Close, Epoch: 7, Sequence: 1, Name: "a"},
|
||||
} {
|
||||
@@ -53,6 +58,41 @@ func TestHelperOperationCodec(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadEpochConsumesOnlyHelloFrame(t *testing.T) {
|
||||
hello, err := EncodeOperation(Operation{Kind: Hello, Epoch: 41})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
down, err := EncodeOperation(Operation{Kind: Down, Epoch: 41, Sequence: 1, Name: "a"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader := bytes.NewReader(append(append([]byte(nil), hello...), down...))
|
||||
epoch, err := ReadEpoch(reader)
|
||||
if err != nil || epoch != 41 {
|
||||
t.Fatalf("ReadEpoch = %d, %v", epoch, err)
|
||||
}
|
||||
if reader.Len() != OperationSize {
|
||||
t.Fatal("ReadEpoch consumed beyond the first fixed frame")
|
||||
}
|
||||
// The next frame stays strictly bound to the adopted epoch.
|
||||
frame := make([]byte, OperationSize)
|
||||
if _, err := reader.Read(frame); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if operation, err := DecodeOperation(frame); err != nil || operation.Epoch != epoch || operation.Sequence != 1 {
|
||||
t.Fatal("first regular operation lost its epoch or sequence", err)
|
||||
}
|
||||
for _, bad := range [][]byte{nil, hello[:OperationSize-1], down} {
|
||||
if _, err := ReadEpoch(bytes.NewReader(bad)); !errors.Is(err, ErrProtocol) {
|
||||
t.Fatal("missing, partial or non-Hello epoch frame accepted", err)
|
||||
}
|
||||
}
|
||||
if _, err := ReadEpoch(nil); !errors.Is(err, ErrProtocol) {
|
||||
t.Fatal("missing control pipe accepted", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelperAckCodec(t *testing.T) {
|
||||
for _, ack := range []Ack{
|
||||
{Kind: Ready, Epoch: 7}, {Kind: Accepted, Epoch: 7, Sequence: 1},
|
||||
|
||||
@@ -41,6 +41,8 @@ type ackWrite struct {
|
||||
// Stream Close must interrupt blocked reads/writes; AdoptPipe supplies that
|
||||
// property for inherited Go file descriptors. The caller supplies one fixed
|
||||
// nonzero epoch and must not launch a replacement until cleanup is confirmed.
|
||||
// A helper process obtains that epoch from ReadEpoch's opening Hello frame
|
||||
// on the control pipe, never from argv; every later frame must still match it.
|
||||
func Run(ctx context.Context, epoch uint64, control, lifetime io.ReadCloser, output io.WriteCloser, target Target) error {
|
||||
return run(ctx, epoch, control, lifetime, output, target, Watchdog)
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ func TestHelperRunnerOrderedClose(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHelperRunnerRejectsStaleAndMalformed(t *testing.T) {
|
||||
for _, failure := range []string{"epoch", "replay", "partial", "reserved", "lifetime-data"} {
|
||||
for _, failure := range []string{"epoch", "replay", "partial", "reserved", "lifetime-data", "late-hello"} {
|
||||
t.Run(failure, func(t *testing.T) {
|
||||
f := startHelper(t, time.Second)
|
||||
f.ack(t)
|
||||
@@ -211,6 +211,10 @@ func TestHelperRunnerRejectsStaleAndMalformed(t *testing.T) {
|
||||
if _, err := f.life.Write([]byte{1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "late-hello":
|
||||
// A Hello is only the opening ReadEpoch frame; Run must
|
||||
// never rebind the epoch mid-stream.
|
||||
f.send(t, Operation{Kind: Hello, Epoch: 8})
|
||||
}
|
||||
if ack := f.ack(t); ack.Kind != Closed || ack.Reason != ReasonProtocol || ack.CleanupFailed {
|
||||
t.Fatal("invalid input did not retire with cleanup", ack)
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -34,15 +33,17 @@ type Supervisor struct {
|
||||
mu sync.Mutex
|
||||
active bool
|
||||
uncertain bool
|
||||
command func(uint64) *exec.Cmd
|
||||
command func() *exec.Cmd
|
||||
}
|
||||
|
||||
func NewSupervisor(executable string) (*Supervisor, error) {
|
||||
if !filepath.IsAbs(executable) {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
return &Supervisor{command: func(epoch uint64) *exec.Cmd {
|
||||
return exec.Command(executable, "--epoch", strconv.FormatUint(epoch, 10))
|
||||
// No arguments at all: the epoch travels as the first frame over the
|
||||
// private control pipe, never in world-readable argv.
|
||||
return &Supervisor{command: func() *exec.Cmd {
|
||||
return exec.Command(executable)
|
||||
}}, nil
|
||||
}
|
||||
|
||||
@@ -173,7 +174,7 @@ func (supervisor *Supervisor) serve(ctx context.Context, epoch uint64, client *C
|
||||
if err != nil {
|
||||
return ErrUnavailable
|
||||
}
|
||||
cmd := supervisor.command(epoch)
|
||||
cmd := supervisor.command()
|
||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = controlRead, outputWrite, io.Discard
|
||||
cmd.ExtraFiles = []*os.File{lifetimeRead}
|
||||
// No CommandContext/Pdeathsig: writer death must allow native retirement.
|
||||
@@ -287,6 +288,21 @@ func (supervisor *Supervisor) serve(ctx context.Context, epoch uint64, client *C
|
||||
return Ack{}, ErrUnavailable
|
||||
}
|
||||
}
|
||||
// Deliver the epoch as the first fixed frame over the control pipe
|
||||
// before expecting Ready; the helper adopts it via ReadEpoch. The
|
||||
// sequence-zero Hello never counts toward sent/accepted operations.
|
||||
hello, helloErr := EncodeOperation(Operation{Kind: Hello, Epoch: epoch})
|
||||
if helloErr == nil {
|
||||
if helloErr = control.SetWriteDeadline(time.Now().Add(operationTimeout)); helloErr == nil {
|
||||
helloErr = writeAll(control, hello)
|
||||
}
|
||||
}
|
||||
if helloErr != nil {
|
||||
if cleanup := retire(); cleanup != nil {
|
||||
return cleanup
|
||||
}
|
||||
return ErrUnavailable
|
||||
}
|
||||
ready, err := readAck()
|
||||
if err != nil || ready != (Ack{Kind: Ready, Epoch: epoch}) {
|
||||
// An ACK consumed here cannot be reinterpreted as a valid terminal.
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -43,7 +42,6 @@ func TestSupervisorProcess(t *testing.T) {
|
||||
if mode == "" {
|
||||
return
|
||||
}
|
||||
epoch, _ := strconv.ParseUint(os.Getenv("CTHULHU_SUPERVISOR_EPOCH"), 10, 64)
|
||||
control, err := AdoptPipe(os.Stdin, false)
|
||||
if err != nil {
|
||||
os.Exit(2)
|
||||
@@ -56,6 +54,12 @@ func TestSupervisorProcess(t *testing.T) {
|
||||
if err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
// The epoch only arrives as the first control-pipe frame; neither
|
||||
// argv nor the environment carries it.
|
||||
epoch, err := ReadEpoch(control)
|
||||
if err != nil {
|
||||
os.Exit(2)
|
||||
}
|
||||
if mode == "no-ready" || mode == "bad-ready" {
|
||||
if mode == "bad-ready" {
|
||||
frame, _ := EncodeAck(Ack{Kind: Ready, Epoch: epoch + 1})
|
||||
@@ -107,10 +111,9 @@ func TestSupervisorProcess(t *testing.T) {
|
||||
func testSupervisor(t *testing.T, mode string) (*Supervisor, func() *exec.Cmd) {
|
||||
t.Helper()
|
||||
var command *exec.Cmd
|
||||
supervisor := &Supervisor{command: func(epoch uint64) *exec.Cmd {
|
||||
supervisor := &Supervisor{command: func() *exec.Cmd {
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestSupervisorProcess$")
|
||||
cmd.Env = append(os.Environ(), "CTHULHU_SUPERVISOR_TEST="+mode,
|
||||
"CTHULHU_SUPERVISOR_EPOCH="+strconv.FormatUint(epoch, 10))
|
||||
cmd.Env = append(os.Environ(), "CTHULHU_SUPERVISOR_TEST="+mode)
|
||||
t.Cleanup(func() {
|
||||
if cmd.Process != nil {
|
||||
cmd.Process.Kill()
|
||||
@@ -230,6 +233,23 @@ pending:
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupervisorCommandOmitsEpochFromArgv(t *testing.T) {
|
||||
executable := filepath.Join(t.TempDir(), "helper")
|
||||
supervisor, err := NewSupervisor(executable)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
command := supervisor.command()
|
||||
if len(command.Args) != 1 || command.Args[0] != executable {
|
||||
t.Fatalf("helper argv = %v, want only the executable (epoch must travel over the control pipe)", command.Args)
|
||||
}
|
||||
for _, argument := range command.Args {
|
||||
if argument == "--epoch" {
|
||||
t.Fatal("epoch flag present in helper argv")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupervisorLaunchValidationAndFailure(t *testing.T) {
|
||||
if _, err := NewSupervisor("relative-helper"); !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatal("relative executable accepted", err)
|
||||
|
||||
Reference in new issue
Block a user