Add plugin user-agent helper (#5039)

This commit is contained in:
Jim Kalafut 2018-08-06 09:02:04 -07:00 committed by GitHub
parent 982fd6f9c8
commit 41df651499
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"runtime"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/version"
)
@ -23,7 +24,24 @@ var (
)
// String returns the consistent user-agent string for Vault.
//
// e.g. Vault/0.10.4 (+https://www.vaultproject.io/; go1.10.1)
func String() string {
return fmt.Sprintf("Vault/%s (+%s; %s)",
versionFunc(), projectURL, rt)
}
// PluginString is usable by plugins to return a user-agent string reflecting
// the running Vault version and an optional plugin name.
//
// e.g. Vault/0.10.4 (+https://www.vaultproject.io/; azure-auth; go1.10.1)
func PluginString(env *logical.PluginEnvironment, pluginName string) string {
var name string
if pluginName != "" {
name = pluginName + "; "
}
return fmt.Sprintf("Vault/%s (+%s; %s%s)",
env.VaultVersion, projectURL, name, rt)
}

View File

@ -2,6 +2,8 @@ package useragent
import (
"testing"
"github.com/hashicorp/vault/logical"
)
func TestUserAgent(t *testing.T) {
@ -16,3 +18,27 @@ func TestUserAgent(t *testing.T) {
t.Errorf("expected %q to be %q", act, exp)
}
}
func TestUserAgentPlugin(t *testing.T) {
projectURL = "https://vault-test.com"
rt = "go5.0"
env := &logical.PluginEnvironment{
VaultVersion: "1.2.3",
}
pluginName := "azure-auth"
act := PluginString(env, pluginName)
exp := "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0)"
if exp != act {
t.Errorf("expected %q to be %q", act, exp)
}
pluginName = ""
act = PluginString(env, pluginName)
exp = "Vault/1.2.3 (+https://vault-test.com; go5.0)"
if exp != act {
t.Errorf("expected %q to be %q", act, exp)
}
}