From 8449017592bbb746bddc83321a41deeef12cb74c Mon Sep 17 00:00:00 2001 From: woodsaj Date: Fri, 27 Nov 2015 15:21:57 +0800 Subject: [PATCH 1/5] Add plugin type field to externalPlugin model --- pkg/plugins/models.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index 49156739b5a..565f7eafccd 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -51,6 +51,7 @@ type ExternalPluginCss struct { } type ExternalPlugin struct { + Type string `json:"type"` Routes []*ExternalPluginRoute `json:"routes"` Js []*ExternalPluginJs `json:"js"` Css []*ExternalPluginCss `json:"css"` From 700b77c450c276d5faddc095958fe6803f6e1cc6 Mon Sep 17 00:00:00 2001 From: woodsaj Date: Fri, 27 Nov 2015 16:26:30 +0800 Subject: [PATCH 2/5] implement role access checks on external-plugin routes. --- pkg/api/externalplugin.go | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/pkg/api/externalplugin.go b/pkg/api/externalplugin.go index e4a7b8639c1..331de160ec7 100644 --- a/pkg/api/externalplugin.go +++ b/pkg/api/externalplugin.go @@ -9,33 +9,32 @@ import ( "github.com/Unknwon/macaron" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" + m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/util" ) func InitExternalPluginRoutes(r *macaron.Macaron) { - /* - // Handle Auth and role requirements - if route.ReqSignedIn { - c.Invoke(middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})) - } - if route.ReqGrafanaAdmin { - c.Invoke(middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})) - } - if route.ReqRole != nil { - if *route.ReqRole == m.ROLE_EDITOR { - c.Invoke(middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)) - } - if *route.ReqRole == m.ROLE_ADMIN { - c.Invoke(middleware.RoleAuth(m.ROLE_ADMIN)) - } - } - */ for _, plugin := range plugins.ExternalPlugins { log.Info("Plugin: Adding proxy routes for backend plugin") for _, route := range plugin.Routes { url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path) - r.Route(url, route.Method, ExternalPlugin(route.Url)) + handlers := make([]macaron.Handler, 0) + if route.ReqSignedIn { + handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true})) + } + if route.ReqGrafanaAdmin { + handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true})) + } + if route.ReqSignedIn && route.ReqRole != "" { + if route.ReqRole == m.ROLE_ADMIN { + handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN)) + } else if route.ReqRole == m.ROLE_EDITOR { + handlers = append(handlers, middleware.RoleAuth(m.ROLE_EDITOR, m.ROLE_ADMIN)) + } + } + handlers = append(handlers, ExternalPlugin(route.Url)) + r.Route(url, route.Method, handlers...) log.Info("Plugin: Adding route %s", url) } } From 1b5c40dd1fe8ef101fd72e4d3d5b2509477bfb24 Mon Sep 17 00:00:00 2001 From: woodsaj Date: Fri, 27 Nov 2015 16:27:14 +0800 Subject: [PATCH 3/5] add role access limitions for menu items. This allows external-plugin menu items to conditionally be added to the UI depending on the logged in users current role. --- pkg/api/index.go | 23 ++++++++++++++++++++++- pkg/plugins/models.go | 7 ++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/api/index.go b/pkg/api/index.go index fb18045a18a..efc0fd7a69a 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -70,7 +70,28 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { data.PluginCss = append(data.PluginCss, css.Href) } for _, item := range plugin.MainNavLinks { - data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: item.Text, Href: item.Href, Icon: item.Icon}) + // only show menu items for the specified roles. + var validRoles []m.RoleType + if string(item.ReqRole) == "" || item.ReqRole == m.ROLE_VIEWER { + validRoles = []m.RoleType{m.ROLE_ADMIN, m.ROLE_EDITOR, m.ROLE_VIEWER} + } else if item.ReqRole == m.ROLE_EDITOR { + validRoles = []m.RoleType{m.ROLE_ADMIN, m.ROLE_EDITOR} + } else if item.ReqRole == m.ROLE_ADMIN { + validRoles = []m.RoleType{m.ROLE_ADMIN} + } + ok := true + if len(validRoles) > 0 { + ok = false + for _, role := range validRoles { + if role == c.OrgRole { + ok = true + break + } + } + } + if ok { + data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: item.Text, Href: item.Href, Icon: item.Icon}) + } } } diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index 565f7eafccd..14a4407adad 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -41,9 +41,10 @@ type ExternalPluginJs struct { } type ExternalPluginNavLink struct { - Text string `json:"text"` - Icon string `json:"icon"` - Href string `json:"href"` + Text string `json:"text"` + Icon string `json:"icon"` + Href string `json:"href"` + ReqRole models.RoleType `json:"reqRole"` } type ExternalPluginCss struct { From 4f6a52503d952c25af21949a19177932b81e44be Mon Sep 17 00:00:00 2001 From: woodsaj Date: Fri, 27 Nov 2015 17:04:43 +0800 Subject: [PATCH 4/5] fix plugin unit test --- pkg/plugins/plugins_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/plugins/plugins_test.go b/pkg/plugins/plugins_test.go index 4d3e2c98836..d0027ccec64 100644 --- a/pkg/plugins/plugins_test.go +++ b/pkg/plugins/plugins_test.go @@ -4,14 +4,15 @@ import ( "path/filepath" "testing" + "github.com/grafana/grafana/pkg/setting" . "github.com/smartystreets/goconvey/convey" ) func TestPluginScans(t *testing.T) { Convey("When scaning for plugins", t, func() { - path, _ := filepath.Abs("../../public/app/plugins") - err := scan(path) + setting.StaticRootPath = filepath.Abs("../../public/") + err := Init() So(err, ShouldBeNil) So(len(DataSources), ShouldBeGreaterThan, 1) From 79d29db18b8aecb274ce4bdc4ca5d306898f2e9d Mon Sep 17 00:00:00 2001 From: woodsaj Date: Fri, 27 Nov 2015 17:17:27 +0800 Subject: [PATCH 5/5] really fix unit tests. --- pkg/plugins/plugins_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/plugins/plugins_test.go b/pkg/plugins/plugins_test.go index d0027ccec64..bbeac4bba81 100644 --- a/pkg/plugins/plugins_test.go +++ b/pkg/plugins/plugins_test.go @@ -6,12 +6,14 @@ import ( "github.com/grafana/grafana/pkg/setting" . "github.com/smartystreets/goconvey/convey" + "gopkg.in/ini.v1" ) func TestPluginScans(t *testing.T) { Convey("When scaning for plugins", t, func() { - setting.StaticRootPath = filepath.Abs("../../public/") + setting.StaticRootPath, _ = filepath.Abs("../../public/") + setting.Cfg = ini.Empty() err := Init() So(err, ShouldBeNil)