Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version protocol switch #3833

Merged
merged 11 commits into from
Jan 23, 2018
9 changes: 8 additions & 1 deletion builtin/logical/database/dbplugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/tls"

"github.com/hashicorp/go-plugin"
"github.com/hashicorp/vault/helper/pluginutil"
)

// Serve is called from within a plugin and wraps the provided
Expand All @@ -23,10 +24,16 @@ func ServeConfig(db Database, tlsProvider func() (*tls.Config, error)) *plugin.S
"database": dbPlugin,
}

return &plugin.ServeConfig{
conf := &plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
TLSProvider: tlsProvider,
GRPCServer: plugin.DefaultGRPCServer,
}

if !pluginutil.GRPCSupport() {
conf.GRPCServer = nil
}

return conf
}
2 changes: 2 additions & 0 deletions helper/pluginutil/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
plugin "github.com/hashicorp/go-plugin"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/helper/wrapping"
"github.com/hashicorp/vault/version"
log "github.com/mgutz/logxi/v1"
)

Expand Down Expand Up @@ -70,6 +71,7 @@ func (r *PluginRunner) runCommon(ctx context.Context, wrapper RunnerUtil, plugin
if wrapper.MlockEnabled() {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginMlockEnabled, "true"))
}
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginVaultVersionEnv, version.GetVersion().Version))

// Create logger for the plugin client
clogger := &hclogFaker{
Expand Down
40 changes: 40 additions & 0 deletions helper/pluginutil/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pluginutil

import (
"os"

"github.com/hashicorp/go-version"
)

var (
// PluginVaultVersionEnv is the ENV name used to pass the version of the
// vault server to the plugin
PluginVaultVersionEnv = "VAULT_VERSION"
)

// 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
}

constraint, err := version.NewConstraint(">= 0.9.2")
if err != nil {
return true
}

return constraint.Check(ver)
}

return true
}
57 changes: 57 additions & 0 deletions helper/pluginutil/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package pluginutil

import (
"os"
"testing"
)

func TestGRPCSupport(t *testing.T) {
cases := []struct {
envVersion string
expected bool
}{
{
"0.8.3",
false,
},
{
"0.9.2",
true,
},
{
"0.9.2+ent",
true,
},
{
"0.9.2-beta",
false,
},
{
"0.9.3",
true,
},
{
"unknown",
true,
},
{
"",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows up somewhat weird on the test output, wondering if it's worth adding a name field for this

=== RUN   TestGRPCSupport
=== RUN   TestGRPCSupport/0.8.3
=== RUN   TestGRPCSupport/0.9.2
=== RUN   TestGRPCSupport/0.9.2+ent
=== RUN   TestGRPCSupport/0.9.2-beta
=== RUN   TestGRPCSupport/unknown
=== RUN   TestGRPCSupport/#00
--- PASS: TestGRPCSupport (0.00s)
    --- PASS: TestGRPCSupport/0.8.3 (0.00s)
    --- PASS: TestGRPCSupport/0.9.2 (0.00s)
    --- PASS: TestGRPCSupport/0.9.2+ent (0.00s)
    --- PASS: TestGRPCSupport/0.9.2-beta (0.00s)
    --- PASS: TestGRPCSupport/unknown (0.00s)
    --- PASS: TestGRPCSupport/#00 (0.00s)
PASS
ok  	github.com/hashicorp/vault/helper/pluginutil	0.014s

false,
},
}

for _, tc := range cases {
t.Run(tc.envVersion, func(t *testing.T) {
err := os.Setenv(PluginVaultVersionEnv, tc.envVersion)
if err != nil {
t.Fatal(err)
}

result := GRPCSupport()

if result != tc.expected {
t.Fatalf("got: %t, expected: %t", result, tc.expected)
}
})
}
}
12 changes: 9 additions & 3 deletions logical/plugin/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,22 @@ func Serve(opts *ServeOpts) error {
return err
}

// If FetchMetadata is true, run without TLSProvider
plugin.Serve(&plugin.ServeConfig{
serveOpts := &plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
Plugins: pluginMap,
TLSProvider: opts.TLSProviderFunc,
Logger: logger,

// A non-nil value here enables gRPC serving for this plugin...
GRPCServer: plugin.DefaultGRPCServer,
})
}

if !pluginutil.GRPCSupport() {
serveOpts.GRPCServer = nil
}

// If FetchMetadata is true, run without TLSProvider
plugin.Serve(serveOpts)

return nil
}
Expand Down
Loading