Cassandra Connect Support (#5952)
* Init cassandra * Init cassandra * Initial Cassandra Command Support For cqlsh * Add Make Gen * adding cassandra test * Cleanup cassandra docker setup * Adding container wait * Container restart * Minor Fix * Cleanup docker and test * Adding final tests * Removing comment * Fixing cleanup (uncomment) * Removing bad yaml * Cleanup * Cleanup * Adressing PR review comments * Adding test support * fix spacing * Adding changelog * docs * docs * adding cassandra page * nav * Versbose commenting * cleanup naming of file * updating helper func to return errors * doc change * Removed an incorrect comment and now passing the config by reference. * Comment cleanup * Fix option warning in cassandra connect command (#5951) * Add healthcheck to caller function as it's resposible for container health * Resolve Comments * Update Log Typo --------- Co-authored-by: Bharath Gajjala <120367134+bgajjala8@users.noreply.github.com>
This commit is contained in:
1 parent
029aa361bd
commit
4d3cb76c28
10 files changed
+504
-1
No files matched your search
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -26,6 +27,14 @@ type Container struct {
|
||||
UriNetwork string
|
||||
}
|
||||
|
||||
// cassandraConfig stores configuration details for the Cassandra container
|
||||
type cassandraConfig struct {
|
||||
User string
|
||||
Password string
|
||||
Keyspace 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 {
|
||||
@@ -344,3 +353,129 @@ func StartMysql(t testing.TB, pool *dockertest.Pool, network *dockertest.Network
|
||||
UriNetwork: fmt.Sprintf("mysql://%s:%s@%s:3306/%s", mysqlUser, mysqlPassword, networkAlias, mysqlDb),
|
||||
}
|
||||
}
|
||||
|
||||
// StartCassandra starts a Cassandra database in a docker container.
|
||||
// Returns information about the container
|
||||
func StartCassandra(t testing.TB, pool *dockertest.Pool, network *dockertest.Network, repository, tag string) *Container {
|
||||
t.Log("Starting Cassandra 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 := cassandraConfig{
|
||||
User: "e2eboundary",
|
||||
Password: "e2eboundary",
|
||||
Keyspace: "e2eboundarykeyspace",
|
||||
NetworkAlias: "e2ecassandra",
|
||||
}
|
||||
|
||||
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
|
||||
Repository: fmt.Sprintf("%s/%s", c.DockerMirror, repository),
|
||||
Tag: tag,
|
||||
Env: []string{
|
||||
"CASSANDRA_CLUSTER_NAME=e2e-boundary-cluster",
|
||||
},
|
||||
|
||||
ExposedPorts: []string{"9042/tcp"},
|
||||
Name: config.NetworkAlias,
|
||||
Networks: []*dockertest.Network{network},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Cassandra container takes a while to start due to the gossip protocol needing to settle and establish connections.
|
||||
// This relies on the pool's extended maxWait time to ensure the container is healthy.
|
||||
err = pool.Retry(func() error {
|
||||
cmd := exec.Command("docker", "exec", config.NetworkAlias, "cqlsh", "-e", "SELECT now() FROM system.local;")
|
||||
output, cmdErr := cmd.CombinedOutput()
|
||||
if cmdErr != nil {
|
||||
return fmt.Errorf("failed to connect to Cassandra container '%s': %v\nOutput: %s", config.NetworkAlias, cmdErr, string(output))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err, "Cassandra container did not start in time or is not healthy")
|
||||
|
||||
err = setupCassandraAuthAndUser(t, resource, pool, &config)
|
||||
require.NoError(t, err)
|
||||
|
||||
return &Container{
|
||||
Resource: resource,
|
||||
UriLocalhost: fmt.Sprintf(
|
||||
"cassandra://%s:%s@%s/%s",
|
||||
config.User,
|
||||
config.Password,
|
||||
resource.GetHostPort("9042/tcp"),
|
||||
config.Keyspace,
|
||||
),
|
||||
UriNetwork: fmt.Sprintf(
|
||||
"cassandra://%s:%s@%s:9042/%s",
|
||||
config.User,
|
||||
config.Password,
|
||||
config.NetworkAlias,
|
||||
config.Keyspace,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
t.Log("Configuring Cassandra authentication and user permissions...")
|
||||
|
||||
t.Logf("Initializing Cassandra keyspace: %s...", config.Keyspace)
|
||||
createKeyspaceCmd := fmt.Sprintf(
|
||||
"CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};",
|
||||
config.Keyspace,
|
||||
)
|
||||
if err := exec.Command("docker", "exec", config.NetworkAlias, "cqlsh", "-e", createKeyspaceCmd).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Commands to enable authentication and authorization by editing cassandra.yaml
|
||||
sedCmd := []string{
|
||||
"sed", "-i",
|
||||
"-e", "s/^authenticator:.*/authenticator: PasswordAuthenticator/",
|
||||
"-e", "s/^authorizer:.*/authorizer: CassandraAuthorizer/",
|
||||
"-e", "s/^role_manager:.*/role_manager: CassandraRoleManager/",
|
||||
"/etc/cassandra/cassandra.yaml",
|
||||
}
|
||||
if _, err := resource.Exec(sedCmd, dockertest.ExecOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := pool.Client.RestartContainer(resource.Container.ID, uint(pool.MaxWait.Seconds())); err != nil {
|
||||
return err
|
||||
}
|
||||
t.Log("Waiting for Cassandra container to restart and apply authentication settings...")
|
||||
|
||||
// Wait for Cassandra to be up with authentication enabled
|
||||
if err := pool.Retry(func() error {
|
||||
return exec.Command(
|
||||
"docker", "exec", config.NetworkAlias,
|
||||
"cqlsh", "-u", "cassandra", "-p", "cassandra",
|
||||
"-e", "SELECT now() FROM system.local;",
|
||||
).Run()
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.Log("Creating Cassandra user and granting permissions...")
|
||||
cqlCmds := []string{
|
||||
fmt.Sprintf("CREATE ROLE IF NOT EXISTS %s WITH PASSWORD = '%s' AND LOGIN = true;", config.User, config.Password),
|
||||
fmt.Sprintf("GRANT ALL PERMISSIONS ON KEYSPACE %s TO %s;", config.Keyspace, config.User),
|
||||
fmt.Sprintf("USE %s; CREATE TABLE IF NOT EXISTS users (id UUID PRIMARY KEY, name TEXT, created_at TIMESTAMP);", config.Keyspace),
|
||||
}
|
||||
for _, cmd := range cqlCmds {
|
||||
if err := exec.Command(
|
||||
"docker", "exec", config.NetworkAlias,
|
||||
"cqlsh", "-u", "cassandra", "-p", "cassandra", "-e", cmd,
|
||||
).Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user