Merge pull request from baude/remoteconfigport

support non-standard ssh port for remote-client
This commit is contained in:
OpenShift Merge Robot
2019-09-18 21:12:04 +02:00
committed by GitHub
10 changed files with 48 additions and 6 deletions

@ -41,6 +41,7 @@ type MainFlags struct {
VarlinkAddress string VarlinkAddress string
ConnectionName string ConnectionName string
RemoteConfigFilePath string RemoteConfigFilePath string
Port int
} }
type AttachValues struct { type AttachValues struct {

@ -111,6 +111,11 @@ func before(cmd *cobra.Command, args []string) error {
return err return err
} }
// check that global opts input is valid
if err := checkInput(); err != nil {
return err
}
// Set log level; if not log-level is provided, default to error // Set log level; if not log-level is provided, default to error
logLevel := MainGlobalOpts.LogLevel logLevel := MainGlobalOpts.LogLevel
if logLevel == "" { if logLevel == "" {

@ -267,3 +267,8 @@ func setUMask() {
// Be sure we can create directories with 0755 mode. // Be sure we can create directories with 0755 mode.
syscall.Umask(0022) syscall.Umask(0022)
} }
// checkInput can be used to verify any of the globalopt values
func checkInput() error {
return nil
}

@ -3,6 +3,7 @@
package main package main
import ( import (
"github.com/pkg/errors"
"os/user" "os/user"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -18,6 +19,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.ConnectionName, "connection", "", "remote connection name") rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.ConnectionName, "connection", "", "remote connection name")
rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.RemoteConfigFilePath, "remote-config-path", "", "alternate path for configuration file") rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.RemoteConfigFilePath, "remote-config-path", "", "alternate path for configuration file")
rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.RemoteUserName, "username", username, "username on the remote host") rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.RemoteUserName, "username", username, "username on the remote host")
rootCmd.PersistentFlags().IntVar(&MainGlobalOpts.Port, "port", 22, "port on remote host")
rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.RemoteHost, "remote-host", "", "remote host") rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.RemoteHost, "remote-host", "", "remote host")
// TODO maybe we allow the altering of this for bridge connections? // TODO maybe we allow the altering of this for bridge connections?
// rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.VarlinkAddress, "varlink-address", adapter.DefaultAddress, "address of the varlink socket") // rootCmd.PersistentFlags().StringVar(&MainGlobalOpts.VarlinkAddress, "varlink-address", adapter.DefaultAddress, "address of the varlink socket")
@ -42,3 +44,11 @@ func setRLimits() error {
} }
func setUMask() {} func setUMask() {}
// checkInput can be used to verify any of the globalopt values
func checkInput() error {
if MainGlobalOpts.Port < 0 || MainGlobalOpts.Port > 65536 {
return errors.Errorf("remote port must be between 0 and 65536")
}
return nil
}

@ -12,6 +12,7 @@ type RemoteConnection struct {
Destination string `toml:"destination"` Destination string `toml:"destination"`
Username string `toml:"username"` Username string `toml:"username"`
IsDefault bool `toml:"default"` IsDefault bool `toml:"default"`
Port int `toml:"port"`
} }
// GetConfigFilePath is a simple helper to export the configuration file's // GetConfigFilePath is a simple helper to export the configuration file's

@ -13,11 +13,13 @@ var goodConfig = `
[connections.homer] [connections.homer]
destination = "192.168.1.1" destination = "192.168.1.1"
username = "myuser" username = "myuser"
port = 22
default = true default = true
[connections.bart] [connections.bart]
destination = "foobar.com" destination = "foobar.com"
username = "root" username = "root"
port = 22
` `
var noDest = ` var noDest = `
[connections] [connections]
@ -26,9 +28,11 @@ var noDest = `
destination = "192.168.1.1" destination = "192.168.1.1"
username = "myuser" username = "myuser"
default = true default = true
port = 22
[connections.bart] [connections.bart]
username = "root" username = "root"
port = 22
` `
var noUser = ` var noUser = `
@ -36,6 +40,7 @@ var noUser = `
[connections.homer] [connections.homer]
destination = "192.168.1.1" destination = "192.168.1.1"
port = 22
` `
func makeGoodResult() *RemoteConfig { func makeGoodResult() *RemoteConfig {
@ -44,10 +49,12 @@ func makeGoodResult() *RemoteConfig {
Destination: "192.168.1.1", Destination: "192.168.1.1",
Username: "myuser", Username: "myuser",
IsDefault: true, IsDefault: true,
Port: 22,
} }
goodConnections["bart"] = RemoteConnection{ goodConnections["bart"] = RemoteConnection{
Destination: "foobar.com", Destination: "foobar.com",
Username: "root", Username: "root",
Port: 22,
} }
var goodResult = RemoteConfig{ var goodResult = RemoteConfig{
Connections: goodConnections, Connections: goodConnections,
@ -59,6 +66,7 @@ func makeNoUserResult() *RemoteConfig {
var goodConnections = make(map[string]RemoteConnection) var goodConnections = make(map[string]RemoteConnection)
goodConnections["homer"] = RemoteConnection{ goodConnections["homer"] = RemoteConnection{
Destination: "192.168.1.1", Destination: "192.168.1.1",
Port: 22,
} }
var goodResult = RemoteConfig{ var goodResult = RemoteConfig{
Connections: goodConnections, Connections: goodConnections,
@ -135,7 +143,7 @@ func TestRemoteConfig_GetDefault(t *testing.T) {
wantErr bool wantErr bool
}{ }{
// A good toml should return the connection that is marked isDefault // A good toml should return the connection that is marked isDefault
{"good", fields{Connections: makeGoodResult().Connections}, &RemoteConnection{"192.168.1.1", "myuser", true}, false}, {"good", fields{Connections: makeGoodResult().Connections}, &RemoteConnection{"192.168.1.1", "myuser", true, 22}, false},
// If nothing is marked as isDefault and there is more than one connection, error should occur // If nothing is marked as isDefault and there is more than one connection, error should occur
{"nodefault", fields{Connections: noDefault}, nil, true}, {"nodefault", fields{Connections: noDefault}, nil, true},
// if nothing is marked as isDefault but there is only one connection, the one connection is considered the default // if nothing is marked as isDefault but there is only one connection, the one connection is considered the default
@ -175,9 +183,9 @@ func TestRemoteConfig_GetRemoteConnection(t *testing.T) {
wantErr bool wantErr bool
}{ }{
// Good connection // Good connection
{"goodhomer", fields{Connections: makeGoodResult().Connections}, args{name: "homer"}, &RemoteConnection{"192.168.1.1", "myuser", true}, false}, {"goodhomer", fields{Connections: makeGoodResult().Connections}, args{name: "homer"}, &RemoteConnection{"192.168.1.1", "myuser", true, 22}, false},
// Good connection // Good connection
{"goodbart", fields{Connections: makeGoodResult().Connections}, args{name: "bart"}, &RemoteConnection{"foobar.com", "root", false}, false}, {"goodbart", fields{Connections: makeGoodResult().Connections}, args{name: "bart"}, &RemoteConnection{"foobar.com", "root", false, 22}, false},
// Getting an unknown connection should result in error // Getting an unknown connection should result in error
{"noexist", fields{Connections: makeGoodResult().Connections}, args{name: "foobar"}, nil, true}, {"noexist", fields{Connections: makeGoodResult().Connections}, args{name: "foobar"}, nil, true},
// Getting a connection when there are none should result in an error // Getting a connection when there are none should result in an error

@ -35,6 +35,10 @@ Print usage statement
Log messages above specified level: debug, info, warn, error (default), fatal or panic Log messages above specified level: debug, info, warn, error (default), fatal or panic
**--port**=*integer*
Use an alternative port for the ssh connections. The default port is 22
**--remote-config-path**=*path* **--remote-config-path**=*path*
Alternate path for configuration file Alternate path for configuration file

@ -22,6 +22,9 @@ of the user's remote connections.
Denotes whether the connection is the default connection for the user. The default connection Denotes whether the connection is the default connection for the user. The default connection
is used when the user does not specify a destination or connection name to `podman`. is used when the user does not specify a destination or connection name to `podman`.
**port** = int
Use an alternative port for the ssh connections. The default port is 22.
## EXAMPLE ## EXAMPLE
@ -37,6 +40,7 @@ is designated as the default connection.
[connections.host2] [connections.host2]
destination = "192.168.122.133" destination = "192.168.122.133"
username = "fedora" username = "fedora"
port = 2222
``` ```
## FILES ## FILES

@ -35,7 +35,7 @@ func (r RemoteRuntime) RemoteEndpoint() (remoteEndpoint *Endpoint, err error) {
if len(r.cmd.RemoteUserName) < 1 { if len(r.cmd.RemoteUserName) < 1 {
return nil, errors.New("you must provide a username when providing a remote host name") return nil, errors.New("you must provide a username when providing a remote host name")
} }
rc := remoteclientconfig.RemoteConnection{r.cmd.RemoteHost, r.cmd.RemoteUserName, false} rc := remoteclientconfig.RemoteConnection{r.cmd.RemoteHost, r.cmd.RemoteUserName, false, r.cmd.Port}
remoteEndpoint, err = newBridgeConnection("", &rc, r.cmd.LogLevel) remoteEndpoint, err = newBridgeConnection("", &rc, r.cmd.LogLevel)
// if the user has a config file with connections in it // if the user has a config file with connections in it
} else if len(remoteConfigConnections.Connections) > 0 { } else if len(remoteConfigConnections.Connections) > 0 {

@ -10,7 +10,11 @@ import (
) )
func formatDefaultBridge(remoteConn *remoteclientconfig.RemoteConnection, logLevel string) string { func formatDefaultBridge(remoteConn *remoteclientconfig.RemoteConnection, logLevel string) string {
port := remoteConn.Port
if port == 0 {
port = 22
}
return fmt.Sprintf( return fmt.Sprintf(
`ssh -T %s@%s -- /usr/bin/varlink -A \'/usr/bin/podman --log-level=%s varlink \\\$VARLINK_ADDRESS\' bridge`, `ssh -p %d -T %s@%s -- /usr/bin/varlink -A \'/usr/bin/podman --log-level=%s varlink \\\$VARLINK_ADDRESS\' bridge`,
remoteConn.Username, remoteConn.Destination, logLevel) port, remoteConn.Username, remoteConn.Destination, logLevel)
} }