Adding interplugin communication. (#12829)

* Adding interplugin communication.

* Naming changes and moving ResponseTransfer to own file.

* Fix.

* Tests and moving to buffering bytes.

* Switching API to passing plugin ID through path rather than a header.

* Review feedback.
Этот коммит содержится в:
Christopher Speller
2019-11-04 17:35:58 -08:00
коммит произвёл GitHub
родитель 501da809f3
Коммит 428454cee4
9 изменённых файлов: 351 добавлений и 19 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package plugin
import (
"io"
"net/http"
plugin "github.com/hashicorp/go-plugin"
"github.com/mattermost/mattermost-server/model"
@@ -747,6 +748,11 @@ type API interface {
//
// Minimum server version: 5.14
DeleteBotIconImage(botUserId string) *model.AppError
// PluginHTTP allows inter-plugin requests to plugin APIs.
//
// Minimum server version: 5.18
PluginHTTP(request *http.Request) *http.Response
}
var handshake = plugin.HandshakeConfig{

Просмотреть файл

@@ -56,11 +56,13 @@ func (p *hooksPlugin) Client(b *plugin.MuxBroker, client *rpc.Client) (interface
}
type apiRPCClient struct {
client *rpc.Client
client *rpc.Client
muxBroker *plugin.MuxBroker
}
type apiRPCServer struct {
impl API
impl API
muxBroker *plugin.MuxBroker
}
// ErrorString is a fallback for sending unregistered implementations of the error interface across
@@ -171,7 +173,8 @@ type Z_OnActivateReturns struct {
func (g *hooksRPCClient) OnActivate() error {
muxId := g.muxBroker.NextId()
go g.muxBroker.AcceptAndServe(muxId, &apiRPCServer{
impl: g.apiImpl,
impl: g.apiImpl,
muxBroker: g.muxBroker,
})
_args := &Z_OnActivateArgs{
@@ -192,7 +195,8 @@ func (s *hooksRPCServer) OnActivate(args *Z_OnActivateArgs, returns *Z_OnActivat
}
s.apiRPCClient = &apiRPCClient{
client: rpc.NewClient(connection),
client: rpc.NewClient(connection),
muxBroker: s.muxBroker,
}
if mmplugin, ok := s.impl.(interface {
@@ -363,6 +367,76 @@ func (s *hooksRPCServer) ServeHTTP(args *Z_ServeHTTPArgs, returns *struct{}) err
return nil
}
type Z_PluginHTTPArgs struct {
Request *http.Request
RequestBody []byte
}
type Z_PluginHTTPReturns struct {
Response *http.Response
ResponseBody []byte
}
func (g *apiRPCClient) PluginHTTP(request *http.Request) *http.Response {
forwardedRequest := &http.Request{
Method: request.Method,
URL: request.URL,
Proto: request.Proto,
ProtoMajor: request.ProtoMajor,
ProtoMinor: request.ProtoMinor,
Header: request.Header,
Host: request.Host,
RemoteAddr: request.RemoteAddr,
RequestURI: request.RequestURI,
}
requestBody, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
return nil
}
request.Body.Close()
request.Body = nil
_args := &Z_PluginHTTPArgs{
Request: forwardedRequest,
RequestBody: requestBody,
}
_returns := &Z_PluginHTTPReturns{}
if err := g.client.Call("Plugin.PluginHTTP", _args, _returns); err != nil {
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
return nil
}
_returns.Response.Body = ioutil.NopCloser(bytes.NewBuffer(_returns.ResponseBody))
return _returns.Response
}
func (s *apiRPCServer) PluginHTTP(args *Z_PluginHTTPArgs, returns *Z_PluginHTTPReturns) error {
args.Request.Body = ioutil.NopCloser(bytes.NewBuffer(args.RequestBody))
if hook, ok := s.impl.(interface {
PluginHTTP(request *http.Request) *http.Response
}); ok {
response := hook.PluginHTTP(args.Request)
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return encodableError(fmt.Errorf("RPC call to PluginHTTP API failed: %s", err.Error()))
}
response.Body.Close()
response.Body = nil
returns.Response = response
returns.ResponseBody = responseBody
} else {
return encodableError(fmt.Errorf("API PluginHTTP called but not implemented."))
}
return nil
}
func init() {
hookNameToId["FileWillBeUploaded"] = FileWillBeUploadedId
}

Просмотреть файл

@@ -12,4 +12,5 @@ type Context struct {
IpAddress string
AcceptLanguage string
UserAgent string
SourcePluginId string
}

Просмотреть файл

@@ -392,17 +392,18 @@ func getPluginPackageDir() string {
func removeExcluded(info *PluginInterfaceInfo) *PluginInterfaceInfo {
toBeExcluded := func(item string) bool {
excluded := []string{
"OnActivate",
"FileWillBeUploaded",
"Implemented",
"LoadPluginConfiguration",
"ServeHTTP",
"FileWillBeUploaded",
"MessageWillBePosted",
"MessageWillBeUpdated",
"LogDebug",
"LogError",
"LogInfo",
"LogWarn",
"LogError",
"MessageWillBePosted",
"MessageWillBeUpdated",
"OnActivate",
"PluginHTTP",
"ServeHTTP",
}
for _, exclusion := range excluded {
if exclusion == item {

Просмотреть файл

@@ -6,9 +6,11 @@ package plugintest
import (
io "io"
http "net/http"
mock "github.com/stretchr/testify/mock"
model "github.com/mattermost/mattermost-server/model"
mock "github.com/stretchr/testify/mock"
)
// API is an autogenerated mock type for the API type
@@ -2334,6 +2336,22 @@ func (_m *API) PermanentDeleteBot(botUserId string) *model.AppError {
return r0
}
// PluginHTTP provides a mock function with given fields: request
func (_m *API) PluginHTTP(request *http.Request) *http.Response {
ret := _m.Called(request)
var r0 *http.Response
if rf, ok := ret.Get(0).(func(*http.Request) *http.Response); ok {
r0 = rf(request)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*http.Response)
}
}
return r0
}
// PublishWebSocketEvent provides a mock function with given fields: event, payload, broadcast
func (_m *API) PublishWebSocketEvent(event string, payload map[string]interface{}, broadcast *model.WebsocketBroadcast) {
_m.Called(event, payload, broadcast)