Merge branch 'master' into 12556-oauth-pass-thru
This commit is contained in:
@@ -92,14 +92,29 @@ func (this *VictoropsNotifier) Notify(evalContext *alerting.EvalContext) error {
|
||||
messageType = AlertStateRecovery
|
||||
}
|
||||
|
||||
fields := make(map[string]interface{}, 0)
|
||||
fieldLimitCount := 4
|
||||
for index, evt := range evalContext.EvalMatches {
|
||||
fields[evt.Metric] = evt.Value
|
||||
if index > fieldLimitCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
bodyJSON := simplejson.New()
|
||||
bodyJSON.Set("message_type", messageType)
|
||||
bodyJSON.Set("entity_id", evalContext.Rule.Name)
|
||||
bodyJSON.Set("entity_display_name", evalContext.GetNotificationTitle())
|
||||
bodyJSON.Set("timestamp", time.Now().Unix())
|
||||
bodyJSON.Set("state_start_time", evalContext.StartTime.Unix())
|
||||
bodyJSON.Set("state_message", evalContext.Rule.Message)
|
||||
bodyJSON.Set("monitoring_tool", "Grafana v"+setting.BuildVersion)
|
||||
bodyJSON.Set("alert_url", ruleUrl)
|
||||
bodyJSON.Set("metrics", fields)
|
||||
|
||||
if evalContext.Error != nil {
|
||||
bodyJSON.Set("error_message", evalContext.Error.Error())
|
||||
}
|
||||
|
||||
if evalContext.ImagePublicUrl != "" {
|
||||
bodyJSON.Set("image_url", evalContext.ImagePublicUrl)
|
||||
|
||||
@@ -221,6 +221,57 @@ func (s *UserAuthTokenService) RevokeToken(token *models.UserToken) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) RevokeAllUserTokens(userId int64) error {
|
||||
sql := `DELETE from user_auth_token WHERE user_id = ?`
|
||||
res, err := s.SQLStore.NewSession().Exec(sql, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
affected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.log.Debug("all user tokens for user revoked", "userId", userId, "count", affected)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) GetUserToken(userId, userTokenId int64) (*models.UserToken, error) {
|
||||
var token userAuthToken
|
||||
exists, err := s.SQLStore.NewSession().Where("id = ? AND user_id = ?", userTokenId, userId).Get(&token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return nil, models.ErrUserTokenNotFound
|
||||
}
|
||||
|
||||
var result models.UserToken
|
||||
token.toUserToken(&result)
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) GetUserTokens(userId int64) ([]*models.UserToken, error) {
|
||||
var tokens []*userAuthToken
|
||||
err := s.SQLStore.NewSession().Where("user_id = ? AND created_at > ? AND rotated_at > ?", userId, s.createdAfterParam(), s.rotatedAfterParam()).Find(&tokens)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := []*models.UserToken{}
|
||||
for _, token := range tokens {
|
||||
var userToken models.UserToken
|
||||
token.toUserToken(&userToken)
|
||||
result = append(result, &userToken)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *UserAuthTokenService) createdAfterParam() int64 {
|
||||
tokenMaxLifetime := time.Duration(s.Cfg.LoginMaxLifetimeDays) * 24 * time.Hour
|
||||
return getTime().Add(-tokenMaxLifetime).Unix()
|
||||
|
||||
@@ -75,6 +75,47 @@ func TestUserAuthToken(t *testing.T) {
|
||||
err = userAuthTokenService.RevokeToken(userToken)
|
||||
So(err, ShouldEqual, models.ErrUserTokenNotFound)
|
||||
})
|
||||
|
||||
Convey("When creating an additional token", func() {
|
||||
userToken2, err := userAuthTokenService.CreateToken(userID, "192.168.10.11:1234", "some user agent")
|
||||
So(err, ShouldBeNil)
|
||||
So(userToken2, ShouldNotBeNil)
|
||||
|
||||
Convey("Can get first user token", func() {
|
||||
token, err := userAuthTokenService.GetUserToken(userID, userToken.Id)
|
||||
So(err, ShouldBeNil)
|
||||
So(token, ShouldNotBeNil)
|
||||
So(token.Id, ShouldEqual, userToken.Id)
|
||||
})
|
||||
|
||||
Convey("Can get second user token", func() {
|
||||
token, err := userAuthTokenService.GetUserToken(userID, userToken2.Id)
|
||||
So(err, ShouldBeNil)
|
||||
So(token, ShouldNotBeNil)
|
||||
So(token.Id, ShouldEqual, userToken2.Id)
|
||||
})
|
||||
|
||||
Convey("Can get user tokens", func() {
|
||||
tokens, err := userAuthTokenService.GetUserTokens(userID)
|
||||
So(err, ShouldBeNil)
|
||||
So(tokens, ShouldHaveLength, 2)
|
||||
So(tokens[0].Id, ShouldEqual, userToken.Id)
|
||||
So(tokens[1].Id, ShouldEqual, userToken2.Id)
|
||||
})
|
||||
|
||||
Convey("Can revoke all user tokens", func() {
|
||||
err := userAuthTokenService.RevokeAllUserTokens(userID)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
model, err := ctx.getAuthTokenByID(userToken.Id)
|
||||
So(err, ShouldBeNil)
|
||||
So(model, ShouldBeNil)
|
||||
|
||||
model2, err := ctx.getAuthTokenByID(userToken2.Id)
|
||||
So(err, ShouldBeNil)
|
||||
So(model2, ShouldBeNil)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Convey("expires correctly", func() {
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package auth
|
||||
|
||||
import "github.com/grafana/grafana/pkg/models"
|
||||
|
||||
type FakeUserAuthTokenService struct {
|
||||
CreateTokenProvider func(userId int64, clientIP, userAgent string) (*models.UserToken, error)
|
||||
TryRotateTokenProvider func(token *models.UserToken, clientIP, userAgent string) (bool, error)
|
||||
LookupTokenProvider func(unhashedToken string) (*models.UserToken, error)
|
||||
RevokeTokenProvider func(token *models.UserToken) error
|
||||
RevokeAllUserTokensProvider func(userId int64) error
|
||||
ActiveAuthTokenCount func() (int64, error)
|
||||
GetUserTokenProvider func(userId, userTokenId int64) (*models.UserToken, error)
|
||||
GetUserTokensProvider func(userId int64) ([]*models.UserToken, error)
|
||||
}
|
||||
|
||||
func NewFakeUserAuthTokenService() *FakeUserAuthTokenService {
|
||||
return &FakeUserAuthTokenService{
|
||||
CreateTokenProvider: func(userId int64, clientIP, userAgent string) (*models.UserToken, error) {
|
||||
return &models.UserToken{
|
||||
UserId: 0,
|
||||
UnhashedToken: "",
|
||||
}, nil
|
||||
},
|
||||
TryRotateTokenProvider: func(token *models.UserToken, clientIP, userAgent string) (bool, error) {
|
||||
return false, nil
|
||||
},
|
||||
LookupTokenProvider: func(unhashedToken string) (*models.UserToken, error) {
|
||||
return &models.UserToken{
|
||||
UserId: 0,
|
||||
UnhashedToken: "",
|
||||
}, nil
|
||||
},
|
||||
RevokeTokenProvider: func(token *models.UserToken) error {
|
||||
return nil
|
||||
},
|
||||
RevokeAllUserTokensProvider: func(userId int64) error {
|
||||
return nil
|
||||
},
|
||||
ActiveAuthTokenCount: func() (int64, error) {
|
||||
return 10, nil
|
||||
},
|
||||
GetUserTokenProvider: func(userId, userTokenId int64) (*models.UserToken, error) {
|
||||
return nil, nil
|
||||
},
|
||||
GetUserTokensProvider: func(userId int64) ([]*models.UserToken, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) CreateToken(userId int64, clientIP, userAgent string) (*models.UserToken, error) {
|
||||
return s.CreateTokenProvider(userId, clientIP, userAgent)
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) LookupToken(unhashedToken string) (*models.UserToken, error) {
|
||||
return s.LookupTokenProvider(unhashedToken)
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) TryRotateToken(token *models.UserToken, clientIP, userAgent string) (bool, error) {
|
||||
return s.TryRotateTokenProvider(token, clientIP, userAgent)
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) RevokeToken(token *models.UserToken) error {
|
||||
return s.RevokeTokenProvider(token)
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) RevokeAllUserTokens(userId int64) error {
|
||||
return s.RevokeAllUserTokensProvider(userId)
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) ActiveTokenCount() (int64, error) {
|
||||
return s.ActiveAuthTokenCount()
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) GetUserToken(userId, userTokenId int64) (*models.UserToken, error) {
|
||||
return s.GetUserTokenProvider(userId, userTokenId)
|
||||
}
|
||||
|
||||
func (s *FakeUserAuthTokenService) GetUserTokens(userId int64) ([]*models.UserToken, error) {
|
||||
return s.GetUserTokensProvider(userId)
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package migrations
|
||||
|
||||
import "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
|
||||
func addCacheMigration(mg *migrator.Migrator) {
|
||||
var cacheDataV1 = migrator.Table{
|
||||
Name: "cache_data",
|
||||
Columns: []*migrator.Column{
|
||||
{Name: "cache_key", Type: migrator.DB_NVarchar, IsPrimaryKey: true, Length: 168},
|
||||
{Name: "data", Type: migrator.DB_Blob},
|
||||
{Name: "expires", Type: migrator.DB_Integer, Length: 255, Nullable: false},
|
||||
{Name: "created_at", Type: migrator.DB_Integer, Length: 255, Nullable: false},
|
||||
},
|
||||
Indices: []*migrator.Index{
|
||||
{Cols: []string{"cache_key"}, Type: migrator.UniqueIndex},
|
||||
},
|
||||
}
|
||||
|
||||
mg.AddMigration("create cache_data table", migrator.NewAddTableMigration(cacheDataV1))
|
||||
|
||||
mg.AddMigration("add unique index cache_data.cache_key", migrator.NewAddIndexMigration(cacheDataV1, cacheDataV1.Indices[0]))
|
||||
}
|
||||
@@ -33,6 +33,7 @@ func AddMigrations(mg *Migrator) {
|
||||
addUserAuthMigrations(mg)
|
||||
addServerlockMigrations(mg)
|
||||
addUserAuthTokenMigrations(mg)
|
||||
addCacheMigration(mg)
|
||||
}
|
||||
|
||||
func addMigrationLogMigrations(mg *Migrator) {
|
||||
|
||||
Reference in New Issue
Block a user