2017-04-23 01:02:57 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2017-12-14 22:03:11 +00:00
|
|
|
"context"
|
2019-04-22 16:26:10 +00:00
|
|
|
"fmt"
|
2017-04-23 01:02:57 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gocql/gocql"
|
2018-03-21 19:05:56 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2019-04-15 18:10:07 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin"
|
2018-07-11 21:49:13 +00:00
|
|
|
"github.com/ory/dockertest"
|
2017-04-23 01:02:57 +00:00
|
|
|
)
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
func prepareCassandraTestContainer(t *testing.T) (func(), string, int) {
|
2017-04-23 01:02:57 +00:00
|
|
|
if os.Getenv("CASSANDRA_HOST") != "" {
|
2017-09-08 03:04:40 +00:00
|
|
|
return func() {}, os.Getenv("CASSANDRA_HOST"), 0
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
cassandraMountPath := fmt.Sprintf("%s/test-fixtures/:/etc/cassandra/", cwd)
|
|
|
|
|
|
|
|
ro := &dockertest.RunOptions{
|
|
|
|
Repository: "cassandra",
|
|
|
|
Tag: "latest",
|
2017-09-08 03:04:40 +00:00
|
|
|
Env: []string{"CASSANDRA_BROADCAST_ADDRESS=127.0.0.1"},
|
2017-04-23 01:02:57 +00:00
|
|
|
Mounts: []string{cassandraMountPath},
|
|
|
|
}
|
|
|
|
resource, err := pool.RunWithOptions(ro)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local cassandra docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup := func() {
|
2019-04-22 16:26:10 +00:00
|
|
|
docker.CleanupResource(t, pool, resource)
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
port, _ := strconv.Atoi(resource.GetPort("9042/tcp"))
|
2017-09-08 03:04:40 +00:00
|
|
|
address := fmt.Sprintf("127.0.0.1:%d", port)
|
2017-04-23 01:02:57 +00:00
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
2017-09-08 03:04:40 +00:00
|
|
|
clusterConfig := gocql.NewCluster(address)
|
2017-04-23 01:02:57 +00:00
|
|
|
clusterConfig.Authenticator = gocql.PasswordAuthenticator{
|
|
|
|
Username: "cassandra",
|
|
|
|
Password: "cassandra",
|
|
|
|
}
|
|
|
|
clusterConfig.ProtoVersion = 4
|
|
|
|
clusterConfig.Port = port
|
|
|
|
|
|
|
|
session, err := clusterConfig.CreateSession()
|
|
|
|
if err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return errwrap.Wrapf("error creating session: {{err}}", err)
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup()
|
2017-04-23 01:02:57 +00:00
|
|
|
t.Fatalf("Could not connect to cassandra docker container: %s", err)
|
|
|
|
}
|
2017-09-08 03:04:40 +00:00
|
|
|
return cleanup, address, port
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCassandra_Initialize(t *testing.T) {
|
2018-10-19 15:35:21 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2017-05-09 12:36:20 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup, address, port := prepareCassandraTestContainer(t)
|
2017-04-23 01:02:57 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
2017-09-08 03:04:40 +00:00
|
|
|
"hosts": address,
|
|
|
|
"port": port,
|
2017-04-23 01:02:57 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": 4,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
if !db.Initialized {
|
2019-03-19 13:32:45 +00:00
|
|
|
t.Fatal("Database should be initialized")
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-06-15 01:59:27 +00:00
|
|
|
|
|
|
|
// test a string protocol
|
|
|
|
connectionDetails = map[string]interface{}{
|
2017-09-08 03:04:40 +00:00
|
|
|
"hosts": address,
|
|
|
|
"port": strconv.Itoa(port),
|
2017-06-15 01:59:27 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": "4",
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
_, err = db.Init(context.Background(), connectionDetails, true)
|
2017-06-15 01:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCassandra_CreateUser(t *testing.T) {
|
2018-10-19 15:35:21 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2017-05-09 12:36:20 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup, address, port := prepareCassandraTestContainer(t)
|
2017-04-23 01:02:57 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
2017-09-08 03:04:40 +00:00
|
|
|
"hosts": address,
|
|
|
|
"port": port,
|
2017-04-23 01:02:57 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": 4,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testCassandraRole},
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
if err := testCredsExist(t, address, port, username, password); err != nil {
|
2017-04-23 01:02:57 +00:00
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMyCassandra_RenewUser(t *testing.T) {
|
2018-10-19 15:35:21 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2017-05-09 12:36:20 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup, address, port := prepareCassandraTestContainer(t)
|
2017-04-23 01:02:57 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
2017-09-08 03:04:40 +00:00
|
|
|
"hosts": address,
|
|
|
|
"port": port,
|
2017-04-23 01:02:57 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": 4,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testCassandraRole},
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
if err := testCredsExist(t, address, port, username, password); err != nil {
|
2017-04-23 01:02:57 +00:00
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RenewUser(context.Background(), statements, username, time.Now().Add(time.Minute))
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCassandra_RevokeUser(t *testing.T) {
|
2018-10-19 15:35:21 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2017-05-09 12:36:20 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-09-08 03:04:40 +00:00
|
|
|
cleanup, address, port := prepareCassandraTestContainer(t)
|
2017-04-23 01:02:57 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
2017-09-08 03:04:40 +00:00
|
|
|
"hosts": address,
|
|
|
|
"port": port,
|
2017-04-23 01:02:57 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": 4,
|
|
|
|
}
|
|
|
|
|
2018-03-21 19:05:56 +00:00
|
|
|
db := new()
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
statements := dbplugin.Statements{
|
2018-03-21 19:05:56 +00:00
|
|
|
Creation: []string{testCassandraRole},
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 13:49:49 +00:00
|
|
|
usernameConfig := dbplugin.UsernameConfig{
|
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:03:11 +00:00
|
|
|
username, password, err := db.CreateUser(context.Background(), statements, usernameConfig, time.Now().Add(time.Minute))
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
if err = testCredsExist(t, address, port, username, password); err != nil {
|
2017-04-23 01:02:57 +00:00
|
|
|
t.Fatalf("Could not connect with new credentials: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:54:10 +00:00
|
|
|
// Test default revoke statements
|
2017-12-14 22:03:11 +00:00
|
|
|
err = db.RevokeUser(context.Background(), statements, username)
|
2017-04-23 01:02:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
if err = testCredsExist(t, address, port, username, password); err == nil {
|
2017-04-23 01:02:57 +00:00
|
|
|
t.Fatal("Credentials were not revoked")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 17:34:59 +00:00
|
|
|
func TestCassandra_RotateRootCredentials(t *testing.T) {
|
2018-10-19 15:35:21 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2018-09-27 15:43:33 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2018-04-08 17:34:59 +00:00
|
|
|
cleanup, address, port := prepareCassandraTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connectionDetails := map[string]interface{}{
|
|
|
|
"hosts": address,
|
|
|
|
"port": port,
|
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
|
|
|
|
connProducer := db.cassandraConnectionProducer
|
|
|
|
|
|
|
|
_, err := db.Init(context.Background(), connectionDetails, true)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !connProducer.Initialized {
|
|
|
|
t.Fatal("Database should be initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
newConf, err := db.RotateRootCredentials(context.Background(), nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if newConf["password"] == "cassandra" {
|
|
|
|
t.Fatal("password was not updated")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 03:04:40 +00:00
|
|
|
func testCredsExist(t testing.TB, address string, port int, username, password string) error {
|
|
|
|
clusterConfig := gocql.NewCluster(address)
|
2017-04-23 01:02:57 +00:00
|
|
|
clusterConfig.Authenticator = gocql.PasswordAuthenticator{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
clusterConfig.ProtoVersion = 4
|
2017-09-08 03:04:40 +00:00
|
|
|
clusterConfig.Port = port
|
2017-04-23 01:02:57 +00:00
|
|
|
|
|
|
|
session, err := clusterConfig.CreateSession()
|
|
|
|
if err != nil {
|
2018-03-21 19:05:56 +00:00
|
|
|
return errwrap.Wrapf("error creating session: {{err}}", err)
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const testCassandraRole = `CREATE USER '{{username}}' WITH PASSWORD '{{password}}' NOSUPERUSER;
|
|
|
|
GRANT ALL PERMISSIONS ON ALL KEYSPACES TO {{username}};`
|