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_api.go | 146 +++++------------------------------------------------- 1 file changed, 13 insertions(+), 133 deletions(-) (limited to 'app/plugin_api.go') diff --git a/app/plugin_api.go b/app/plugin_api.go index 06eeee146..ffb8cccd6 100644 --- a/app/plugin_api.go +++ b/app/plugin_api.go @@ -5,27 +5,22 @@ package app import ( "encoding/json" - "net/http" - "strings" - "github.com/gorilla/mux" "github.com/mattermost/mattermost-server/model" - "github.com/mattermost/mattermost-server/utils" - - "github.com/mattermost/mattermost-server/plugin" ) type PluginAPI struct { - id string - app *App - keyValueStore *PluginKeyValueStore -} - -type PluginKeyValueStore struct { id string app *App } +func NewPluginAPI(a *App, manifest *model.Manifest) *PluginAPI { + return &PluginAPI{ + id: manifest.Id, + app: a, + } +} + func (api *PluginAPI) LoadPluginConfiguration(dest interface{}) error { if b, err := json.Marshal(api.app.Config().PluginSettings.Plugins[api.id]); err != nil { return err @@ -170,129 +165,14 @@ func (api *PluginAPI) UpdatePost(post *model.Post) (*model.Post, *model.AppError return api.app.UpdatePost(post, false) } -func (api *PluginAPI) KeyValueStore() plugin.KeyValueStore { - return api.keyValueStore -} - -func (s *PluginKeyValueStore) Set(key string, value []byte) *model.AppError { - return s.app.SetPluginKey(s.id, key, value) +func (api *PluginAPI) KVSet(key string, value []byte) *model.AppError { + return api.app.SetPluginKey(api.id, key, value) } -func (s *PluginKeyValueStore) Get(key string) ([]byte, *model.AppError) { - return s.app.GetPluginKey(s.id, key) -} - -func (s *PluginKeyValueStore) Delete(key string) *model.AppError { - return s.app.DeletePluginKey(s.id, key) -} - -type BuiltInPluginAPI struct { - id string - router *mux.Router - app *App -} - -func (api *BuiltInPluginAPI) LoadPluginConfiguration(dest interface{}) error { - if b, err := json.Marshal(api.app.Config().PluginSettings.Plugins[api.id]); err != nil { - return err - } else { - return json.Unmarshal(b, dest) - } -} - -func (api *BuiltInPluginAPI) PluginRouter() *mux.Router { - return api.router -} - -func (api *BuiltInPluginAPI) GetTeamByName(name string) (*model.Team, *model.AppError) { - return api.app.GetTeamByName(name) -} - -func (api *BuiltInPluginAPI) GetUserByName(name string) (*model.User, *model.AppError) { - return api.app.GetUserByUsername(name) -} - -func (api *BuiltInPluginAPI) GetChannelByName(teamId, name string) (*model.Channel, *model.AppError) { - return api.app.GetChannelByName(name, teamId) -} - -func (api *BuiltInPluginAPI) GetDirectChannel(userId1, userId2 string) (*model.Channel, *model.AppError) { - return api.app.GetDirectChannel(userId1, userId2) +func (api *PluginAPI) KVGet(key string) ([]byte, *model.AppError) { + return api.app.GetPluginKey(api.id, key) } -func (api *BuiltInPluginAPI) CreatePost(post *model.Post) (*model.Post, *model.AppError) { - return api.app.CreatePostMissingChannel(post, true) -} - -func (api *BuiltInPluginAPI) GetLdapUserAttributes(userId string, attributes []string) (map[string]string, *model.AppError) { - if api.app.Ldap == nil { - return nil, model.NewAppError("GetLdapUserAttributes", "ent.ldap.disabled.app_error", nil, "", http.StatusNotImplemented) - } - - user, err := api.app.GetUser(userId) - if err != nil { - return nil, err - } - - if user.AuthData == nil { - return map[string]string{}, nil - } - - return api.app.Ldap.GetUserAttributes(*user.AuthData, attributes) -} - -func (api *BuiltInPluginAPI) GetSessionFromRequest(r *http.Request) (*model.Session, *model.AppError) { - token := "" - isTokenFromQueryString := false - - // Attempt to parse token out of the header - authHeader := r.Header.Get(model.HEADER_AUTH) - if len(authHeader) > 6 && strings.ToUpper(authHeader[0:6]) == model.HEADER_BEARER { - // Default session token - token = authHeader[7:] - - } else if len(authHeader) > 5 && strings.ToLower(authHeader[0:5]) == model.HEADER_TOKEN { - // OAuth token - token = authHeader[6:] - } - - // Attempt to parse the token from the cookie - if len(token) == 0 { - if cookie, err := r.Cookie(model.SESSION_COOKIE_TOKEN); err == nil { - token = cookie.Value - - if r.Header.Get(model.HEADER_REQUESTED_WITH) != model.HEADER_REQUESTED_WITH_XML { - return nil, model.NewAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token+" Appears to be a CSRF attempt", http.StatusUnauthorized) - } - } - } - - // Attempt to parse token out of the query string - if len(token) == 0 { - token = r.URL.Query().Get("access_token") - isTokenFromQueryString = true - } - - if len(token) == 0 { - return nil, model.NewAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token, http.StatusUnauthorized) - } - - session, err := api.app.GetSession(token) - - if err != nil { - return nil, model.NewAppError("ServeHTTP", "api.context.session_expired.app_error", nil, "token="+token, http.StatusUnauthorized) - } else if !session.IsOAuth && isTokenFromQueryString { - return nil, model.NewAppError("ServeHTTP", "api.context.token_provided.app_error", nil, "token="+token, http.StatusUnauthorized) - } - - return session, nil -} - -func (api *BuiltInPluginAPI) I18n(id string, r *http.Request) string { - if r != nil { - f, _ := utils.GetTranslationsAndLocale(nil, r) - return f(id) - } - f, _ := utils.GetTranslationsBySystemLocale() - return f(id) +func (api *PluginAPI) KVDelete(key string) *model.AppError { + return api.app.DeletePluginKey(api.id, key) } -- cgit v1.2.3-1-g7c22