2017-08-03 17:24:27 +00:00
|
|
|
package cockroachdb
|
2017-07-23 12:54:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/ory/dockertest"
|
2017-07-23 12:54:33 +00:00
|
|
|
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
func prepareCockroachDBTestContainer(t *testing.T) (cleanup func(), retURL, tableName string) {
|
|
|
|
tableName = os.Getenv("CR_TABLE")
|
|
|
|
if tableName == "" {
|
2020-06-12 17:09:38 +00:00
|
|
|
tableName = defaultTableName
|
2017-07-23 12:54:33 +00:00
|
|
|
}
|
2020-06-12 17:09:38 +00:00
|
|
|
t.Logf("Table name: %s", tableName)
|
2017-07-23 12:54:33 +00:00
|
|
|
retURL = os.Getenv("CR_URL")
|
|
|
|
if retURL != "" {
|
|
|
|
return func() {}, retURL, tableName
|
|
|
|
}
|
|
|
|
|
|
|
|
pool, err := dockertest.NewPool("")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dockerOptions := &dockertest.RunOptions{
|
|
|
|
Repository: "cockroachdb/cockroach",
|
|
|
|
Tag: "release-1.0",
|
|
|
|
Cmd: []string{"start", "--insecure"},
|
|
|
|
}
|
|
|
|
resource, err := pool.RunWithOptions(dockerOptions)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local CockroachDB docker container: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
2019-04-22 16:26:10 +00:00
|
|
|
docker.CleanupResource(t, pool, resource)
|
2017-07-23 12:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retURL = fmt.Sprintf("postgresql://root@localhost:%s/?sslmode=disable", resource.GetPort("26257/tcp"))
|
2020-06-12 17:09:38 +00:00
|
|
|
database := "vault"
|
|
|
|
tableName = fmt.Sprintf("%s.%s", database, tableName)
|
2017-07-23 12:54:33 +00:00
|
|
|
|
|
|
|
// exponential backoff-retry
|
|
|
|
if err = pool.Retry(func() error {
|
|
|
|
var err error
|
|
|
|
db, err := sql.Open("postgres", retURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-26 15:28:58 +00:00
|
|
|
defer db.Close()
|
2020-06-12 17:09:38 +00:00
|
|
|
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", database))
|
2017-07-23 12:54:33 +00:00
|
|
|
return err
|
|
|
|
}); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("Could not connect to docker: %s", err)
|
|
|
|
}
|
|
|
|
return cleanup, retURL, tableName
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCockroachDBBackend(t *testing.T) {
|
|
|
|
cleanup, connURL, table := prepareCockroachDBTestContainer(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Run vault tests
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2017-07-23 12:54:33 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewCockroachDBBackend(map[string]string{
|
2017-07-23 12:54:33 +00:00
|
|
|
"connection_url": connURL,
|
|
|
|
"table": table,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2017-07-23 12:54:33 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
truncate(t, b)
|
|
|
|
}()
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
2017-07-23 12:54:33 +00:00
|
|
|
truncate(t, b)
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2017-07-23 12:54:33 +00:00
|
|
|
truncate(t, b)
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseTransactionalBackend(t, b)
|
2017-07-23 12:54:33 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
func truncate(t *testing.T, b physical.Backend) {
|
2017-07-23 12:54:33 +00:00
|
|
|
crdb := b.(*CockroachDBBackend)
|
|
|
|
_, err := crdb.client.Exec("TRUNCATE TABLE " + crdb.table)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to drop table: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 17:09:38 +00:00
|
|
|
|
|
|
|
func TestValidateDBTable(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
table string
|
|
|
|
expectErr bool
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := map[string]testCase{
|
|
|
|
"first character is letter": {"abcdef", false},
|
|
|
|
"first character is underscore": {"_bcdef", false},
|
|
|
|
"exclamation point": {"ab!def", true},
|
|
|
|
"at symbol": {"ab@def", true},
|
|
|
|
"hash": {"ab#def", true},
|
|
|
|
"percent": {"ab%def", true},
|
|
|
|
"carrot": {"ab^def", true},
|
|
|
|
"ampersand": {"ab&def", true},
|
|
|
|
"star": {"ab*def", true},
|
|
|
|
"left paren": {"ab(def", true},
|
|
|
|
"right paren": {"ab)def", true},
|
|
|
|
"dash": {"ab-def", true},
|
|
|
|
"digit": {"a123ef", false},
|
|
|
|
"dollar end": {"abcde$", false},
|
|
|
|
"dollar middle": {"ab$def", false},
|
|
|
|
"dollar start": {"$bcdef", true},
|
|
|
|
"backtick prefix": {"`bcdef", true},
|
|
|
|
"backtick middle": {"ab`def", true},
|
|
|
|
"backtick suffix": {"abcde`", true},
|
|
|
|
"single quote prefix": {"'bcdef", true},
|
|
|
|
"single quote middle": {"ab'def", true},
|
|
|
|
"single quote suffix": {"abcde'", true},
|
|
|
|
"double quote prefix": {`"bcdef`, true},
|
|
|
|
"double quote middle": {`ab"def`, true},
|
|
|
|
"double quote suffix": {`abcde"`, true},
|
|
|
|
"underscore with all runes": {"_bcd123__a__$", false},
|
|
|
|
"all runes": {"abcd123__a__$", false},
|
|
|
|
"default table name": {defaultTableName, false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
err := validateDBTable(test.table)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run(fmt.Sprintf("database: %s", name), func(t *testing.T) {
|
|
|
|
dbTable := fmt.Sprintf("%s.%s", test.table, test.table)
|
|
|
|
err := validateDBTable(dbTable)
|
|
|
|
if test.expectErr && err == nil {
|
|
|
|
t.Fatalf("err expected, got nil")
|
|
|
|
}
|
|
|
|
if !test.expectErr && err != nil {
|
|
|
|
t.Fatalf("no error expected, got: %s", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|