Add kube connect subcommand (#816)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-12-14 16:52:11 -05:00
1 parent d9a6bd1a60
commit b809d3f12a
4 files changed
+124 -5

No files matched your search

+10 -4
View File
@@ -257,10 +257,16 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
Func: "http",
}, nil
},
"connect ssh": func() (cli.Command, error) {
"connect kube": func() (cli.Command, error) {
return &connect.Command{
Command: base.NewCommand(ui),
Func: "ssh",
Func: "kube",
}, nil
},
"connect postgres": func() (cli.Command, error) {
return &connect.Command{
Command: base.NewCommand(ui),
Func: "postgres",
}, nil
},
"connect rdp": func() (cli.Command, error) {
@@ -269,10 +275,10 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
Func: "rdp",
}, nil
},
"connect postgres": func() (cli.Command, error) {
"connect ssh": func() (cli.Command, error) {
return &connect.Command{
Command: base.NewCommand(ui),
Func: "postgres",
Func: "ssh",
}, nil
},
+19
View File
@@ -72,6 +72,9 @@ type Command struct {
// HTTP
httpFlags
// Kube
kubeFlags
// Postgres
postgresFlags
@@ -110,6 +113,8 @@ func (c *Command) Synopsis() string {
return rdpSynopsis
case "ssh":
return sshSynopsis
case "kube":
return kubeSynopsis
default:
return ""
}
@@ -237,6 +242,9 @@ func (c *Command) Flags() *base.FlagSets {
case "ssh":
sshOptions(c, set)
case "kube":
kubeOptions(c, set)
}
return set
@@ -300,6 +308,8 @@ func (c *Command) Run(args []string) (retCode int) {
c.flagExec = c.postgresFlags.defaultExec()
case "rdp":
c.flagExec = c.rdpFlags.defaultExec()
case "kube":
c.flagExec = c.kubeFlags.defaultExec()
}
}
@@ -750,6 +760,15 @@ func (c *Command) handleExec(passthroughArgs []string) {
case "ssh":
args = append(args, c.sshFlags.buildArgs(c, port, ip, addr)...)
case "kube":
kubeArgs, err := c.kubeFlags.buildArgs(c, port, ip, addr)
if err != nil {
c.Error(fmt.Sprintf("Error parsing session args: %s", err))
c.execCmdReturnValue.Store(int32(3))
return
}
args = append(args, kubeArgs...)
}
args = append(passthroughArgs, args...)
+76
View File
@@ -0,0 +1,76 @@
package connect
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/posener/complete"
)
const (
kubeSynopsis = "Authorize a session against a target and invoke a Kubernetes client to connect"
)
func kubeOptions(c *Command, set *base.FlagSets) {
f := set.NewFlagSet("Kubernetes Options")
f.StringVar(&base.StringVar{
Name: "style",
Target: &c.flagKubeStyle,
EnvVar: fmt.Sprintf("BOUNDARY_CONNECT_%s_STYLE", strings.ToUpper(c.Func)),
Completion: complete.PredictSet("kubectl"),
Default: "kubectl",
Usage: `Specifies how the CLI will attempt to invoke a Kubernetes client. This will also set a suitable default for -exec if a value was not specified. Currently-understood values are "kubectl".`,
})
f.StringVar(&base.StringVar{
Name: "host",
Target: &c.flagKubeHost,
EnvVar: fmt.Sprintf("BOUNDARY_CONNECT_%s_HOST", strings.ToUpper(c.Func)),
Completion: complete.PredictNothing,
Usage: `Specifies the host value to use, overriding the endpoint address from the session information. The specified hostname will be passed through to the client (if supported) for use in the TLS SNI value.`,
})
f.StringVar(&base.StringVar{
Name: "scheme",
Target: &c.flagKubeScheme,
Default: "https",
EnvVar: fmt.Sprintf("BOUNDARY_CONNECT_%s_SCHEME", strings.ToUpper(c.Func)),
Completion: complete.PredictNothing,
Usage: `Specifies the scheme to use.`,
})
}
type kubeFlags struct {
flagKubeStyle string
flagKubeHost string
flagKubeScheme string
}
func (f *kubeFlags) defaultExec() string {
return strings.ToLower(f.flagKubeStyle)
}
func (f *kubeFlags) buildArgs(c *Command, port, ip, addr string) ([]string, error) {
var args []string
host := f.flagKubeHost
if host == "" && c.sessionAuthzData.GetEndpoint() != "" {
hostUrl := c.sessionAuthzData.GetEndpoint()
u, err := url.Parse(hostUrl)
if err != nil {
return nil, fmt.Errorf("error parsing endpoint URL: %w", err)
}
host = u.Hostname()
}
switch f.flagKubeStyle {
case "kubectl":
if host != "" && f.flagKubeScheme == "https" {
host = strings.TrimSuffix(host, "/")
args = append(args, "--tls-server-name", host)
}
args = append(args, "--server", fmt.Sprintf("%s://%s", f.flagKubeScheme, addr))
}
return args, nil
}