cloud: split shepherd up

Change-Id: I8e386d9eaaf17543743e1e8a37a8d71426910d59
Reviewed-on: https://review.monogon.dev/c/monogon/+/2213
Reviewed-by: Serge Bazanski <serge@monogon.tech>
Tested-by: Jenkins CI
diff --git a/cloud/shepherd/manager/test_agent/BUILD.bazel b/cloud/shepherd/manager/test_agent/BUILD.bazel
new file mode 100644
index 0000000..7636cdd
--- /dev/null
+++ b/cloud/shepherd/manager/test_agent/BUILD.bazel
@@ -0,0 +1,28 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
+load("//build/static_binary_tarball:def.bzl", "static_binary_tarball")
+
+go_binary(
+    name = "test_agent",
+    embed = [":test_agent_lib"],
+    visibility = [
+        "//cloud/shepherd/manager:__pkg__",
+    ],
+)
+
+go_library(
+    name = "test_agent_lib",
+    srcs = ["main.go"],
+    importpath = "source.monogon.dev/cloud/shepherd/manager/test_agent",
+    visibility = ["//visibility:private"],
+    deps = [
+        "//cloud/agent/api",
+        "@org_golang_google_protobuf//proto",
+    ],
+)
+
+# Used by container_images, forces a static build of the test_agent.
+static_binary_tarball(
+    name = "test_agent_layer",
+    executable = ":test_agent",
+    visibility = ["//visibility:public"],
+)
diff --git a/cloud/shepherd/manager/test_agent/main.go b/cloud/shepherd/manager/test_agent/main.go
new file mode 100644
index 0000000..8f29c30
--- /dev/null
+++ b/cloud/shepherd/manager/test_agent/main.go
@@ -0,0 +1,54 @@
+// test_agent is used by the Equinix Metal Manager test code. Its only role
+// is to ensure successful delivery of the BMaaS agent executable to the test
+// hosts, together with its subsequent execution.
+package main
+
+import (
+	"crypto/ed25519"
+	"crypto/rand"
+	"fmt"
+	"io"
+	"os"
+
+	"google.golang.org/protobuf/proto"
+
+	apb "source.monogon.dev/cloud/agent/api"
+)
+
+func main() {
+	// The agent initialization message will arrive from Shepherd on Agent's
+	// standard input.
+	aimb, err := io.ReadAll(os.Stdin)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "while reading AgentInit message: %v\n", err)
+		return
+	}
+	var aim apb.TakeoverInit
+	if err := proto.Unmarshal(aimb, &aim); err != nil {
+		fmt.Fprintf(os.Stderr, "while unmarshaling TakeoverInit message: %v\n", err)
+		return
+	}
+
+	// Agent should send back apb.TakeoverResponse on its standard output.
+	pub, _, err := ed25519.GenerateKey(rand.Reader)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "while generating agent public key: %v\n", err)
+		return
+	}
+	arsp := apb.TakeoverResponse{
+		Result: &apb.TakeoverResponse_Success{Success: &apb.TakeoverSuccess{
+			InitMessage: &aim,
+			Key:         pub,
+		}},
+	}
+	arspb, err := proto.Marshal(&arsp)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "while marshaling TakeoverResponse message: %v\n", err)
+		return
+	}
+	if _, err := os.Stdout.Write(arspb); err != nil {
+		fmt.Fprintf(os.Stderr, "while writing TakeoverResponse message: %v\n", err)
+	}
+	// The agent must detach and/or terminate after sending back the reply.
+	// Failure to do so will leave the session hanging.
+}