ICU-14484: redis connect helper (#6001)
* add redis connect support * Setup redis in docker for e2e testing * Update docs and changelog * Commenting out e2e tests for now, will revisit in a separate task
This commit is contained in:
1 parent
f8a5894e29
commit
4548028efe
10 files changed
+386
No files matched your search
@@ -423,6 +423,12 @@ func initCommands(ui, serverCmdUi cli.Ui, runOpts *RunOptions) {
|
||||
Func: "cassandra",
|
||||
}
|
||||
}),
|
||||
"connect redis": wrapper.Wrap(func() wrapper.WrappableCommand {
|
||||
return &connect.Command{
|
||||
Command: base.NewCommand(ui, opts...),
|
||||
Func: "redis",
|
||||
}
|
||||
}),
|
||||
"connect rdp": wrapper.Wrap(func() wrapper.WrappableCommand {
|
||||
return &connect.Command{
|
||||
Command: base.NewCommand(ui, opts...),
|
||||
|
||||
@@ -84,6 +84,9 @@ type Command struct {
|
||||
// Cassandra
|
||||
cassandraFlags
|
||||
|
||||
// Redis
|
||||
redisFlags
|
||||
|
||||
// RDP
|
||||
rdpFlags
|
||||
|
||||
@@ -114,6 +117,8 @@ func (c *Command) Synopsis() string {
|
||||
return mysqlSynopsis
|
||||
case "cassandra":
|
||||
return cassandraSynopsis
|
||||
case "redis":
|
||||
return redisSynopsis
|
||||
case "rdp":
|
||||
return rdpSynopsis
|
||||
case "ssh":
|
||||
@@ -239,6 +244,9 @@ func (c *Command) Flags() *base.FlagSets {
|
||||
case "cassandra":
|
||||
cassandraOptions(c, set)
|
||||
|
||||
case "redis":
|
||||
redisOptions(c, set)
|
||||
|
||||
case "rdp":
|
||||
rdpOptions(c, set)
|
||||
|
||||
@@ -330,6 +338,8 @@ func (c *Command) Run(args []string) (retCode int) {
|
||||
c.flagExec = c.mysqlFlags.defaultExec()
|
||||
case "cassandra":
|
||||
c.flagExec = c.cassandraFlags.defaultExec()
|
||||
case "redis":
|
||||
c.flagExec = c.redisFlags.defaultExec()
|
||||
case "rdp":
|
||||
c.flagExec = c.rdpFlags.defaultExec()
|
||||
case "kube":
|
||||
@@ -714,6 +724,16 @@ func (c *Command) handleExec(clientProxy *apiproxy.ClientProxy, passthroughArgs
|
||||
envs = append(envs, cassandraEnvs...)
|
||||
creds = cassandraCreds
|
||||
|
||||
case "redis":
|
||||
redisArgs, redisEnvs, redisCreds, redisErr := c.redisFlags.buildArgs(c, port, host, addr, creds)
|
||||
if redisErr != nil {
|
||||
argsErr = redisErr
|
||||
break
|
||||
}
|
||||
args = append(args, redisArgs...)
|
||||
envs = append(envs, redisEnvs...)
|
||||
creds = redisCreds
|
||||
|
||||
case "rdp":
|
||||
args = append(args, c.rdpFlags.buildArgs(c, port, host, addr)...)
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package connect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/api/proxy"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
const (
|
||||
redisSynopsis = "Authorize a session against a target and invoke a redis client to connect"
|
||||
)
|
||||
|
||||
func redisOptions(c *Command, set *base.FlagSets) {
|
||||
f := set.NewFlagSet("Redis Options")
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "style",
|
||||
Target: &c.flagRedisStyle,
|
||||
EnvVar: "BOUNDARY_CONNECT_REDIS_STYLE",
|
||||
Completion: complete.PredictSet("redis-cli"),
|
||||
Default: "redis-cli",
|
||||
Usage: `Specifies how the CLI will attempt to invoke a Redis client. This will also set a suitable default for -exec if a value was not specified. Currently-understood values are "redis-cli".`,
|
||||
})
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "username",
|
||||
Target: &c.flagUsername,
|
||||
EnvVar: "BOUNDARY_CONNECT_USERNAME",
|
||||
Completion: complete.PredictNothing,
|
||||
Usage: `Specifies the username to pass through to the client. May be overridden by credentials sourced from a credential store.`,
|
||||
})
|
||||
}
|
||||
|
||||
type redisFlags struct {
|
||||
flagRedisStyle string
|
||||
}
|
||||
|
||||
func (r *redisFlags) defaultExec() string {
|
||||
return strings.ToLower(r.flagRedisStyle)
|
||||
}
|
||||
|
||||
func (r *redisFlags) buildArgs(c *Command, port, ip, _ string, creds proxy.Credentials) (args, envs []string, retCreds proxy.Credentials, retErr error) {
|
||||
var username, password string
|
||||
|
||||
retCreds = creds
|
||||
if len(retCreds.UsernamePassword) > 0 {
|
||||
// Mark credential as consumed, such that it is not printed to the user
|
||||
retCreds.UsernamePassword[0].Consumed = true
|
||||
|
||||
// Grab the first available username/password credential brokered
|
||||
username = retCreds.UsernamePassword[0].Username
|
||||
password = retCreds.UsernamePassword[0].Password
|
||||
}
|
||||
|
||||
switch r.flagRedisStyle {
|
||||
case "redis-cli":
|
||||
args = append(args, "-h", ip)
|
||||
if port != "" {
|
||||
args = append(args, "-p", port)
|
||||
}
|
||||
|
||||
switch {
|
||||
case username != "":
|
||||
args = append(args, "--user", username)
|
||||
case c.flagUsername != "":
|
||||
args = append(args, "--user", c.flagUsername, "--askpass")
|
||||
}
|
||||
|
||||
// Password is read by redis-cli via environment variable. The password disappears after the command exits.
|
||||
if password != "" {
|
||||
envs = append(envs, fmt.Sprintf("REDISCLI_AUTH=%s", password))
|
||||
}
|
||||
}
|
||||
|
||||
return args, envs, retCreds, retErr
|
||||
}
|
||||
Reference in new issue
Block a user