osbase/net/sshtakeover: refactor package
This is an extensive refactoring of the sshtakeover package.
The package is renamed from //go/net/ssh to //osbase/net/sshtakeover,
to make it more clear what the package is for and avoid clashes with
golang.org/x/crypto/ssh.
The previous Client type was removed, and Dial is now a top-level
function which takes an ssh.ClientConfig. The previous Connection type
was renamed to Client, which makes the naming match ssh.Client.
The Client interface type was moved to //cloud/shepherd/manager. This
allows us to add more functions to sshtakeover.Client without breaking
consumers of the package, which would need to add dummy implementations
for functions which they don't need.
The Upload function was renamed to UploadExecutable, and the new Upload
function can be used for files that don't need to be executable.
The sftp client is now created at the same time as the client, instead
of creating a new one for each uploaded file.
Change-Id: I3be9c346713cb4e5c2b33f9c8c9a6f11ca569a75
Reviewed-on: https://review.monogon.dev/c/monogon/+/4047
Tested-by: Jenkins CI
Reviewed-by: Tim Windelschmidt <tim@monogon.tech>
diff --git a/cloud/shepherd/manager/provider_test.go b/cloud/shepherd/manager/provider_test.go
index 003fd5e..2dc3fa9 100644
--- a/cloud/shepherd/manager/provider_test.go
+++ b/cloud/shepherd/manager/provider_test.go
@@ -8,15 +8,14 @@
"fmt"
"net/netip"
"sync"
- "time"
"github.com/google/uuid"
+ "golang.org/x/crypto/ssh"
"k8s.io/klog/v2"
"source.monogon.dev/cloud/bmaas/bmdb"
"source.monogon.dev/cloud/bmaas/bmdb/model"
"source.monogon.dev/cloud/shepherd"
- "source.monogon.dev/go/net/ssh"
)
type dummyMachine struct {
@@ -43,17 +42,12 @@
}
type dummySSHClient struct {
- ssh.Client
- dp *dummyProvider
-}
-
-type dummySSHConnection struct {
- ssh.Connection
+ SSHClient
m *dummyMachine
}
-func (dsc *dummySSHConnection) Execute(ctx context.Context, command string, stdin []byte) ([]byte, []byte, error) {
- stdout, stderr, err := dsc.Connection.Execute(ctx, command, stdin)
+func (dsc *dummySSHClient) Execute(ctx context.Context, command string, stdin []byte) ([]byte, []byte, error) {
+ stdout, stderr, err := dsc.SSHClient.Execute(ctx, command, stdin)
if err != nil {
return nil, nil, err
}
@@ -62,8 +56,8 @@
return stdout, stderr, nil
}
-func (dsc *dummySSHClient) Dial(ctx context.Context, address string, timeout time.Duration) (ssh.Connection, error) {
- conn, err := dsc.Client.Dial(ctx, address, timeout)
+func (dp *dummyProvider) FakeSSHDial(ctx context.Context, address string, config *ssh.ClientConfig) (SSHClient, error) {
+ conn, err := FakeSSHDial(ctx, address, config)
if err != nil {
return nil, err
}
@@ -74,21 +68,14 @@
return nil, err
}
- dsc.dp.muMachines.RLock()
- m := dsc.dp.machines[shepherd.ProviderID(uid.String())]
- dsc.dp.muMachines.RUnlock()
+ dp.muMachines.RLock()
+ m := dp.machines[shepherd.ProviderID(uid.String())]
+ dp.muMachines.RUnlock()
if m == nil {
return nil, fmt.Errorf("failed finding machine in map")
}
- return &dummySSHConnection{conn, m}, nil
-}
-
-func (dp *dummyProvider) sshClient() ssh.Client {
- return &dummySSHClient{
- Client: &FakeSSHClient{},
- dp: dp,
- }
+ return &dummySSHClient{conn, m}, nil
}
func newDummyProvider(cap int) *dummyProvider {