Files
gvisor-tap-vsock/cmd/ssh-over-vsock/client.go
Christophe Fergeau 8c72b59df3 ioutil: Switch away from deprecated package
ioutil has been deprecated since golang 1.16.
This commit uses the replacement methods from the os or io packages.
This should fix some golangci-lint CI failures.

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
2022-09-06 18:33:45 +05:30

67 lines
1.3 KiB
Go

package main
import (
"fmt"
"net"
"os"
"time"
"golang.org/x/crypto/ssh"
)
type client struct {
Conn net.Conn
Config *ssh.ClientConfig
}
func newClient(conn net.Conn, user string, key string) (*client, error) {
config, err := newConfig(user, key)
if err != nil {
return nil, fmt.Errorf("Error getting config for native Go SSH: %s", err)
}
return &client{
Conn: conn,
Config: config,
}, nil
}
func newConfig(user string, keyFile string) (*ssh.ClientConfig, error) {
key, err := os.ReadFile(keyFile)
if err != nil {
return nil, err
}
privateKey, err := ssh.ParsePrivateKey(key)
if err != nil {
return nil, err
}
return &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{ssh.PublicKeys(privateKey)},
// #nosec G106
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Minute,
}, nil
}
func (client *client) output(command string) (string, error) {
c, chans, reqs, err := ssh.NewClientConn(client.Conn, "", client.Config)
if err != nil {
return "", err
}
conn := ssh.NewClient(c, chans, reqs)
session, err := conn.NewSession()
if err != nil {
_ = conn.Close()
return "", err
}
defer conn.Close()
defer session.Close()
output, err := session.CombinedOutput(command)
if err != nil {
return "", err
}
return string(output), nil
}