sql: added code migration type

This commit is contained in:
Leonard Gram
2018-08-22 16:16:25 +02:00
parent 7a06a47e63
commit 7baecf0d0d
6 changed files with 89 additions and 9 deletions
+8 -1
View File
@@ -78,7 +78,14 @@ func tryLoginUsingRememberCookie(c *middleware.Context) bool {
user := userQuery.Result user := userQuery.Result
// validate remember me cookie // validate remember me cookie
if val, _ := c.GetSuperSecureCookie(user.Rands+user.Password, setting.CookieRememberName); val != user.Login { signingKey := user.Rands + user.Password
if len(signingKey) < 10 {
c.Logger.Error("Invalid user signingKey")
return false
}
if val, _ := c.GetSuperSecureCookie(signingKey, setting.CookieRememberName); val != user.Login {
return false return false
} }
+39 -1
View File
@@ -1,6 +1,11 @@
package migrations package migrations
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" import (
"fmt"
"github.com/go-xorm/xorm"
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/util"
)
func addUserMigrations(mg *Migrator) { func addUserMigrations(mg *Migrator) {
userV1 := Table{ userV1 := Table{
@@ -107,4 +112,37 @@ func addUserMigrations(mg *Migrator) {
mg.AddMigration("Add last_seen_at column to user", NewAddColumnMigration(userV2, &Column{ mg.AddMigration("Add last_seen_at column to user", NewAddColumnMigration(userV2, &Column{
Name: "last_seen_at", Type: DB_DateTime, Nullable: true, Name: "last_seen_at", Type: DB_DateTime, Nullable: true,
})) }))
// Adds salt & rands for old users who used ldap or oauth
mg.AddMigration("Add missing user data", &AddMissingUserSaltAndRandsMigration{})
}
type AddMissingUserSaltAndRandsMigration struct {
MigrationBase
}
func (m *AddMissingUserSaltAndRandsMigration) Sql(dialect Dialect) string {
return "code migration"
}
type TempUserDTO struct {
Id int64
Login string
}
func (m *AddMissingUserSaltAndRandsMigration) Exec(sess *xorm.Session, mg *Migrator) error {
users := make([]*TempUserDTO, 0)
err := sess.Sql(fmt.Sprintf("SELECT id, login from %s WHERE rands = ''", mg.Dialect.Quote("user"))).Find(&users)
if err != nil {
return err
}
for _, user := range users {
_, err := sess.Exec("UPDATE "+mg.Dialect.Quote("user")+" SET salt = ?, rands = ? WHERE id = ?", util.GetRandomString(10), util.GetRandomString(10), user.Id)
if err != nil {
return err
}
}
return nil
} }
+11 -5
View File
@@ -12,7 +12,7 @@ import (
type Migrator struct { type Migrator struct {
x *xorm.Engine x *xorm.Engine
dialect Dialect Dialect Dialect
migrations []Migration migrations []Migration
Logger log.Logger Logger log.Logger
} }
@@ -31,7 +31,7 @@ func NewMigrator(engine *xorm.Engine) *Migrator {
mg.x = engine mg.x = engine
mg.Logger = log.New("migrator") mg.Logger = log.New("migrator")
mg.migrations = make([]Migration, 0) mg.migrations = make([]Migration, 0)
mg.dialect = NewDialect(mg.x.DriverName()) mg.Dialect = NewDialect(mg.x.DriverName())
return mg return mg
} }
@@ -82,7 +82,7 @@ func (mg *Migrator) Start() error {
continue continue
} }
sql := m.Sql(mg.dialect) sql := m.Sql(mg.Dialect)
record := MigrationLog{ record := MigrationLog{
MigrationId: m.Id(), MigrationId: m.Id(),
@@ -120,7 +120,7 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
condition := m.GetCondition() condition := m.GetCondition()
if condition != nil { if condition != nil {
sql, args := condition.Sql(mg.dialect) sql, args := condition.Sql(mg.Dialect)
results, err := sess.Query(sql, args...) results, err := sess.Query(sql, args...)
if err != nil || len(results) == 0 { if err != nil || len(results) == 0 {
mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id()) mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id())
@@ -128,7 +128,13 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
} }
} }
_, err := sess.Exec(m.Sql(mg.dialect)) var err error
if codeMigration, ok := m.(CodeMigration); ok {
err = codeMigration.Exec(sess, mg)
} else {
_, err = sess.Exec(m.Sql(mg.Dialect))
}
if err != nil { if err != nil {
mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err) mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err)
return err return err
+6
View File
@@ -2,6 +2,7 @@ package migrator
import ( import (
"fmt" "fmt"
"github.com/go-xorm/xorm"
"strings" "strings"
) )
@@ -18,6 +19,11 @@ type Migration interface {
GetCondition() MigrationCondition GetCondition() MigrationCondition
} }
type CodeMigration interface {
Migration
Exec(sess *xorm.Session, migrator * Migrator) error
}
type SQLType string type SQLType string
type ColumnType string type ColumnType string
+3 -2
View File
@@ -99,9 +99,10 @@ func CreateUser(cmd *m.CreateUserCommand) error {
LastSeenAt: time.Now().AddDate(-10, 0, 0), LastSeenAt: time.Now().AddDate(-10, 0, 0),
} }
user.Salt = util.GetRandomString(10)
user.Rands = util.GetRandomString(10)
if len(cmd.Password) > 0 { if len(cmd.Password) > 0 {
user.Salt = util.GetRandomString(10)
user.Rands = util.GetRandomString(10)
user.Password = util.EncodePassword(cmd.Password, user.Salt) user.Password = util.EncodePassword(cmd.Password, user.Salt)
} }
+22
View File
@@ -14,6 +14,28 @@ func TestUserDataAccess(t *testing.T) {
Convey("Testing DB", t, func() { Convey("Testing DB", t, func() {
InitTestDB(t) InitTestDB(t)
Convey("Creating a user", func() {
cmd := &models.CreateUserCommand{
Email: "usertest@test.com",
Name: "user name",
Login: "user_test_login",
}
err := CreateUser(cmd)
So(err, ShouldBeNil)
Convey("Loading a user", func() {
query := models.GetUserByIdQuery{Id: cmd.Result.Id}
err := GetUserById(&query)
So(err, ShouldBeNil)
So(query.Result.Email, ShouldEqual, "usertest@test.com")
So(query.Result.Password, ShouldEqual, "")
So(query.Result.Rands, ShouldHaveLength, 10)
So(query.Result.Salt, ShouldHaveLength, 10)
})
})
var err error var err error
for i := 0; i < 5; i++ { for i := 0; i < 5; i++ {
err = CreateUser(&models.CreateUserCommand{ err = CreateUser(&models.CreateUserCommand{