| 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 | |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 4 | package manager |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "crypto/ed25519" |
| 9 | "crypto/rand" |
| 10 | "fmt" |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 11 | "io" |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 12 | "time" |
| 13 | |
| 14 | "google.golang.org/protobuf/proto" |
| 15 | |
| 16 | apb "source.monogon.dev/cloud/agent/api" |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 17 | |
| 18 | "source.monogon.dev/go/net/ssh" |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 19 | ) |
| 20 | |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 21 | // FakeSSHClient is an Client that pretends to start an agent, but in reality |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 22 | // just responds with what an agent would respond on every execution attempt. |
| 23 | type FakeSSHClient struct{} |
| 24 | |
| 25 | type fakeSSHConnection struct{} |
| 26 | |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 27 | func (f *FakeSSHClient) Dial(ctx context.Context, address string, timeout time.Duration) (ssh.Connection, error) { |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 28 | return &fakeSSHConnection{}, nil |
| 29 | } |
| 30 | |
| 31 | func (f *fakeSSHConnection) Execute(ctx context.Context, command string, stdin []byte) (stdout []byte, stderr []byte, err error) { |
| 32 | var aim apb.TakeoverInit |
| 33 | if err := proto.Unmarshal(stdin, &aim); err != nil { |
| Tim Windelschmidt | 327cdba | 2024-05-21 13:51:32 +0200 | [diff] [blame] | 34 | return nil, nil, fmt.Errorf("while unmarshaling TakeoverInit message: %w", err) |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Agent should send back apb.TakeoverResponse on its standard output. |
| 38 | pub, _, err := ed25519.GenerateKey(rand.Reader) |
| 39 | if err != nil { |
| Tim Windelschmidt | 327cdba | 2024-05-21 13:51:32 +0200 | [diff] [blame] | 40 | return nil, nil, fmt.Errorf("while generating agent public key: %w", err) |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 41 | } |
| 42 | arsp := apb.TakeoverResponse{ |
| 43 | Result: &apb.TakeoverResponse_Success{Success: &apb.TakeoverSuccess{ |
| 44 | InitMessage: &aim, |
| 45 | Key: pub, |
| 46 | }}, |
| 47 | } |
| 48 | arspb, err := proto.Marshal(&arsp) |
| 49 | if err != nil { |
| Tim Windelschmidt | 327cdba | 2024-05-21 13:51:32 +0200 | [diff] [blame] | 50 | return nil, nil, fmt.Errorf("while marshaling TakeoverResponse message: %w", err) |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 51 | } |
| 52 | return arspb, nil, nil |
| 53 | } |
| 54 | |
| Tim Windelschmidt | 5f5f330 | 2024-02-22 23:50:24 +0100 | [diff] [blame] | 55 | func (f *fakeSSHConnection) Upload(ctx context.Context, targetPath string, _ io.Reader) error { |
| Tim Windelschmidt | b6308cd | 2023-10-10 21:19:03 +0200 | [diff] [blame] | 56 | if targetPath != "/fake/path" { |
| 57 | return fmt.Errorf("unexpected target path in test") |
| 58 | } |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | func (f *fakeSSHConnection) Close() error { |
| 63 | return nil |
| 64 | } |