mirror of
https://github.com/containers/podman.git
synced 2025-05-22 17:46:52 +08:00

implement new ssh interface into podman this completely redesigns the entire functionality of podman image scp, podman system connection add, and podman --remote. All references to golang.org/x/crypto/ssh have been moved to common as have native ssh/scp execs and the new usage of the sftp package. this PR adds a global flag, --ssh to podman which has two valid inputs `golang` and `native` where golang is the default. Users should not notice any difference in their everyday workflows if they continue using the golang option. UNLESS they have been using an improperly verified ssh key, this will now fail. This is because podman was incorrectly using the ssh callback method to IGNORE the ssh known hosts file which is very insecure and golang tells you not yo use this in production. The native paths allows for immense flexibility, with a new containers.conf field `SSH_CONFIG` that specifies a specific ssh config file to be used in all operations. Else the users ~/.ssh/config file will be used. podman --remote currently only uses the golang path, given its deep interconnection with dialing multiple clients and urls. My goal after this PR is to go back and abstract the idea of podman --remote from golang's dialed clients, as it should not be so intrinsically connected. Overall, this is a v1 of a long process of offering native ssh, and one that covers some good ground with podman system connection add and podman image scp. Signed-off-by: Charlie Doern <cdoern@redhat.com>
183 lines
4.1 KiB
Go
183 lines
4.1 KiB
Go
package ssh
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os/exec"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/containers/common/pkg/config"
|
|
)
|
|
|
|
func nativeConnectionCreate(options ConnectionCreateOptions) error {
|
|
var match bool
|
|
var err error
|
|
if match, err = regexp.Match("^[A-Za-z][A-Za-z0-9+.-]*://", []byte(options.Path)); err != nil {
|
|
return fmt.Errorf("invalid destination: %w", err)
|
|
}
|
|
|
|
if !match {
|
|
options.Path = "ssh://" + options.Path
|
|
}
|
|
|
|
if len(options.Socket) > 0 {
|
|
options.Path += options.Socket
|
|
}
|
|
|
|
dst, uri, err := Validate(options.User, options.Path, options.Port, options.Identity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// test connection
|
|
ssh, err := exec.LookPath("ssh")
|
|
if err != nil {
|
|
return fmt.Errorf("no ssh binary found")
|
|
}
|
|
|
|
if strings.Contains(uri.Host, "/run") {
|
|
uri.Host = strings.Split(uri.Host, "/run")[0]
|
|
}
|
|
conf, err := config.Default()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
args := []string{uri.User.String() + "@" + uri.Hostname()}
|
|
|
|
if len(dst.Identity) > 0 {
|
|
args = append(args, "-i", dst.Identity)
|
|
}
|
|
if len(conf.Engine.SSHConfig) > 0 {
|
|
args = append(args, "-F", conf.Engine.SSHConfig)
|
|
}
|
|
|
|
output := &bytes.Buffer{}
|
|
args = append(args, "podman", "info", "--format", "json")
|
|
info := exec.Command(ssh, args...)
|
|
info.Stdout = output
|
|
err = info.Run()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
remoteInfo := &Info{}
|
|
if err := json.Unmarshal(output.Bytes(), &remoteInfo); err != nil {
|
|
return fmt.Errorf("failed to parse 'podman info' results: %w", err)
|
|
}
|
|
|
|
if remoteInfo.Host.RemoteSocket == nil || len(remoteInfo.Host.RemoteSocket.Path) == 0 {
|
|
return fmt.Errorf("remote podman %q failed to report its UDS socket", uri.Host)
|
|
}
|
|
|
|
cfg, err := config.ReadCustomConfig()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if options.Default {
|
|
cfg.Engine.ActiveService = options.Name
|
|
}
|
|
|
|
if cfg.Engine.ServiceDestinations == nil {
|
|
cfg.Engine.ServiceDestinations = map[string]config.Destination{
|
|
options.Name: *dst,
|
|
}
|
|
cfg.Engine.ActiveService = options.Name
|
|
} else {
|
|
cfg.Engine.ServiceDestinations[options.Name] = *dst
|
|
}
|
|
|
|
return cfg.Write()
|
|
}
|
|
|
|
func nativeConnectionExec(options ConnectionExecOptions) (*ConnectionExecReport, error) {
|
|
dst, uri, err := Validate(options.User, options.Host, options.Port, options.Identity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ssh, err := exec.LookPath("ssh")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("no ssh binary found")
|
|
}
|
|
|
|
output := &bytes.Buffer{}
|
|
errors := &bytes.Buffer{}
|
|
if strings.Contains(uri.Host, "/run") {
|
|
uri.Host = strings.Split(uri.Host, "/run")[0]
|
|
}
|
|
|
|
options.Args = append([]string{uri.User.String() + "@" + uri.Hostname()}, options.Args...)
|
|
conf, err := config.Default()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
args := []string{}
|
|
if len(dst.Identity) > 0 {
|
|
args = append(args, "-i", dst.Identity)
|
|
}
|
|
if len(conf.Engine.SSHConfig) > 0 {
|
|
args = append(args, "-F", conf.Engine.SSHConfig)
|
|
}
|
|
args = append(args, options.Args...)
|
|
info := exec.Command(ssh, args...)
|
|
info.Stdout = output
|
|
info.Stderr = errors
|
|
err = info.Run()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ConnectionExecReport{Response: output.String()}, nil
|
|
}
|
|
|
|
func nativeConnectionScp(options ConnectionScpOptions) (*ConnectionScpReport, error) {
|
|
host, remotePath, localPath, swap, err := ParseScpArgs(options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dst, uri, err := Validate(options.User, host, options.Port, options.Identity)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
scp, err := exec.LookPath("scp")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("no scp binary found")
|
|
}
|
|
|
|
conf, err := config.Default()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
args := []string{}
|
|
if len(dst.Identity) > 0 {
|
|
args = append(args, "-i", dst.Identity)
|
|
}
|
|
if len(conf.Engine.SSHConfig) > 0 {
|
|
args = append(args, "-F", conf.Engine.SSHConfig)
|
|
}
|
|
|
|
userString := ""
|
|
if !strings.Contains(host, "@") {
|
|
userString = uri.User.String() + "@"
|
|
}
|
|
// meaning, we are copying from a remote host
|
|
if swap {
|
|
args = append(args, userString+host+":"+remotePath, localPath)
|
|
} else {
|
|
args = append(args, localPath, userString+host+":"+remotePath)
|
|
}
|
|
|
|
info := exec.Command(scp, args...)
|
|
err = info.Run()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &ConnectionScpReport{Response: remotePath}, nil
|
|
}
|