establish SSH connection when adding a new machine

This commit is contained in:
Pavel Sviderski
2024-09-11 20:46:43 +10:00
parent 03c2c0df39
commit fb236fb922
4 changed files with 68 additions and 15 deletions
-9
View File
@@ -2,7 +2,6 @@ package client
import (
"context"
"crypto/ed25519"
"errors"
"fmt"
"google.golang.org/grpc"
@@ -297,11 +296,3 @@ func (c *ClusterClient) registerNewMachine(
}
return c.client.AddMachine(ctx, req)
}
func privateKeyFromSecret(s secret.Secret) (ed25519.PrivateKey, error) {
// Cluster secret in the config is a hex-encoded private key seed.
if len(s) != ed25519.SeedSize {
return nil, fmt.Errorf("invalid cluster secret length")
}
return ed25519.NewKeyFromSeed(s), nil
}
+14 -4
View File
@@ -41,12 +41,22 @@ func NewSSHConnector(cfg *SSHConnectorConfig) *SSHConnector {
return c
}
func NewSSHConnectorFromClient(client *ssh.Client) *SSHConnector {
return &SSHConnector{client: client}
}
// TODO: handle context cancelation.
func (c *SSHConnector) Connect(ctx context.Context) (*grpc.ClientConn, error) {
var err error
c.client, err = sshexec.Connect(c.config.User, c.config.Host, c.config.Port, c.config.KeyPath)
if err != nil {
return nil, fmt.Errorf("SSH login to %s@%s:%d: %w", c.config.User, c.config.Host, c.config.Port, err)
if c.client == nil {
// Establish an SSH connection if the SSH client is not provided.
if c.config == (SSHConnectorConfig{}) {
return nil, fmt.Errorf("SSH connector not configured")
}
var err error
c.client, err = sshexec.Connect(c.config.User, c.config.Host, c.config.Port, c.config.KeyPath)
if err != nil {
return nil, fmt.Errorf("SSH login to %s@%s:%d: %w", c.config.User, c.config.Host, c.config.Port, err)
}
}
conn, err := grpc.NewClient(