2017-04-04 00:52:29 +00:00
|
|
|
package pluginutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"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-04-24 19:15:01 +00:00
|
|
|
"github.com/hashicorp/vault/helper/wrapping"
|
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-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 {
|
2017-04-21 01:46:41 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Command string `json:"command"`
|
|
|
|
Args []string `json:"args"`
|
|
|
|
Sha256 []byte `json:"sha256"`
|
|
|
|
Builtin bool `json:"builtin"`
|
|
|
|
BuiltinFactory func() (interface{}, error) `json:"-"`
|
2017-04-04 00:52:29 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 00:35:53 +00:00
|
|
|
// Run takes a wrapper instance, and the go-plugin paramaters and executes a
|
|
|
|
// plugin.
|
2017-05-01 21:59:55 +00:00
|
|
|
func (r *PluginRunner) Run(wrapper RunnerUtil, pluginMap map[string]plugin.Plugin, hs plugin.HandshakeConfig, env []string) (*plugin.Client, error) {
|
2017-04-04 00:52:29 +00:00
|
|
|
// Get a CA TLS Certificate
|
2017-04-19 22:46:07 +00:00
|
|
|
certBytes, key, err := GenerateCert()
|
2017-04-04 00:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use CA to sign a client cert and return a configured TLS config
|
2017-04-19 22:46:07 +00:00
|
|
|
clientTLSConfig, err := CreateClientTLSConfig(certBytes, key)
|
2017-04-04 00:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use CA to sign a server cert and wrap the values in a response wrapped
|
|
|
|
// token.
|
2017-04-19 22:46:07 +00:00
|
|
|
wrapToken, err := WrapServerConfig(wrapper, certBytes, key)
|
2017-04-04 00:52:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command(r.Command, r.Args...)
|
|
|
|
cmd.Env = append(cmd.Env, env...)
|
2017-04-11 00:12:52 +00:00
|
|
|
// Add the response wrap token to the ENV of the plugin
|
2017-04-04 00:52:29 +00:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginUnwrapTokenEnv, wrapToken))
|
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
|
|
|
|
|
|
|
secureConfig := &plugin.SecureConfig{
|
|
|
|
Checksum: r.Sha256,
|
|
|
|
Hash: sha256.New(),
|
|
|
|
}
|
|
|
|
|
|
|
|
client := plugin.NewClient(&plugin.ClientConfig{
|
|
|
|
HandshakeConfig: hs,
|
|
|
|
Plugins: pluginMap,
|
|
|
|
Cmd: cmd,
|
|
|
|
TLSConfig: clientTLSConfig,
|
|
|
|
SecureConfig: secureConfig,
|
|
|
|
})
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|