mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
PublicDashboards: validate only one public dashboard per dashboard (#66228)
This commit is contained in:
parent
18cb2ac9dd
commit
3224b4c8f0
@ -324,7 +324,7 @@ func TestIntegrationUnauthenticatedUserCanGetPubdashPanelQueryData(t *testing.T)
|
|||||||
store := publicdashboardsStore.ProvideStore(db)
|
store := publicdashboardsStore.ProvideStore(db)
|
||||||
cfg := setting.NewCfg()
|
cfg := setting.NewCfg()
|
||||||
ac := acmock.New()
|
ac := acmock.New()
|
||||||
ws := &publicdashboards.FakePublicDashboardServiceWrapper{}
|
ws := publicdashboardsService.ProvideServiceWrapper(store)
|
||||||
cfg.RBACEnabled = false
|
cfg.RBACEnabled = false
|
||||||
service := publicdashboardsService.ProvideService(cfg, store, qds, annotationsService, ac, ws)
|
service := publicdashboardsService.ProvideService(cfg, store, qds, annotationsService, ac, ws)
|
||||||
pubdash, err := service.Create(context.Background(), &user.SignedInUser{}, savePubDashboardCmd)
|
pubdash, err := service.Create(context.Background(), &user.SignedInUser{}, savePubDashboardCmd)
|
||||||
|
@ -20,6 +20,7 @@ var (
|
|||||||
ErrInvalidMaxDataPoints = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.maxDataPoints", errutil.WithPublicMessage("maxDataPoints should be greater than 0"))
|
ErrInvalidMaxDataPoints = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.maxDataPoints", errutil.WithPublicMessage("maxDataPoints should be greater than 0"))
|
||||||
ErrInvalidTimeRange = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.invalidTimeRange", errutil.WithPublicMessage("Invalid time range"))
|
ErrInvalidTimeRange = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.invalidTimeRange", errutil.WithPublicMessage("Invalid time range"))
|
||||||
ErrInvalidShareType = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.invalidShareType", errutil.WithPublicMessage("Invalid share type"))
|
ErrInvalidShareType = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.invalidShareType", errutil.WithPublicMessage("Invalid share type"))
|
||||||
|
ErrDashboardIsPublic = errutil.NewBase(errutil.StatusBadRequest, "publicdashboards.dashboardIsPublic", errutil.WithPublicMessage("Dashboard is already public"))
|
||||||
|
|
||||||
ErrPublicDashboardNotEnabled = errutil.NewBase(errutil.StatusForbidden, "publicdashboards.notEnabled", errutil.WithPublicMessage("Public dashboard paused"))
|
ErrPublicDashboardNotEnabled = errutil.NewBase(errutil.StatusForbidden, "publicdashboards.notEnabled", errutil.WithPublicMessage("Public dashboard paused"))
|
||||||
)
|
)
|
||||||
|
@ -651,7 +651,7 @@ func TestGetQueryDataResponse(t *testing.T) {
|
|||||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotatest.New(false, nil))
|
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotatest.New(false, nil))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
publicdashboardStore := database.ProvideStore(sqlStore)
|
publicdashboardStore := database.ProvideStore(sqlStore)
|
||||||
|
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
|
||||||
fakeQueryService := &query.FakeQueryService{}
|
fakeQueryService := &query.FakeQueryService{}
|
||||||
fakeQueryService.On("QueryData", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&backend.QueryDataResponse{}, nil)
|
fakeQueryService.On("QueryData", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(&backend.QueryDataResponse{}, nil)
|
||||||
|
|
||||||
@ -660,6 +660,7 @@ func TestGetQueryDataResponse(t *testing.T) {
|
|||||||
store: publicdashboardStore,
|
store: publicdashboardStore,
|
||||||
intervalCalculator: intervalv2.NewCalculator(),
|
intervalCalculator: intervalv2.NewCalculator(),
|
||||||
QueryDataService: fakeQueryService,
|
QueryDataService: fakeQueryService,
|
||||||
|
serviceWrapper: serviceWrapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
publicDashboardQueryDTO := PublicDashboardQueryDTO{
|
publicDashboardQueryDTO := PublicDashboardQueryDTO{
|
||||||
@ -1180,7 +1181,7 @@ func TestBuildMetricRequest(t *testing.T) {
|
|||||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotatest.New(false, nil))
|
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotatest.New(false, nil))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
publicdashboardStore := database.ProvideStore(sqlStore)
|
publicdashboardStore := database.ProvideStore(sqlStore)
|
||||||
|
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
|
||||||
publicDashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
publicDashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
||||||
nonPublicDashboard := insertTestDashboard(t, dashboardStore, "testNonPublicDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
nonPublicDashboard := insertTestDashboard(t, dashboardStore, "testNonPublicDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
||||||
from, to := internal.GetTimeRangeFromDashboard(t, publicDashboard.Data)
|
from, to := internal.GetTimeRangeFromDashboard(t, publicDashboard.Data)
|
||||||
@ -1189,6 +1190,7 @@ func TestBuildMetricRequest(t *testing.T) {
|
|||||||
log: log.New("test.logger"),
|
log: log.New("test.logger"),
|
||||||
store: publicdashboardStore,
|
store: publicdashboardStore,
|
||||||
intervalCalculator: intervalv2.NewCalculator(),
|
intervalCalculator: intervalv2.NewCalculator(),
|
||||||
|
serviceWrapper: serviceWrapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
publicDashboardQueryDTO := PublicDashboardQueryDTO{
|
publicDashboardQueryDTO := PublicDashboardQueryDTO{
|
||||||
|
@ -152,13 +152,14 @@ func (pd *PublicDashboardServiceImpl) Create(ctx context.Context, u *user.Signed
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify public dashboard does not exist and that we didn't get one from the
|
// validate the dashboard does not already have a public dashboard
|
||||||
// request
|
existingPubdash, err := pd.FindByDashboardUid(ctx, u.OrgID, dto.DashboardUid)
|
||||||
existingPubdash, err := pd.store.Find(ctx, dto.PublicDashboard.Uid)
|
if err != nil && !errors.Is(err, ErrPublicDashboardNotFound) {
|
||||||
if err != nil {
|
return nil, err
|
||||||
return nil, ErrInternalServerError.Errorf("Create: failed to find the public dashboard: %w", err)
|
}
|
||||||
} else if existingPubdash != nil {
|
|
||||||
return nil, ErrBadRequest.Errorf("Create: public dashboard already exists: %s", dto.PublicDashboard.Uid)
|
if existingPubdash != nil {
|
||||||
|
return nil, ErrDashboardIsPublic.Errorf("Create: public dashboard for dashboard %s already exists", dto.DashboardUid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// set default value for time settings
|
// set default value for time settings
|
||||||
@ -314,7 +315,7 @@ func (pd *PublicDashboardServiceImpl) NewPublicDashboardAccessToken(ctx context.
|
|||||||
return accessToken, nil
|
return accessToken, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", ErrInternalServerError.Errorf("failed to generate a unique accesssToken for public dashboard")
|
return "", ErrInternalServerError.Errorf("failed to generate a unique accessToken for public dashboard")
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindAll Returns a list of public dashboards by orgId
|
// FindAll Returns a list of public dashboards by orgId
|
||||||
|
@ -329,10 +329,14 @@ func TestCreatePublicDashboard(t *testing.T) {
|
|||||||
publicDashboardStore.On("FindDashboard", mock.Anything, mock.Anything, mock.Anything).Return(dashboard, nil)
|
publicDashboardStore.On("FindDashboard", mock.Anything, mock.Anything, mock.Anything).Return(dashboard, nil)
|
||||||
publicDashboardStore.On("Find", mock.Anything, mock.Anything).Return(nil, nil)
|
publicDashboardStore.On("Find", mock.Anything, mock.Anything).Return(nil, nil)
|
||||||
publicDashboardStore.On("FindByAccessToken", mock.Anything, mock.Anything).Return(pubdash, nil)
|
publicDashboardStore.On("FindByAccessToken", mock.Anything, mock.Anything).Return(pubdash, nil)
|
||||||
|
publicDashboardStore.On("FindByDashboardUid", mock.Anything, mock.Anything, mock.Anything).Return(nil, ErrPublicDashboardNotFound.Errorf(""))
|
||||||
|
|
||||||
|
serviceWrapper := ProvideServiceWrapper(publicDashboardStore)
|
||||||
|
|
||||||
service := &PublicDashboardServiceImpl{
|
service := &PublicDashboardServiceImpl{
|
||||||
log: log.New("test.logger"),
|
log: log.New("test.logger"),
|
||||||
store: publicDashboardStore,
|
store: publicDashboardStore,
|
||||||
|
serviceWrapper: serviceWrapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
dto := &SavePublicDashboardDTO{
|
dto := &SavePublicDashboardDTO{
|
||||||
@ -348,7 +352,7 @@ func TestCreatePublicDashboard(t *testing.T) {
|
|||||||
|
|
||||||
_, err := service.Create(context.Background(), SignedInUser, dto)
|
_, err := service.Create(context.Background(), SignedInUser, dto)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
require.Equal(t, err, ErrInternalServerError.Errorf("failed to generate a unique accesssToken for public dashboard"))
|
require.Equal(t, err, ErrInternalServerError.Errorf("failed to generate a unique accessToken for public dashboard"))
|
||||||
publicDashboardStore.AssertNotCalled(t, "Create")
|
publicDashboardStore.AssertNotCalled(t, "Create")
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -356,12 +360,20 @@ func TestCreatePublicDashboard(t *testing.T) {
|
|||||||
sqlStore := db.InitTestDB(t)
|
sqlStore := db.InitTestDB(t)
|
||||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotatest.New(false, nil))
|
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotatest.New(false, nil))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
publicdashboardStore := database.ProvideStore(sqlStore)
|
|
||||||
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
||||||
|
|
||||||
|
publicdashboardStore := &FakePublicDashboardStore{}
|
||||||
|
publicdashboardStore.On("FindByDashboardUid", mock.Anything, mock.Anything, mock.Anything).Return(&PublicDashboard{Uid: "newPubdashUid"}, nil)
|
||||||
|
publicdashboardStore.On("FindDashboard", mock.Anything, mock.Anything, mock.Anything).Return(dashboard, nil)
|
||||||
|
publicdashboardStore.On("Find", mock.Anything, mock.Anything).Return(nil, nil)
|
||||||
|
|
||||||
|
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
|
||||||
|
|
||||||
service := &PublicDashboardServiceImpl{
|
service := &PublicDashboardServiceImpl{
|
||||||
log: log.New("test.logger"),
|
log: log.New("test.logger"),
|
||||||
store: publicdashboardStore,
|
store: publicdashboardStore,
|
||||||
|
serviceWrapper: serviceWrapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
dto := &SavePublicDashboardDTO{
|
dto := &SavePublicDashboardDTO{
|
||||||
@ -376,30 +388,9 @@ func TestCreatePublicDashboard(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
savedPubdash, err := service.Create(context.Background(), SignedInUser, dto)
|
savedPubdash, err := service.Create(context.Background(), SignedInUser, dto)
|
||||||
require.NoError(t, err)
|
assert.Error(t, err)
|
||||||
|
assert.Nil(t, savedPubdash)
|
||||||
// attempt to overwrite settings
|
assert.True(t, ErrDashboardIsPublic.Is(err))
|
||||||
dto = &SavePublicDashboardDTO{
|
|
||||||
DashboardUid: dashboard.UID,
|
|
||||||
OrgId: dashboard.OrgID,
|
|
||||||
UserId: 8,
|
|
||||||
PublicDashboard: &PublicDashboard{
|
|
||||||
Uid: savedPubdash.Uid,
|
|
||||||
OrgId: 9,
|
|
||||||
DashboardUid: "abc1234",
|
|
||||||
CreatedBy: 9,
|
|
||||||
CreatedAt: time.Time{},
|
|
||||||
|
|
||||||
IsEnabled: true,
|
|
||||||
AnnotationsEnabled: true,
|
|
||||||
TimeSettings: timeSettings,
|
|
||||||
AccessToken: "NOTAREALUUID",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = service.Create(context.Background(), SignedInUser, dto)
|
|
||||||
require.Error(t, err)
|
|
||||||
assert.True(t, ErrBadRequest.Is(err))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Validate pubdash has default share value", func(t *testing.T) {
|
t.Run("Validate pubdash has default share value", func(t *testing.T) {
|
||||||
@ -445,11 +436,13 @@ func TestUpdatePublicDashboard(t *testing.T) {
|
|||||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
|
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
publicdashboardStore := database.ProvideStore(sqlStore)
|
publicdashboardStore := database.ProvideStore(sqlStore)
|
||||||
|
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
|
||||||
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
||||||
|
|
||||||
service := &PublicDashboardServiceImpl{
|
service := &PublicDashboardServiceImpl{
|
||||||
log: log.New("test.logger"),
|
log: log.New("test.logger"),
|
||||||
store: publicdashboardStore,
|
store: publicdashboardStore,
|
||||||
|
serviceWrapper: serviceWrapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
dto := &SavePublicDashboardDTO{
|
dto := &SavePublicDashboardDTO{
|
||||||
@ -512,11 +505,14 @@ func TestUpdatePublicDashboard(t *testing.T) {
|
|||||||
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
|
dashboardStore, err := dashboardsDB.ProvideDashboardStore(sqlStore, sqlStore.Cfg, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg), quotaService)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
publicdashboardStore := database.ProvideStore(sqlStore)
|
publicdashboardStore := database.ProvideStore(sqlStore)
|
||||||
|
serviceWrapper := ProvideServiceWrapper(publicdashboardStore)
|
||||||
|
|
||||||
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
dashboard := insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true, []map[string]interface{}{}, nil)
|
||||||
|
|
||||||
service := &PublicDashboardServiceImpl{
|
service := &PublicDashboardServiceImpl{
|
||||||
log: log.New("test.logger"),
|
log: log.New("test.logger"),
|
||||||
store: publicdashboardStore,
|
store: publicdashboardStore,
|
||||||
|
serviceWrapper: serviceWrapper,
|
||||||
}
|
}
|
||||||
|
|
||||||
dto := &SavePublicDashboardDTO{
|
dto := &SavePublicDashboardDTO{
|
||||||
|
Loading…
Reference in New Issue
Block a user