Public Dashboards: Add audit table (#54508)

This PR adds an audit table for public dashboards allowing a user to view all public dashboards on an instance of grafana. The public dashboards team is working on a proposal for adding RBAC support to the audit table for 9.3

Co-authored-by: juanicabanas <juan.cabanas@grafana.com>
This commit is contained in:
Jeff Levin
2022-10-12 21:36:05 -08:00
committed by GitHub
co-authored by juanicabanas
parent c7c640d903
commit cc27214dca
20 changed files with 516 additions and 44 deletions
@@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards"
dashboardsDB "github.com/grafana/grafana/pkg/services/dashboards/database"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/publicdashboards/internal/tokens"
. "github.com/grafana/grafana/pkg/services/publicdashboards/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
@@ -29,6 +30,39 @@ func TestLogPrefix(t *testing.T) {
assert.Equal(t, LogPrefix, "publicdashboards.store")
}
func TestIntegrationListPublicDashboard(t *testing.T) {
sqlStore := sqlstore.InitTestDB(t, sqlstore.InitTestDBOpt{FeatureFlags: []string{featuremgmt.FlagPublicDashboards}})
dashboardStore := dashboardsDB.ProvideDashboardStore(sqlStore, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg))
publicdashboardStore := ProvideStore(sqlStore)
var orgId int64 = 1
aDash := insertTestDashboard(t, dashboardStore, "a", orgId, 0, true)
bDash := insertTestDashboard(t, dashboardStore, "b", orgId, 0, true)
cDash := insertTestDashboard(t, dashboardStore, "c", orgId, 0, true)
// these are in order of how they should be returned from ListPUblicDashboards
a := insertPublicDashboard(t, publicdashboardStore, bDash.Uid, orgId, true)
b := insertPublicDashboard(t, publicdashboardStore, cDash.Uid, orgId, true)
c := insertPublicDashboard(t, publicdashboardStore, aDash.Uid, orgId, false)
// this is case that can happen as of now, however, postgres and mysql sort
// null in the exact opposite fashion and there is no shared syntax to sort
// nulls in the same way in all 3 db's.
//d := insertPublicDashboard(t, publicdashboardStore, "missing", orgId, false)
// should not be included in response
_ = insertPublicDashboard(t, publicdashboardStore, "wrongOrgId", 777, false)
resp, err := publicdashboardStore.ListPublicDashboards(context.Background(), orgId)
require.NoError(t, err)
assert.Len(t, resp, 3)
assert.Equal(t, resp[0].Uid, a.Uid)
assert.Equal(t, resp[1].Uid, b.Uid)
assert.Equal(t, resp[2].Uid, c.Uid)
}
func TestIntegrationGetDashboard(t *testing.T) {
var sqlStore *sqlstore.SQLStore
var dashboardStore *dashboardsDB.DashboardStore
@@ -506,7 +540,7 @@ func TestIntegrationGetPublicDashboardOrgId(t *testing.T) {
})
}
// helper function insertTestDashboard
// helper function to insert a dashboard
func insertTestDashboard(t *testing.T, dashboardStore *dashboardsDB.DashboardStore, title string, orgId int64,
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
t.Helper()
@@ -527,3 +561,35 @@ func insertTestDashboard(t *testing.T, dashboardStore *dashboardsDB.DashboardSto
dash.Data.Set("uid", dash.Uid)
return dash
}
// helper function to insert a public dashboard
func insertPublicDashboard(t *testing.T, publicdashboardStore *PublicDashboardStoreImpl, dashboardUid string, orgId int64, isEnabled bool) *PublicDashboard {
ctx := context.Background()
uid, err := publicdashboardStore.GenerateNewPublicDashboardUid(ctx)
require.NoError(t, err)
accessToken, err := tokens.GenerateAccessToken()
require.NoError(t, err)
cmd := SavePublicDashboardConfigCommand{
PublicDashboard: PublicDashboard{
Uid: uid,
DashboardUid: dashboardUid,
OrgId: orgId,
IsEnabled: isEnabled,
TimeSettings: &TimeSettings{},
CreatedBy: 1,
CreatedAt: time.Now(),
AccessToken: accessToken,
},
}
err = publicdashboardStore.SavePublicDashboardConfig(ctx, cmd)
require.NoError(t, err)
pubdash, err := publicdashboardStore.GetPublicDashboardByUid(ctx, uid)
require.NoError(t, err)
return pubdash
}