2015-06-19 17:10:19 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2018-01-19 06:44:44 +00:00
|
|
|
"context"
|
2015-06-19 17:10:19 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2017-11-06 22:15:45 +00:00
|
|
|
"strconv"
|
2017-01-25 20:37:55 +00:00
|
|
|
"sync"
|
2015-06-19 17:10:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
"github.com/gocql/gocql"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
|
2015-06-19 17:10:19 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2018-07-11 21:49:13 +00:00
|
|
|
"github.com/ory/dockertest"
|
2015-06-19 17:10:19 +00:00
|
|
|
)
|
|
|
|
|
2017-01-25 20:37:55 +00:00
|
|
|
var (
|
|
|
|
testImagePull sync.Once
|
|
|
|
)
|
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
func prepareCassandraTestContainer(t *testing.T) (func(), string, int) {
|
2017-01-25 20:37:55 +00:00
|
|
|
if os.Getenv("CASSANDRA_HOST") != "" {
|
2017-11-06 22:15:45 +00:00
|
|
|
return func() {}, os.Getenv("CASSANDRA_HOST"), 0
|
2017-01-25 20:37:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
2017-01-25 20:37:55 +00:00
|
|
|
|
|
|
|
cwd, _ := os.Getwd()
|
2017-11-06 22:15:45 +00:00
|
|
|
cassandraMountPath := fmt.Sprintf("%s/test-fixtures/:/etc/cassandra/", cwd)
|
2017-01-25 20:37:55 +00:00
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
ro := &dockertest.RunOptions{
|
|
|
|
Repository: "cassandra",
|
|
|
|
Tag: "latest",
|
|
|
|
Env: []string{"CASSANDRA_BROADCAST_ADDRESS=127.0.0.1"},
|
|
|
|
Mounts: []string{cassandraMountPath},
|
|
|
|
}
|
|
|
|
resource, err := pool.RunWithOptions(ro)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local cassandra docker container: %s", err)
|
|
|
|
}
|
2017-01-25 20:37:55 +00:00
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
cleanup := func() {
|
|
|
|
err := pool.Purge(resource)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to cleanup local container: %s", err)
|
2017-01-25 20:37:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
port, _ := strconv.Atoi(resource.GetPort("9042/tcp"))
|
|
|
|
address := fmt.Sprintf("127.0.0.1:%d", port)
|
2017-01-25 20:37:55 +00:00
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
|
|
|
clusterConfig := gocql.NewCluster(address)
|
|
|
|
clusterConfig.Authenticator = gocql.PasswordAuthenticator{
|
|
|
|
Username: "cassandra",
|
|
|
|
Password: "cassandra",
|
|
|
|
}
|
|
|
|
clusterConfig.ProtoVersion = 4
|
|
|
|
clusterConfig.Port = port
|
|
|
|
|
|
|
|
session, err := clusterConfig.CreateSession()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating session: %s", err)
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("Could not connect to cassandra docker container: %s", err)
|
2017-01-25 20:37:55 +00:00
|
|
|
}
|
2017-11-06 22:15:45 +00:00
|
|
|
return cleanup, address, port
|
2017-01-25 20:37:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 17:10:19 +00:00
|
|
|
func TestBackend_basic(t *testing.T) {
|
2018-10-19 15:09:28 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2017-05-09 12:36:20 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-01-25 20:37:55 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-01-25 20:37:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
cleanup, hostname, _ := prepareCassandraTestContainer(t)
|
|
|
|
defer cleanup()
|
2015-06-19 17:10:19 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2015-06-19 17:10:19 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2017-01-25 20:37:55 +00:00
|
|
|
testAccStepConfig(t, hostname),
|
2015-06-19 17:10:19 +00:00
|
|
|
testAccStepRole(t),
|
|
|
|
testAccStepReadCreds(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
2018-10-19 15:09:28 +00:00
|
|
|
if os.Getenv("VAULT_ACC") == "" {
|
2017-05-09 12:36:20 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2017-01-25 20:37:55 +00:00
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-01-25 20:37:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-11-06 22:15:45 +00:00
|
|
|
cleanup, hostname, _ := prepareCassandraTestContainer(t)
|
|
|
|
defer cleanup()
|
2015-06-19 17:10:19 +00:00
|
|
|
|
|
|
|
logicaltest.Test(t, logicaltest.TestCase{
|
2018-11-07 01:21:24 +00:00
|
|
|
LogicalBackend: b,
|
2015-06-19 17:10:19 +00:00
|
|
|
Steps: []logicaltest.TestStep{
|
2017-01-25 20:37:55 +00:00
|
|
|
testAccStepConfig(t, hostname),
|
2016-10-10 16:16:55 +00:00
|
|
|
testAccStepRole(t),
|
2016-10-09 00:22:26 +00:00
|
|
|
testAccStepRoleWithOptions(t),
|
2015-06-19 17:10:19 +00:00
|
|
|
testAccStepReadRole(t, "test", testRole),
|
2016-10-10 16:16:55 +00:00
|
|
|
testAccStepReadRole(t, "test2", testRole),
|
2015-06-19 17:10:19 +00:00
|
|
|
testAccStepDeleteRole(t, "test"),
|
2016-10-10 16:16:55 +00:00
|
|
|
testAccStepDeleteRole(t, "test2"),
|
2015-06-19 17:10:19 +00:00
|
|
|
testAccStepReadRole(t, "test", ""),
|
2016-10-10 16:16:55 +00:00
|
|
|
testAccStepReadRole(t, "test2", ""),
|
2015-06-19 17:10:19 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccPreCheck(t *testing.T) {
|
|
|
|
if v := os.Getenv("CASSANDRA_HOST"); v == "" {
|
|
|
|
t.Fatal("CASSANDRA_HOST must be set for acceptance tests")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 20:37:55 +00:00
|
|
|
func testAccStepConfig(t *testing.T, hostname string) logicaltest.TestStep {
|
2015-06-19 17:10:19 +00:00
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-06-19 17:10:19 +00:00
|
|
|
Path: "config/connection",
|
|
|
|
Data: map[string]interface{}{
|
2017-01-25 20:37:55 +00:00
|
|
|
"hosts": hostname,
|
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": 3,
|
2015-06-19 17:10:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepRole(t *testing.T) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
2016-01-07 15:30:47 +00:00
|
|
|
Operation: logical.UpdateOperation,
|
2015-06-19 17:10:19 +00:00
|
|
|
Path: "roles/test",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"creation_cql": testRole,
|
2016-10-09 00:22:26 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepRoleWithOptions(t *testing.T) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "roles/test2",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"creation_cql": testRole,
|
|
|
|
"lease": "30s",
|
2016-09-27 17:25:18 +00:00
|
|
|
"consistency": "All",
|
2015-06-19 17:10:19 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepDeleteRole(t *testing.T, n string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.DeleteOperation,
|
|
|
|
Path: "roles/" + n,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadCreds(t *testing.T, name string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "creds/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
var d struct {
|
|
|
|
Username string `mapstructure:"username"`
|
|
|
|
Password string `mapstructure:"password"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Printf("[WARN] Generated credentials: %v", d)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccStepReadRole(t *testing.T, name string, cql string) logicaltest.TestStep {
|
|
|
|
return logicaltest.TestStep{
|
|
|
|
Operation: logical.ReadOperation,
|
|
|
|
Path: "roles/" + name,
|
|
|
|
Check: func(resp *logical.Response) error {
|
|
|
|
if resp == nil {
|
|
|
|
if cql == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("response is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
CreationCQL string `mapstructure:"creation_cql"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.CreationCQL != cql {
|
|
|
|
return fmt.Errorf("bad: %#v\n%#v\n%#v\n", resp, cql, d.CreationCQL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testRole = `CREATE USER '{{username}}' WITH PASSWORD '{{password}}' NOSUPERUSER;
|
2015-08-20 08:07:25 +00:00
|
|
|
GRANT ALL PERMISSIONS ON ALL KEYSPACES TO {{username}};`
|