1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
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.MatchString("^[A-Za-z][A-Za-z0-9+.-]*://", 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
}
|