2018-01-23 22:29:26 +00:00
|
|
|
package pluginutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2018-04-03 17:36:14 +00:00
|
|
|
version "github.com/hashicorp/go-version"
|
|
|
|
"github.com/hashicorp/vault/helper/mlock"
|
2018-01-23 22:29:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-04-03 17:36:14 +00:00
|
|
|
// PluginMlockEnabled is the ENV name used to pass the configuration for
|
|
|
|
// enabling mlock
|
|
|
|
PluginMlockEnabled = "VAULT_PLUGIN_MLOCK_ENABLED"
|
|
|
|
|
2018-01-23 22:29:26 +00:00
|
|
|
// PluginVaultVersionEnv is the ENV name used to pass the version of the
|
|
|
|
// vault server to the plugin
|
|
|
|
PluginVaultVersionEnv = "VAULT_VERSION"
|
2018-04-03 17:36:14 +00:00
|
|
|
|
|
|
|
// PluginMetadataModeEnv is an ENV name used to disable TLS communication
|
|
|
|
// to bootstrap mounting plugins.
|
|
|
|
PluginMetadataModeEnv = "VAULT_PLUGIN_METADATA_MODE"
|
2018-01-23 22:29:26 +00:00
|
|
|
)
|
|
|
|
|
2018-04-03 17:36:14 +00:00
|
|
|
// OptionallyEnableMlock determines if mlock should be called, and if so enables
|
|
|
|
// mlock.
|
|
|
|
func OptionallyEnableMlock() error {
|
|
|
|
if os.Getenv(PluginMlockEnabled) == "true" {
|
|
|
|
return mlock.LockMemory()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 22:29:26 +00:00
|
|
|
// GRPCSupport defaults to returning true, unless VAULT_VERSION is missing or
|
|
|
|
// it fails to meet the version constraint.
|
|
|
|
func GRPCSupport() bool {
|
|
|
|
verString := os.Getenv(PluginVaultVersionEnv)
|
|
|
|
// If the env var is empty, we fall back to netrpc for backward compatibility.
|
|
|
|
if verString == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if verString != "unknown" {
|
|
|
|
ver, err := version.NewVersion(verString)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2018-02-01 22:30:17 +00:00
|
|
|
// Due to some regressions on 0.9.2 & 0.9.3 we now require version 0.9.4
|
|
|
|
// to allow the plugin framework to default to gRPC.
|
|
|
|
constraint, err := version.NewConstraint(">= 0.9.4")
|
2018-01-23 22:29:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return constraint.Check(ver)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-04-03 17:36:14 +00:00
|
|
|
|
2018-10-19 22:56:17 +00:00
|
|
|
// InMetadataMode returns true if the plugin calling this function is running in metadata mode.
|
2018-04-03 17:36:14 +00:00
|
|
|
func InMetadataMode() bool {
|
|
|
|
return os.Getenv(PluginMetadataModeEnv) == "true"
|
|
|
|
}
|