blob: db13b5bf1668f068814558a22a3c283170c5dd7a [file] [log] [blame]
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +02001package manager
2
3import (
4 "context"
5 "crypto/ed25519"
6 "crypto/rand"
7 "fmt"
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +01008 "io"
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +02009 "time"
10
11 "google.golang.org/protobuf/proto"
12
13 apb "source.monogon.dev/cloud/agent/api"
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010014
15 "source.monogon.dev/go/net/ssh"
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020016)
17
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010018// FakeSSHClient is an Client that pretends to start an agent, but in reality
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020019// just responds with what an agent would respond on every execution attempt.
20type FakeSSHClient struct{}
21
22type fakeSSHConnection struct{}
23
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010024func (f *FakeSSHClient) Dial(ctx context.Context, address string, timeout time.Duration) (ssh.Connection, error) {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020025 return &fakeSSHConnection{}, nil
26}
27
28func (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 {
31 return nil, nil, fmt.Errorf("while unmarshaling TakeoverInit message: %v", err)
32 }
33
34 // Agent should send back apb.TakeoverResponse on its standard output.
35 pub, _, err := ed25519.GenerateKey(rand.Reader)
36 if err != nil {
37 return nil, nil, fmt.Errorf("while generating agent public key: %v", err)
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 {
47 return nil, nil, fmt.Errorf("while marshaling TakeoverResponse message: %v", err)
48 }
49 return arspb, nil, nil
50}
51
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010052func (f *fakeSSHConnection) Upload(ctx context.Context, targetPath string, _ io.Reader) error {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020053 if targetPath != "/fake/path" {
54 return fmt.Errorf("unexpected target path in test")
55 }
56 return nil
57}
58
59func (f *fakeSSHConnection) Close() error {
60 return nil
61}