Skip to content

Commit

Permalink
all: add cockroachdb support
Browse files Browse the repository at this point in the history
add cockroachdb support

Signed-off-by: David López <not4rent@gmail.com>
  • Loading branch information
lopezator committed May 6, 2019
1 parent 84573d9 commit c19e195
Show file tree
Hide file tree
Showing 45 changed files with 1,174 additions and 409 deletions.
79 changes: 69 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,20 @@ jobs:
docker:
- image: circleci/golang:1.12
environment:
- GO111MODULE=on
- TEST_DATABASE_POSTGRESQL=postgres://test:test@localhost:5432/hydra?sslmode=disable
- TEST_DATABASE_MYSQL=root:test@(localhost:3306)/mysql?parseTime=true
- GO111MODULE=on
- TEST_DATABASE_POSTGRESQL=postgres://test:test@localhost:5432/hydra?sslmode=disable
- TEST_DATABASE_MYSQL=mysql://root:test@(localhost:3306)/mysql?parseTime=true
- TEST_DATABASE_COCKROACHDB=cockroach://root@localhost:26257/defaultdb?sslmode=disable
- image: postgres:9.6
environment:
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=hydra
- POSTGRES_USER=test
- POSTGRES_PASSWORD=test
- POSTGRES_DB=hydra
- image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=test
- MYSQL_ROOT_PASSWORD=test
- image: cockroachdb/cockroach:v2.1.6
command: start --insecure
working_directory: /go/src/github.com/ory/hydra
steps:
- checkout
Expand All @@ -52,8 +55,8 @@ jobs:
paths:
- "/go/pkg/mod"
- run: go-acc -o coverage.txt ./... -- -failfast -timeout=20m
# Running race conditions requires parallel tests, otherwise it's worthless (which is the case)
# - run: go test -race -short $(go list ./... | grep -v cmd)
# Running race conditions requires parallel tests, otherwise it's worthless (which is the case)
# - run: go test -race -short $(go list ./... | grep -v cmd)
- run: test -z "$CIRCLE_PR_NUMBER" && goveralls -service=circle-ci -coverprofile=coverage.txt -repotoken=$COVERALLS_REPO_TOKEN || echo "forks are not allowed to push to coveralls"

test-e2e-memory:
Expand Down Expand Up @@ -210,6 +213,57 @@ jobs:
- run: ./test/e2e/circle-ci.bash postgres
- run: ./test/e2e/circle-ci.bash postgres-jwt

test-e2e-cockroach:
docker:
- image: oryd/e2e-env:latest
environment:
- GO111MODULE=on
- TEST_DATABASE_COCKROACHDB=cockroach://root@localhost:26257/defaultdb?sslmode=disable
- image: cockroachdb/cockroach:v2.1.6
command: start --insecure
working_directory: /go/src/github.com/ory/hydra
steps:
- checkout
# core node_modules cache
- restore_cache:
keys:
- v1-deps-{{ checksum "package-lock.json" }}
- v1-deps
- run: npm ci
- save_cache:
key: v1-deps-{{ checksum "package-lock.json" }}
# cache NPM modules and the folder with the Cypress binary
paths:
- ~/.npm
- ~/.cache

# oauth2_client node_modules cache
- restore_cache:
keys:
- v1-deps-oauth2-client-{{ checksum "test/e2e/oauth2-client/package-lock.json" }}
- v1-deps
- run: cd test/e2e/oauth2-client; npm ci
- save_cache:
key: v1-deps-oauth2-client-{{ checksum "test/e2e/oauth2-client/package-lock.json" }}
# cache NPM modules and the folder with the Cypress binary
paths:
- ~/.npm
- ~/.cache
- /test/e2e/oauth2-client/node_modules

# go modules cache
- restore_cache:
keys:
- go-mod-v1-{{ checksum "go.sum" }}
- run: go install .
- save_cache:
key: go-mod-v1-{{ checksum "go.sum" }}
paths:
- "/go/pkg/mod"

- run: ./test/e2e/circle-ci.bash cockroach
- run: ./test/e2e/circle-ci.bash cockroach-jwt

test-e2e-plugin:
docker:
- image: oryd/e2e-env:latest
Expand Down Expand Up @@ -360,6 +414,10 @@ workflows:
filters:
tags:
only: /.*/
- test-e2e-cockroach:
filters:
tags:
only: /.*/
- test-e2e-plugin:
filters:
tags:
Expand All @@ -384,6 +442,7 @@ workflows:
- test-e2e-memory
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
- test-e2e-plugin
filters:
tags:
Expand All @@ -406,4 +465,4 @@ workflows:
tags:
only: /.*/
branches:
ignore: /.*/
ignore: /.*/
16 changes: 12 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ tools:
test:
make test-resetdb
make sqlbin
TEST_DATABASE_MYSQL='root:secret@(127.0.0.1:3444)/mysql?parseTime=true' \
TEST_DATABASE_POSTGRESQL='postgres://postgres:secret@127.0.0.1:3445/hydra?sslmode=disable' \
go-acc ./... -- -failfast -timeout=20m
TEST_DATABASE_MYSQL='mysql://root:secret@(127.0.0.1:3444)/mysql?parseTime=true' \
TEST_DATABASE_POSTGRESQL='postgres://postgres:secret@127.0.0.1:3445/hydra?sslmode=disable' \
TEST_DATABASE_COCKROACHDB='cockroach://root@127.0.0.1:3446/defaultdb?sslmode=disable' \
go-acc ./... -- -failfast -timeout=20m
docker rm -f hydra_test_database_mysql
docker rm -f hydra_test_database_postgres
docker rm -f hydra_test_database_cockroach

# Resets the test databases
.PHONY: test-resetdb
test-resetdb:
docker kill hydra_test_database_mysql || true
docker kill hydra_test_database_postgres || true
docker kill hydra_test_database_cockroach || true
docker rm -f hydra_test_database_mysql || true
docker rm -f hydra_test_database_postgres || true
docker rm -f hydra_test_database_cockroach || true
docker run --rm --name hydra_test_database_mysql -p 3444:3306 -e MYSQL_ROOT_PASSWORD=secret -d mysql:5.7
docker run --rm --name hydra_test_database_postgres -p 3445:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=hydra -d postgres:9.6
docker run --rm --name hydra_test_database_cockroach -p 3446:26257 -d cockroachdb/cockroach:v2.1.6 start --insecure

# Runs tests in short mode, without database adapters
.PHONY: docker
Expand All @@ -38,14 +43,17 @@ docker:
.PHONY: e2e
e2e:
make test-resetdb
export TEST_DATABASE_MYSQL='root:secret@(127.0.0.1:3444)/mysql?parseTime=true'
export TEST_DATABASE_MYSQL='mysql://root:secret@(127.0.0.1:3444)/mysql?parseTime=true'
export TEST_DATABASE_POSTGRESQL='postgres://postgres:secret@127.0.0.1:3445/hydra?sslmode=disable'
export TEST_DATABASE_COCKROACHDB='cockroach://root@127.0.0.1:3446/defaultdb?sslmode=disable'
./test/e2e/circle-ci.bash memory
./test/e2e/circle-ci.bash memory-jwt
./test/e2e/circle-ci.bash postgres
./test/e2e/circle-ci.bash postgres-jwt
./test/e2e/circle-ci.bash mysql
./test/e2e/circle-ci.bash mysql-jwt
./test/e2e/circle-ci.bash cockroach
./test/e2e/circle-ci.bash cockroach-jwt
./test/e2e/circle-ci.bash plugin
./test/e2e/circle-ci.bash plugin-jwt

Expand Down
13 changes: 7 additions & 6 deletions client/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ import (
)

var Migrations = map[string]*dbal.PackrMigrationSource{
dbal.DriverMySQL: dbal.NewMustPackerMigrationSource(logrus.New(), AssetNames(), Asset, []string{"migrations/sql/shared", "migrations/sql/mysql"}, true),
dbal.DriverPostgreSQL: dbal.NewMustPackerMigrationSource(logrus.New(), AssetNames(), Asset, []string{"migrations/sql/shared", "migrations/sql/postgres"}, true),
dbal.DriverMySQL: dbal.NewMustPackerMigrationSource(logrus.New(), AssetNames(), Asset, []string{"migrations/sql/shared", "migrations/sql/mysql"}, true),
dbal.DriverPostgreSQL: dbal.NewMustPackerMigrationSource(logrus.New(), AssetNames(), Asset, []string{"migrations/sql/shared", "migrations/sql/postgres"}, true),
dbal.DriverCockroachDB: dbal.NewMustPackerMigrationSource(logrus.New(), AssetNames(), Asset, []string{"migrations/sql/cockroach"}, true),
}

func NewSQLManager(db *sqlx.DB, r InternalRegistry) *SQLManager {
Expand Down Expand Up @@ -225,15 +226,15 @@ func (d *sqlData) ToClient() (*Client, error) {
return c, nil
}

func (m *SQLManager) PlanMigration() ([]*migrate.PlannedMigration, error) {
func (m *SQLManager) PlanMigration(dbName string) ([]*migrate.PlannedMigration, error) {
migrate.SetTable("hydra_client_migration")
plan, _, err := migrate.PlanMigration(m.DB.DB, m.DB.DriverName(), Migrations[dbal.Canonicalize(m.DB.DriverName())], migrate.Up, 0)
plan, _, err := migrate.PlanMigration(m.DB.DB, dbal.Canonicalize(m.DB.DriverName()), Migrations[dbName], migrate.Up, 0)
return plan, errors.WithStack(err)
}

func (m *SQLManager) CreateSchemas() (int, error) {
func (m *SQLManager) CreateSchemas(dbName string) (int, error) {
migrate.SetTable("hydra_client_migration")
n, err := migrate.Exec(m.DB.DB, m.DB.DriverName(), Migrations[dbal.Canonicalize(m.DB.DriverName())], migrate.Up)
n, err := migrate.Exec(m.DB.DB, dbal.Canonicalize(m.DB.DriverName()), Migrations[dbName], migrate.Up)
if err != nil {
return 0, errors.Wrapf(err, "Could not migrate sql schema, applied %d Migrations", n)
}
Expand Down
20 changes: 17 additions & 3 deletions client/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ import (
"sync"
"testing"

"github.com/ory/hydra/internal"

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
"github.com/stretchr/testify/require"

. "github.com/ory/hydra/client"
"github.com/ory/hydra/internal"
"github.com/ory/x/sqlcon/dockertest"
)

Expand Down Expand Up @@ -74,6 +73,20 @@ func connectToPG() {
m.Unlock()
}

func connectToCRDB() {
db, err := dockertest.ConnectToTestCockroachDB()
if err != nil {
log.Fatalf("Could not connect to database: %v", err)
}

conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistrySQL(conf, db)

m.Lock()
clientManagers["cockroach"] = reg.ClientManager()
m.Unlock()
}

func TestManagers(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
reg := internal.NewRegistry(conf)
Expand All @@ -84,6 +97,7 @@ func TestManagers(t *testing.T) {
dockertest.Parallel([]func(){
connectToPG,
connectToMySQL,
connectToCRDB,
})
}

Expand All @@ -92,7 +106,7 @@ func TestManagers(t *testing.T) {
s, ok := m.(*SQLManager)
if ok {
CleanTestDB(t, s.DB)
x, err := s.CreateSchemas()
x, err := s.CreateSchemas(k)
if err != nil {
t.Fatal("Could not create schemas", err.Error())
} else {
Expand Down
39 changes: 39 additions & 0 deletions client/migrations/sql/cockroach/1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- +migrate Up
CREATE TABLE IF NOT EXISTS hydra_client (
pk SERIAL PRIMARY KEY,
id varchar(255) NOT NULL,
client_name text NOT NULL,
client_secret text NOT NULL,
redirect_uris text NOT NULL,
grant_types text NOT NULL,
response_types text NOT NULL,
scope text NOT NULL,
owner text NOT NULL,
policy_uri text NOT NULL,
tos_uri text NOT NULL,
client_uri text NOT NULL,
logo_uri text NOT NULL,
contacts text NOT NULL,
client_secret_expires_at INTEGER NOT NULL DEFAULT 0,
sector_identifier_uri text NOT NULL,
jwks text NOT NULL,
jwks_uri text NOT NULL,
request_uris text NOT NULL,
token_endpoint_auth_method VARCHAR(25) NOT NULL DEFAULT '',
request_object_signing_alg VARCHAR(10) NOT NULL DEFAULT '',
userinfo_signed_response_alg VARCHAR(10) NOT NULL DEFAULT '',
subject_type VARCHAR(15) NOT NULL DEFAULT '',
allowed_cors_origins text NOT NULL,
audience text NOT NULL,
frontchannel_logout_uri TEXT NOT NULL DEFAULT '',
frontchannel_logout_session_required BOOL NOT NULL DEFAULT FALSE,
post_logout_redirect_uris TEXT NOT NULL DEFAULT '',
backchannel_logout_uri TEXT NOT NULL DEFAULT '',
backchannel_logout_session_required BOOL NOT NULL DEFAULT FALSE,
created_at timestamp NOT NULL DEFAULT now(),
updated_at timestamp NOT NULL DEFAULT now(),
UNIQUE (id)
);

-- +migrate Down
DROP TABLE hydra_client;
4 changes: 4 additions & 0 deletions client/migrations/sql/tests/cockroach/1_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- +migrate Up
INSERT INTO hydra_client (id, allowed_cors_origins, client_name, client_secret, redirect_uris, grant_types, response_types, scope, owner, policy_uri, tos_uri, client_uri, logo_uri, contacts, client_secret_expires_at, sector_identifier_uri, jwks, jwks_uri, token_endpoint_auth_method, request_uris, request_object_signing_alg, userinfo_signed_response_alg, subject_type, audience, frontchannel_logout_uri, frontchannel_logout_session_required, post_logout_redirect_uris, backchannel_logout_uri, backchannel_logout_session_required, created_at, updated_at) VALUES ('1-data', 'http://localhost|http://google', 'some-client', 'abcdef', 'http://localhost|http://google', 'authorize_code|implicit', 'token|id_token', 'foo|bar', 'aeneas', 'http://policy', 'http://tos', 'http://client', 'http://logo', 'aeneas|foo', 0, 'http://sector', '{"keys": []}', 'http://jwks', 'none', 'http://uri1|http://uri2', 'rs256', 'rs526', 'public', 'https://www.ory.sh/api', 'http://fc-logout/', true, 'http://redir1/|http://redir2/', 'http://bc-logout/', true, NOW(), NOW());

-- +migrate Down
Loading

0 comments on commit c19e195

Please sign in to comment.