Skip to content

Commit

Permalink
MM-57018: support reattaching plugins (mattermost#26421)
Browse files Browse the repository at this point in the history
* ProfileImageBytes for EnsureBotOptions

* leverage plugintest.NewAPI

* fix linting

* add UpdateUserRoles to plugin api

* MM-57018: support reattaching plugins

Expose a local-only API for reattaching plugins: instead of the server starting and managing the process itself, allow the plugin to be launched externally (eg within a unit test) and reattach to an existing server instance to provide the unit test with a fully functional RPC API, sidestepping the need for mocking the plugin API in most cases.

In the future, this may become the basis for running plugins in a sidecar container.

Fixes: https://mattermost.atlassian.net/browse/MM-57018

* drop unused supervisor.pid

* factor out checkMinServerVersion

* factor out startPluginServer

* restore missing setPluginState on successful reattach

* avoid passing around a stale registeredPlugin

* inline initializePluginImplementation

* have IsValid return an error

* explicitly close rpcClient

In the case of reattached plugins, the Unix socket won't necessarily disappear leaving the muxBrokers blocked indefinitely. And `Kill()` doesn't do anything if there's no process being managed.

* explicitly detachPlugin

* emphasize gRPC not being supported

---------

Co-authored-by: Mattermost Build <[email protected]>
  • Loading branch information
lieut-data and mattermost-build authored Apr 11, 2024
1 parent f5ea554 commit 2230fb6
Show file tree
Hide file tree
Showing 12 changed files with 582 additions and 72 deletions.
47 changes: 47 additions & 0 deletions server/channels/api4/plugin_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@

package api4

import (
"encoding/json"
"net/http"

"github.com/mattermost/mattermost/server/public/model"
)

func (api *API) InitPluginLocal() {
api.BaseRoutes.Plugins.Handle("", api.APILocal(uploadPlugin, handlerParamFileAPI)).Methods("POST")
api.BaseRoutes.Plugins.Handle("", api.APILocal(getPlugins)).Methods("GET")
Expand All @@ -12,4 +19,44 @@ func (api *API) InitPluginLocal() {
api.BaseRoutes.Plugin.Handle("/disable", api.APILocal(disablePlugin)).Methods("POST")
api.BaseRoutes.Plugins.Handle("/marketplace", api.APILocal(installMarketplacePlugin)).Methods("POST")
api.BaseRoutes.Plugins.Handle("/marketplace", api.APILocal(getMarketplacePlugins)).Methods("GET")
api.BaseRoutes.Plugins.Handle("/reattach", api.APILocal(reattachPlugin)).Methods("POST")
api.BaseRoutes.Plugin.Handle("/detach", api.APILocal(detachPlugin)).Methods("POST")
}

// reattachPlugin allows the server to bind to an existing plugin instance launched elsewhere.
//
// This API is only exposed over a local socket.
func reattachPlugin(c *Context, w http.ResponseWriter, r *http.Request) {
var pluginReattachRequest model.PluginReattachRequest
if err := json.NewDecoder(r.Body).Decode(&pluginReattachRequest); err != nil {
c.Err = model.NewAppError("reattachPlugin", "api4.plugin.reattachPlugin.invalid_request", nil, err.Error(), http.StatusBadRequest)
return
}

if err := pluginReattachRequest.IsValid(); err != nil {
c.Err = err
return
}

err := c.App.ReattachPlugin(pluginReattachRequest.Manifest, pluginReattachRequest.PluginReattachConfig)
if err != nil {
c.Err = err
return
}
}

// detachPlugin detaches a previously reattached plugin.
//
// This API is only exposed over a local socket.
func detachPlugin(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequirePluginId()
if c.Err != nil {
return
}

err := c.App.DetachPlugin(c.Params.PluginId)
if err != nil {
c.Err = err
return
}
}
4 changes: 4 additions & 0 deletions server/channels/app/app_iface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions server/channels/app/opentracing/opentracing_layer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions server/channels/app/plugin_reattach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app

import (
"net/http"

"github.com/mattermost/mattermost/server/public/model"
)

// ReattachPlugin allows the server to bind to an existing plugin instance launched elsewhere.
func (a *App) ReattachPlugin(manifest *model.Manifest, pluginReattachConfig *model.PluginReattachConfig) *model.AppError {
return a.ch.ReattachPlugin(manifest, pluginReattachConfig)
}

// ReattachPlugin allows the server to bind to an existing plugin instance launched elsewhere.
func (ch *Channels) ReattachPlugin(manifest *model.Manifest, pluginReattachConfig *model.PluginReattachConfig) *model.AppError {
pluginsEnvironment := ch.GetPluginsEnvironment()
if pluginsEnvironment == nil {
return model.NewAppError("ReattachPlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
}

ch.DetachPlugin(manifest.Id)

// Reattach to the plugin
if err := pluginsEnvironment.Reattach(manifest, pluginReattachConfig); err != nil {
return model.NewAppError("ReattachPlugin", "app.plugin.reattach.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
}

return nil
}

// DetachPlugin allows the server to bind to an existing plugin instance launched elsewhere.
func (a *App) DetachPlugin(pluginId string) *model.AppError {
return a.ch.DetachPlugin(pluginId)
}

// DetachPlugin allows the server to bind to an existing plugin instance launched elsewhere.
func (ch *Channels) DetachPlugin(pluginID string) *model.AppError {
pluginsEnvironment := ch.GetPluginsEnvironment()
if pluginsEnvironment == nil {
return model.NewAppError("DetachPlugin", "app.plugin.disabled.app_error", nil, "", http.StatusNotImplemented)
}

// Deactivate and remove any existing plugin, if present.
pluginsEnvironment.Deactivate(pluginID)
pluginsEnvironment.RemovePlugin(pluginID)

return nil
}
16 changes: 16 additions & 0 deletions server/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -4614,6 +4614,10 @@
"id": "api.websocket_handler.server_busy.app_error",
"translation": "Server is busy, non-critical services are temporarily unavailable."
},
{
"id": "api4.plugin.reattachPlugin.invalid_request",
"translation": "Failed to parse request"
},
{
"id": "app.acknowledgement.delete.app_error",
"translation": "Unable to delete acknowledgement."
Expand Down Expand Up @@ -6290,6 +6294,10 @@
"id": "app.plugin.not_installed.app_error",
"translation": "Plugin is not installed."
},
{
"id": "app.plugin.reattach.app_error",
"translation": "Failed to reattach plugin"
},
{
"id": "app.plugin.remove.app_error",
"translation": "Unable to delete plugin."
Expand Down Expand Up @@ -10142,6 +10150,14 @@
"id": "plugin_api.send_mail.missing_to",
"translation": "Missing TO address."
},
{
"id": "plugin_reattach_request.is_valid.manifest.app_error",
"translation": "Missing manifest"
},
{
"id": "plugin_reattach_request.is_valid.plugin_reattach_config.app_error",
"translation": "Missing plugin reattach config"
},
{
"id": "searchengine.bleve.disabled.error",
"translation": "Error purging Bleve indexes: engine is disabled"
Expand Down
30 changes: 30 additions & 0 deletions server/public/model/client4.go
Original file line number Diff line number Diff line change
Expand Up @@ -7352,6 +7352,36 @@ func (c *Client4) InstallMarketplacePlugin(ctx context.Context, request *Install
return &m, BuildResponse(r), nil
}

// ReattachPlugin asks the server to reattach to a plugin launched by another process.
//
// Only available in local mode, and currently only used for testing.
func (c *Client4) ReattachPlugin(ctx context.Context, request *PluginReattachRequest) (*Response, error) {
buf, err := json.Marshal(request)
if err != nil {
return nil, NewAppError("ReattachPlugin", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
r, err := c.DoAPIPost(ctx, c.pluginsRoute()+"/reattach", string(buf))
if err != nil {
return BuildResponse(r), err
}
defer closeBody(r)

return BuildResponse(r), nil
}

// DetachPlugin detaches a previously reattached plugin.
//
// Only available in local mode, and currently only used for testing.
func (c *Client4) DetachPlugin(ctx context.Context, pluginID string) (*Response, error) {
r, err := c.DoAPIPost(ctx, c.pluginRoute(pluginID)+"/detach", "")
if err != nil {
return BuildResponse(r), err
}
defer closeBody(r)

return BuildResponse(r), nil
}

// GetPlugins will return a list of plugin manifests for currently active plugins.
func (c *Client4) GetPlugins(ctx context.Context) (*PluginsResponse, *Response, error) {
r, err := c.DoAPIGet(ctx, c.pluginsRoute(), "")
Expand Down
59 changes: 59 additions & 0 deletions server/public/model/plugin_reattach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package model

import (
"net"
"net/http"

"github.com/hashicorp/go-plugin"
)

// PluginReattachConfig is a serializable version of go-plugin's ReattachConfig.
type PluginReattachConfig struct {
Protocol string
ProtocolVersion int
Addr net.UnixAddr
Pid int
Test bool
}

func NewPluginReattachConfig(pluginReattachmentConfig *plugin.ReattachConfig) *PluginReattachConfig {
return &PluginReattachConfig{
Protocol: string(pluginReattachmentConfig.Protocol),
ProtocolVersion: pluginReattachmentConfig.ProtocolVersion,
Addr: net.UnixAddr{
Name: pluginReattachmentConfig.Addr.String(),
Net: pluginReattachmentConfig.Addr.Network(),
},
Pid: pluginReattachmentConfig.Pid,
Test: pluginReattachmentConfig.Test,
}
}

func (prc *PluginReattachConfig) ToHashicorpPluginReattachmentConfig() *plugin.ReattachConfig {
addr := prc.Addr

return &plugin.ReattachConfig{
Protocol: plugin.Protocol(prc.Protocol),
ProtocolVersion: prc.ProtocolVersion,
Addr: &addr,
Pid: prc.Pid,
ReattachFunc: nil,
Test: prc.Test,
}
}

type PluginReattachRequest struct {
Manifest *Manifest
PluginReattachConfig *PluginReattachConfig
}

func (prr *PluginReattachRequest) IsValid() *AppError {
if prr.Manifest == nil {
return NewAppError("PluginReattachRequest.IsValid", "plugin_reattach_request.is_valid.manifest.app_error", nil, "", http.StatusBadRequest)
}
if prr.PluginReattachConfig == nil {
return NewAppError("PluginReattachRequest.IsValid", "plugin_reattach_request.is_valid.plugin_reattach_config.app_error", nil, "", http.StatusBadRequest)
}

return nil
}
Loading

0 comments on commit 2230fb6

Please sign in to comment.