2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-15 23:30:31 +00:00
|
|
|
package useragent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2022-03-31 17:31:11 +00:00
|
|
|
"strings"
|
2018-02-15 23:30:31 +00:00
|
|
|
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-02-15 23:30:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// projectURL is the project URL.
|
|
|
|
projectURL = "https://www.vaultproject.io/"
|
|
|
|
|
|
|
|
// rt is the runtime - variable for tests.
|
|
|
|
rt = runtime.Version()
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the consistent user-agent string for Vault.
|
2023-02-08 17:30:27 +00:00
|
|
|
// Deprecated: use PluginString instead.
|
2018-08-06 16:02:04 +00:00
|
|
|
//
|
2023-02-08 17:30:27 +00:00
|
|
|
// Example output:
|
2022-03-31 17:31:11 +00:00
|
|
|
//
|
2023-02-08 17:30:27 +00:00
|
|
|
// Vault (+https://www.vaultproject.io/; go1.19.5)
|
2022-03-31 17:31:11 +00:00
|
|
|
//
|
2023-02-08 17:30:27 +00:00
|
|
|
// Given comments will be appended to the semicolon-delimited comment section:
|
|
|
|
//
|
|
|
|
// Vault (+https://www.vaultproject.io/; go1.19.5; comment-0; comment-1)
|
2022-12-07 18:29:51 +00:00
|
|
|
//
|
|
|
|
// At one point the user-agent string returned contained the Vault
|
2023-02-08 17:30:27 +00:00
|
|
|
// version hardcoded into the vault/sdk/version/ package. This worked for builtin
|
2022-12-07 18:29:51 +00:00
|
|
|
// plugins that are compiled into the `vault` binary, in that it correctly described
|
2023-02-08 17:30:27 +00:00
|
|
|
// the version of that Vault binary. It did not work for external plugins: for them,
|
2022-12-07 18:29:51 +00:00
|
|
|
// the version will be based on the version stored in the sdk based on the
|
2023-02-08 17:30:27 +00:00
|
|
|
// contents of the external plugin's go.mod. We've kept the String method around
|
|
|
|
// to avoid breaking builds, but you should be using PluginString.
|
2022-03-31 17:31:11 +00:00
|
|
|
func String(comments ...string) string {
|
|
|
|
c := append([]string{"+" + projectURL, rt}, comments...)
|
2023-02-08 17:30:27 +00:00
|
|
|
return fmt.Sprintf("Vault (%s)", strings.Join(c, "; "))
|
2018-02-15 23:30:31 +00:00
|
|
|
}
|
2018-08-06 16:02:04 +00:00
|
|
|
|
|
|
|
// 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)
|
2022-03-31 17:31:11 +00:00
|
|
|
//
|
|
|
|
// 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)
|
2022-04-05 17:07:33 +00:00
|
|
|
//
|
|
|
|
// Returns an empty string if the given env is nil.
|
2022-03-31 17:31:11 +00:00
|
|
|
func PluginString(env *logical.PluginEnvironment, pluginName string, comments ...string) string {
|
2022-04-05 17:07:33 +00:00
|
|
|
if env == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct comments
|
2022-03-31 17:31:11 +00:00
|
|
|
c := []string{"+" + projectURL}
|
2018-08-06 16:02:04 +00:00
|
|
|
if pluginName != "" {
|
2022-03-31 17:31:11 +00:00
|
|
|
c = append(c, pluginName)
|
2018-08-06 16:02:04 +00:00
|
|
|
}
|
2022-03-31 17:31:11 +00:00
|
|
|
c = append(c, rt)
|
|
|
|
c = append(c, comments...)
|
2022-04-05 17:07:33 +00:00
|
|
|
|
|
|
|
// 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, "; "))
|
2018-08-06 16:02:04 +00:00
|
|
|
}
|