2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-04-23 01:02:57 +00:00
|
|
|
package cassandra
|
|
|
|
|
|
|
|
import (
|
2021-02-16 19:39:24 +00:00
|
|
|
"context"
|
2020-10-12 20:46:17 +00:00
|
|
|
"reflect"
|
2017-04-23 01:02:57 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-16 19:39:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
backoff "github.com/cenkalti/backoff/v3"
|
2017-04-23 01:02:57 +00:00
|
|
|
"github.com/gocql/gocql"
|
2020-09-15 14:01:26 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/cassandra"
|
2020-10-15 19:20:12 +00:00
|
|
|
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
|
|
|
|
dbtesting "github.com/hashicorp/vault/sdk/database/dbplugin/v5/testing"
|
2017-04-23 01:02:57 +00:00
|
|
|
)
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func getCassandra(t *testing.T, protocolVersion interface{}) (*Cassandra, func()) {
|
2021-04-16 21:52:35 +00:00
|
|
|
host, cleanup := cassandra.PrepareTestContainer(t,
|
2021-07-27 12:45:32 +00:00
|
|
|
cassandra.Version("3.11"),
|
2021-04-16 21:52:35 +00:00
|
|
|
cassandra.CopyFromTo(insecureFileMounts),
|
|
|
|
)
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
db := new()
|
2020-10-15 19:20:12 +00:00
|
|
|
initReq := dbplugin.InitializeRequest{
|
2020-10-12 20:46:17 +00:00
|
|
|
Config: map[string]interface{}{
|
2021-04-16 21:52:35 +00:00
|
|
|
"hosts": host.ConnectionURL(),
|
|
|
|
"port": host.Port,
|
2020-10-12 20:46:17 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": protocolVersion,
|
|
|
|
"connect_timeout": "20s",
|
|
|
|
},
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedConfig := map[string]interface{}{
|
2021-04-16 21:52:35 +00:00
|
|
|
"hosts": host.ConnectionURL(),
|
|
|
|
"port": host.Port,
|
2017-04-23 01:02:57 +00:00
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
2020-09-15 14:01:26 +00:00
|
|
|
"protocol_version": protocolVersion,
|
|
|
|
"connect_timeout": "20s",
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
initResp := dbtesting.AssertInitialize(t, db, initReq)
|
|
|
|
if !reflect.DeepEqual(initResp.Config, expectedConfig) {
|
|
|
|
t.Fatalf("Initialize response config actual: %#v\nExpected: %#v", initResp.Config, expectedConfig)
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
return db, cleanup
|
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2021-04-16 21:52:35 +00:00
|
|
|
func TestInitialize(t *testing.T) {
|
2021-06-21 17:38:08 +00:00
|
|
|
t.Run("integer protocol version", func(t *testing.T) {
|
|
|
|
// getCassandra performs an Initialize call
|
|
|
|
db, cleanup := getCassandra(t, 4)
|
|
|
|
t.Cleanup(cleanup)
|
2017-06-15 01:59:27 +00:00
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
})
|
2020-09-15 14:01:26 +00:00
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
t.Run("string protocol version", func(t *testing.T) {
|
|
|
|
// getCassandra performs an Initialize call
|
|
|
|
db, cleanup := getCassandra(t, "4")
|
|
|
|
t.Cleanup(cleanup)
|
|
|
|
|
|
|
|
err := db.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
})
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:52:35 +00:00
|
|
|
func TestCreateUser(t *testing.T) {
|
2021-02-16 19:39:24 +00:00
|
|
|
type testCase struct {
|
|
|
|
// Config will have the hosts & port added to it during the test
|
|
|
|
config map[string]interface{}
|
|
|
|
newUserReq dbplugin.NewUserRequest
|
|
|
|
expectErr bool
|
|
|
|
expectedUsernameRegex string
|
2021-06-21 17:38:08 +00:00
|
|
|
assertCreds func(t testing.TB, address string, port int, username, password string, sslOpts *gocql.SslOptions, timeout time.Duration)
|
2021-02-16 19:39:24 +00:00
|
|
|
}
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2021-02-16 19:39:24 +00:00
|
|
|
tests := map[string]testCase{
|
|
|
|
"default username_template": {
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": "4",
|
|
|
|
"connect_timeout": "20s",
|
|
|
|
},
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: "token",
|
|
|
|
RoleName: "mylongrolenamewithmanycharacters",
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: "bfn985wjAHIh6t",
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectedUsernameRegex: `^v_token_mylongrolenamew_[a-z0-9]{20}_[0-9]{10}$`,
|
|
|
|
assertCreds: assertCreds,
|
2020-10-12 20:46:17 +00:00
|
|
|
},
|
2021-02-16 19:39:24 +00:00
|
|
|
"custom username_template": {
|
|
|
|
config: map[string]interface{}{
|
|
|
|
"username": "cassandra",
|
|
|
|
"password": "cassandra",
|
|
|
|
"protocol_version": "4",
|
|
|
|
"connect_timeout": "20s",
|
|
|
|
"username_template": `foo_{{random 20}}_{{.RoleName | replace "e" "3"}}_{{unix_time}}`,
|
|
|
|
},
|
|
|
|
newUserReq: dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
|
|
|
DisplayName: "token",
|
|
|
|
RoleName: "mylongrolenamewithmanycharacters",
|
|
|
|
},
|
|
|
|
Statements: dbplugin.Statements{
|
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: "bfn985wjAHIh6t",
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
|
|
|
},
|
|
|
|
expectErr: false,
|
|
|
|
expectedUsernameRegex: `^foo_[a-zA-Z0-9]{20}_mylongrol3nam3withmanycharact3rs_[0-9]{10}$`,
|
|
|
|
assertCreds: assertCreds,
|
2020-10-12 20:46:17 +00:00
|
|
|
},
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 19:39:24 +00:00
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2021-04-16 21:52:35 +00:00
|
|
|
host, cleanup := cassandra.PrepareTestContainer(t,
|
2021-07-27 12:45:32 +00:00
|
|
|
cassandra.Version("3.11"),
|
2021-04-16 21:52:35 +00:00
|
|
|
cassandra.CopyFromTo(insecureFileMounts),
|
|
|
|
)
|
2021-02-16 19:39:24 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
db := new()
|
|
|
|
|
|
|
|
config := test.config
|
2021-04-16 21:52:35 +00:00
|
|
|
config["hosts"] = host.ConnectionURL()
|
|
|
|
config["port"] = host.Port
|
2021-02-16 19:39:24 +00:00
|
|
|
|
|
|
|
initReq := dbplugin.InitializeRequest{
|
|
|
|
Config: config,
|
|
|
|
VerifyConnection: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
dbtesting.AssertInitialize(t, db, initReq)
|
|
|
|
|
|
|
|
require.True(t, db.Initialized, "Database is not initialized")
|
|
|
|
|
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
newUserResp, err := db.NewUser(ctx, test.newUserReq)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
require.Regexp(t, test.expectedUsernameRegex, newUserResp.Username)
|
2021-06-21 17:38:08 +00:00
|
|
|
test.assertCreds(t, db.Hosts, db.Port, newUserResp.Username, test.newUserReq.Password, nil, 5*time.Second)
|
2021-02-16 19:39:24 +00:00
|
|
|
})
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 21:52:35 +00:00
|
|
|
func TestUpdateUserPassword(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
db, cleanup := getCassandra(t, 4)
|
2017-04-23 01:02:57 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
password := "myreallysecurepassword"
|
2020-10-15 19:20:12 +00:00
|
|
|
createReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2020-10-12 20:46:17 +00:00
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-12 20:46:17 +00:00
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: password,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
createResp := dbtesting.AssertNewUser(t, db, createReq)
|
2017-06-06 13:49:49 +00:00
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
assertCreds(t, db.Hosts, db.Port, createResp.Username, password, nil, 5*time.Second)
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
newPassword := "somenewpassword"
|
2020-10-15 19:20:12 +00:00
|
|
|
updateReq := dbplugin.UpdateUserRequest{
|
2020-10-12 20:46:17 +00:00
|
|
|
Username: createResp.Username,
|
2020-10-15 19:20:12 +00:00
|
|
|
Password: &dbplugin.ChangePassword{
|
2020-10-12 20:46:17 +00:00
|
|
|
NewPassword: newPassword,
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{},
|
2020-10-12 20:46:17 +00:00
|
|
|
},
|
|
|
|
Expiration: nil,
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
dbtesting.AssertUpdateUser(t, db, updateReq)
|
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
assertCreds(t, db.Hosts, db.Port, createResp.Username, newPassword, nil, 5*time.Second)
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:52:35 +00:00
|
|
|
func TestDeleteUser(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
db, cleanup := getCassandra(t, 4)
|
2017-04-23 01:02:57 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
password := "myreallysecurepassword"
|
2020-10-15 19:20:12 +00:00
|
|
|
createReq := dbplugin.NewUserRequest{
|
|
|
|
UsernameConfig: dbplugin.UsernameMetadata{
|
2020-10-12 20:46:17 +00:00
|
|
|
DisplayName: "test",
|
|
|
|
RoleName: "test",
|
|
|
|
},
|
2020-10-15 19:20:12 +00:00
|
|
|
Statements: dbplugin.Statements{
|
2020-10-12 20:46:17 +00:00
|
|
|
Commands: []string{createUserStatements},
|
|
|
|
},
|
|
|
|
Password: password,
|
|
|
|
Expiration: time.Now().Add(1 * time.Minute),
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
createResp := dbtesting.AssertNewUser(t, db, createReq)
|
2017-06-06 13:49:49 +00:00
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
assertCreds(t, db.Hosts, db.Port, createResp.Username, password, nil, 5*time.Second)
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2020-10-15 19:20:12 +00:00
|
|
|
deleteReq := dbplugin.DeleteUserRequest{
|
2020-10-12 20:46:17 +00:00
|
|
|
Username: createResp.Username,
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
dbtesting.AssertDeleteUser(t, db, deleteReq)
|
2017-04-23 01:02:57 +00:00
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
assertNoCreds(t, db.Hosts, db.Port, createResp.Username, password, nil, 5*time.Second)
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
func assertCreds(t testing.TB, address string, port int, username, password string, sslOpts *gocql.SslOptions, timeout time.Duration) {
|
2020-10-12 20:46:17 +00:00
|
|
|
t.Helper()
|
|
|
|
op := func() error {
|
2021-06-21 17:38:08 +00:00
|
|
|
return connect(t, address, port, username, password, sslOpts)
|
2018-04-08 17:34:59 +00:00
|
|
|
}
|
2020-10-12 20:46:17 +00:00
|
|
|
bo := backoff.NewExponentialBackOff()
|
|
|
|
bo.MaxElapsedTime = timeout
|
|
|
|
bo.InitialInterval = 500 * time.Millisecond
|
|
|
|
bo.MaxInterval = bo.InitialInterval
|
|
|
|
bo.RandomizationFactor = 0.0
|
2018-04-08 17:34:59 +00:00
|
|
|
|
2020-10-12 20:46:17 +00:00
|
|
|
err := backoff.Retry(op, bo)
|
2018-04-08 17:34:59 +00:00
|
|
|
if err != nil {
|
2020-10-12 20:46:17 +00:00
|
|
|
t.Fatalf("failed to connect after %s: %s", timeout, err)
|
2018-04-08 17:34:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
func connect(t testing.TB, address string, port int, username, password string, sslOpts *gocql.SslOptions) error {
|
2020-10-12 20:46:17 +00:00
|
|
|
t.Helper()
|
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: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
clusterConfig.ProtoVersion = 4
|
2017-09-08 03:04:40 +00:00
|
|
|
clusterConfig.Port = port
|
2021-06-21 17:38:08 +00:00
|
|
|
clusterConfig.SslOpts = sslOpts
|
2017-04-23 01:02:57 +00:00
|
|
|
|
|
|
|
session, err := clusterConfig.CreateSession()
|
|
|
|
if err != nil {
|
2020-10-12 20:46:17 +00:00
|
|
|
return err
|
2017-04-23 01:02:57 +00:00
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 17:38:08 +00:00
|
|
|
func assertNoCreds(t testing.TB, address string, port int, username, password string, sslOpts *gocql.SslOptions, timeout time.Duration) {
|
2020-10-12 20:46:17 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
op := func() error {
|
|
|
|
// "Invert" the error so the backoff logic sees a failure to connect as a success
|
2021-06-21 17:38:08 +00:00
|
|
|
err := connect(t, address, port, username, password, sslOpts)
|
2020-10-12 20:46:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-10-19 00:29:47 +00:00
|
|
|
return nil
|
2020-10-12 20:46:17 +00:00
|
|
|
}
|
|
|
|
bo := backoff.NewExponentialBackOff()
|
|
|
|
bo.MaxElapsedTime = timeout
|
|
|
|
bo.InitialInterval = 500 * time.Millisecond
|
|
|
|
bo.MaxInterval = bo.InitialInterval
|
|
|
|
bo.RandomizationFactor = 0.0
|
|
|
|
|
|
|
|
err := backoff.Retry(op, bo)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("successfully connected after %s when it shouldn't", timeout)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createUserStatements = `CREATE USER '{{username}}' WITH PASSWORD '{{password}}' NOSUPERUSER;
|
2021-02-16 19:39:24 +00:00
|
|
|
GRANT ALL PERMISSIONS ON ALL KEYSPACES TO '{{username}}';`
|