2023-01-03 09:18:30 +00:00
|
|
|
// Copyright (C) 2019-2023 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-plugin"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk/plugin/metadata"
|
2021-12-16 17:18:36 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
2021-12-16 17:18:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type metadataPlugin struct {
|
|
|
|
config Config
|
|
|
|
metadater metadata.Metadater
|
|
|
|
client *plugin.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMetadaterPlugin(config Config) (*metadataPlugin, error) {
|
|
|
|
p := &metadataPlugin{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
if err := p.initialize(); err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to create metadata plugin: %v, config %+v", err, config)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *metadataPlugin) exited() bool {
|
|
|
|
return p.client.Exited()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *metadataPlugin) cleanup() {
|
|
|
|
p.client.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *metadataPlugin) initialize() error {
|
|
|
|
killProcess(p.config.Cmd)
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "create new metadata plugin %q", p.config.Cmd)
|
2022-11-04 19:25:01 +00:00
|
|
|
secureConfig, err := p.config.getSecureConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-16 17:18:36 +00:00
|
|
|
}
|
|
|
|
client := plugin.NewClient(&plugin.ClientConfig{
|
|
|
|
HandshakeConfig: metadata.Handshake,
|
|
|
|
Plugins: metadata.PluginMap,
|
|
|
|
Cmd: exec.Command(p.config.Cmd, p.config.Args...),
|
|
|
|
AllowedProtocols: []plugin.Protocol{
|
|
|
|
plugin.ProtocolGRPC,
|
|
|
|
},
|
|
|
|
Managed: false,
|
|
|
|
AutoMTLS: p.config.AutoMTLS,
|
|
|
|
SecureConfig: secureConfig,
|
|
|
|
Logger: &logger.HCLogAdapter{
|
|
|
|
Logger: hclog.New(&hclog.LoggerOptions{
|
|
|
|
Name: fmt.Sprintf("%v.%v", logSender, metadata.PluginName),
|
|
|
|
Level: pluginsLogLevel,
|
|
|
|
DisableTime: true,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
rpcClient, err := client.Client()
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "unable to get rpc client for plugin %q: %v", p.config.Cmd, err)
|
2021-12-16 17:18:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
raw, err := rpcClient.Dispense(metadata.PluginName)
|
|
|
|
if err != nil {
|
2023-02-27 18:02:43 +00:00
|
|
|
logger.Debug(logSender, "", "unable to get plugin %v from rpc client for command %q: %v",
|
2021-12-16 17:18:36 +00:00
|
|
|
metadata.PluginName, p.config.Cmd, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.client = client
|
|
|
|
p.metadater = raw.(metadata.Metadater)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|