diff options
Diffstat (limited to 'cmd/podman/system')
-rw-r--r-- | cmd/podman/system/connection/add.go | 87 |
1 files changed, 22 insertions, 65 deletions
diff --git a/cmd/podman/system/connection/add.go b/cmd/podman/system/connection/add.go index 191603718..f3b61b254 100644 --- a/cmd/podman/system/connection/add.go +++ b/cmd/podman/system/connection/add.go @@ -1,23 +1,19 @@ package connection import ( - "encoding/json" "errors" "fmt" - "net" "net/url" "os" "regexp" "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/config" + "github.com/containers/common/pkg/ssh" "github.com/containers/podman/v4/cmd/podman/registry" "github.com/containers/podman/v4/cmd/podman/system" - "github.com/containers/podman/v4/libpod/define" - "github.com/containers/podman/v4/pkg/domain/utils" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "golang.org/x/crypto/ssh" ) var ( @@ -74,6 +70,15 @@ func init() { func add(cmd *cobra.Command, args []string) error { // Default to ssh schema if none given + + entities := &ssh.ConnectionCreateOptions{ + Port: cOpts.Port, + Path: args[1], + Identity: cOpts.Identity, + Name: args[0], + Socket: cOpts.UDSPath, + Default: cOpts.Default, + } dest := args[1] if match, err := regexp.Match("^[A-Za-z][A-Za-z0-9+.-]*://", []byte(dest)); err != nil { return fmt.Errorf("invalid destination: %w", err) @@ -89,30 +94,20 @@ func add(cmd *cobra.Command, args []string) error { uri.Path = cmd.Flag("socket-path").Value.String() } - switch uri.Scheme { - case "ssh": - if uri.User.Username() == "" { - if uri.User, err = utils.GetUserInfo(uri); err != nil { - return err - } - } + var sshMode ssh.EngineMode + containerConfig := registry.PodmanConfig() - if cmd.Flags().Changed("port") { - uri.Host = net.JoinHostPort(uri.Hostname(), cmd.Flag("port").Value.String()) - } + flag := containerConfig.SSHMode - if uri.Port() == "" { - uri.Host = net.JoinHostPort(uri.Hostname(), cmd.Flag("port").DefValue) - } - iden := "" - if cmd.Flags().Changed("identity") { - iden = cOpts.Identity - } - if uri.Path == "" || uri.Path == "/" { - if uri.Path, err = getUDS(uri, iden); err != nil { - return err - } - } + sshMode = ssh.DefineMode(flag) + + if sshMode == ssh.InvalidMode { + return fmt.Errorf("invalid ssh mode") + } + + switch uri.Scheme { + case "ssh": + return ssh.Create(entities, sshMode) case "unix": if cmd.Flags().Changed("identity") { return errors.New("--identity option not supported for unix scheme") @@ -176,41 +171,3 @@ func add(cmd *cobra.Command, args []string) error { } return cfg.Write() } - -func getUDS(uri *url.URL, iden string) (string, error) { - cfg, err := utils.ValidateAndConfigure(uri, iden) - if err != nil { - return "", fmt.Errorf("failed to validate: %w", err) - } - dial, err := ssh.Dial("tcp", uri.Host, cfg) - if err != nil { - return "", fmt.Errorf("failed to connect: %w", err) - } - defer dial.Close() - - session, err := dial.NewSession() - if err != nil { - return "", fmt.Errorf("failed to create new ssh session on %q: %w", uri.Host, err) - } - defer session.Close() - - // Override podman binary for testing etc - podman := "podman" - if v, found := os.LookupEnv("PODMAN_BINARY"); found { - podman = v - } - infoJSON, err := utils.ExecRemoteCommand(dial, podman+" info --format=json") - if err != nil { - return "", err - } - - var info define.Info - if err := json.Unmarshal(infoJSON, &info); err != nil { - return "", fmt.Errorf("failed to parse 'podman info' results: %w", err) - } - - if info.Host.RemoteSocket == nil || len(info.Host.RemoteSocket.Path) == 0 { - return "", fmt.Errorf("remote podman %q failed to report its UDS socket", uri.Host) - } - return info.Host.RemoteSocket.Path, nil -} |