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