Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed panic when DSN="memory" is configured #574

Merged
merged 7 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ workflows:
filters:
tags:
only: /.*/
- test-e2e:
name: test-e2e-memory
flavor: memory
filters:
tags:
only: /.*/
- test-e2e:
name: test-e2e-postgres
flavor: postgres
Expand Down Expand Up @@ -164,6 +170,7 @@ workflows:
requires:
- test
- test-e2e-sqlite
- test-e2e-memory
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
Expand All @@ -177,6 +184,7 @@ workflows:
requires:
- test
- test-e2e-sqlite
- test-e2e-memory
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
Expand All @@ -190,6 +198,7 @@ workflows:
requires:
- test
- test-e2e-sqlite
- test-e2e-memory
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
Expand All @@ -205,6 +214,7 @@ workflows:
requires:
- test
- test-e2e-sqlite
- test-e2e-memory
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
Expand All @@ -225,6 +235,7 @@ workflows:
- goreleaser/test
- test
- test-e2e-sqlite
- test-e2e-memory
- test-e2e-postgres
- test-e2e-mysql
- test-e2e-cockroach
Expand Down
25 changes: 25 additions & 0 deletions driver/driver_default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package driver

import (
"context"
"net/url"
"strings"

"github.com/pkg/errors"

"github.com/ory/x/logrusx"
Expand All @@ -13,6 +17,17 @@ type DefaultDriver struct {
r Registry
}

// IsSQLiteMemoryMode returns true if SQLite if configured to use memory mode
func IsSQLiteMemoryMode(dsn string) bool {
tricky42 marked this conversation as resolved.
Show resolved Hide resolved
if urlParts := strings.SplitN(dsn, "?", 2); len(urlParts) == 2 && strings.HasPrefix(dsn, "sqlite://") {
queryVals, err := url.ParseQuery(urlParts[1])
if err == nil && queryVals.Get("mode") == "memory" {
return true
}
}
return false
}

func NewDefaultDriver(l *logrusx.Logger, version, build, date string, dev bool) (Driver, error) {
if l == nil {
l = logrusx.New("ORY Kratos", version)
Expand All @@ -35,6 +50,16 @@ func NewDefaultDriver(l *logrusx.Logger, version, build, date string, dev bool)
return nil, errors.Wrap(err, "unable to initialize service registry")
}

dsn := c.DSN()
// if dsn is memory we have to run the migrations on every start
if IsSQLiteMemoryMode(dsn) {
r.Logger().Print("Kratos is running migrations on every startup as DSN is memory.\n")
r.Logger().Print("This means your data is lost when Kratos terminates.\n")
if err := r.Persister().MigrateUp(context.Background()); err != nil {
return nil, err
}
}

return &DefaultDriver{r: r, c: c}, nil
}

Expand Down
File renamed without changes.
23 changes: 0 additions & 23 deletions driver/registry.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package driver

import (
"context"
"net/url"
"strings"

"github.com/gorilla/sessions"
"github.com/pkg/errors"

Expand Down Expand Up @@ -126,17 +122,6 @@ type Registry interface {
x.CSRFTokenGeneratorProvider
}

// IsSQLiteMemoryMode returns true if SQLite if configured to use memory mode
func IsSQLiteMemoryMode(dsn string) bool {
if urlParts := strings.SplitN(dsn, "?", 2); len(urlParts) == 2 && strings.HasPrefix(dsn, "sqlite://") {
queryVals, err := url.ParseQuery(urlParts[1])
if err == nil && queryVals.Get("mode") == "memory" {
return true
}
}
return false
}

func NewRegistry(c configuration.Provider) (Registry, error) {
dsn := c.DSN()
driver, err := dbal.GetDriverFor(dsn)
Expand All @@ -149,13 +134,5 @@ func NewRegistry(c configuration.Provider) (Registry, error) {
return nil, errors.Errorf("driver of type %T does not implement interface Registry", driver)
}

// if dsn is memory we have to run the migrations on every start
if IsSQLiteMemoryMode(dsn) {
registry.Logger().Print("Kratos is running migrations on every startup as DSN is memory.\n")
registry.Logger().Print("This means your data is lost when Kratos terminates.\n")
if err := registry.Persister().MigrateUp(context.Background()); err != nil {
return nil, err
}
}
return registry, nil
}
9 changes: 8 additions & 1 deletion test/e2e/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ if [ -z ${TEST_DATABASE_POSTGRESQL+x} ]; then
export TEST_DATABASE_MYSQL="mysql://root:secret@(127.0.0.1:3444)/mysql?parseTime=true&multiStatements=true"
export TEST_DATABASE_POSTGRESQL="postgres://postgres:secret@127.0.0.1:3445/postgres?sslmode=disable"
export TEST_DATABASE_COCKROACHDB="cockroach://root@127.0.0.1:3446/defaultdb?sslmode=disable"
export TEST_DATABASE_MEMORY="memory"

fi

! nc -zv 127.0.0.1 4434
Expand Down Expand Up @@ -109,7 +111,7 @@ run() {

yq merge test/e2e/profiles/kratos.base.yml "test/e2e/profiles/${profile}/.kratos.yml" > test/e2e/kratos.generated.yml
($kratos serve --dev -c test/e2e/kratos.generated.yml > "${base}/test/e2e/kratos.${profile}.e2e.log" 2>&1 &)

npm run wait-on -- -t 10000 http-get://127.0.0.1:4434/health/ready \
http-get://127.0.0.1:4455/health \
http-get://127.0.0.1:4445/health/ready \
Expand Down Expand Up @@ -179,11 +181,16 @@ if [[ $dev = "yes" ]]; then
fi

export TEST_DATABASE_SQLITE="sqlite:///$(mktemp -d -t ci-XXXXXXXXXX)/db.sqlite?_fk=true"

case "$1" in
sqlite)
db="${TEST_DATABASE_SQLITE}"
;;

memory)
db="${TEST_DATABASE_MEMORY}"
;;

mysql)
db="${TEST_DATABASE_MYSQL}"
;;
Expand Down