From 1e5c432e1029601a664454388ae366ef69618d62 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Mon, 25 Jun 2018 12:33:13 -0700 Subject: MM-10702 Moving plugins to use hashicorp go-plugin. (#8978) * Moving plugins to use hashicorp go-plugin. * Tweaks from feedback. --- app/plugin_requests.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 app/plugin_requests.go (limited to 'app/plugin_requests.go') diff --git a/app/plugin_requests.go b/app/plugin_requests.go new file mode 100644 index 000000000..b7515d950 --- /dev/null +++ b/app/plugin_requests.go @@ -0,0 +1,75 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +package app + +import ( + "net/http" + "strings" + + "github.com/gorilla/mux" + "github.com/mattermost/mattermost-server/mlog" + "github.com/mattermost/mattermost-server/model" +) + +func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) { + if a.Plugins == nil || !*a.Config().PluginSettings.Enable { + err := model.NewAppError("ServePluginRequest", "app.plugin.disabled.app_error", nil, "Enable plugins to serve plugin requests", http.StatusNotImplemented) + a.Log.Error(err.Error()) + w.WriteHeader(err.StatusCode) + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(err.ToJson())) + return + } + + params := mux.Vars(r) + hooks, err := a.Plugins.HooksForPlugin(params["plugin_id"]) + if err != nil { + a.Log.Error("Access to route for non-existant plugin", mlog.String("missing_plugin_id", params["plugin_id"]), mlog.Err(err)) + http.NotFound(w, r) + return + } + + a.servePluginRequest(w, r, hooks.ServeHTTP) +} + +func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler http.HandlerFunc) { + token := "" + + authHeader := r.Header.Get(model.HEADER_AUTH) + if strings.HasPrefix(strings.ToUpper(authHeader), model.HEADER_BEARER+" ") { + token = authHeader[len(model.HEADER_BEARER)+1:] + } else if strings.HasPrefix(strings.ToLower(authHeader), model.HEADER_TOKEN+" ") { + token = authHeader[len(model.HEADER_TOKEN)+1:] + } else if cookie, _ := r.Cookie(model.SESSION_COOKIE_TOKEN); cookie != nil && (r.Method == "GET" || r.Header.Get(model.HEADER_REQUESTED_WITH) == model.HEADER_REQUESTED_WITH_XML) { + token = cookie.Value + } else { + token = r.URL.Query().Get("access_token") + } + + r.Header.Del("Mattermost-User-Id") + if token != "" { + if session, err := a.GetSession(token); session != nil && err == nil { + r.Header.Set("Mattermost-User-Id", session.UserId) + } + } + + cookies := r.Cookies() + r.Header.Del("Cookie") + for _, c := range cookies { + if c.Name != model.SESSION_COOKIE_TOKEN { + r.AddCookie(c) + } + } + r.Header.Del(model.HEADER_AUTH) + r.Header.Del("Referer") + + params := mux.Vars(r) + + newQuery := r.URL.Query() + newQuery.Del("access_token") + r.URL.RawQuery = newQuery.Encode() + r.URL.Path = strings.TrimPrefix(r.URL.Path, "/plugins/"+params["plugin_id"]) + + handler(w, r) +} -- cgit v1.2.3-1-g7c22 From 4c1ddcff10b359baf5728b334acb60cc3e1b1123 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 6 Jul 2018 06:07:09 -0700 Subject: MM-10703 Adding blank request context to plugin hooks for future use. (#9043) * Adding blank request context to plugin hooks for future use. * Rename RequestContext to Context * Adding context to ServeHTTP and ExecuteCommand * Fixing import cycle in test. --- app/plugin_requests.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'app/plugin_requests.go') diff --git a/app/plugin_requests.go b/app/plugin_requests.go index b7515d950..10ef758b4 100644 --- a/app/plugin_requests.go +++ b/app/plugin_requests.go @@ -10,6 +10,7 @@ import ( "github.com/gorilla/mux" "github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/plugin" ) func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) { @@ -33,7 +34,7 @@ func (a *App) ServePluginRequest(w http.ResponseWriter, r *http.Request) { a.servePluginRequest(w, r, hooks.ServeHTTP) } -func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler http.HandlerFunc) { +func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler func(*plugin.Context, http.ResponseWriter, *http.Request)) { token := "" authHeader := r.Header.Get(model.HEADER_AUTH) @@ -71,5 +72,5 @@ func (a *App) servePluginRequest(w http.ResponseWriter, r *http.Request, handler r.URL.RawQuery = newQuery.Encode() r.URL.Path = strings.TrimPrefix(r.URL.Path, "/plugins/"+params["plugin_id"]) - handler(w, r) + handler(plugin.NewBlankContext(), w, r) } -- cgit v1.2.3-1-g7c22