blob: 5dd5ccd90c4890b0dac6c18adc756f72762e4c5d [file] [log] [blame]
Serge Bazanskicaa12082023-02-16 14:54:04 +01001// test_agent is used by the Equinix Metal Manager test code. Its only role
2// is to ensure successful delivery of the BMaaS agent executable to the test
3// hosts, together with its subsequent execution.
4package main
5
6import (
7 "crypto/ed25519"
8 "crypto/rand"
9 "fmt"
10 "io"
11 "os"
12
13 "google.golang.org/protobuf/proto"
14
15 apb "source.monogon.dev/cloud/agent/api"
16)
17
18func main() {
19 // The agent initialization message will arrive from Shepherd on Agent's
20 // standard input.
21 aimb, err := io.ReadAll(os.Stdin)
22 if err != nil {
23 fmt.Fprintf(os.Stderr, "while reading AgentInit message: %v\n", err)
24 return
25 }
26 var aim apb.TakeoverInit
27 if err := proto.Unmarshal(aimb, &aim); err != nil {
28 fmt.Fprintf(os.Stderr, "while unmarshaling TakeoverInit message: %v\n", err)
29 return
30 }
31
32 // Agent should send back apb.TakeoverResponse on its standard output.
33 pub, _, err := ed25519.GenerateKey(rand.Reader)
34 if err != nil {
35 fmt.Fprintf(os.Stderr, "while generating agent public key: %v\n", err)
36 return
37 }
38 arsp := apb.TakeoverResponse{
39 InitMessage: &aim,
40 Key: pub,
41 }
42 arspb, err := proto.Marshal(&arsp)
43 if err != nil {
44 fmt.Fprintf(os.Stderr, "while marshaling TakeoverResponse message: %v\n", err)
45 return
46 }
47 if _, err := os.Stdout.Write(arspb); err != nil {
48 fmt.Fprintf(os.Stderr, "while writing TakeoverResponse message: %v\n", err)
49 }
50 // The agent must detach and/or terminate after sending back the reply.
51 // Failure to do so will leave the session hanging.
52}