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:
Justin Nguyen authored and GitHub committed 2025-09-19 16:20:11 -05:00
1 parent f8a5894e29
commit 4548028efe
10 files changed
+386

No files matched your search

+79
View File
@@ -35,6 +35,12 @@ type cassandraConfig struct {
NetworkAlias string
}
type redisConfig struct {
User string
Password string
NetworkAlias string
}
// StartBoundaryDatabase spins up a postgres database in a docker container.
// Returns information about the container
func StartBoundaryDatabase(t testing.TB, pool *dockertest.Pool, network *dockertest.Network, repository, tag string) *Container {
@@ -421,6 +427,63 @@ func StartCassandra(t testing.TB, pool *dockertest.Pool, network *dockertest.Net
}
}
// StartRedis starts a Redis database in a docker container.
// Returns information about the container
func StartRedis(t testing.TB, pool *dockertest.Pool, network *dockertest.Network, repository, tag string) *Container {
t.Log("Starting Redis database...")
c, err := LoadConfig()
require.NoError(t, err)
err = pool.Client.PullImage(docker.PullImageOptions{
Repository: fmt.Sprintf("%s/%s", c.DockerMirror, repository),
Tag: tag,
}, docker.AuthConfiguration{})
require.NoError(t, err)
config := redisConfig{
User: "e2eboundary",
Password: "e2eboundary",
NetworkAlias: "e2eredis",
}
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: fmt.Sprintf("%s/%s", c.DockerMirror, repository),
Tag: tag,
ExposedPorts: []string{"6379/tcp"},
Name: config.NetworkAlias,
Networks: []*dockertest.Network{network},
})
require.NoError(t, err)
err = pool.Retry(func() error {
cmd := exec.Command("docker", "exec", config.NetworkAlias, "redis-cli", "PING")
output, cmdErr := cmd.CombinedOutput()
if cmdErr != nil {
return fmt.Errorf("failed to connect to Redis container '%s': %v\nOutput: %s", config.NetworkAlias, cmdErr, string(output))
}
return nil
})
require.NoError(t, err, "Redis container did not start in time or is not healthy")
err = setupRedisAuthAndUser(t, resource, pool, &config)
require.NoError(t, err)
return &Container{
Resource: resource,
UriLocalhost: fmt.Sprintf(
"redis://%s:%s@localhost:6379",
config.User,
config.Password,
),
UriNetwork: fmt.Sprintf(
"redis://%s:%s@%s:6379",
config.User,
config.Password,
config.NetworkAlias,
),
}
}
// setupCassandraAuthAndUser enables authentication on a Cassandra container and creates a user with permissions.
func setupCassandraAuthAndUser(t testing.TB, resource *dockertest.Resource, pool *dockertest.Pool, config *cassandraConfig) error {
t.Helper()
@@ -479,3 +542,19 @@ func setupCassandraAuthAndUser(t testing.TB, resource *dockertest.Resource, pool
}
return nil
}
// setupRedisAuthAndUser configures a Redis container by creating a user with permissions.
func setupRedisAuthAndUser(t testing.TB, resource *dockertest.Resource, pool *dockertest.Pool, config *redisConfig) error {
t.Helper()
t.Log("Configuring Redis authentication and user permissions...")
err := exec.Command(
"docker", "exec", config.NetworkAlias, "redis-cli",
"ACL", "SETUSER", config.User, "on", fmt.Sprintf(">%s", config.Password), "+@read", "+@write", "allkeys",
).Run()
if err != nil {
return err
}
return nil
}
@@ -0,0 +1,125 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package base_test
import (
"testing"
)
// TestCliTcpTargetConnectRedis uses the boundary cli to connect to a target using `connect redis`
func TestCliTcpTargetConnectRedis(t *testing.T) {
t.Skip("Skipped (TODO: ICU-17634). Test fails due to issues between redis-cli and pty.")
return //nolint
// e2e.MaybeSkipTest(t)
// pool, err := dockertest.NewPool("")
// require.NoError(t, err)
// ctx := context.Background()
// network, err := pool.NetworksByName("e2e_cluster")
// require.NoError(t, err, "Failed to get e2e_cluster network")
// c := infra.StartRedis(t, pool, &network[0], "redis", "latest")
// require.NotNil(t, c, "Redis container should not be nil")
// t.Cleanup(func() {
// if err := pool.Purge(c.Resource); err != nil {
// t.Logf("Failed to purge Redis container: %v", err)
// }
// })
// u, err := url.Parse(c.UriNetwork)
// t.Log(u)
// require.NoError(t, err, "Failed to parse Redis URL")
// user, hostname, port := u.User.Username(), u.Hostname(), u.Port()
// pw, pwSet := u.User.Password()
// t.Logf("Redis info: user=%s, host=%s, port=%s, password-set:%t",
// user, hostname, port, pwSet)
// // Wait for Redis to be ready
// err = pool.Retry(func() error {
// out, e := exec.CommandContext(ctx, "docker", "exec", hostname,
// "redis-cli", "-h", hostname, "-p", port, "PING").CombinedOutput()
// t.Logf("Redis PING output: %s", out)
// return e
// })
// require.NoError(t, err, "Redis container failed to start")
// boundary.AuthenticateAdminCli(t, ctx)
// orgId, err := boundary.CreateOrgCli(t, ctx)
// require.NoError(t, err)
// t.Cleanup(func() {
// ctx := context.Background()
// boundary.AuthenticateAdminCli(t, ctx)
// output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", orgId))
// require.NoError(t, output.Err, string(output.Stderr))
// })
// projectId, err := boundary.CreateProjectCli(t, ctx, orgId)
// require.NoError(t, err)
// targetId, err := boundary.CreateTargetCli(
// t,
// ctx,
// projectId,
// port,
// target.WithAddress(hostname),
// )
// require.NoError(t, err)
// storeId, err := boundary.CreateCredentialStoreStaticCli(t, ctx, projectId)
// require.NoError(t, err)
// credentialId, err := boundary.CreateStaticCredentialPasswordCli(
// t,
// ctx,
// storeId,
// user,
// pw,
// )
// require.NoError(t, err)
// err = boundary.AddBrokeredCredentialSourceToTargetCli(t, ctx, targetId, credentialId)
// require.NoError(t, err)
// t.Logf("Attempting to connect to Redis target %s", targetId)
// cmd := exec.CommandContext(ctx,
// "boundary",
// "connect", "redis",
// "-target-id", targetId,
// )
// f, err := pty.Start(cmd)
// require.NoError(t, err)
// t.Cleanup(func() {
// err := f.Close()
// require.NoError(t, err)
// })
// _, err = f.Write([]byte("SET e2etestkey e2etestvalue\r\n"))
// require.NoError(t, err)
// _, err = f.Write([]byte("GET e2etestkey\r\n"))
// require.NoError(t, err)
// _, err = f.Write([]byte("QUIT\r\n"))
// require.NoError(t, err)
// _, err = f.Write([]byte{4})
// require.NoError(t, err)
//// io.Copy will hang because not all bytes seem to be written to pty (QUIT is not recognized).
// var buf bytes.Buffer
// _, _ = io.Copy(&buf, f)
// output := buf.String()
// t.Logf("Redis session output: %s", output)
// require.Contains(t, output, "OK")
// require.Contains(t, output, "\"e2etestvalue\"")
// t.Log("Successfully connected to Redis target")
}