2016-09-15 01:11:57 +00:00
|
|
|
package sql
|
2016-09-18 01:01:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2019-12-06 15:49:30 +00:00
|
|
|
"strconv"
|
2016-09-18 01:01:15 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-07-25 20:45:17 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-03 06:44:44 +00:00
|
|
|
|
2019-07-12 14:29:46 +00:00
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/dexidp/dex/storage/conformance"
|
2016-09-18 01:01:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func withTimeout(t time.Duration, f func()) {
|
|
|
|
c := make(chan struct{})
|
|
|
|
defer close(c)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
case <-time.After(t):
|
|
|
|
// Dump a stack trace of the program. Useful for debugging deadlocks.
|
|
|
|
buf := make([]byte, 2<<20)
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", buf[:runtime.Stack(buf, true)])
|
|
|
|
panic("test took too long")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanDB(c *conn) error {
|
2020-11-03 19:50:09 +00:00
|
|
|
tables := []string{
|
|
|
|
"client", "auth_request", "auth_code",
|
|
|
|
"refresh_token", "keys", "password",
|
|
|
|
}
|
2017-04-21 15:51:55 +00:00
|
|
|
|
|
|
|
for _, tbl := range tables {
|
|
|
|
_, err := c.Exec("delete from " + tbl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2016-09-18 01:01:15 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 23:35:46 +00:00
|
|
|
var logger = &logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: &logrus.TextFormatter{DisableColors: true},
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:51:55 +00:00
|
|
|
type opener interface {
|
2019-07-12 14:29:46 +00:00
|
|
|
open(logger log.Logger) (*conn, error)
|
2017-04-21 15:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testDB(t *testing.T, o opener, withTransactions bool) {
|
|
|
|
// t.Fatal has a bad habbit of not actually printing the error
|
|
|
|
fatal := func(i interface{}) {
|
|
|
|
fmt.Fprintln(os.Stdout, i)
|
|
|
|
t.Fatal(i)
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:01:15 +00:00
|
|
|
newStorage := func() storage.Storage {
|
2017-04-21 15:51:55 +00:00
|
|
|
conn, err := o.open(logger)
|
2016-09-18 01:01:15 +00:00
|
|
|
if err != nil {
|
2017-04-21 15:51:55 +00:00
|
|
|
fatal(err)
|
|
|
|
}
|
|
|
|
if err := cleanDB(conn); err != nil {
|
|
|
|
fatal(err)
|
2016-09-18 01:01:15 +00:00
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
2017-04-21 15:51:55 +00:00
|
|
|
withTimeout(time.Minute*1, func() {
|
2016-10-13 01:48:09 +00:00
|
|
|
conformance.RunTests(t, newStorage)
|
2016-09-18 01:01:15 +00:00
|
|
|
})
|
2017-04-21 15:51:55 +00:00
|
|
|
if withTransactions {
|
|
|
|
withTimeout(time.Minute*1, func() {
|
|
|
|
conformance.RunTransactionTests(t, newStorage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 06:00:55 +00:00
|
|
|
func getenv(key, defaultVal string) string {
|
|
|
|
if val := os.Getenv(key); val != "" {
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
return defaultVal
|
|
|
|
}
|
|
|
|
|
|
|
|
const testPostgresEnv = "DEX_POSTGRES_HOST"
|
|
|
|
|
2018-08-23 19:36:08 +00:00
|
|
|
func TestCreateDataSourceName(t *testing.T) {
|
2020-11-03 19:50:09 +00:00
|
|
|
testCases := []struct {
|
2018-08-23 19:36:08 +00:00
|
|
|
description string
|
|
|
|
input *Postgres
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
description: "with no configuration",
|
|
|
|
input: &Postgres{},
|
|
|
|
expected: "connect_timeout=0 sslmode='verify-full'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with typical configuration",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "1.2.3.4",
|
|
|
|
Port: 6543,
|
|
|
|
User: "some-user",
|
|
|
|
Password: "some-password",
|
|
|
|
Database: "some-db",
|
|
|
|
},
|
2018-08-23 19:36:08 +00:00
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='1.2.3.4' port=6543 user='some-user' password='some-password' dbname='some-db' sslmode='verify-full'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with unix socket host",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "/var/run/postgres",
|
|
|
|
},
|
|
|
|
SSL: SSL{
|
2018-08-23 19:36:08 +00:00
|
|
|
Mode: "disable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='/var/run/postgres' sslmode='disable'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with tcp host",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "coreos.com",
|
|
|
|
},
|
|
|
|
SSL: SSL{
|
2018-08-23 19:36:08 +00:00
|
|
|
Mode: "disable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='coreos.com' sslmode='disable'",
|
|
|
|
},
|
2018-11-20 15:22:39 +00:00
|
|
|
{
|
|
|
|
description: "with tcp host:port",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "coreos.com:6543",
|
|
|
|
},
|
2018-11-20 15:22:39 +00:00
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='coreos.com' port=6543 sslmode='verify-full'",
|
|
|
|
},
|
2018-08-23 19:36:08 +00:00
|
|
|
{
|
|
|
|
description: "with tcp host and port",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "coreos.com",
|
|
|
|
Port: 6543,
|
|
|
|
},
|
2018-08-23 19:36:08 +00:00
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='coreos.com' port=6543 sslmode='verify-full'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with ssl ca cert",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "coreos.com",
|
|
|
|
},
|
|
|
|
SSL: SSL{
|
2018-08-23 19:36:08 +00:00
|
|
|
Mode: "verify-ca",
|
|
|
|
CAFile: "/some/file/path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='coreos.com' sslmode='verify-ca' sslrootcert='/some/file/path'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with ssl client cert",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "coreos.com",
|
|
|
|
},
|
|
|
|
SSL: SSL{
|
2018-08-23 19:36:08 +00:00
|
|
|
Mode: "verify-ca",
|
|
|
|
CAFile: "/some/ca/path",
|
|
|
|
CertFile: "/some/cert/path",
|
|
|
|
KeyFile: "/some/key/path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: "connect_timeout=0 host='coreos.com' sslmode='verify-ca' sslrootcert='/some/ca/path' sslcert='/some/cert/path' sslkey='/some/key/path'",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "with funny characters in credentials",
|
|
|
|
input: &Postgres{
|
2019-07-12 14:29:46 +00:00
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Host: "coreos.com",
|
|
|
|
User: `some'user\slashed`,
|
|
|
|
Password: "some'password!",
|
|
|
|
},
|
2018-08-23 19:36:08 +00:00
|
|
|
},
|
|
|
|
expected: `connect_timeout=0 host='coreos.com' user='some\'user\\slashed' password='some\'password!' sslmode='verify-full'`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var actual string
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
t.Run(testCase.description, func(t *testing.T) {
|
|
|
|
actual = testCase.input.createDataSourceName()
|
|
|
|
|
|
|
|
if actual != testCase.expected {
|
|
|
|
t.Fatalf("%s != %s", actual, testCase.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 01:01:15 +00:00
|
|
|
func TestPostgres(t *testing.T) {
|
2016-11-01 06:00:55 +00:00
|
|
|
host := os.Getenv(testPostgresEnv)
|
|
|
|
if host == "" {
|
|
|
|
t.Skipf("test environment variable %q not set, skipping", testPostgresEnv)
|
2016-09-18 01:01:15 +00:00
|
|
|
}
|
2019-12-06 15:49:30 +00:00
|
|
|
|
|
|
|
port := uint64(5432)
|
|
|
|
if rawPort := os.Getenv("DEX_POSTGRES_PORT"); rawPort != "" {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
port, err = strconv.ParseUint(rawPort, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid postgres port %q: %s", rawPort, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:51:55 +00:00
|
|
|
p := &Postgres{
|
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Database: getenv("DEX_POSTGRES_DATABASE", "postgres"),
|
|
|
|
User: getenv("DEX_POSTGRES_USER", "postgres"),
|
|
|
|
Password: getenv("DEX_POSTGRES_PASSWORD", "postgres"),
|
|
|
|
Host: host,
|
2019-12-06 15:49:30 +00:00
|
|
|
Port: uint16(port),
|
2017-04-21 15:51:55 +00:00
|
|
|
ConnectionTimeout: 5,
|
|
|
|
},
|
|
|
|
SSL: SSL{
|
|
|
|
Mode: pgSSLDisable, // Postgres container doesn't support SSL.
|
2016-09-18 01:01:15 +00:00
|
|
|
},
|
|
|
|
}
|
2017-04-21 15:51:55 +00:00
|
|
|
testDB(t, p, true)
|
|
|
|
}
|
2016-10-13 04:56:47 +00:00
|
|
|
|
2017-04-21 15:51:55 +00:00
|
|
|
const testMySQLEnv = "DEX_MYSQL_HOST"
|
2016-09-18 01:01:15 +00:00
|
|
|
|
2017-04-21 15:51:55 +00:00
|
|
|
func TestMySQL(t *testing.T) {
|
|
|
|
host := os.Getenv(testMySQLEnv)
|
|
|
|
if host == "" {
|
|
|
|
t.Skipf("test environment variable %q not set, skipping", testMySQLEnv)
|
2016-09-18 01:01:15 +00:00
|
|
|
}
|
2019-12-06 15:49:30 +00:00
|
|
|
|
|
|
|
port := uint64(3306)
|
|
|
|
if rawPort := os.Getenv("DEX_MYSQL_PORT"); rawPort != "" {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
port, err = strconv.ParseUint(rawPort, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("invalid mysql port %q: %s", rawPort, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 15:51:55 +00:00
|
|
|
s := &MySQL{
|
|
|
|
NetworkDB: NetworkDB{
|
|
|
|
Database: getenv("DEX_MYSQL_DATABASE", "mysql"),
|
|
|
|
User: getenv("DEX_MYSQL_USER", "mysql"),
|
2019-08-03 13:02:54 +00:00
|
|
|
Password: getenv("DEX_MYSQL_PASSWORD", "mysql"),
|
2017-04-21 15:51:55 +00:00
|
|
|
Host: host,
|
2019-12-06 15:49:30 +00:00
|
|
|
Port: uint16(port),
|
2017-04-21 15:51:55 +00:00
|
|
|
ConnectionTimeout: 5,
|
|
|
|
},
|
|
|
|
SSL: SSL{
|
|
|
|
Mode: mysqlSSLFalse,
|
|
|
|
},
|
|
|
|
params: map[string]string{
|
|
|
|
"innodb_lock_wait_timeout": "3",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testDB(t, s, true)
|
2016-09-18 01:01:15 +00:00
|
|
|
}
|