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