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