2017-04-04 00:52:29 +00:00
|
|
|
package pluginutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2017-09-01 05:02:03 +00:00
|
|
|
"crypto/tls"
|
2017-05-02 21:40:11 +00:00
|
|
|
"flag"
|
2017-04-04 00:52:29 +00:00
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
2017-04-11 00:12:52 +00:00
|
|
|
"time"
|
2017-04-04 00:52:29 +00:00
|
|
|
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2017-05-02 21:40:11 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2017-04-24 19:15:01 +00:00
|
|
|
"github.com/hashicorp/vault/helper/wrapping"
|
2017-08-15 14:16:48 +00:00
|
|
|
log "github.com/mgutz/logxi/v1"
|
2017-04-04 00:52:29 +00:00
|
|
|
)
|
|
|
|
|
2017-04-13 00:35:53 +00:00
|
|
|
// Looker defines the plugin Lookup function that looks into the plugin catalog
|
|
|
|
// for availible plugins and returns a PluginRunner
|
2017-04-04 00:52:29 +00:00
|
|
|
type Looker interface {
|
|
|
|
LookupPlugin(string) (*PluginRunner, error)
|
|
|
|
}
|
|
|
|
|
2017-04-13 00:35:53 +00:00
|
|
|
// Wrapper interface defines the functions needed by the runner to wrap the
|
|
|
|
// metadata needed to run a plugin process. This includes looking up Mlock
|
|
|
|
// configuration and wrapping data in a respose wrapped token.
|
2017-09-01 05:02:03 +00:00
|
|
|
// logical.SystemView implementataions satisfy this interface.
|
2017-05-01 21:59:55 +00:00
|
|
|
type RunnerUtil interface {
|
2017-04-24 19:15:01 +00:00
|
|
|
ResponseWrapData(data map[string]interface{}, ttl time.Duration, jwt bool) (*wrapping.ResponseWrapInfo, error)
|
2017-04-24 19:21:49 +00:00
|
|
|
MlockEnabled() bool
|
2017-04-11 00:12:52 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 00:35:53 +00:00
|
|
|
// LookWrapper defines the functions for both Looker and Wrapper
|
2017-05-01 21:59:55 +00:00
|
|
|
type LookRunnerUtil interface {
|
2017-04-06 19:20:10 +00:00
|
|
|
Looker
|
2017-05-01 21:59:55 +00:00
|
|
|
RunnerUtil
|
2017-04-06 19:20:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 00:35:53 +00:00
|
|
|
// PluginRunner defines the metadata needed to run a plugin securely with
|
|
|
|
// go-plugin.
|
2017-04-04 00:52:29 +00:00
|
|
|
type PluginRunner struct {
|
Backend plugin system (#2874)
* Add backend plugin changes
* Fix totp backend plugin tests
* Fix logical/plugin InvalidateKey test
* Fix plugin catalog CRUD test, fix NoopBackend
* Clean up commented code block
* Fix system backend mount test
* Set plugin_name to omitempty, fix handleMountTable config parsing
* Clean up comments, keep shim connections alive until cleanup
* Include pluginClient, disallow LookupPlugin call from within a plugin
* Add wrapper around backendPluginClient for proper cleanup
* Add logger shim tests
* Add logger, storage, and system shim tests
* Use pointer receivers for system view shim
* Use plugin name if no path is provided on mount
* Enable plugins for auth backends
* Add backend type attribute, move builtin/plugin/package
* Fix merge conflict
* Fix missing plugin name in mount config
* Add integration tests on enabling auth backend plugins
* Remove dependency cycle on mock-plugin
* Add passthrough backend plugin, use logical.BackendType to determine lease generation
* Remove vault package dependency on passthrough package
* Add basic impl test for passthrough plugin
* Incorporate feedback; set b.backend after shims creation on backendPluginServer
* Fix totp plugin test
* Add plugin backends docs
* Fix tests
* Fix builtin/plugin tests
* Remove flatten from PluginRunner fields
* Move mock plugin to logical/plugin, remove totp and passthrough plugins
* Move pluginMap into newPluginClient
* Do not create storage RPC connection on HandleRequest and HandleExistenceCheck
* Change shim logger's Fatal to no-op
* Change BackendType to uint32, match UX backend types
* Change framework.Backend Setup signature
* Add Setup func to logical.Backend interface
* Move OptionallyEnableMlock call into plugin.Serve, update docs and comments
* Remove commented var in plugin package
* RegisterLicense on logical.Backend interface (#3017)
* Add RegisterLicense to logical.Backend interface
* Update RegisterLicense to use callback func on framework.Backend
* Refactor framework.Backend.RegisterLicense
* plugin: Prevent plugin.SystemViewClient.ResponseWrapData from getting JWTs
* plugin: Revert BackendType to remove TypePassthrough and related references
* Fix typo in plugin backends docs
2017-07-20 17:28:40 +00:00
|
|
|
Name string `json:"name" structs:"name"`
|
|
|
|
Command string `json:"command" structs:"command"`
|
|
|
|
Args []string `json:"args" structs:"args"`
|
|
|
|
Sha256 []byte `json:"sha256" structs:"sha256"`
|
|
|
|
Builtin bool `json:"builtin" structs:"builtin"`
|
|
|
|
BuiltinFactory func() (interface{}, error) `json:"-" structs:"-"`
|
2017-04-04 00:52:29 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
// Run takes a wrapper RunnerUtil instance along with the go-plugin paramaters and
|
|
|
|
// returns a configured plugin.Client with TLS Configured and a wrapping token set
|
|
|
|
// on PluginUnwrapTokenEnv for plugin process consumption.
|
2017-08-15 14:16:48 +00:00
|
|
|
func (r *PluginRunner) Run(wrapper RunnerUtil, pluginMap map[string]plugin.Plugin, hs plugin.HandshakeConfig, env []string, logger log.Logger) (*plugin.Client, error) {
|
2017-09-01 05:02:03 +00:00
|
|
|
return r.runCommon(wrapper, pluginMap, hs, env, logger, false)
|
|
|
|
}
|
2017-04-04 00:52:29 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
// RunMetadataMode returns a configured plugin.Client that will dispense a plugin
|
|
|
|
// in metadata mode. The PluginMetadaModeEnv is passed in as part of the Cmd to
|
|
|
|
// plugin.Client, and consumed by the plugin process on pluginutil.VaultPluginTLSProvider.
|
|
|
|
func (r *PluginRunner) RunMetadataMode(wrapper RunnerUtil, pluginMap map[string]plugin.Plugin, hs plugin.HandshakeConfig, env []string, logger log.Logger) (*plugin.Client, error) {
|
|
|
|
return r.runCommon(wrapper, pluginMap, hs, env, logger, true)
|
2017-04-04 00:52:29 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
2017-04-04 00:52:29 +00:00
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
func (r *PluginRunner) runCommon(wrapper RunnerUtil, pluginMap map[string]plugin.Plugin, hs plugin.HandshakeConfig, env []string, logger log.Logger, isMetadataMode bool) (*plugin.Client, error) {
|
2017-04-04 00:52:29 +00:00
|
|
|
cmd := exec.Command(r.Command, r.Args...)
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
2017-09-01 05:02:03 +00:00
|
|
|
|
2017-04-11 00:12:52 +00:00
|
|
|
// Add the mlock setting to the ENV of the plugin
|
2017-04-24 19:21:49 +00:00
|
|
|
if wrapper.MlockEnabled() {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginMlockEnabled, "true"))
|
|
|
|
}
|
2017-04-04 00:52:29 +00:00
|
|
|
|
2017-08-15 14:16:48 +00:00
|
|
|
// Create logger for the plugin client
|
|
|
|
clogger := &hclogFaker{
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
namedLogger := clogger.ResetNamed("plugin")
|
|
|
|
|
2017-09-01 05:02:03 +00:00
|
|
|
var clientTLSConfig *tls.Config
|
|
|
|
if !isMetadataMode {
|
|
|
|
// Add the metadata mode ENV and set it to false
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginMetadaModeEnv, "false"))
|
|
|
|
|
|
|
|
// Get a CA TLS Certificate
|
|
|
|
certBytes, key, err := generateCert()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use CA to sign a client cert and return a configured TLS config
|
|
|
|
clientTLSConfig, err = createClientTLSConfig(certBytes, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use CA to sign a server cert and wrap the values in a response wrapped
|
|
|
|
// token.
|
|
|
|
wrapToken, err := wrapServerConfig(wrapper, certBytes, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the response wrap token to the ENV of the plugin
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginUnwrapTokenEnv, wrapToken))
|
|
|
|
} else {
|
|
|
|
namedLogger = clogger.ResetNamed("plugin.metadata")
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginMetadaModeEnv, "true"))
|
|
|
|
}
|
|
|
|
|
|
|
|
secureConfig := &plugin.SecureConfig{
|
|
|
|
Checksum: r.Sha256,
|
|
|
|
Hash: sha256.New(),
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConfig := &plugin.ClientConfig{
|
2017-04-04 00:52:29 +00:00
|
|
|
HandshakeConfig: hs,
|
|
|
|
Plugins: pluginMap,
|
|
|
|
Cmd: cmd,
|
|
|
|
SecureConfig: secureConfig,
|
2017-09-01 05:02:03 +00:00
|
|
|
TLSConfig: clientTLSConfig,
|
2017-08-15 14:16:48 +00:00
|
|
|
Logger: namedLogger,
|
2017-09-01 05:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client := plugin.NewClient(clientConfig)
|
2017-04-04 00:52:29 +00:00
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
2017-05-02 21:40:11 +00:00
|
|
|
|
|
|
|
type APIClientMeta struct {
|
|
|
|
// These are set by the command line flags.
|
|
|
|
flagCACert string
|
|
|
|
flagCAPath string
|
|
|
|
flagClientCert string
|
|
|
|
flagClientKey string
|
|
|
|
flagInsecure bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *APIClientMeta) FlagSet() *flag.FlagSet {
|
2017-09-01 05:02:03 +00:00
|
|
|
fs := flag.NewFlagSet("vault plugin settings", flag.ContinueOnError)
|
2017-05-02 21:40:11 +00:00
|
|
|
|
|
|
|
fs.StringVar(&f.flagCACert, "ca-cert", "", "")
|
|
|
|
fs.StringVar(&f.flagCAPath, "ca-path", "", "")
|
|
|
|
fs.StringVar(&f.flagClientCert, "client-cert", "", "")
|
|
|
|
fs.StringVar(&f.flagClientKey, "client-key", "", "")
|
|
|
|
fs.BoolVar(&f.flagInsecure, "tls-skip-verify", false, "")
|
|
|
|
|
|
|
|
return fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *APIClientMeta) GetTLSConfig() *api.TLSConfig {
|
|
|
|
// If we need custom TLS configuration, then set it
|
|
|
|
if f.flagCACert != "" || f.flagCAPath != "" || f.flagClientCert != "" || f.flagClientKey != "" || f.flagInsecure {
|
|
|
|
t := &api.TLSConfig{
|
|
|
|
CACert: f.flagCACert,
|
|
|
|
CAPath: f.flagCAPath,
|
|
|
|
ClientCert: f.flagClientCert,
|
|
|
|
ClientKey: f.flagClientKey,
|
|
|
|
TLSServerName: "",
|
|
|
|
Insecure: f.flagInsecure,
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|