Merge remote-tracking branch 'origin/master' into envelope

This commit is contained in:
Emil Tullstedt
2020-10-15 14:29:48 +02:00
1264 changed files with 24235 additions and 43996 deletions
@@ -165,7 +165,7 @@ func createTestAnnotations(t *testing.T, sqlstore *SqlStore, expectedCount int,
}
// mark every third as an API annotation
// that doesnt belong to a dashboard
// that does not belong to a dashboard
if i%3 == 1 {
a.DashboardId = 0
}
+26 -14
View File
@@ -4,6 +4,8 @@ import (
"time"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/securedata"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)
@@ -45,22 +47,32 @@ func CreateDashboardSnapshot(cmd *models.CreateDashboardSnapshotCommand) error {
expires = time.Now().Add(time.Second * time.Duration(cmd.Expires))
}
snapshot := &models.DashboardSnapshot{
Name: cmd.Name,
Key: cmd.Key,
DeleteKey: cmd.DeleteKey,
OrgId: cmd.OrgId,
UserId: cmd.UserId,
External: cmd.External,
ExternalUrl: cmd.ExternalUrl,
ExternalDeleteUrl: cmd.ExternalDeleteUrl,
Dashboard: cmd.Dashboard,
Expires: expires,
Created: time.Now(),
Updated: time.Now(),
marshalledData, err := cmd.Dashboard.Encode()
if err != nil {
return err
}
_, err := sess.Insert(snapshot)
encryptedDashboard, err := securedata.Encrypt(marshalledData)
if err != nil {
return err
}
snapshot := &models.DashboardSnapshot{
Name: cmd.Name,
Key: cmd.Key,
DeleteKey: cmd.DeleteKey,
OrgId: cmd.OrgId,
UserId: cmd.UserId,
External: cmd.External,
ExternalUrl: cmd.ExternalUrl,
ExternalDeleteUrl: cmd.ExternalDeleteUrl,
Dashboard: simplejson.New(),
DashboardEncrypted: encryptedDashboard,
Expires: expires,
Created: time.Now(),
Updated: time.Now(),
}
_, err = sess.Insert(snapshot)
cmd.Result = snapshot
return err
+113 -95
View File
@@ -4,7 +4,8 @@ import (
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
@@ -12,140 +13,157 @@ import (
)
func TestDashboardSnapshotDBAccess(t *testing.T) {
Convey("Testing DashboardSnapshot data access", t, func() {
InitTestDB(t)
InitTestDB(t)
Convey("Given saved snapshot", func() {
origSecret := setting.SecretKey
setting.SecretKey = "dashboard_snapshot_testing"
t.Cleanup(func() {
setting.SecretKey = origSecret
})
t.Run("Given saved snapshot", func(t *testing.T) {
cmd := models.CreateDashboardSnapshotCommand{
Key: "hej",
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 1000,
OrgId: 1,
}
err := CreateDashboardSnapshot(&cmd)
require.NoError(t, err)
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
query := models.GetDashboardSnapshotQuery{Key: "hej"}
err := GetDashboardSnapshot(&query)
require.NoError(t, err)
assert.NotNil(t, query.Result)
dashboard, err := query.Result.DashboardJSON()
require.NoError(t, err)
assert.Equal(t, "mupp", dashboard.Get("hello").MustString())
})
t.Run("And the user has the admin role", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
err := SearchDashboardSnapshots(&query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
assert.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
})
})
t.Run("And the user has the editor role and has created a snapshot", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000},
}
err := SearchDashboardSnapshots(&query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
})
})
t.Run("And the user has the editor role and has not created any snapshot", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2},
}
err := SearchDashboardSnapshots(&query)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
})
})
t.Run("And the user is anonymous", func(t *testing.T) {
cmd := models.CreateDashboardSnapshotCommand{
Key: "hej",
Key: "strangesnapshotwithuserid0",
DeleteKey: "adeletekey",
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 1000,
UserId: 0,
OrgId: 1,
}
err := CreateDashboardSnapshot(&cmd)
So(err, ShouldBeNil)
require.NoError(t, err)
Convey("Should be able to get snapshot by key", func() {
query := models.GetDashboardSnapshotQuery{Key: "hej"}
err = GetDashboardSnapshot(&query)
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(query.Result.Dashboard.Get("hello").MustString(), ShouldEqual, "mupp")
})
Convey("And the user has the admin role", func() {
Convey("Should return all the snapshots", func() {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
err := SearchDashboardSnapshots(&query)
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(len(query.Result), ShouldEqual, 1)
})
})
Convey("And the user has the editor role and has created a snapshot", func() {
Convey("Should return all the snapshots", func() {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000},
}
err := SearchDashboardSnapshots(&query)
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(len(query.Result), ShouldEqual, 1)
})
})
Convey("And the user has the editor role and has not created any snapshot", func() {
Convey("Should not return any snapshots", func() {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2},
}
err := SearchDashboardSnapshots(&query)
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(len(query.Result), ShouldEqual, 0)
})
})
Convey("And the user is anonymous", func() {
cmd := models.CreateDashboardSnapshotCommand{
Key: "strangesnapshotwithuserid0",
DeleteKey: "adeletekey",
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 0,
OrgId: 1,
t.Run("Should not return any snapshots", func(t *testing.T) {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
}
err := CreateDashboardSnapshot(&cmd)
So(err, ShouldBeNil)
err := SearchDashboardSnapshots(&query)
require.NoError(t, err)
Convey("Should not return any snapshots", func() {
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
}
err := SearchDashboardSnapshots(&query)
So(err, ShouldBeNil)
So(query.Result, ShouldNotBeNil)
So(len(query.Result), ShouldEqual, 0)
})
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
})
})
t.Run("Should have encrypted dashboard data", func(t *testing.T) {
original, err := cmd.Dashboard.Encode()
require.NoError(t, err)
decrypted, err := cmd.Result.DashboardEncrypted.Decrypt()
require.NoError(t, err)
require.Equal(t, decrypted, original)
})
})
}
func TestDeleteExpiredSnapshots(t *testing.T) {
sqlstore := InitTestDB(t)
Convey("Testing dashboard snapshots clean up", t, func() {
t.Run("Testing dashboard snapshots clean up", func(t *testing.T) {
setting.SnapShotRemoveExpired = true
notExpiredsnapshot := createTestSnapshot(sqlstore, "key1", 48000)
createTestSnapshot(sqlstore, "key2", -1200)
createTestSnapshot(sqlstore, "key3", -1200)
nonExpiredSnapshot := createTestSnapshot(t, sqlstore, "key1", 48000)
createTestSnapshot(t, sqlstore, "key2", -1200)
createTestSnapshot(t, sqlstore, "key3", -1200)
err := DeleteExpiredSnapshots(&models.DeleteExpiredSnapshotsCommand{})
So(err, ShouldBeNil)
require.NoError(t, err)
query := models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
err = SearchDashboardSnapshots(&query)
So(err, ShouldBeNil)
require.NoError(t, err)
So(len(query.Result), ShouldEqual, 1)
So(query.Result[0].Key, ShouldEqual, notExpiredsnapshot.Key)
assert.Len(t, query.Result, 1)
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
err = DeleteExpiredSnapshots(&models.DeleteExpiredSnapshotsCommand{})
So(err, ShouldBeNil)
require.NoError(t, err)
query = models.GetDashboardSnapshotsQuery{
OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
}
err = SearchDashboardSnapshots(&query)
So(err, ShouldBeNil)
require.NoError(t, err)
So(len(query.Result), ShouldEqual, 1)
So(query.Result[0].Key, ShouldEqual, notExpiredsnapshot.Key)
require.Len(t, query.Result, 1)
require.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
})
}
func createTestSnapshot(sqlstore *SqlStore, key string, expires int64) *models.DashboardSnapshot {
func createTestSnapshot(t *testing.T, sqlstore *SqlStore, key string, expires int64) *models.DashboardSnapshot {
cmd := models.CreateDashboardSnapshotCommand{
Key: key,
DeleteKey: "delete" + key,
@@ -157,13 +175,13 @@ func createTestSnapshot(sqlstore *SqlStore, key string, expires int64) *models.D
Expires: expires,
}
err := CreateDashboardSnapshot(&cmd)
So(err, ShouldBeNil)
require.NoError(t, err)
// Set expiry date manually - to be able to create expired snapshots
if expires < 0 {
expireDate := time.Now().Add(time.Second * time.Duration(expires))
_, err = sqlstore.engine.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, cmd.Result.Id)
So(err, ShouldBeNil)
require.NoError(t, err)
}
return cmd.Result
@@ -175,4 +175,12 @@ func addAlertMigrations(mg *Migrator) {
// change column type of alert.settings
mg.AddMigration("alter alert.settings to mediumtext", NewRawSqlMigration("").
Mysql("ALTER TABLE alert MODIFY settings MEDIUMTEXT;"))
mg.AddMigration("Add non-unique index alert_notification_state_alert_id", NewAddIndexMigration(alert_notification_state, &Index{
Cols: []string{"alert_id"}, Type: IndexType,
}))
mg.AddMigration("Add non-unique index alert_rule_tag_alert_id", NewAddIndexMigration(alertRuleTagTable, &Index{
Cols: []string{"alert_id"}, Type: IndexType,
}))
}
@@ -64,4 +64,8 @@ func addDashboardSnapshotMigrations(mg *Migrator) {
mg.AddMigration("Add column external_delete_url to dashboard_snapshots table", NewAddColumnMigration(snapshotV5, &Column{
Name: "external_delete_url", Type: DB_NVarchar, Length: 255, Nullable: true,
}))
mg.AddMigration("Add encrypted dashboard json column", NewAddColumnMigration(snapshotV5, &Column{
Name: "dashboard_encrypted", Type: DB_Blob, Nullable: true,
}))
}
@@ -34,6 +34,7 @@ func AddMigrations(mg *Migrator) {
addServerlockMigrations(mg)
addUserAuthTokenMigrations(mg)
addCacheMigration(mg)
addShortURLMigrations(mg)
addDataKeysMigrations(mg)
}
+28
View File
@@ -0,0 +1,28 @@
package migrations
import (
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
)
func addShortURLMigrations(mg *Migrator) {
shortURLV1 := Table{
Name: "short_url",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, Nullable: false, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: false},
{Name: "path", Type: DB_Text, Nullable: false},
{Name: "created_by", Type: DB_Int, Nullable: false},
{Name: "created_at", Type: DB_Int, Nullable: false},
{Name: "last_seen_at", Type: DB_Int, Nullable: true},
},
Indices: []*migrator.Index{
{Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},
},
}
mg.AddMigration("create short_url table v1", NewAddTableMigration(shortURLV1))
mg.AddMigration("add index short_url.org_id-uid", migrator.NewAddIndexMigration(shortURLV1, shortURLV1.Indices[0]))
}
+67 -1
View File
@@ -1,6 +1,11 @@
package migrations
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
import (
"time"
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"xorm.io/xorm"
)
func addTempUserMigrations(mg *Migrator) {
tempUserV1 := Table{
@@ -44,4 +49,65 @@ func addTempUserMigrations(mg *Migrator) {
{Name: "status", Type: DB_Varchar, Length: 20},
{Name: "remote_addr", Type: DB_Varchar, Length: 255, Nullable: true},
}))
tempUserV2 := Table{
Name: "temp_user",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "version", Type: DB_Int, Nullable: false},
{Name: "email", Type: DB_NVarchar, Length: 190},
{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
{Name: "role", Type: DB_NVarchar, Length: 20, Nullable: true},
{Name: "code", Type: DB_NVarchar, Length: 190},
{Name: "status", Type: DB_Varchar, Length: 20},
{Name: "invited_by_user_id", Type: DB_BigInt, Nullable: true},
{Name: "email_sent", Type: DB_Bool},
{Name: "email_sent_on", Type: DB_DateTime, Nullable: true},
{Name: "remote_addr", Type: DB_Varchar, Length: 255, Nullable: true},
{Name: "created", Type: DB_Int, Default: "0", Nullable: false},
{Name: "updated", Type: DB_Int, Default: "0", Nullable: false},
},
Indices: []*Index{
{Cols: []string{"email"}, Type: IndexType},
{Cols: []string{"org_id"}, Type: IndexType},
{Cols: []string{"code"}, Type: IndexType},
{Cols: []string{"status"}, Type: IndexType},
},
}
addTableReplaceMigrations(mg, tempUserV1, tempUserV2, 2, map[string]string{
"id": "id",
"org_id": "org_id",
"version": "version",
"email": "email",
"name": "name",
"role": "role",
"code": "code",
"status": "status",
"invited_by_user_id": "invited_by_user_id",
"email_sent": "email_sent",
"email_sent_on": "email_sent_on",
"remote_addr": "remote_addr",
})
// Ensure outstanding invites are given a valid lifetime post-migration
mg.AddMigration("Set created for temp users that will otherwise prematurely expire", &SetCreatedForOutstandingInvites{})
}
type SetCreatedForOutstandingInvites struct {
MigrationBase
}
func (m *SetCreatedForOutstandingInvites) Sql(dialect Dialect) string {
return "code migration"
}
func (m *SetCreatedForOutstandingInvites) Exec(sess *xorm.Session, mg *Migrator) error {
created := time.Now().Unix()
if _, err := sess.Exec("UPDATE "+mg.Dialect.Quote("temp_user")+
" SET created = ?, updated = ? WHERE created = '0' AND status in ('SignUpStarted', 'InvitePending')", created, created); err != nil {
return err
}
return nil
}
+15 -2
View File
@@ -13,6 +13,7 @@ func init() {
bus.AddHandler("sql", UpdateTempUserStatus)
bus.AddHandler("sql", GetTempUserByCode)
bus.AddHandler("sql", UpdateTempUserWithEmailSent)
bus.AddHandler("sql", ExpireOldUserInvites)
}
func UpdateTempUserStatus(cmd *models.UpdateTempUserStatusCommand) error {
@@ -36,8 +37,8 @@ func CreateTempUser(cmd *models.CreateTempUserCommand) error {
RemoteAddr: cmd.RemoteAddr,
InvitedByUserId: cmd.InvitedByUserId,
EmailSentOn: time.Now(),
Created: time.Now(),
Updated: time.Now(),
Created: time.Now().Unix(),
Updated: time.Now().Unix(),
}
if _, err := sess.Insert(user); err != nil {
@@ -132,3 +133,15 @@ func GetTempUserByCode(query *models.GetTempUserByCodeQuery) error {
query.Result = &tempUser
return err
}
func ExpireOldUserInvites(cmd *models.ExpireTempUsersCommand) error {
return inTransaction(func(sess *DBSession) error {
var rawSql = "UPDATE temp_user SET status = ?, updated = ? WHERE created <= ? AND status in (?, ?)"
if result, err := sess.Exec(rawSql, string(models.TmpUserExpired), time.Now().Unix(), cmd.OlderThan.Unix(), string(models.TmpUserSignUpStarted), string(models.TmpUserInvitePending)); err != nil {
return err
} else if cmd.NumExpired, err = result.RowsAffected(); err != nil {
return err
}
return nil
})
}
+17 -3
View File
@@ -53,8 +53,8 @@ func TestTempUserCommandsAndQueries(t *testing.T) {
})
Convey("Should be able update email sent and email sent on", func() {
cmd3 := models.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code}
err := UpdateTempUserWithEmailSent(&cmd3)
cmd2 := models.UpdateTempUserWithEmailSentCommand{Code: cmd.Result.Code}
err := UpdateTempUserWithEmailSent(&cmd2)
So(err, ShouldBeNil)
query := models.GetTempUsersQuery{OrgId: 2256, Status: models.TmpUserInvitePending}
@@ -62,7 +62,21 @@ func TestTempUserCommandsAndQueries(t *testing.T) {
So(err, ShouldBeNil)
So(query.Result[0].EmailSent, ShouldBeTrue)
So(query.Result[0].EmailSentOn, ShouldHappenOnOrAfter, (query.Result[0].Created))
So(query.Result[0].EmailSentOn.UTC(), ShouldHappenOnOrAfter, query.Result[0].Created.UTC())
})
Convey("Should be able expire temp user", func() {
cmd2 := models.ExpireTempUsersCommand{OlderThan: timeNow()}
err := ExpireOldUserInvites(&cmd2)
So(err, ShouldBeNil)
So(cmd2.NumExpired, ShouldEqual, 1)
Convey("Should do nothing when no temp users to expire", func() {
cmd2 = models.ExpireTempUsersCommand{OlderThan: timeNow()}
err := ExpireOldUserInvites(&cmd2)
So(err, ShouldBeNil)
So(cmd2.NumExpired, ShouldEqual, 0)
})
})
})
})