diff --git a/helper/useragent/useragent.go b/helper/useragent/useragent.go index e81325448..549b71131 100644 --- a/helper/useragent/useragent.go +++ b/helper/useragent/useragent.go @@ -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) +} diff --git a/helper/useragent/useragent_test.go b/helper/useragent/useragent_test.go index cb0cf3294..8b47e6105 100644 --- a/helper/useragent/useragent_test.go +++ b/helper/useragent/useragent_test.go @@ -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) + } +}