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