blob: 97de57522023db5bf8b2585a3bbcf6cf05b33545 [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
Jan Schär0175d7a2025-03-26 12:57:23 +000013 "golang.org/x/crypto/ssh"
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020014 "google.golang.org/protobuf/proto"
15
16 apb "source.monogon.dev/cloud/agent/api"
17)
18
Jan Schär0175d7a2025-03-26 12:57:23 +000019type fakeSSHClient struct{}
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020020
Jan Schär0175d7a2025-03-26 12:57:23 +000021// FakeSSHDial pretends to start an agent, but in reality just responds with
22// what an agent would respond on every execution attempt.
23func FakeSSHDial(ctx context.Context, address string, config *ssh.ClientConfig) (SSHClient, error) {
24 return &fakeSSHClient{}, nil
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020025}
26
Jan Schär0175d7a2025-03-26 12:57:23 +000027func (f *fakeSSHClient) Execute(ctx context.Context, command string, stdin []byte) (stdout []byte, stderr []byte, err error) {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020028 var aim apb.TakeoverInit
29 if err := proto.Unmarshal(stdin, &aim); err != nil {
Tim Windelschmidt327cdba2024-05-21 13:51:32 +020030 return nil, nil, fmt.Errorf("while unmarshaling TakeoverInit message: %w", err)
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020031 }
32
33 // Agent should send back apb.TakeoverResponse on its standard output.
34 pub, _, err := ed25519.GenerateKey(rand.Reader)
35 if err != nil {
Tim Windelschmidt327cdba2024-05-21 13:51:32 +020036 return nil, nil, fmt.Errorf("while generating agent public key: %w", err)
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020037 }
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 Windelschmidt327cdba2024-05-21 13:51:32 +020046 return nil, nil, fmt.Errorf("while marshaling TakeoverResponse message: %w", err)
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020047 }
48 return arspb, nil, nil
49}
50
Jan Schär0175d7a2025-03-26 12:57:23 +000051func (f *fakeSSHClient) UploadExecutable(ctx context.Context, targetPath string, _ io.Reader) error {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020052 if targetPath != "/fake/path" {
53 return fmt.Errorf("unexpected target path in test")
54 }
55 return nil
56}
57
Jan Schär0175d7a2025-03-26 12:57:23 +000058func (f *fakeSSHClient) Close() error {
Tim Windelschmidtb6308cd2023-10-10 21:19:03 +020059 return nil
60}