update init command to send RPC calls over SSH tunnel

This commit is contained in:
Pavel Sviderski
2024-09-10 13:45:49 +10:00
parent 86d949ae48
commit 2514c804dc
5 changed files with 50 additions and 90 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
package cmdexec
package sshexec
import (
"context"
+5 -1
View File
@@ -1,4 +1,4 @@
package cmdexec
package sshexec
import (
"context"
@@ -11,6 +11,10 @@ type Remote struct {
client *ssh.Client
}
func NewRemote(client *ssh.Client) *Remote {
return &Remote{client: client}
}
// Run runs the command on the remote host and returns its output with all leading and trailing
// white space removed.
func (r *Remote) Run(ctx context.Context, cmd string) (string, error) {
+4 -8
View File
@@ -1,4 +1,4 @@
package cmdexec
package sshexec
import (
"fmt"
@@ -10,7 +10,7 @@ import (
"time"
)
func Connect(user, host string, port int, sshKeyPath string) (*Remote, error) {
func Connect(user, host string, port int, sshKeyPath string) (*ssh.Client, error) {
addr := net.JoinHostPort(host, strconv.Itoa(port))
// Try to connect using SSH agent only.
agentAuth, agentClose, agentErr := sshAgentAuth()
@@ -24,9 +24,7 @@ func Connect(user, host string, port int, sshKeyPath string) (*Remote, error) {
}
var client *ssh.Client
if client, agentErr = ssh.Dial("tcp", addr, config); agentErr == nil {
return &Remote{
client: client,
}, nil
return client, nil
}
}
// Fall back to using private key as the connection attempt using SSH agent failed.
@@ -47,9 +45,7 @@ func Connect(user, host string, port int, sshKeyPath string) (*Remote, error) {
return nil, fmt.Errorf("connect using private key %q: %w", sshKeyPath, err)
}
return &Remote{
client: client,
}, nil
return client, nil
}
func sshAgentAuth() (ssh.AuthMethod, func(), error) {