diff --git a/sdk/helper/useragent/useragent.go b/sdk/helper/useragent/useragent.go index 9bc212535..e7e23ac2b 100644 --- a/sdk/helper/useragent/useragent.go +++ b/sdk/helper/useragent/useragent.go @@ -44,12 +44,29 @@ func String(comments ...string) string { // Given comments will be appended to the semicolon-delimited comment section. // // e.g. Vault/0.10.4 (+https://www.vaultproject.io/; azure-auth; go1.10.1; comment-0; comment-1) +// +// Returns an empty string if the given env is nil. func PluginString(env *logical.PluginEnvironment, pluginName string, comments ...string) string { + if env == nil { + return "" + } + + // Construct comments c := []string{"+" + projectURL} if pluginName != "" { c = append(c, pluginName) } c = append(c, rt) c = append(c, comments...) - return fmt.Sprintf("Vault/%s (%s)", env.VaultVersion, strings.Join(c, "; ")) + + // Construct version string + v := env.VaultVersion + if env.VaultVersionPrerelease != "" { + v = fmt.Sprintf("%s-%s", v, env.VaultVersionPrerelease) + } + if env.VaultVersionMetadata != "" { + v = fmt.Sprintf("%s+%s", v, env.VaultVersionMetadata) + } + + return fmt.Sprintf("Vault/%s (%s)", v, strings.Join(c, "; ")) } diff --git a/sdk/helper/useragent/useragent_test.go b/sdk/helper/useragent/useragent_test.go index bfd439603..f0d014f6c 100644 --- a/sdk/helper/useragent/useragent_test.go +++ b/sdk/helper/useragent/useragent_test.go @@ -51,12 +51,10 @@ func TestUserAgent(t *testing.T) { func TestUserAgentPlugin(t *testing.T) { projectURL = "https://vault-test.com" rt = "go5.0" - env := &logical.PluginEnvironment{ - VaultVersion: "1.2.3", - } type args struct { pluginName string + pluginEnv *logical.PluginEnvironment comments []string } tests := []struct { @@ -64,15 +62,38 @@ func TestUserAgentPlugin(t *testing.T) { args args want string }{ + { + name: "Plugin user agent with nil plugin env", + args: args{ + pluginEnv: nil, + }, + want: "", + }, { name: "Plugin user agent without plugin name", - args: args{}, + args: args{ + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + }, + }, + want: "Vault/1.2.3 (+https://vault-test.com; go5.0)", + }, + { + name: "Plugin user agent without plugin name", + args: args{ + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + }, + }, want: "Vault/1.2.3 (+https://vault-test.com; go5.0)", }, { name: "Plugin user agent with plugin name", args: args{ pluginName: "azure-auth", + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + }, }, want: "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0)", }, @@ -80,7 +101,10 @@ func TestUserAgentPlugin(t *testing.T) { name: "Plugin user agent with plugin name and additional comment", args: args{ pluginName: "azure-auth", - comments: []string{"pid-abcdefg"}, + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + }, + comments: []string{"pid-abcdefg"}, }, want: "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg)", }, @@ -88,21 +112,64 @@ func TestUserAgentPlugin(t *testing.T) { name: "Plugin user agent with plugin name and additional comments", args: args{ pluginName: "azure-auth", - comments: []string{"pid-abcdefg", "cloud-provider"}, + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + }, + comments: []string{"pid-abcdefg", "cloud-provider"}, }, want: "Vault/1.2.3 (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)", }, { name: "Plugin user agent with no plugin name and additional comments", args: args{ + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + }, comments: []string{"pid-abcdefg", "cloud-provider"}, }, want: "Vault/1.2.3 (+https://vault-test.com; go5.0; pid-abcdefg; cloud-provider)", }, + { + name: "Plugin user agent with version prerelease", + args: args{ + pluginName: "azure-auth", + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + VaultVersionPrerelease: "dev", + }, + comments: []string{"pid-abcdefg", "cloud-provider"}, + }, + want: "Vault/1.2.3-dev (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)", + }, + { + name: "Plugin user agent with version metadata", + args: args{ + pluginName: "azure-auth", + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + VaultVersionMetadata: "ent", + }, + comments: []string{"pid-abcdefg", "cloud-provider"}, + }, + want: "Vault/1.2.3+ent (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)", + }, + { + name: "Plugin user agent with version prerelease and metadata", + args: args{ + pluginName: "azure-auth", + pluginEnv: &logical.PluginEnvironment{ + VaultVersion: "1.2.3", + VaultVersionPrerelease: "dev", + VaultVersionMetadata: "ent", + }, + comments: []string{"pid-abcdefg", "cloud-provider"}, + }, + want: "Vault/1.2.3-dev+ent (+https://vault-test.com; azure-auth; go5.0; pid-abcdefg; cloud-provider)", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := PluginString(env, tt.args.pluginName, tt.args.comments...); got != tt.want { + if got := PluginString(tt.args.pluginEnv, tt.args.pluginName, tt.args.comments...); got != tt.want { t.Errorf("PluginString() = %v, want %v", got, tt.want) } })