blob: d932210347e4d282d815cc44c080411d3e9b8ae6 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +02004package manager
5
6import (
7 "context"
8 "crypto/ed25519"
9 "crypto/rand"
10 "fmt"
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010011 "io"
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020012 "time"
13
14 "google.golang.org/protobuf/proto"
15
16 apb "source.monogon.dev/cloud/agent/api"
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010017
18 "source.monogon.dev/go/net/ssh"
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020019)
20
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010021// FakeSSHClient is an Client that pretends to start an agent, but in reality
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020022// just responds with what an agent would respond on every execution attempt.
23type FakeSSHClient struct{}
24
25type fakeSSHConnection struct{}
26
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010027func (f *FakeSSHClient) Dial(ctx context.Context, address string, timeout time.Duration) (ssh.Connection, error) {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020028 return &fakeSSHConnection{}, nil
29}
30
31func (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 Windelschmidt327cdba2024-05-21 13:51:32 +020034 return nil, nil, fmt.Errorf("while unmarshaling TakeoverInit message: %w", err)
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020035 }
36
37 // Agent should send back apb.TakeoverResponse on its standard output.
38 pub, _, err := ed25519.GenerateKey(rand.Reader)
39 if err != nil {
Tim Windelschmidt327cdba2024-05-21 13:51:32 +020040 return nil, nil, fmt.Errorf("while generating agent public key: %w", err)
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020041 }
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 Windelschmidt327cdba2024-05-21 13:51:32 +020050 return nil, nil, fmt.Errorf("while marshaling TakeoverResponse message: %w", err)
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020051 }
52 return arspb, nil, nil
53}
54
Tim Windelschmidt5f5f3302024-02-22 23:50:24 +010055func (f *fakeSSHConnection) Upload(ctx context.Context, targetPath string, _ io.Reader) error {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020056 if targetPath != "/fake/path" {
57 return fmt.Errorf("unexpected target path in test")
58 }
59 return nil
60}
61
62func (f *fakeSSHConnection) Close() error {
63 return nil
64}