From 0053be98824f6244162217fcbf05b2d278600401 Mon Sep 17 00:00:00 2001 From: Leonard Gram Date: Fri, 16 Feb 2018 17:25:46 +0100 Subject: [PATCH 1/2] login: uses epochs for login throtting. Closes #10937 --- pkg/models/login_attempt.go | 2 +- pkg/services/sqlstore/login_attempt.go | 8 ++++---- pkg/services/sqlstore/migrations/common.go | 1 + .../sqlstore/migrations/dashboard_mig.go | 5 +---- .../sqlstore/migrations/login_attempt_mig.go | 20 +++++++++++++++++++ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/pkg/models/login_attempt.go b/pkg/models/login_attempt.go index e4391927702..6e0976bc506 100644 --- a/pkg/models/login_attempt.go +++ b/pkg/models/login_attempt.go @@ -8,7 +8,7 @@ type LoginAttempt struct { Id int64 Username string IpAddress string - Created time.Time + Created int64 } // --------------------- diff --git a/pkg/services/sqlstore/login_attempt.go b/pkg/services/sqlstore/login_attempt.go index 805d726df48..78da198e8e7 100644 --- a/pkg/services/sqlstore/login_attempt.go +++ b/pkg/services/sqlstore/login_attempt.go @@ -21,7 +21,7 @@ func CreateLoginAttempt(cmd *m.CreateLoginAttemptCommand) error { loginAttempt := m.LoginAttempt{ Username: cmd.Username, IpAddress: cmd.IpAddress, - Created: getTimeNow(), + Created: getTimeNow().Unix(), } if _, err := sess.Insert(&loginAttempt); err != nil { @@ -37,8 +37,8 @@ func CreateLoginAttempt(cmd *m.CreateLoginAttemptCommand) error { func DeleteOldLoginAttempts(cmd *m.DeleteOldLoginAttemptsCommand) error { return inTransaction(func(sess *DBSession) error { var maxId int64 - sql := "SELECT max(id) as id FROM login_attempt WHERE created < " + dialect.DateTimeFunc("?") - result, err := sess.Query(sql, cmd.OlderThan) + sql := "SELECT max(id) as id FROM login_attempt WHERE created < ?" + result, err := sess.Query(sql, cmd.OlderThan.Unix()) if err != nil { return err @@ -66,7 +66,7 @@ func GetUserLoginAttemptCount(query *m.GetUserLoginAttemptCountQuery) error { loginAttempt := new(m.LoginAttempt) total, err := x. Where("username = ?", query.Username). - And("created >="+dialect.DateTimeFunc("?"), query.Since). + And("created >= ?", query.Since.Unix()). Count(loginAttempt) if err != nil { diff --git a/pkg/services/sqlstore/migrations/common.go b/pkg/services/sqlstore/migrations/common.go index bafb8292fd9..cc31b1d4580 100644 --- a/pkg/services/sqlstore/migrations/common.go +++ b/pkg/services/sqlstore/migrations/common.go @@ -34,6 +34,7 @@ func addTableReplaceMigrations(mg *Migrator, from Table, to Table, migrationVers copyTableData := fmt.Sprintf("copy %v %v to %v", to.Name, fromV, toV) dropTable := fmt.Sprintf("drop %v", tmpTableName) + addDropAllIndicesMigrations(mg, fromV, from) addTableRenameMigration(mg, from.Name, tmpTableName, fromV) mg.AddMigration(createTable, NewAddTableMigration(to)) addTableIndicesMigrations(mg, toV, to) diff --git a/pkg/services/sqlstore/migrations/dashboard_mig.go b/pkg/services/sqlstore/migrations/dashboard_mig.go index 7641dedcde5..296950ee497 100644 --- a/pkg/services/sqlstore/migrations/dashboard_mig.go +++ b/pkg/services/sqlstore/migrations/dashboard_mig.go @@ -187,10 +187,7 @@ func addDashboardMigration(mg *Migrator) { {Name: "external_id", Type: DB_Text, Nullable: false}, {Name: "updated", Type: DB_DateTime, Nullable: false}, }, - Indices: []*Index{ - {Cols: []string{"dashboard_id"}}, - {Cols: []string{"dashboard_id", "name"}, Type: IndexType}, - }, + Indices: []*Index{}, } mg.AddMigration("create dashboard_provisioning", NewAddTableMigration(dashboardExtrasTable)) diff --git a/pkg/services/sqlstore/migrations/login_attempt_mig.go b/pkg/services/sqlstore/migrations/login_attempt_mig.go index e576ccd1a50..42ef262664c 100644 --- a/pkg/services/sqlstore/migrations/login_attempt_mig.go +++ b/pkg/services/sqlstore/migrations/login_attempt_mig.go @@ -20,4 +20,24 @@ func addLoginAttemptMigrations(mg *Migrator) { mg.AddMigration("create login attempt table", NewAddTableMigration(loginAttemptV1)) // add indices mg.AddMigration("add index login_attempt.username", NewAddIndexMigration(loginAttemptV1, loginAttemptV1.Indices[0])) + + loginAttemptV2 := Table{ + Name: "login_attempt", + Columns: []*Column{ + {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, + {Name: "username", Type: DB_NVarchar, Length: 190, Nullable: false}, + {Name: "ip_address", Type: DB_NVarchar, Length: 30, Nullable: false}, + {Name: "created", Type: DB_Int, Default: "0", Nullable: false}, + }, + Indices: []*Index{ + {Cols: []string{"username"}}, + }, + } + + addTableReplaceMigrations(mg, loginAttemptV1, loginAttemptV2, 2, map[string]string{ + "id": "id", + "username": "username", + "ip_address": "ip_address", + "created": "created", + }) } From fe357a72d929d33d1c5a60d53ec953ab9178c7f4 Mon Sep 17 00:00:00 2001 From: Leonard Gram Date: Fri, 16 Feb 2018 17:36:28 +0100 Subject: [PATCH 2/2] login: migration fix. --- pkg/services/sqlstore/migrations/login_attempt_mig.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/services/sqlstore/migrations/login_attempt_mig.go b/pkg/services/sqlstore/migrations/login_attempt_mig.go index 42ef262664c..df14eb4effa 100644 --- a/pkg/services/sqlstore/migrations/login_attempt_mig.go +++ b/pkg/services/sqlstore/migrations/login_attempt_mig.go @@ -38,6 +38,5 @@ func addLoginAttemptMigrations(mg *Migrator) { "id": "id", "username": "username", "ip_address": "ip_address", - "created": "created", }) }