From 7fbdbe7d933a32825c7144f76b564dfad7f6663f Mon Sep 17 00:00:00 2001 From: Jim Kalafut Date: Mon, 6 Aug 2018 09:02:04 -0700 Subject: [PATCH] Add plugin user-agent helper (#5039) --- helper/useragent/useragent.go | 18 ++++++++++++++++++ helper/useragent/useragent_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/helper/useragent/useragent.go b/helper/useragent/useragent.go index e813254482..549b711316 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 cb0cf32942..8b47e6105f 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) + } +}