From 4f931cd6097d9fb7c45f4569f624fb42a5e76f19 Mon Sep 17 00:00:00 2001 From: "Aeneas Rekkas (arekkas)" Date: Mon, 2 Jan 2017 14:38:44 +0100 Subject: [PATCH] sql: improve sql migration routine and add test --- client/manager_sql.go | 1 + cmd/server/handler_jwk_factory.go | 2 +- config/config.go | 3 --- integration/sql_schema_test.go | 43 +++++++++++++++++++++++++++++++ policy/manager.go | 15 ++++++++++- warden/group/manager_sql.go | 1 + 6 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 integration/sql_schema_test.go diff --git a/client/manager_sql.go b/client/manager_sql.go index 3d2cdd0902f..092d6f499e1 100644 --- a/client/manager_sql.go +++ b/client/manager_sql.go @@ -118,6 +118,7 @@ func (d *sqlData) ToClient() *Client { } func (s *SQLManager) CreateSchemas() error { + migrate.SetTable("hydra_client_migration") n, err := migrate.Exec(s.DB.DB, s.DB.DriverName(), migrations, migrate.Up) if err != nil { return errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n) diff --git a/cmd/server/handler_jwk_factory.go b/cmd/server/handler_jwk_factory.go index 95b8b213a79..848ec22895d 100644 --- a/cmd/server/handler_jwk_factory.go +++ b/cmd/server/handler_jwk_factory.go @@ -50,7 +50,7 @@ func injectJWKManager(c *config.Config) { m := &jwk.RedisManager{ DB: con.RedisSession(), Cipher: &jwk.AEAD{ - c.GetSystemSecret(), + Key: c.GetSystemSecret(), }, } ctx.KeyManager = m diff --git a/config/config.go b/config/config.go index 3b9dae47d18..179e203f5ad 100644 --- a/config/config.go +++ b/config/config.go @@ -21,7 +21,6 @@ import ( "github.com/ory-am/hydra/warden/group" "github.com/ory-am/ladon" "github.com/pkg/errors" - "github.com/rubenv/sql-migrate" "github.com/spf13/cobra" "github.com/spf13/viper" "golang.org/x/net/context" @@ -172,13 +171,11 @@ func (c *Config) Context() *Context { break case *SQLConnection: m := ladon.NewSQLManager(con.GetDatabase(), nil) - migrate.SetTable("hydra_policy_migration") if err := m.CreateSchemas(); err != nil { logrus.Fatalf("Could not create policy schema: %s", err) } manager = m - migrate.SetTable("hydra_groups_migration") gm := &group.SQLManager{DB: con.GetDatabase()} if err := gm.CreateSchemas(); err != nil { logrus.Fatalf("Could not create group schema: %s", err) diff --git a/integration/sql_schema_test.go b/integration/sql_schema_test.go new file mode 100644 index 00000000000..ed11ca13315 --- /dev/null +++ b/integration/sql_schema_test.go @@ -0,0 +1,43 @@ +package integration + +import ( + "testing" + "github.com/ory-am/hydra/client" + "github.com/ory-am/fosite" + "github.com/stretchr/testify/require" + "github.com/ory-am/hydra/policy" + "github.com/ory-am/hydra/jwk" + "github.com/ory-am/hydra/oauth2" + "github.com/ory-am/ladon" + "github.com/ory-am/hydra/warden/group" +) + +func TestSQLSchema(t *testing.T) { + var testGenerator = &jwk.RS256Generator{} + ks, _ := testGenerator.Generate("") + p1 := ks.Key("private") + r := fosite.NewRequest() + r.ID = "foo" + db := ConnectToMySQL() + + cm := &client.SQLManager{DB: db, Hasher: &fosite.BCrypt{}} + gm := group.SQLManager{DB: db} + jm := jwk.SQLManager{DB: db, Cipher: &jwk.AEAD{Key: []byte("11111111111111111111111111111111")}} + om := oauth2.FositeSQLStore{Manager: cm, DB: db } + pm, err := policy.NewSQLManager(db) + require.Nil(t, err) + + require.Nil(t, cm.CreateSchemas()) + require.Nil(t, gm.CreateSchemas()) + require.Nil(t, jm.CreateSchemas()) + require.Nil(t, om.CreateSchemas()) + + require.Nil(t, jm.AddKey("foo", jwk.First(p1))) + require.Nil(t, pm.Create(&ladon.DefaultPolicy{ID: "foo"})) + require.Nil(t, cm.CreateClient(&client.Client{ID: "foo"})) + require.Nil(t, om.CreateAccessTokenSession(nil, "asdfasdf", r)) + require.Nil(t, gm.CreateGroup(&group.Group{ + ID: "asdfas", + Members: []string{"asdf"}, + })) +} diff --git a/policy/manager.go b/policy/manager.go index 1e8d70bb143..d6caeee7c43 100644 --- a/policy/manager.go +++ b/policy/manager.go @@ -1,6 +1,10 @@ package policy -import "github.com/ory-am/ladon" +import ( + "github.com/ory-am/ladon" + "github.com/jmoiron/sqlx" + "github.com/rubenv/sql-migrate" +) // Manager is responsible for managing and persisting policies. type Manager interface { @@ -9,3 +13,12 @@ type Manager interface { // Update a policy. Update(policy ladon.Policy) error } + +func NewSQLManager(db *sqlx.DB) (ladon.Manager, error) { + m := ladon.NewSQLManager(db, nil) + migrate.SetTable("hydra_policy_migration") + if err := m.CreateSchemas(); err != nil { + return nil, err + } + return m, nil +} \ No newline at end of file diff --git a/warden/group/manager_sql.go b/warden/group/manager_sql.go index 66e340ad7fe..f7f12b5e6d5 100644 --- a/warden/group/manager_sql.go +++ b/warden/group/manager_sql.go @@ -32,6 +32,7 @@ type SQLManager struct { } func (s *SQLManager) CreateSchemas() error { + migrate.SetTable("hydra_groups_migration") n, err := migrate.Exec(s.DB.DB, s.DB.DriverName(), migrations, migrate.Up) if err != nil { return errors.Wrapf(err, "Could not migrate sql schema, applied %d migrations", n)