2018-06-25 15:33:13 -04:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
|
|
//go:generate go run interface_generator/main.go
|
|
|
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
"database/sql"
|
|
|
|
|
"database/sql/driver"
|
2018-06-25 15:33:13 -04:00
|
|
|
"encoding/gob"
|
|
|
|
|
"encoding/json"
|
2018-07-03 12:58:28 -04:00
|
|
|
"fmt"
|
2018-07-27 08:25:53 -04:00
|
|
|
"io"
|
2018-07-03 12:58:28 -04:00
|
|
|
"log"
|
2018-06-25 15:33:13 -04:00
|
|
|
"net/http"
|
|
|
|
|
"net/rpc"
|
2025-03-11 13:44:42 -04:00
|
|
|
"net/url"
|
2018-07-03 12:58:28 -04:00
|
|
|
"os"
|
2018-06-25 15:33:13 -04:00
|
|
|
"reflect"
|
MM-37165: Fix improper plugin shutdown (#18044)
* MM-37165: Fix improper plugin shutdown
This was caught from a race test failure. While the failure manifested due to a log being
written from a test after the test exited, the real reason was hidden further deeper.
What was happening is that the server would always listen for plugin requests in a
separate goroutine via `g.muxBroker.AcceptAndServe` in the `OnActivate` hook. But the
plugin shutdown process would just close the plugin connections and move on, leading
to the classic case of improperly shut down goroutines.
When this happened, an opportunity opens up in a way that the server
would still be executing a request whereas the main goroutine and therefore the parent
test has already finished. This would lead to an error like
```
{"level":"error","ts":1626451258.4141135,"caller":"mlog/sugar.go:25","msg":"pluginAPI scheduleOnce poller encountered an error but is still polling","plugin_id":"com.mattermost.plugin-incident-management","error":"ListPluginKeys: Unable to list all the plugin keys., failed to get PluginKeyValues with pluginId=com.mattermost.plugin-incident-management: sql: database is closed
```
And now, this finally calls `mlog.Error`, which finally triggers our race condition :)
To fix this, we use some basic synchronization via waitgroups and just wait for it
to finish after closing the plugin process.
https://mattermost.atlassian.net/browse/MM-37165
```release-note
NONE
```
* gofmt
```release-note
NONE
```
* split waitgroup additions
```release-note
NONE
```
2021-08-10 00:37:35 -04:00
|
|
|
"sync"
|
2018-06-25 15:33:13 -04:00
|
|
|
|
2019-10-29 23:17:04 -04:00
|
|
|
"github.com/hashicorp/go-plugin"
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
"github.com/lib/pq"
|
2021-01-07 12:12:43 -05:00
|
|
|
|
2023-06-11 01:24:35 -04:00
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
2018-06-25 15:33:13 -04:00
|
|
|
)
|
|
|
|
|
|
2023-06-13 04:38:36 -04:00
|
|
|
var hookNameToId = make(map[string]int)
|
2018-06-25 15:33:13 -04:00
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type hooksRPCClient struct {
|
2018-06-25 15:33:13 -04:00
|
|
|
client *rpc.Client
|
|
|
|
|
log *mlog.Logger
|
|
|
|
|
muxBroker *plugin.MuxBroker
|
|
|
|
|
apiImpl API
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
driver Driver
|
2021-02-18 09:36:56 -05:00
|
|
|
implemented [TotalHooksID]bool
|
MM-37165: Fix improper plugin shutdown (#18044)
* MM-37165: Fix improper plugin shutdown
This was caught from a race test failure. While the failure manifested due to a log being
written from a test after the test exited, the real reason was hidden further deeper.
What was happening is that the server would always listen for plugin requests in a
separate goroutine via `g.muxBroker.AcceptAndServe` in the `OnActivate` hook. But the
plugin shutdown process would just close the plugin connections and move on, leading
to the classic case of improperly shut down goroutines.
When this happened, an opportunity opens up in a way that the server
would still be executing a request whereas the main goroutine and therefore the parent
test has already finished. This would lead to an error like
```
{"level":"error","ts":1626451258.4141135,"caller":"mlog/sugar.go:25","msg":"pluginAPI scheduleOnce poller encountered an error but is still polling","plugin_id":"com.mattermost.plugin-incident-management","error":"ListPluginKeys: Unable to list all the plugin keys., failed to get PluginKeyValues with pluginId=com.mattermost.plugin-incident-management: sql: database is closed
```
And now, this finally calls `mlog.Error`, which finally triggers our race condition :)
To fix this, we use some basic synchronization via waitgroups and just wait for it
to finish after closing the plugin process.
https://mattermost.atlassian.net/browse/MM-37165
```release-note
NONE
```
* gofmt
```release-note
NONE
```
* split waitgroup additions
```release-note
NONE
```
2021-08-10 00:37:35 -04:00
|
|
|
doneWg sync.WaitGroup
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type hooksRPCServer struct {
|
2022-07-05 02:46:50 -04:00
|
|
|
impl any
|
2018-06-25 15:33:13 -04:00
|
|
|
muxBroker *plugin.MuxBroker
|
2018-07-13 10:29:50 -04:00
|
|
|
apiRPCClient *apiRPCClient
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implements hashicorp/go-plugin/plugin.Plugin interface to connect the hooks of a plugin
|
2018-07-13 10:29:50 -04:00
|
|
|
type hooksPlugin struct {
|
2022-07-05 02:46:50 -04:00
|
|
|
hooks any
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
apiImpl API
|
|
|
|
|
driverImpl Driver
|
|
|
|
|
log *mlog.Logger
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (p *hooksPlugin) Server(b *plugin.MuxBroker) (any, error) {
|
2018-07-13 10:29:50 -04:00
|
|
|
return &hooksRPCServer{impl: p.hooks, muxBroker: b}, nil
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (p *hooksPlugin) Client(b *plugin.MuxBroker, client *rpc.Client) (any, error) {
|
2025-03-11 13:44:42 -04:00
|
|
|
return &hooksRPCClient{
|
|
|
|
|
client: client,
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
log: p.log,
|
|
|
|
|
muxBroker: b,
|
|
|
|
|
apiImpl: p.apiImpl,
|
|
|
|
|
driver: p.driverImpl,
|
|
|
|
|
}, nil
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type apiRPCClient struct {
|
2019-11-04 20:35:58 -05:00
|
|
|
client *rpc.Client
|
|
|
|
|
muxBroker *plugin.MuxBroker
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
type apiRPCServer struct {
|
2019-11-04 20:35:58 -05:00
|
|
|
impl API
|
|
|
|
|
muxBroker *plugin.MuxBroker
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-13 14:31:22 -04:00
|
|
|
// ErrorString is a fallback for sending unregistered implementations of the error interface across
|
|
|
|
|
// rpc. For example, the errorString type from the github.com/pkg/errors package cannot be
|
|
|
|
|
// registered since it is not exported, but this precludes common error handling paradigms.
|
|
|
|
|
// ErrorString merely preserves the string description of the error, while satisfying the error
|
|
|
|
|
// interface itself to allow other registered types (such as model.AppError) to be sent unmodified.
|
|
|
|
|
type ErrorString struct {
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
Code int // Code to map to various error variables
|
|
|
|
|
Err string
|
2018-09-13 14:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e ErrorString) Error() string {
|
|
|
|
|
return e.Err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func encodableError(err error) error {
|
|
|
|
|
if err == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if _, ok := err.(*model.AppError); ok {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
if _, ok := err.(*pq.Error); ok {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret := &ErrorString{
|
2018-09-13 14:31:22 -04:00
|
|
|
Err: err.Error(),
|
|
|
|
|
}
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
|
|
|
|
|
switch err {
|
|
|
|
|
case io.EOF:
|
|
|
|
|
ret.Code = 1
|
|
|
|
|
case sql.ErrNoRows:
|
|
|
|
|
ret.Code = 2
|
|
|
|
|
case sql.ErrConnDone:
|
|
|
|
|
ret.Code = 3
|
|
|
|
|
case sql.ErrTxDone:
|
|
|
|
|
ret.Code = 4
|
|
|
|
|
case driver.ErrSkip:
|
|
|
|
|
ret.Code = 5
|
|
|
|
|
case driver.ErrBadConn:
|
|
|
|
|
ret.Code = 6
|
|
|
|
|
case driver.ErrRemoveArgument:
|
|
|
|
|
ret.Code = 7
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func decodableError(err error) error {
|
|
|
|
|
if encErr, ok := err.(*ErrorString); ok {
|
|
|
|
|
switch encErr.Code {
|
|
|
|
|
case 1:
|
|
|
|
|
return io.EOF
|
|
|
|
|
case 2:
|
|
|
|
|
return sql.ErrNoRows
|
|
|
|
|
case 3:
|
|
|
|
|
return sql.ErrConnDone
|
|
|
|
|
case 4:
|
|
|
|
|
return sql.ErrTxDone
|
|
|
|
|
case 5:
|
|
|
|
|
return driver.ErrSkip
|
|
|
|
|
case 6:
|
|
|
|
|
return driver.ErrBadConn
|
|
|
|
|
case 7:
|
|
|
|
|
return driver.ErrRemoveArgument
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return err
|
2018-09-13 14:31:22 -04:00
|
|
|
}
|
|
|
|
|
|
2018-06-25 15:33:13 -04:00
|
|
|
// Registering some types used by MM for encoding/gob used by rpc
|
|
|
|
|
func init() {
|
|
|
|
|
gob.Register([]*model.SlackAttachment{})
|
2022-07-05 02:46:50 -04:00
|
|
|
gob.Register([]any{})
|
|
|
|
|
gob.Register(map[string]any{})
|
2018-07-31 10:34:40 -04:00
|
|
|
gob.Register(&model.AppError{})
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
gob.Register(&pq.Error{})
|
2018-09-13 14:31:22 -04:00
|
|
|
gob.Register(&ErrorString{})
|
2020-05-21 04:24:56 -04:00
|
|
|
gob.Register(&model.AutocompleteDynamicListArg{})
|
|
|
|
|
gob.Register(&model.AutocompleteStaticListArg{})
|
|
|
|
|
gob.Register(&model.AutocompleteTextArg{})
|
2021-09-23 13:27:09 -04:00
|
|
|
gob.Register(&model.PreviewPost{})
|
2025-06-10 19:10:28 -04:00
|
|
|
gob.Register(model.PropertyOptions[*model.PluginPropertyOption]{})
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// These enforce compile time checks to make sure types implement the interface
|
|
|
|
|
// If you are getting an error here, you probably need to run `make pluginapi` to
|
|
|
|
|
// autogenerate RPC glue code
|
2025-03-11 13:44:42 -04:00
|
|
|
var (
|
|
|
|
|
_ plugin.Plugin = &hooksPlugin{}
|
|
|
|
|
_ Hooks = &hooksRPCClient{}
|
|
|
|
|
)
|
2018-06-25 15:33:13 -04:00
|
|
|
|
|
|
|
|
//
|
2020-02-14 15:47:43 -05:00
|
|
|
// Below are special cases for hooks or APIs that can not be auto generated
|
2018-06-25 15:33:13 -04:00
|
|
|
//
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (g *hooksRPCClient) Implemented() (impl []string, err error) {
|
2018-06-25 15:33:13 -04:00
|
|
|
err = g.client.Call("Plugin.Implemented", struct{}{}, &impl)
|
|
|
|
|
for _, hookName := range impl {
|
2021-03-23 05:32:54 -04:00
|
|
|
if hookId, ok := hookNameToId[hookName]; ok {
|
|
|
|
|
g.implemented[hookId] = true
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implemented replies with the names of the hooks that are implemented.
|
2018-07-13 10:29:50 -04:00
|
|
|
func (s *hooksRPCServer) Implemented(args struct{}, reply *[]string) error {
|
2025-11-04 06:09:11 -05:00
|
|
|
ifaceType := reflect.TypeFor[Hooks]()
|
2018-06-25 15:33:13 -04:00
|
|
|
implType := reflect.TypeOf(s.impl)
|
2025-11-04 06:09:11 -05:00
|
|
|
selfType := reflect.TypeFor[*hooksRPCServer]()
|
2018-06-25 15:33:13 -04:00
|
|
|
var methods []string
|
|
|
|
|
for i := 0; i < ifaceType.NumMethod(); i++ {
|
|
|
|
|
method := ifaceType.Method(i)
|
2024-03-22 00:53:21 -04:00
|
|
|
m, ok := implType.MethodByName(method.Name)
|
|
|
|
|
if !ok {
|
2018-06-25 15:33:13 -04:00
|
|
|
continue
|
|
|
|
|
} else if m.Type.NumIn() != method.Type.NumIn()+1 {
|
|
|
|
|
continue
|
|
|
|
|
} else if m.Type.NumOut() != method.Type.NumOut() {
|
|
|
|
|
continue
|
2024-03-22 00:53:21 -04:00
|
|
|
}
|
|
|
|
|
match := true
|
|
|
|
|
for j := 0; j < method.Type.NumIn(); j++ {
|
|
|
|
|
if m.Type.In(j+1) != method.Type.In(j) {
|
|
|
|
|
match = false
|
|
|
|
|
break
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
2024-03-22 00:53:21 -04:00
|
|
|
}
|
|
|
|
|
for j := 0; j < method.Type.NumOut(); j++ {
|
|
|
|
|
if m.Type.Out(j) != method.Type.Out(j) {
|
|
|
|
|
match = false
|
|
|
|
|
break
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-22 00:53:21 -04:00
|
|
|
if !match {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-06-25 15:33:13 -04:00
|
|
|
if _, ok := selfType.MethodByName(method.Name); !ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
methods = append(methods, method.Name)
|
|
|
|
|
}
|
|
|
|
|
*reply = methods
|
2018-09-13 14:31:22 -04:00
|
|
|
return encodableError(nil)
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_OnActivateArgs struct {
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
APIMuxId uint32
|
|
|
|
|
DriverMuxId uint32
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_OnActivateReturns struct {
|
2018-06-25 15:33:13 -04:00
|
|
|
A error
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (g *hooksRPCClient) OnActivate() error {
|
2021-03-23 05:32:54 -04:00
|
|
|
muxId := g.muxBroker.NextId()
|
MM-37165: Fix improper plugin shutdown (#18044)
* MM-37165: Fix improper plugin shutdown
This was caught from a race test failure. While the failure manifested due to a log being
written from a test after the test exited, the real reason was hidden further deeper.
What was happening is that the server would always listen for plugin requests in a
separate goroutine via `g.muxBroker.AcceptAndServe` in the `OnActivate` hook. But the
plugin shutdown process would just close the plugin connections and move on, leading
to the classic case of improperly shut down goroutines.
When this happened, an opportunity opens up in a way that the server
would still be executing a request whereas the main goroutine and therefore the parent
test has already finished. This would lead to an error like
```
{"level":"error","ts":1626451258.4141135,"caller":"mlog/sugar.go:25","msg":"pluginAPI scheduleOnce poller encountered an error but is still polling","plugin_id":"com.mattermost.plugin-incident-management","error":"ListPluginKeys: Unable to list all the plugin keys., failed to get PluginKeyValues with pluginId=com.mattermost.plugin-incident-management: sql: database is closed
```
And now, this finally calls `mlog.Error`, which finally triggers our race condition :)
To fix this, we use some basic synchronization via waitgroups and just wait for it
to finish after closing the plugin process.
https://mattermost.atlassian.net/browse/MM-37165
```release-note
NONE
```
* gofmt
```release-note
NONE
```
* split waitgroup additions
```release-note
NONE
```
2021-08-10 00:37:35 -04:00
|
|
|
g.doneWg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer g.doneWg.Done()
|
|
|
|
|
g.muxBroker.AcceptAndServe(muxId, &apiRPCServer{
|
|
|
|
|
impl: g.apiImpl,
|
|
|
|
|
muxBroker: g.muxBroker,
|
|
|
|
|
})
|
|
|
|
|
}()
|
2018-06-25 15:33:13 -04:00
|
|
|
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
nextID := g.muxBroker.NextId()
|
MM-37165: Fix improper plugin shutdown (#18044)
* MM-37165: Fix improper plugin shutdown
This was caught from a race test failure. While the failure manifested due to a log being
written from a test after the test exited, the real reason was hidden further deeper.
What was happening is that the server would always listen for plugin requests in a
separate goroutine via `g.muxBroker.AcceptAndServe` in the `OnActivate` hook. But the
plugin shutdown process would just close the plugin connections and move on, leading
to the classic case of improperly shut down goroutines.
When this happened, an opportunity opens up in a way that the server
would still be executing a request whereas the main goroutine and therefore the parent
test has already finished. This would lead to an error like
```
{"level":"error","ts":1626451258.4141135,"caller":"mlog/sugar.go:25","msg":"pluginAPI scheduleOnce poller encountered an error but is still polling","plugin_id":"com.mattermost.plugin-incident-management","error":"ListPluginKeys: Unable to list all the plugin keys., failed to get PluginKeyValues with pluginId=com.mattermost.plugin-incident-management: sql: database is closed
```
And now, this finally calls `mlog.Error`, which finally triggers our race condition :)
To fix this, we use some basic synchronization via waitgroups and just wait for it
to finish after closing the plugin process.
https://mattermost.atlassian.net/browse/MM-37165
```release-note
NONE
```
* gofmt
```release-note
NONE
```
* split waitgroup additions
```release-note
NONE
```
2021-08-10 00:37:35 -04:00
|
|
|
g.doneWg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer g.doneWg.Done()
|
|
|
|
|
g.muxBroker.AcceptAndServe(nextID, &dbRPCServer{
|
|
|
|
|
dbImpl: g.driver,
|
|
|
|
|
})
|
|
|
|
|
}()
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_OnActivateArgs{
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
APIMuxId: muxId,
|
|
|
|
|
DriverMuxId: nextID,
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
2021-03-23 05:32:54 -04:00
|
|
|
_returns := &Z_OnActivateReturns{}
|
2018-06-25 15:33:13 -04:00
|
|
|
|
|
|
|
|
if err := g.client.Call("Plugin.OnActivate", _args, _returns); err != nil {
|
|
|
|
|
g.log.Error("RPC call to OnActivate plugin failed.", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
return _returns.A
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *hooksRPCServer) OnActivate(args *Z_OnActivateArgs, returns *Z_OnActivateReturns) error {
|
|
|
|
|
connection, err := s.muxBroker.Dial(args.APIMuxId)
|
2018-06-25 15:33:13 -04:00
|
|
|
if err != nil {
|
2018-07-03 12:58:28 -04:00
|
|
|
return err
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
conn2, err := s.muxBroker.Dial(args.DriverMuxId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
s.apiRPCClient = &apiRPCClient{
|
2019-11-04 20:35:58 -05:00
|
|
|
client: rpc.NewClient(connection),
|
|
|
|
|
muxBroker: s.muxBroker,
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
dbClient := &dbRPCClient{
|
|
|
|
|
client: rpc.NewClient(conn2),
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 15:33:13 -04:00
|
|
|
if mmplugin, ok := s.impl.(interface {
|
2019-05-08 14:54:52 -04:00
|
|
|
SetAPI(api API)
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
SetDriver(driver Driver)
|
2018-10-03 13:13:19 -04:00
|
|
|
}); ok {
|
2019-05-08 14:54:52 -04:00
|
|
|
mmplugin.SetAPI(s.apiRPCClient)
|
DB driver implementation via RPC (#17779)
This PR builds up on the pass-through DB driver to a fully functioning DB driver implementation via our RPC layer.
To keep things separate from the plugin RPC API, and have the ability to move fast with changes, a separate field Driver is added to MattermostPlugin. Typically the field which is required to be compatible are the API and Helpers. It would be well-documented that Driver is purely for internal use by Mattermost plugins.
A new Driver interface was created which would have a client and server implementation. Every object (connection, statement, etc.) is created and added to a map on the server side. On the client side, the wrapper structs hold the object id, and communicate via the RPC API using this id.
When the server gets the object id, it picks up the appropriate object from its map and performs the operation, and sends back the data.
Some things that need to be handled are errors. Typical error types like pq.Error and mysql.MySQLError are registered with encoding/gob. But for error variables like sql.ErrNoRows, a special integer is encoded with the ErrorString struct. And on the cilent side, the integer is checked, and the appropriate error variable is returned.
Some pending things:
- Context support. This is tricky. Since context.Context is an interface, it's not possible to marshal it. We have to find a way to get the timeout value from the context and pass it.
- RowsColumnScanType(rowsID string, index int) reflect.Type API. Again, reflect.Type is an interface.
- Master/Replica API support.
2021-06-16 23:23:52 -04:00
|
|
|
mmplugin.SetDriver(dbClient)
|
2018-10-03 13:13:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if mmplugin, ok := s.impl.(interface {
|
|
|
|
|
OnConfigurationChange() error
|
|
|
|
|
}); ok {
|
|
|
|
|
if err := mmplugin.OnConfigurationChange(); err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] call to OnConfigurationChange failed, error: %v", err.Error())
|
|
|
|
|
}
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2018-07-03 12:58:28 -04:00
|
|
|
// Capture output of standard logger because go-plugin
|
|
|
|
|
// redirects it.
|
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
|
|
2018-06-25 15:33:13 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
OnActivate() error
|
|
|
|
|
}); ok {
|
2018-09-13 14:31:22 -04:00
|
|
|
returns.A = encodableError(hook.OnActivate())
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
type Z_LoadPluginConfigurationArgsArgs struct{}
|
2018-06-25 15:33:13 -04:00
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_LoadPluginConfigurationArgsReturns struct {
|
2018-06-25 15:33:13 -04:00
|
|
|
A []byte
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (g *apiRPCClient) LoadPluginConfiguration(dest any) error {
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_LoadPluginConfigurationArgsArgs{}
|
|
|
|
|
_returns := &Z_LoadPluginConfigurationArgsReturns{}
|
2018-06-25 15:33:13 -04:00
|
|
|
if err := g.client.Call("Plugin.LoadPluginConfiguration", _args, _returns); err != nil {
|
2018-07-27 11:17:29 -04:00
|
|
|
log.Printf("RPC call to LoadPluginConfiguration API failed: %s", err.Error())
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
2018-07-27 08:25:53 -04:00
|
|
|
if err := json.Unmarshal(_returns.A, dest); err != nil {
|
2018-07-27 12:57:17 -04:00
|
|
|
log.Printf("LoadPluginConfiguration API failed to unmarshal: %s", err.Error())
|
2018-07-27 08:25:53 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) LoadPluginConfiguration(args *Z_LoadPluginConfigurationArgsArgs, returns *Z_LoadPluginConfigurationArgsReturns) error {
|
2022-07-05 02:46:50 -04:00
|
|
|
var config any
|
2018-06-25 15:33:13 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
2022-07-05 02:46:50 -04:00
|
|
|
LoadPluginConfiguration(dest any) error
|
2018-06-25 15:33:13 -04:00
|
|
|
}); ok {
|
|
|
|
|
if err := hook.LoadPluginConfiguration(&config); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
returns.A = b
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2021-02-18 09:36:56 -05:00
|
|
|
hookNameToId["ServeHTTP"] = ServeHTTPID
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
// Using a subset of http.Request prevents a known incompatibility when decoding Go v1.23+ gob-encoded x509.Certificate
|
|
|
|
|
// structs from Go v1.22 compiled plugins. These come from http.Request.TLS field (*tls.ConnectionState).
|
|
|
|
|
type HTTPRequestSubset struct {
|
|
|
|
|
Method string
|
|
|
|
|
URL *url.URL
|
|
|
|
|
Proto string
|
|
|
|
|
ProtoMajor int
|
|
|
|
|
ProtoMinor int
|
|
|
|
|
Header http.Header
|
|
|
|
|
Host string
|
|
|
|
|
RemoteAddr string
|
|
|
|
|
RequestURI string
|
|
|
|
|
Body io.ReadCloser
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *HTTPRequestSubset) GetHTTPRequest() *http.Request {
|
|
|
|
|
return &http.Request{
|
|
|
|
|
Method: r.Method,
|
|
|
|
|
URL: r.URL,
|
|
|
|
|
Proto: r.Proto,
|
|
|
|
|
ProtoMajor: r.ProtoMajor,
|
|
|
|
|
ProtoMinor: r.ProtoMinor,
|
|
|
|
|
Header: r.Header,
|
|
|
|
|
Host: r.Host,
|
|
|
|
|
RemoteAddr: r.RemoteAddr,
|
|
|
|
|
RequestURI: r.RequestURI,
|
|
|
|
|
Body: r.Body,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_ServeHTTPArgs struct {
|
2018-06-25 15:33:13 -04:00
|
|
|
ResponseWriterStream uint32
|
2025-03-11 13:44:42 -04:00
|
|
|
Request *HTTPRequestSubset
|
2018-07-06 09:07:09 -04:00
|
|
|
Context *Context
|
2018-06-25 15:33:13 -04:00
|
|
|
RequestBodyStream uint32
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-13 10:29:50 -04:00
|
|
|
func (g *hooksRPCClient) ServeHTTP(c *Context, w http.ResponseWriter, r *http.Request) {
|
2021-02-18 09:36:56 -05:00
|
|
|
if !g.implemented[ServeHTTPID] {
|
2018-06-25 15:33:13 -04:00
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
serveHTTPStreamId := g.muxBroker.NextId()
|
2018-06-25 15:33:13 -04:00
|
|
|
go func() {
|
2021-03-23 05:32:54 -04:00
|
|
|
connection, err := g.muxBroker.Accept(serveHTTPStreamId)
|
2018-06-25 15:33:13 -04:00
|
|
|
if err != nil {
|
2023-11-23 04:30:08 -05:00
|
|
|
g.log.Error("Plugin failed to ServeHTTP, muxBroker couldn't accept connection", mlog.Uint("serve_http_stream_id", serveHTTPStreamId), mlog.Err(err))
|
2018-06-25 15:33:13 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
|
|
rpcServer := rpc.NewServer()
|
2019-10-31 13:27:49 -04:00
|
|
|
if err := rpcServer.RegisterName("Plugin", &httpResponseWriterRPCServer{w: w, log: g.log}); err != nil {
|
|
|
|
|
g.log.Error("Plugin failed to ServeHTTP, couldn't register RPC name", mlog.Err(err))
|
2018-06-25 15:33:13 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
rpcServer.ServeConn(connection)
|
|
|
|
|
}()
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
requestBodyStreamId := uint32(0)
|
2018-06-25 15:33:13 -04:00
|
|
|
if r.Body != nil {
|
2021-03-23 05:32:54 -04:00
|
|
|
requestBodyStreamId = g.muxBroker.NextId()
|
2018-06-25 15:33:13 -04:00
|
|
|
go func() {
|
2021-03-23 05:32:54 -04:00
|
|
|
bodyConnection, err := g.muxBroker.Accept(requestBodyStreamId)
|
2018-06-25 15:33:13 -04:00
|
|
|
if err != nil {
|
2018-09-05 08:26:03 -04:00
|
|
|
g.log.Error("Plugin failed to ServeHTTP, muxBroker couldn't Accept request body connection", mlog.Err(err))
|
2018-06-25 15:33:13 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer bodyConnection.Close()
|
2018-07-13 10:29:50 -04:00
|
|
|
serveIOReader(r.Body, bodyConnection)
|
2018-06-25 15:33:13 -04:00
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
forwardedRequest := &HTTPRequestSubset{
|
2018-06-25 15:33:13 -04:00
|
|
|
Method: r.Method,
|
|
|
|
|
URL: r.URL,
|
|
|
|
|
Proto: r.Proto,
|
|
|
|
|
ProtoMajor: r.ProtoMajor,
|
|
|
|
|
ProtoMinor: r.ProtoMinor,
|
|
|
|
|
Header: r.Header,
|
|
|
|
|
Host: r.Host,
|
|
|
|
|
RemoteAddr: r.RemoteAddr,
|
|
|
|
|
RequestURI: r.RequestURI,
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
if err := g.client.Call("Plugin.ServeHTTP", Z_ServeHTTPArgs{
|
2018-07-06 09:07:09 -04:00
|
|
|
Context: c,
|
2021-03-23 05:32:54 -04:00
|
|
|
ResponseWriterStream: serveHTTPStreamId,
|
2018-06-25 15:33:13 -04:00
|
|
|
Request: forwardedRequest,
|
2021-03-23 05:32:54 -04:00
|
|
|
RequestBodyStream: requestBodyStreamId,
|
2018-06-25 15:33:13 -04:00
|
|
|
}, nil); err != nil {
|
2018-07-03 12:58:28 -04:00
|
|
|
g.log.Error("Plugin failed to ServeHTTP, RPC call failed", mlog.Err(err))
|
2018-06-25 15:33:13 -04:00
|
|
|
http.Error(w, "500 internal server error", http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *hooksRPCServer) ServeHTTP(args *Z_ServeHTTPArgs, returns *struct{}) error {
|
2018-06-25 15:33:13 -04:00
|
|
|
connection, err := s.muxBroker.Dial(args.ResponseWriterStream)
|
|
|
|
|
if err != nil {
|
2018-07-03 12:58:28 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote response writer stream, error: %v", err.Error())
|
2018-06-25 15:33:13 -04:00
|
|
|
return err
|
|
|
|
|
}
|
2018-07-13 10:29:50 -04:00
|
|
|
w := connectHTTPResponseWriter(connection)
|
2018-06-25 15:33:13 -04:00
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
|
|
r := args.Request
|
|
|
|
|
if args.RequestBodyStream != 0 {
|
|
|
|
|
connection, err := s.muxBroker.Dial(args.RequestBodyStream)
|
|
|
|
|
if err != nil {
|
2018-07-03 12:58:28 -04:00
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote request body stream, error: %v", err.Error())
|
2018-06-25 15:33:13 -04:00
|
|
|
return err
|
|
|
|
|
}
|
2018-07-13 10:29:50 -04:00
|
|
|
r.Body = connectIOReader(connection)
|
2018-06-25 15:33:13 -04:00
|
|
|
} else {
|
2022-08-09 07:25:46 -04:00
|
|
|
r.Body = io.NopCloser(&bytes.Buffer{})
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
httpReq := r.GetHTTPRequest()
|
|
|
|
|
|
2018-07-06 09:07:09 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
ServeHTTP(c *Context, w http.ResponseWriter, r *http.Request)
|
|
|
|
|
}); ok {
|
2025-03-11 13:44:42 -04:00
|
|
|
hook.ServeHTTP(args.Context, w, httpReq)
|
2018-06-25 15:33:13 -04:00
|
|
|
} else {
|
2025-03-11 13:44:42 -04:00
|
|
|
http.NotFound(w, httpReq)
|
2018-06-25 15:33:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-07-27 08:25:53 -04:00
|
|
|
|
2025-11-05 09:21:13 -05:00
|
|
|
// PluginHTTPStream - Streaming version of PluginHTTP that uses MuxBroker for streaming request/response bodies.
|
|
|
|
|
// This avoids buffering large payloads in memory.
|
|
|
|
|
|
|
|
|
|
// Legacy buffered structs (kept for backward compatibility with old servers)
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_PluginHTTPArgs struct {
|
2025-03-11 13:44:42 -04:00
|
|
|
Request *HTTPRequestSubset
|
2019-11-04 20:35:58 -05:00
|
|
|
RequestBody []byte
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_PluginHTTPReturns struct {
|
2019-11-04 20:35:58 -05:00
|
|
|
Response *http.Response
|
|
|
|
|
ResponseBody []byte
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 09:21:13 -05:00
|
|
|
// New streaming structs
|
|
|
|
|
type Z_PluginHTTPStreamArgs struct {
|
|
|
|
|
ResponseBodyStream uint32
|
|
|
|
|
Request *HTTPRequestSubset
|
|
|
|
|
RequestBodyStream uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_PluginHTTPStreamReturns struct {
|
|
|
|
|
StatusCode int
|
|
|
|
|
Header http.Header
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:35:58 -05:00
|
|
|
func (g *apiRPCClient) PluginHTTP(request *http.Request) *http.Response {
|
2025-11-05 09:21:13 -05:00
|
|
|
// Try to use the streaming version first (if server supports it)
|
|
|
|
|
// Fall back to buffered version if not available (signaled by nil)
|
|
|
|
|
response, err := g.pluginHTTPStream(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// If we error for some other reason other than stream not being
|
|
|
|
|
// implemented just report and fail
|
|
|
|
|
log.Print(err.Error())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if response != nil {
|
|
|
|
|
return response
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fallback to buffered version
|
|
|
|
|
return g.pluginHTTPBuffered(request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pluginHTTPStream attempts to use the new streaming endpoint
|
|
|
|
|
func (g *apiRPCClient) pluginHTTPStream(request *http.Request) (*http.Response, error) {
|
|
|
|
|
// Set up request body stream
|
|
|
|
|
requestBodyStreamId := uint32(0)
|
|
|
|
|
if request.Body != nil {
|
|
|
|
|
requestBodyStreamId = g.muxBroker.NextId()
|
|
|
|
|
go func() {
|
|
|
|
|
bodyConnection, err := g.muxBroker.Accept(requestBodyStreamId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Plugin failed to accept request body connection for PluginHTTPStream: %s", err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer bodyConnection.Close()
|
|
|
|
|
serveIOReader(request.Body, bodyConnection)
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set up response body stream
|
|
|
|
|
responseBodyStreamId := g.muxBroker.NextId()
|
|
|
|
|
responsePipe := make(chan io.ReadCloser, 1)
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
connection, err := g.muxBroker.Accept(responseBodyStreamId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("Plugin failed to accept response body connection for PluginHTTPStream: %s", err.Error())
|
|
|
|
|
responsePipe <- nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// Don't close connection here - it will be closed when response body is read
|
|
|
|
|
responsePipe <- connectIOReader(connection)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
forwardedRequest := &HTTPRequestSubset{
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_args := &Z_PluginHTTPStreamArgs{
|
|
|
|
|
ResponseBodyStream: responseBodyStreamId,
|
|
|
|
|
Request: forwardedRequest,
|
|
|
|
|
RequestBodyStream: requestBodyStreamId,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_returns := &Z_PluginHTTPStreamReturns{}
|
|
|
|
|
if err := g.client.Call("Plugin.PluginHTTPStream", _args, _returns); err != nil {
|
|
|
|
|
// If the method doesn't exist, return nil to trigger fallback
|
|
|
|
|
if err.Error() == "rpc: can't find method Plugin.PluginHTTPStream" {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("RPC call to PluginHTTPStream API failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wait for response body reader
|
|
|
|
|
responseBody := <-responsePipe
|
|
|
|
|
if responseBody == nil {
|
|
|
|
|
return nil, fmt.Errorf("Failed to get response body stream for PluginHTTPStream")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create response with streamed body
|
|
|
|
|
response := &http.Response{
|
|
|
|
|
StatusCode: _returns.StatusCode,
|
|
|
|
|
Header: _returns.Header,
|
|
|
|
|
Body: responseBody,
|
|
|
|
|
Proto: request.Proto,
|
|
|
|
|
ProtoMajor: request.ProtoMajor,
|
|
|
|
|
ProtoMinor: request.ProtoMinor,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pluginHTTPBuffered is the original buffered implementation
|
|
|
|
|
func (g *apiRPCClient) pluginHTTPBuffered(request *http.Request) *http.Response {
|
2025-03-11 13:44:42 -04:00
|
|
|
forwardedRequest := &HTTPRequestSubset{
|
2019-11-04 20:35:58 -05:00
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-14 09:07:31 -04:00
|
|
|
_args := &Z_PluginHTTPArgs{
|
|
|
|
|
Request: forwardedRequest,
|
2019-11-04 20:35:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-07-14 09:07:31 -04:00
|
|
|
if request.Body != nil {
|
2022-08-09 07:25:46 -04:00
|
|
|
requestBody, err := io.ReadAll(request.Body)
|
2021-07-14 09:07:31 -04:00
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
request.Body.Close()
|
|
|
|
|
request.Body = nil
|
|
|
|
|
|
|
|
|
|
_args.RequestBody = requestBody
|
2019-11-04 20:35:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
_returns := &Z_PluginHTTPReturns{}
|
2019-11-04 20:35:58 -05:00
|
|
|
if err := g.client.Call("Plugin.PluginHTTP", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to PluginHTTP API failed: %s", err.Error())
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-09 07:25:46 -04:00
|
|
|
_returns.Response.Body = io.NopCloser(bytes.NewBuffer(_returns.ResponseBody))
|
2019-11-04 20:35:58 -05:00
|
|
|
|
|
|
|
|
return _returns.Response
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-05 09:21:13 -05:00
|
|
|
func (s *apiRPCServer) PluginHTTPStream(args *Z_PluginHTTPStreamArgs, returns *Z_PluginHTTPStreamReturns) error {
|
|
|
|
|
responseConnection, err := s.muxBroker.Dial(args.ResponseBodyStream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return encodableError(fmt.Errorf("can't connect to remote response body stream: %w", err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect to request body stream
|
|
|
|
|
r := args.Request
|
|
|
|
|
if args.RequestBodyStream != 0 {
|
|
|
|
|
requestConnection, err := s.muxBroker.Dial(args.RequestBodyStream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return encodableError(fmt.Errorf("can't connect to remote request body stream: %w", err))
|
|
|
|
|
}
|
|
|
|
|
r.Body = connectIOReader(requestConnection)
|
|
|
|
|
} else {
|
|
|
|
|
r.Body = io.NopCloser(&bytes.Buffer{})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
httpReq := r.GetHTTPRequest()
|
|
|
|
|
|
|
|
|
|
// Call the PluginHTTP implementation
|
|
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
PluginHTTP(request *http.Request) *http.Response
|
|
|
|
|
}); ok {
|
|
|
|
|
response := hook.PluginHTTP(httpReq)
|
|
|
|
|
if response != nil {
|
|
|
|
|
returns.StatusCode = response.StatusCode
|
|
|
|
|
returns.Header = response.Header
|
|
|
|
|
|
|
|
|
|
// Connect to response body stream and stream the response body
|
|
|
|
|
go func() {
|
2025-12-01 11:26:33 -05:00
|
|
|
defer r.Body.Close()
|
2025-11-05 09:21:13 -05:00
|
|
|
if response.Body != nil {
|
|
|
|
|
// Stream the response body through the connection
|
|
|
|
|
if _, err := io.Copy(responseConnection, response.Body); err != nil {
|
|
|
|
|
log.Printf("error streaming response body: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
response.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
responseConnection.Close()
|
|
|
|
|
}()
|
2025-12-01 11:26:33 -05:00
|
|
|
} else {
|
|
|
|
|
r.Body.Close()
|
2025-11-05 09:21:13 -05:00
|
|
|
}
|
|
|
|
|
} else {
|
2025-12-01 11:26:33 -05:00
|
|
|
r.Body.Close()
|
2025-11-05 09:21:13 -05:00
|
|
|
return encodableError(fmt.Errorf("API PluginHTTP called but not implemented"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Server-side handler for old buffered PluginHTTP (for backward compatibility)
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) PluginHTTP(args *Z_PluginHTTPArgs, returns *Z_PluginHTTPReturns) error {
|
2022-08-09 07:25:46 -04:00
|
|
|
args.Request.Body = io.NopCloser(bytes.NewBuffer(args.RequestBody))
|
2019-11-04 20:35:58 -05:00
|
|
|
|
|
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
PluginHTTP(request *http.Request) *http.Response
|
|
|
|
|
}); ok {
|
2025-03-11 13:44:42 -04:00
|
|
|
response := hook.PluginHTTP(args.Request.GetHTTPRequest())
|
2019-11-04 20:35:58 -05:00
|
|
|
|
2022-08-09 07:25:46 -04:00
|
|
|
responseBody, err := io.ReadAll(response.Body)
|
2019-11-04 20:35:58 -05:00
|
|
|
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 {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("API PluginHTTP called but not implemented"))
|
2019-11-04 20:35:58 -05:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 08:25:53 -04:00
|
|
|
func init() {
|
2021-02-18 09:36:56 -05:00
|
|
|
hookNameToId["FileWillBeUploaded"] = FileWillBeUploadedID
|
2018-07-27 08:25:53 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_FileWillBeUploadedArgs struct {
|
2018-07-27 08:25:53 -04:00
|
|
|
A *Context
|
|
|
|
|
B *model.FileInfo
|
|
|
|
|
UploadedFileStream uint32
|
|
|
|
|
ReplacementFileStream uint32
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_FileWillBeUploadedReturns struct {
|
2018-07-27 08:25:53 -04:00
|
|
|
A *model.FileInfo
|
|
|
|
|
B string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *hooksRPCClient) FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string) {
|
2021-02-18 09:36:56 -05:00
|
|
|
if !g.implemented[FileWillBeUploadedID] {
|
2018-07-27 08:25:53 -04:00
|
|
|
return info, ""
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
uploadedFileStreamId := g.muxBroker.NextId()
|
2018-07-27 08:25:53 -04:00
|
|
|
go func() {
|
2021-03-23 05:32:54 -04:00
|
|
|
uploadedFileConnection, err := g.muxBroker.Accept(uploadedFileStreamId)
|
2018-07-27 08:25:53 -04:00
|
|
|
if err != nil {
|
|
|
|
|
g.log.Error("Plugin failed to serve upload file stream. MuxBroker could not Accept connection", mlog.Err(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer uploadedFileConnection.Close()
|
|
|
|
|
serveIOReader(file, uploadedFileConnection)
|
|
|
|
|
}()
|
|
|
|
|
|
2019-09-19 21:02:19 -04:00
|
|
|
replacementDone := make(chan bool)
|
2021-03-23 05:32:54 -04:00
|
|
|
replacementFileStreamId := g.muxBroker.NextId()
|
2018-07-27 08:25:53 -04:00
|
|
|
go func() {
|
2019-09-19 21:02:19 -04:00
|
|
|
defer close(replacementDone)
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
replacementFileConnection, err := g.muxBroker.Accept(replacementFileStreamId)
|
2018-07-27 08:25:53 -04:00
|
|
|
if err != nil {
|
|
|
|
|
g.log.Error("Plugin failed to serve replacement file stream. MuxBroker could not Accept connection", mlog.Err(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer replacementFileConnection.Close()
|
2019-09-19 21:02:19 -04:00
|
|
|
if _, err := io.Copy(output, replacementFileConnection); err != nil {
|
2018-07-27 08:25:53 -04:00
|
|
|
g.log.Error("Error reading replacement file.", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_FileWillBeUploadedArgs{c, info, uploadedFileStreamId, replacementFileStreamId}
|
|
|
|
|
_returns := &Z_FileWillBeUploadedReturns{A: _args.B}
|
2019-09-19 21:02:19 -04:00
|
|
|
if err := g.client.Call("Plugin.FileWillBeUploaded", _args, _returns); err != nil {
|
|
|
|
|
g.log.Error("RPC call FileWillBeUploaded to plugin failed.", mlog.Err(err))
|
2018-07-27 08:25:53 -04:00
|
|
|
}
|
2019-09-19 21:02:19 -04:00
|
|
|
|
|
|
|
|
// Ensure the io.Copy from the replacementFileConnection above completes.
|
|
|
|
|
<-replacementDone
|
|
|
|
|
|
2018-07-27 08:25:53 -04:00
|
|
|
return _returns.A, _returns.B
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *hooksRPCServer) FileWillBeUploaded(args *Z_FileWillBeUploadedArgs, returns *Z_FileWillBeUploadedReturns) error {
|
2018-07-27 08:25:53 -04:00
|
|
|
uploadFileConnection, err := s.muxBroker.Dial(args.UploadedFileStream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote upload file stream, error: %v", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer uploadFileConnection.Close()
|
|
|
|
|
fileReader := connectIOReader(uploadFileConnection)
|
|
|
|
|
defer fileReader.Close()
|
|
|
|
|
|
|
|
|
|
replacementFileConnection, err := s.muxBroker.Dial(args.ReplacementFileStream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote replacement file stream, error: %v", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer replacementFileConnection.Close()
|
|
|
|
|
returnFileWriter := replacementFileConnection
|
|
|
|
|
|
|
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
FileWillBeUploaded(c *Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string)
|
|
|
|
|
}); ok {
|
|
|
|
|
returns.A, returns.B = hook.FileWillBeUploaded(args.A, args.B, fileReader, returnFileWriter)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return fmt.Errorf("hook FileWillBeUploaded called but not implemented")
|
2018-07-27 08:25:53 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-01-14 12:03:16 -05:00
|
|
|
|
2019-11-15 07:13:38 -05:00
|
|
|
// MessageWillBePosted is in this file because of the difficulty of identifying which fields need special behaviour.
|
2019-01-14 12:03:16 -05:00
|
|
|
// The special behaviour needed is decoding the returned post into the original one to avoid the unintentional removal
|
|
|
|
|
// of fields by older plugins.
|
|
|
|
|
func init() {
|
2021-02-18 09:36:56 -05:00
|
|
|
hookNameToId["MessageWillBePosted"] = MessageWillBePostedID
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_MessageWillBePostedArgs struct {
|
2019-01-14 12:03:16 -05:00
|
|
|
A *Context
|
|
|
|
|
B *model.Post
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_MessageWillBePostedReturns struct {
|
2019-01-14 12:03:16 -05:00
|
|
|
A *model.Post
|
|
|
|
|
B string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *hooksRPCClient) MessageWillBePosted(c *Context, post *model.Post) (*model.Post, string) {
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_MessageWillBePostedArgs{c, post}
|
|
|
|
|
_returns := &Z_MessageWillBePostedReturns{A: _args.B}
|
2021-02-18 09:36:56 -05:00
|
|
|
if g.implemented[MessageWillBePostedID] {
|
2019-01-14 12:03:16 -05:00
|
|
|
if err := g.client.Call("Plugin.MessageWillBePosted", _args, _returns); err != nil {
|
|
|
|
|
g.log.Error("RPC call MessageWillBePosted to plugin failed.", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _returns.A, _returns.B
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *hooksRPCServer) MessageWillBePosted(args *Z_MessageWillBePostedArgs, returns *Z_MessageWillBePostedReturns) error {
|
2019-01-14 12:03:16 -05:00
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
MessageWillBePosted(c *Context, post *model.Post) (*model.Post, string)
|
|
|
|
|
}); ok {
|
|
|
|
|
returns.A, returns.B = hook.MessageWillBePosted(args.A, args.B)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("hook MessageWillBePosted called but not implemented"))
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 07:13:38 -05:00
|
|
|
// MessageWillBeUpdated is in this file because of the difficulty of identifying which fields need special behaviour.
|
|
|
|
|
// The special behaviour needed is decoding the returned post into the original one to avoid the unintentional removal
|
2019-01-14 12:03:16 -05:00
|
|
|
// of fields by older plugins.
|
|
|
|
|
func init() {
|
2021-02-18 09:36:56 -05:00
|
|
|
hookNameToId["MessageWillBeUpdated"] = MessageWillBeUpdatedID
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_MessageWillBeUpdatedArgs struct {
|
2019-01-14 12:03:16 -05:00
|
|
|
A *Context
|
|
|
|
|
B *model.Post
|
|
|
|
|
C *model.Post
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_MessageWillBeUpdatedReturns struct {
|
2019-01-14 12:03:16 -05:00
|
|
|
A *model.Post
|
|
|
|
|
B string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *hooksRPCClient) MessageWillBeUpdated(c *Context, newPost, oldPost *model.Post) (*model.Post, string) {
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_MessageWillBeUpdatedArgs{c, newPost, oldPost}
|
2023-10-26 09:15:00 -04:00
|
|
|
_default_returns := &Z_MessageWillBeUpdatedReturns{A: _args.B}
|
2021-02-18 09:36:56 -05:00
|
|
|
if g.implemented[MessageWillBeUpdatedID] {
|
2023-10-26 09:15:00 -04:00
|
|
|
_returns := &Z_MessageWillBeUpdatedReturns{}
|
2019-01-14 12:03:16 -05:00
|
|
|
if err := g.client.Call("Plugin.MessageWillBeUpdated", _args, _returns); err != nil {
|
|
|
|
|
g.log.Error("RPC call MessageWillBeUpdated to plugin failed.", mlog.Err(err))
|
2023-10-26 09:15:00 -04:00
|
|
|
return _default_returns.A, _default_returns.B
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
2023-10-26 09:15:00 -04:00
|
|
|
return _returns.A, _returns.B
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
2023-10-26 09:15:00 -04:00
|
|
|
return _default_returns.A, _default_returns.B
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *hooksRPCServer) MessageWillBeUpdated(args *Z_MessageWillBeUpdatedArgs, returns *Z_MessageWillBeUpdatedReturns) error {
|
2019-01-14 12:03:16 -05:00
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
MessageWillBeUpdated(c *Context, newPost, oldPost *model.Post) (*model.Post, string)
|
|
|
|
|
}); ok {
|
|
|
|
|
returns.A, returns.B = hook.MessageWillBeUpdated(args.A, args.B, args.C)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("hook MessageWillBeUpdated called but not implemented"))
|
2023-10-23 10:12:46 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MessagesWillBeConsumed is in this file because of the difficulty of identifying which fields need special behaviour.
|
|
|
|
|
// The special behaviour needed is decoding the returned post into the original one to avoid the unintentional removal
|
|
|
|
|
// of fields by older plugins.
|
|
|
|
|
func init() {
|
|
|
|
|
hookNameToId["MessagesWillBeConsumed"] = MessagesWillBeConsumedID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_MessagesWillBeConsumedArgs struct {
|
|
|
|
|
A []*model.Post
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_MessagesWillBeConsumedReturns struct {
|
|
|
|
|
A []*model.Post
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *hooksRPCClient) MessagesWillBeConsumed(posts []*model.Post) []*model.Post {
|
|
|
|
|
_args := &Z_MessagesWillBeConsumedArgs{posts}
|
|
|
|
|
_returns := &Z_MessagesWillBeConsumedReturns{}
|
|
|
|
|
if g.implemented[MessagesWillBeConsumedID] {
|
|
|
|
|
if err := g.client.Call("Plugin.MessagesWillBeConsumed", _args, _returns); err != nil {
|
|
|
|
|
g.log.Error("RPC call MessagesWillBeConsumed to plugin failed.", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _returns.A
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *hooksRPCServer) MessagesWillBeConsumed(args *Z_MessagesWillBeConsumedArgs, returns *Z_MessagesWillBeConsumedReturns) error {
|
|
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
MessagesWillBeConsumed(posts []*model.Post) []*model.Post
|
|
|
|
|
}); ok {
|
|
|
|
|
returns.A = hook.MessagesWillBeConsumed(args.A)
|
|
|
|
|
} else {
|
|
|
|
|
return encodableError(fmt.Errorf("hook MessagesWillBeConsumed called but not implemented"))
|
2019-01-14 12:03:16 -05:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-10-29 23:17:04 -04:00
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_LogDebugArgs struct {
|
2019-10-29 23:17:04 -04:00
|
|
|
A string
|
2022-07-05 02:46:50 -04:00
|
|
|
B []any
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
type Z_LogDebugReturns struct{}
|
2019-10-29 23:17:04 -04:00
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (g *apiRPCClient) LogDebug(msg string, keyValuePairs ...any) {
|
2019-10-29 23:17:04 -04:00
|
|
|
stringifiedPairs := stringifyToObjects(keyValuePairs)
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_LogDebugArgs{msg, stringifiedPairs}
|
|
|
|
|
_returns := &Z_LogDebugReturns{}
|
2019-10-29 23:17:04 -04:00
|
|
|
if err := g.client.Call("Plugin.LogDebug", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to LogDebug API failed: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) LogDebug(args *Z_LogDebugArgs, returns *Z_LogDebugReturns) error {
|
2019-10-29 23:17:04 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
2022-07-05 02:46:50 -04:00
|
|
|
LogDebug(msg string, keyValuePairs ...any)
|
2019-10-29 23:17:04 -04:00
|
|
|
}); ok {
|
|
|
|
|
hook.LogDebug(args.A, args.B...)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("API LogDebug called but not implemented"))
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_LogInfoArgs struct {
|
2019-10-29 23:17:04 -04:00
|
|
|
A string
|
2022-07-05 02:46:50 -04:00
|
|
|
B []any
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
type Z_LogInfoReturns struct{}
|
2019-10-29 23:17:04 -04:00
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (g *apiRPCClient) LogInfo(msg string, keyValuePairs ...any) {
|
2019-10-29 23:17:04 -04:00
|
|
|
stringifiedPairs := stringifyToObjects(keyValuePairs)
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_LogInfoArgs{msg, stringifiedPairs}
|
|
|
|
|
_returns := &Z_LogInfoReturns{}
|
2019-10-29 23:17:04 -04:00
|
|
|
if err := g.client.Call("Plugin.LogInfo", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to LogInfo API failed: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) LogInfo(args *Z_LogInfoArgs, returns *Z_LogInfoReturns) error {
|
2019-10-29 23:17:04 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
2022-07-05 02:46:50 -04:00
|
|
|
LogInfo(msg string, keyValuePairs ...any)
|
2019-10-29 23:17:04 -04:00
|
|
|
}); ok {
|
|
|
|
|
hook.LogInfo(args.A, args.B...)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("API LogInfo called but not implemented"))
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_LogWarnArgs struct {
|
2019-10-29 23:17:04 -04:00
|
|
|
A string
|
2022-07-05 02:46:50 -04:00
|
|
|
B []any
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
type Z_LogWarnReturns struct{}
|
2019-10-29 23:17:04 -04:00
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (g *apiRPCClient) LogWarn(msg string, keyValuePairs ...any) {
|
2019-10-29 23:17:04 -04:00
|
|
|
stringifiedPairs := stringifyToObjects(keyValuePairs)
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_LogWarnArgs{msg, stringifiedPairs}
|
|
|
|
|
_returns := &Z_LogWarnReturns{}
|
2019-10-29 23:17:04 -04:00
|
|
|
if err := g.client.Call("Plugin.LogWarn", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to LogWarn API failed: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) LogWarn(args *Z_LogWarnArgs, returns *Z_LogWarnReturns) error {
|
2019-10-29 23:17:04 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
2022-07-05 02:46:50 -04:00
|
|
|
LogWarn(msg string, keyValuePairs ...any)
|
2019-10-29 23:17:04 -04:00
|
|
|
}); ok {
|
|
|
|
|
hook.LogWarn(args.A, args.B...)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("API LogWarn called but not implemented"))
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_LogErrorArgs struct {
|
2019-10-29 23:17:04 -04:00
|
|
|
A string
|
2022-07-05 02:46:50 -04:00
|
|
|
B []any
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
type Z_LogErrorReturns struct{}
|
2019-10-29 23:17:04 -04:00
|
|
|
|
2022-07-05 02:46:50 -04:00
|
|
|
func (g *apiRPCClient) LogError(msg string, keyValuePairs ...any) {
|
2019-10-29 23:17:04 -04:00
|
|
|
stringifiedPairs := stringifyToObjects(keyValuePairs)
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_LogErrorArgs{msg, stringifiedPairs}
|
|
|
|
|
_returns := &Z_LogErrorReturns{}
|
2019-10-29 23:17:04 -04:00
|
|
|
if err := g.client.Call("Plugin.LogError", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to LogError API failed: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) LogError(args *Z_LogErrorArgs, returns *Z_LogErrorReturns) error {
|
2019-10-29 23:17:04 -04:00
|
|
|
if hook, ok := s.impl.(interface {
|
2022-07-05 02:46:50 -04:00
|
|
|
LogError(msg string, keyValuePairs ...any)
|
2019-10-29 23:17:04 -04:00
|
|
|
}); ok {
|
|
|
|
|
hook.LogError(args.A, args.B...)
|
|
|
|
|
} else {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("API LogError called but not implemented"))
|
2019-10-29 23:17:04 -04:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-11-29 06:41:17 -05:00
|
|
|
|
2025-06-25 20:37:32 -04:00
|
|
|
type Z_LogAuditRecArgs struct {
|
|
|
|
|
A *model.AuditRecord
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_LogAuditRecReturns struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Custom audit logging methods with gob safety checks
|
|
|
|
|
func (g *apiRPCClient) LogAuditRec(rec *model.AuditRecord) {
|
|
|
|
|
gobSafeRec := makeAuditRecordGobSafe(*rec)
|
|
|
|
|
_args := &Z_LogAuditRecArgs{&gobSafeRec}
|
|
|
|
|
_returns := &Z_LogAuditRecReturns{}
|
|
|
|
|
if err := g.client.Call("Plugin.LogAuditRec", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to LogAuditRec API failed: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *apiRPCServer) LogAuditRec(args *Z_LogAuditRecArgs, returns *Z_LogAuditRecReturns) error {
|
|
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
LogAuditRec(rec *model.AuditRecord)
|
|
|
|
|
}); ok {
|
|
|
|
|
hook.LogAuditRec(args.A)
|
|
|
|
|
} else {
|
|
|
|
|
return encodableError(fmt.Errorf("API LogAuditRec called but not implemented"))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_LogAuditRecWithLevelArgs struct {
|
|
|
|
|
A *model.AuditRecord
|
|
|
|
|
B mlog.Level
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_LogAuditRecWithLevelReturns struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *apiRPCClient) LogAuditRecWithLevel(rec *model.AuditRecord, level mlog.Level) {
|
|
|
|
|
gobSafeRec := makeAuditRecordGobSafe(*rec)
|
|
|
|
|
_args := &Z_LogAuditRecWithLevelArgs{&gobSafeRec, level}
|
|
|
|
|
_returns := &Z_LogAuditRecWithLevelReturns{}
|
|
|
|
|
if err := g.client.Call("Plugin.LogAuditRecWithLevel", _args, _returns); err != nil {
|
|
|
|
|
log.Printf("RPC call to LogAuditRecWithLevel API failed: %s", err.Error())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *apiRPCServer) LogAuditRecWithLevel(args *Z_LogAuditRecWithLevelArgs, returns *Z_LogAuditRecWithLevelReturns) error {
|
|
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
LogAuditRecWithLevel(rec *model.AuditRecord, level mlog.Level)
|
|
|
|
|
}); ok {
|
|
|
|
|
hook.LogAuditRecWithLevel(args.A, args.B)
|
|
|
|
|
} else {
|
|
|
|
|
return encodableError(fmt.Errorf("API LogAuditRecWithLevel called but not implemented"))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_InstallPluginArgs struct {
|
2019-11-29 06:41:17 -05:00
|
|
|
PluginStreamID uint32
|
|
|
|
|
B bool
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
type Z_InstallPluginReturns struct {
|
2019-11-29 06:41:17 -05:00
|
|
|
A *model.Manifest
|
|
|
|
|
B *model.AppError
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *apiRPCClient) InstallPlugin(file io.Reader, replace bool) (*model.Manifest, *model.AppError) {
|
|
|
|
|
pluginStreamID := g.muxBroker.NextId()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
uploadPluginConnection, err := g.muxBroker.Accept(pluginStreamID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print("Plugin failed to upload plugin. MuxBroker could not Accept connection", mlog.Err(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer uploadPluginConnection.Close()
|
|
|
|
|
serveIOReader(file, uploadPluginConnection)
|
|
|
|
|
}()
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
_args := &Z_InstallPluginArgs{pluginStreamID, replace}
|
|
|
|
|
_returns := &Z_InstallPluginReturns{}
|
2019-11-29 06:41:17 -05:00
|
|
|
if err := g.client.Call("Plugin.InstallPlugin", _args, _returns); err != nil {
|
|
|
|
|
log.Print("RPC call InstallPlugin to plugin failed.", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _returns.A, _returns.B
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 05:32:54 -04:00
|
|
|
func (s *apiRPCServer) InstallPlugin(args *Z_InstallPluginArgs, returns *Z_InstallPluginReturns) error {
|
2020-01-07 04:47:03 -05:00
|
|
|
hook, ok := s.impl.(interface {
|
2019-11-29 06:41:17 -05:00
|
|
|
InstallPlugin(file io.Reader, replace bool) (*model.Manifest, *model.AppError)
|
|
|
|
|
})
|
|
|
|
|
if !ok {
|
2021-02-23 00:22:27 -05:00
|
|
|
return encodableError(fmt.Errorf("API InstallPlugin called but not implemented"))
|
2019-11-29 06:41:17 -05:00
|
|
|
}
|
|
|
|
|
|
2020-01-07 04:47:03 -05:00
|
|
|
receivePluginConnection, err := s.muxBroker.Dial(args.PluginStreamID)
|
2019-11-29 06:41:17 -05:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote plugin stream, error: %v", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pluginReader := connectIOReader(receivePluginConnection)
|
|
|
|
|
defer pluginReader.Close()
|
|
|
|
|
|
|
|
|
|
returns.A, returns.B = hook.InstallPlugin(pluginReader, args.B)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-11-22 16:26:22 -05:00
|
|
|
|
|
|
|
|
type Z_UploadDataArgs struct {
|
|
|
|
|
A *model.UploadSession
|
|
|
|
|
PluginStreamID uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_UploadDataReturns struct {
|
|
|
|
|
A *model.FileInfo
|
|
|
|
|
B error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *apiRPCClient) UploadData(us *model.UploadSession, rd io.Reader) (*model.FileInfo, error) {
|
|
|
|
|
pluginStreamID := g.muxBroker.NextId()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
pluginConnection, err := g.muxBroker.Accept(pluginStreamID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Print("Failed to upload data. MuxBroker could not Accept connection", mlog.Err(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer pluginConnection.Close()
|
|
|
|
|
serveIOReader(rd, pluginConnection)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
_args := &Z_UploadDataArgs{us, pluginStreamID}
|
|
|
|
|
_returns := &Z_UploadDataReturns{}
|
|
|
|
|
if err := g.client.Call("Plugin.UploadData", _args, _returns); err != nil {
|
|
|
|
|
log.Print("RPC call UploadData to plugin failed.", mlog.Err(err))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _returns.A, _returns.B
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *apiRPCServer) UploadData(args *Z_UploadDataArgs, returns *Z_UploadDataReturns) error {
|
|
|
|
|
hook, ok := s.impl.(interface {
|
|
|
|
|
UploadData(us *model.UploadSession, rd io.Reader) (*model.FileInfo, error)
|
|
|
|
|
})
|
|
|
|
|
if !ok {
|
|
|
|
|
return encodableError(fmt.Errorf("API UploadData called but not implemented"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
receivePluginConnection, err := s.muxBroker.Dial(args.PluginStreamID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote plugin stream, error: %v", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
pluginReader := connectIOReader(receivePluginConnection)
|
|
|
|
|
defer pluginReader.Close()
|
|
|
|
|
|
|
|
|
|
returns.A, returns.B = hook.UploadData(args.A, pluginReader)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-11-17 15:39:06 -05:00
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
hookNameToId["ServeMetrics"] = ServeMetricsID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Z_ServeMetricsArgs struct {
|
|
|
|
|
ResponseWriterStream uint32
|
2025-03-11 13:44:42 -04:00
|
|
|
Request *HTTPRequestSubset
|
2023-11-17 15:39:06 -05:00
|
|
|
Context *Context
|
|
|
|
|
RequestBodyStream uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (g *hooksRPCClient) ServeMetrics(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !g.implemented[ServeMetricsID] {
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serveMetricsStreamId := g.muxBroker.NextId()
|
|
|
|
|
go func() {
|
|
|
|
|
connection, err := g.muxBroker.Accept(serveMetricsStreamId)
|
|
|
|
|
if err != nil {
|
2023-11-23 04:30:08 -05:00
|
|
|
g.log.Error("Plugin failed to ServeMetrics, muxBroker couldn't accept connection", mlog.Uint("serve_http_stream_id", serveMetricsStreamId), mlog.Err(err))
|
2023-11-17 15:39:06 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
|
|
rpcServer := rpc.NewServer()
|
|
|
|
|
if err := rpcServer.RegisterName("Plugin", &httpResponseWriterRPCServer{w: w, log: g.log}); err != nil {
|
|
|
|
|
g.log.Error("Plugin failed to ServeMetrics, couldn't register RPC name", mlog.Err(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
rpcServer.ServeConn(connection)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
requestBodyStreamId := uint32(0)
|
|
|
|
|
if r.Body != nil {
|
|
|
|
|
requestBodyStreamId = g.muxBroker.NextId()
|
|
|
|
|
go func() {
|
|
|
|
|
bodyConnection, err := g.muxBroker.Accept(requestBodyStreamId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
g.log.Error("Plugin failed to ServeMetrics, muxBroker couldn't Accept request body connection", mlog.Err(err))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer bodyConnection.Close()
|
|
|
|
|
serveIOReader(r.Body, bodyConnection)
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
forwardedRequest := &HTTPRequestSubset{
|
2023-11-17 15:39:06 -05:00
|
|
|
Method: r.Method,
|
|
|
|
|
URL: r.URL,
|
|
|
|
|
Proto: r.Proto,
|
|
|
|
|
ProtoMajor: r.ProtoMajor,
|
|
|
|
|
ProtoMinor: r.ProtoMinor,
|
|
|
|
|
Header: r.Header,
|
|
|
|
|
Host: r.Host,
|
|
|
|
|
RemoteAddr: r.RemoteAddr,
|
|
|
|
|
RequestURI: r.RequestURI,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := g.client.Call("Plugin.ServeMetrics", Z_ServeMetricsArgs{
|
|
|
|
|
Context: c,
|
|
|
|
|
ResponseWriterStream: serveMetricsStreamId,
|
|
|
|
|
Request: forwardedRequest,
|
|
|
|
|
RequestBodyStream: requestBodyStreamId,
|
|
|
|
|
}, nil); err != nil {
|
|
|
|
|
g.log.Error("Plugin failed to ServeMetrics, RPC call failed", mlog.Err(err))
|
|
|
|
|
http.Error(w, "500 internal server error", http.StatusInternalServerError)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *hooksRPCServer) ServeMetrics(args *Z_ServeMetricsArgs, returns *struct{}) error {
|
|
|
|
|
connection, err := s.muxBroker.Dial(args.ResponseWriterStream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote response writer stream, error: %v", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
w := connectHTTPResponseWriter(connection)
|
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
|
|
r := args.Request
|
|
|
|
|
if args.RequestBodyStream != 0 {
|
|
|
|
|
connection, err := s.muxBroker.Dial(args.RequestBodyStream)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "[ERROR] Can't connect to remote request body stream, error: %v", err.Error())
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
r.Body = connectIOReader(connection)
|
|
|
|
|
} else {
|
|
|
|
|
r.Body = io.NopCloser(&bytes.Buffer{})
|
|
|
|
|
}
|
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
2025-03-11 13:44:42 -04:00
|
|
|
httpReq := r.GetHTTPRequest()
|
|
|
|
|
|
2023-11-17 15:39:06 -05:00
|
|
|
if hook, ok := s.impl.(interface {
|
|
|
|
|
ServeMetrics(c *Context, w http.ResponseWriter, r *http.Request)
|
|
|
|
|
}); ok {
|
2025-03-11 13:44:42 -04:00
|
|
|
hook.ServeMetrics(args.Context, w, httpReq)
|
2023-11-17 15:39:06 -05:00
|
|
|
} else {
|
2025-03-11 13:44:42 -04:00
|
|
|
http.NotFound(w, httpReq)
|
2023-11-17 15:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|