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

This commit is contained in:
Austin Gebauer 2022-04-05 10:07:33 -07:00 committed by GitHub
parent e37fa346bb
commit ec877c20a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 8 deletions

View File

@ -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, "; "))
}

View File

@ -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)
}
})