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"
|
|
|
|
"testing"
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/cassandra"
|
2019-04-12 21:54:35 +00:00
|
|
|
logicaltest "github.com/hashicorp/vault/helper/testhelpers/logical"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2015-06-19 17:10:19 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBackend_basic(t *testing.T) {
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-16 21:52:35 +00:00
|
|
|
copyFromTo := map[string]string{
|
|
|
|
"test-fixtures/cassandra.yaml": "/etc/cassandra/cassandra.yaml",
|
|
|
|
}
|
|
|
|
host, cleanup := cassandra.PrepareTestContainer(t,
|
|
|
|
cassandra.CopyFromTo(copyFromTo),
|
|
|
|
)
|
2017-11-06 22:15:45 +00:00
|
|
|
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{
|
2021-04-16 21:52:35 +00:00
|
|
|
testAccStepConfig(t, host.ConnectionURL()),
|
2015-06-19 17:10:19 +00:00
|
|
|
testAccStepRole(t),
|
|
|
|
testAccStepReadCreds(t, "test"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_roleCrud(t *testing.T) {
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-16 21:52:35 +00:00
|
|
|
copyFromTo := map[string]string{
|
|
|
|
"test-fixtures/cassandra.yaml": "/etc/cassandra/cassandra.yaml",
|
|
|
|
}
|
|
|
|
host, cleanup := cassandra.PrepareTestContainer(t,
|
|
|
|
cassandra.CopyFromTo(copyFromTo))
|
2017-11-06 22:15:45 +00:00
|
|
|
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{
|
2021-04-16 21:52:35 +00:00
|
|
|
testAccStepConfig(t, host.ConnectionURL()),
|
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
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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}};`
|