PublicDashboards: Variables refactor (#73476)
Co-authored-by: Juan Cabanas <juan.cabanas@grafana.com> Co-authored-by: Ezequiel Victorero <ezequiel.victorero@grafana.com> Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
co-authored by
Juan Cabanas
Ezequiel Victorero
Ryan McKinley
parent
2245a3d0d1
commit
3ee26df41e
@@ -61,19 +61,19 @@ func CopyWithReqContext(ctx context.Context) context.Context {
|
||||
Resp: web.NewResponseWriter(origReqCtx.Req.Method, response.CreateNormalResponse(http.Header{}, []byte{}, 0)),
|
||||
}
|
||||
reqCtx := &contextmodel.ReqContext{
|
||||
Context: webCtx,
|
||||
SignedInUser: origReqCtx.SignedInUser,
|
||||
UserToken: origReqCtx.UserToken,
|
||||
IsSignedIn: origReqCtx.IsSignedIn,
|
||||
IsRenderCall: origReqCtx.IsRenderCall,
|
||||
AllowAnonymous: origReqCtx.AllowAnonymous,
|
||||
SkipDSCache: origReqCtx.SkipDSCache,
|
||||
SkipQueryCache: origReqCtx.SkipQueryCache,
|
||||
Logger: origReqCtx.Logger,
|
||||
Error: origReqCtx.Error,
|
||||
RequestNonce: origReqCtx.RequestNonce,
|
||||
IsPublicDashboardView: origReqCtx.IsPublicDashboardView,
|
||||
LookupTokenErr: origReqCtx.LookupTokenErr,
|
||||
Context: webCtx,
|
||||
SignedInUser: origReqCtx.SignedInUser,
|
||||
UserToken: origReqCtx.UserToken,
|
||||
IsSignedIn: origReqCtx.IsSignedIn,
|
||||
IsRenderCall: origReqCtx.IsRenderCall,
|
||||
AllowAnonymous: origReqCtx.AllowAnonymous,
|
||||
SkipDSCache: origReqCtx.SkipDSCache,
|
||||
SkipQueryCache: origReqCtx.SkipQueryCache,
|
||||
Logger: origReqCtx.Logger,
|
||||
Error: origReqCtx.Error,
|
||||
RequestNonce: origReqCtx.RequestNonce,
|
||||
PublicDashboardAccessToken: origReqCtx.PublicDashboardAccessToken,
|
||||
LookupTokenErr: origReqCtx.LookupTokenErr,
|
||||
}
|
||||
return context.WithValue(ctx, reqContextKey{}, reqCtx)
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ type ReqContext struct {
|
||||
Logger log.Logger
|
||||
Error error
|
||||
// RequestNonce is a cryptographic request identifier for use with Content Security Policy.
|
||||
RequestNonce string
|
||||
IsPublicDashboardView bool
|
||||
RequestNonce string
|
||||
PublicDashboardAccessToken string
|
||||
|
||||
PerfmonTimer prometheus.Summary
|
||||
LookupTokenErr error
|
||||
@@ -60,6 +60,10 @@ func (ctx *ReqContext) IsApiRequest() bool {
|
||||
return strings.HasPrefix(ctx.Req.URL.Path, "/api")
|
||||
}
|
||||
|
||||
func (ctx *ReqContext) IsPublicDashboardView() bool {
|
||||
return ctx.PublicDashboardAccessToken != ""
|
||||
}
|
||||
|
||||
func (ctx *ReqContext) JsonApiErr(status int, message string, err error) {
|
||||
resp := make(map[string]interface{})
|
||||
traceID := tracing.TraceIDFromContext(ctx.Req.Context(), false)
|
||||
|
||||
@@ -95,7 +95,7 @@ func (s *ServiceImpl) GetNavTree(c *contextmodel.ReqContext, prefs *pref.Prefere
|
||||
})
|
||||
}
|
||||
|
||||
if c.IsPublicDashboardView || hasAccess(ac.EvalAny(
|
||||
if c.IsPublicDashboardView() || hasAccess(ac.EvalAny(
|
||||
ac.EvalPermission(dashboards.ActionFoldersRead), ac.EvalPermission(dashboards.ActionFoldersCreate),
|
||||
ac.EvalPermission(dashboards.ActionDashboardsRead), ac.EvalPermission(dashboards.ActionDashboardsCreate)),
|
||||
) {
|
||||
|
||||
@@ -46,16 +46,16 @@ func TestAlertingProxy_createProxyContext(t *testing.T) {
|
||||
Context: &web.Context{
|
||||
Req: &http.Request{},
|
||||
},
|
||||
SignedInUser: &user.SignedInUser{},
|
||||
UserToken: &auth.UserToken{},
|
||||
IsSignedIn: rand.Int63()%2 == 1,
|
||||
IsRenderCall: rand.Int63()%2 == 1,
|
||||
AllowAnonymous: rand.Int63()%2 == 1,
|
||||
SkipDSCache: rand.Int63()%2 == 1,
|
||||
SkipQueryCache: rand.Int63()%2 == 1,
|
||||
Logger: log.New("test"),
|
||||
RequestNonce: util.GenerateShortUID(),
|
||||
IsPublicDashboardView: rand.Int63()%2 == 1,
|
||||
SignedInUser: &user.SignedInUser{},
|
||||
UserToken: &auth.UserToken{},
|
||||
IsSignedIn: rand.Int63()%2 == 1,
|
||||
IsRenderCall: rand.Int63()%2 == 1,
|
||||
AllowAnonymous: rand.Int63()%2 == 1,
|
||||
SkipDSCache: rand.Int63()%2 == 1,
|
||||
SkipQueryCache: rand.Int63()%2 == 1,
|
||||
Logger: log.New("test"),
|
||||
RequestNonce: util.GenerateShortUID(),
|
||||
PublicDashboardAccessToken: util.GenerateShortUID(),
|
||||
}
|
||||
|
||||
t.Run("should create a copy of request context", func(t *testing.T) {
|
||||
@@ -81,7 +81,7 @@ func TestAlertingProxy_createProxyContext(t *testing.T) {
|
||||
require.Equal(t, ctx.SkipQueryCache, newCtx.SkipQueryCache)
|
||||
require.Equal(t, ctx.Logger, newCtx.Logger)
|
||||
require.Equal(t, ctx.RequestNonce, newCtx.RequestNonce)
|
||||
require.Equal(t, ctx.IsPublicDashboardView, newCtx.IsPublicDashboardView)
|
||||
require.Equal(t, ctx.PublicDashboardAccessToken, newCtx.PublicDashboardAccessToken)
|
||||
}
|
||||
})
|
||||
t.Run("should overwrite response writer", func(t *testing.T) {
|
||||
|
||||
@@ -36,7 +36,7 @@ var ResourceCachingRequestHistogram = prometheus.NewHistogramVec(prometheus.Hist
|
||||
}, []string{"plugin_id", "cache"})
|
||||
|
||||
func getQueryType(req *contextmodel.ReqContext) string {
|
||||
if req.IsPublicDashboardView {
|
||||
if req.IsPublicDashboardView() {
|
||||
return QueryPubdash
|
||||
}
|
||||
return QueryDashboard
|
||||
|
||||
@@ -28,9 +28,9 @@ func SetPublicDashboardOrgIdOnContext(publicDashboardService publicdashboards.Se
|
||||
}
|
||||
}
|
||||
|
||||
// SetPublicDashboardFlag Adds public dashboard flag on context
|
||||
func SetPublicDashboardFlag(c *contextmodel.ReqContext) {
|
||||
c.IsPublicDashboardView = true
|
||||
// SetPublicDashboardAccessToken Adds public dashboard flag on context
|
||||
func SetPublicDashboardAccessToken(c *contextmodel.ReqContext) {
|
||||
c.PublicDashboardAccessToken = web.Params(c.Req)[":accessToken"]
|
||||
}
|
||||
|
||||
// RequiresExistingAccessToken Middleware to enforce that a public dashboards exists before continuing to handler. This
|
||||
|
||||
@@ -145,10 +145,10 @@ func TestSetPublicDashboardOrgIdOnContext(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSetPublicDashboardFlag(t *testing.T) {
|
||||
t.Run("Adds context.IsPublicDashboardView=true to request", func(t *testing.T) {
|
||||
ctx := &contextmodel.ReqContext{}
|
||||
SetPublicDashboardFlag(ctx)
|
||||
assert.True(t, ctx.IsPublicDashboardView)
|
||||
t.Run("Adds context.PublicDashboardAccessToken to request", func(t *testing.T) {
|
||||
ctx := &contextmodel.ReqContext{Context: &web.Context{Req: web.SetURLParams(&http.Request{}, map[string]string{":accessToken": "asdfasdfasdfsadfasdfsfd"})}}
|
||||
SetPublicDashboardAccessToken(ctx)
|
||||
assert.NotEmpty(t, ctx.PublicDashboardAccessToken)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -30,21 +30,21 @@ func (api *Api) ViewPublicDashboard(c *contextmodel.ReqContext) response.Respons
|
||||
}
|
||||
|
||||
meta := dtos.DashboardMeta{
|
||||
Slug: dash.Slug,
|
||||
Type: dashboards.DashTypeDB,
|
||||
CanStar: false,
|
||||
CanSave: false,
|
||||
CanEdit: false,
|
||||
CanAdmin: false,
|
||||
CanDelete: false,
|
||||
Created: dash.Created,
|
||||
Updated: dash.Updated,
|
||||
Version: dash.Version,
|
||||
IsFolder: false,
|
||||
FolderId: dash.FolderID,
|
||||
PublicDashboardAccessToken: pubdash.AccessToken,
|
||||
PublicDashboardEnabled: pubdash.IsEnabled,
|
||||
Slug: dash.Slug,
|
||||
Type: dashboards.DashTypeDB,
|
||||
CanStar: false,
|
||||
CanSave: false,
|
||||
CanEdit: false,
|
||||
CanAdmin: false,
|
||||
CanDelete: false,
|
||||
Created: dash.Created,
|
||||
Updated: dash.Updated,
|
||||
Version: dash.Version,
|
||||
IsFolder: false,
|
||||
FolderId: dash.FolderID,
|
||||
PublicDashboardEnabled: pubdash.IsEnabled,
|
||||
}
|
||||
|
||||
dash.Data.Get("timepicker").Set("hidden", !pubdash.TimeSelectionEnabled)
|
||||
|
||||
dto := dtos.DashboardFullWithMeta{Meta: meta, Dashboard: dash.Data}
|
||||
|
||||
@@ -287,11 +287,10 @@ func TestQueryDataMultipleSources(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
queries := []*simplejson.Json{query1, query2}
|
||||
reqDTO := dtos.MetricRequest{
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
PublicDashboardAccessToken: "abc123",
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "http://localhost:3000", nil)
|
||||
@@ -351,11 +350,10 @@ func TestQueryDataMultipleSources(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
queries := []*simplejson.Json{query1, query2, query3}
|
||||
reqDTO := dtos.MetricRequest{
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
PublicDashboardAccessToken: "abc123",
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
}
|
||||
|
||||
// without query parameter
|
||||
@@ -406,11 +404,10 @@ func TestQueryDataMultipleSources(t *testing.T) {
|
||||
queries := []*simplejson.Json{query1, query2}
|
||||
|
||||
reqDTO := dtos.MetricRequest{
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
PublicDashboardAccessToken: "abc123",
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
}
|
||||
|
||||
res, err := tc.queryService.QueryData(context.Background(), tc.signedInUser, true, reqDTO)
|
||||
@@ -436,11 +433,10 @@ func TestQueryDataMultipleSources(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
queries := []*simplejson.Json{query1}
|
||||
reqDTO := dtos.MetricRequest{
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
PublicDashboardAccessToken: "abc123",
|
||||
From: "2022-01-01",
|
||||
To: "2022-01-02",
|
||||
Queries: queries,
|
||||
Debug: false,
|
||||
}
|
||||
|
||||
_, err = tc.queryService.QueryData(context.Background(), tc.signedInUser, true, reqDTO)
|
||||
|
||||
Reference in New Issue
Block a user