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

sdk/useragent: plugin version string consistent with Vault version string #14912

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion sdk/helper/useragent/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "; "))
}
81 changes: 74 additions & 7 deletions sdk/helper/useragent/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,58 +51,125 @@ 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 {
name string
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)",
},
{
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)",
},
{
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)
}
})
Expand Down