github.com/hashicorp/vault/sdk@v0.11.0/helper/useragent/useragent.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package useragent
     5  
     6  import (
     7  	"fmt"
     8  	"runtime"
     9  	"strings"
    10  
    11  	"github.com/hashicorp/vault/sdk/logical"
    12  )
    13  
    14  var (
    15  	// projectURL is the project URL.
    16  	projectURL = "https://www.vaultproject.io/"
    17  
    18  	// rt is the runtime - variable for tests.
    19  	rt = runtime.Version()
    20  )
    21  
    22  // String returns the consistent user-agent string for Vault.
    23  // Deprecated: use PluginString instead.
    24  //
    25  // Example output:
    26  //
    27  //	Vault (+https://www.vaultproject.io/; go1.19.5)
    28  //
    29  // Given comments will be appended to the semicolon-delimited comment section:
    30  //
    31  //	Vault (+https://www.vaultproject.io/; go1.19.5; comment-0; comment-1)
    32  //
    33  // At one point the user-agent string returned contained the Vault
    34  // version hardcoded into the vault/sdk/version/ package.  This worked for builtin
    35  // plugins that are compiled into the `vault` binary, in that it correctly described
    36  // the version of that Vault binary.  It did not work for external plugins: for them,
    37  // the version will be based on the version stored in the sdk based on the
    38  // contents of the external plugin's go.mod.  We've kept the String method around
    39  // to avoid breaking builds, but you should be using PluginString.
    40  func String(comments ...string) string {
    41  	c := append([]string{"+" + projectURL, rt}, comments...)
    42  	return fmt.Sprintf("Vault (%s)", strings.Join(c, "; "))
    43  }
    44  
    45  // PluginString is usable by plugins to return a user-agent string reflecting
    46  // the running Vault version and an optional plugin name.
    47  //
    48  // e.g. Vault/0.10.4 (+https://www.vaultproject.io/; azure-auth; go1.10.1)
    49  //
    50  // Given comments will be appended to the semicolon-delimited comment section.
    51  //
    52  // e.g. Vault/0.10.4 (+https://www.vaultproject.io/; azure-auth; go1.10.1; comment-0; comment-1)
    53  //
    54  // Returns an empty string if the given env is nil.
    55  func PluginString(env *logical.PluginEnvironment, pluginName string, comments ...string) string {
    56  	if env == nil {
    57  		return ""
    58  	}
    59  
    60  	// Construct comments
    61  	c := []string{"+" + projectURL}
    62  	if pluginName != "" {
    63  		c = append(c, pluginName)
    64  	}
    65  	c = append(c, rt)
    66  	c = append(c, comments...)
    67  
    68  	// Construct version string
    69  	v := env.VaultVersion
    70  	if env.VaultVersionPrerelease != "" {
    71  		v = fmt.Sprintf("%s-%s", v, env.VaultVersionPrerelease)
    72  	}
    73  	if env.VaultVersionMetadata != "" {
    74  		v = fmt.Sprintf("%s+%s", v, env.VaultVersionMetadata)
    75  	}
    76  
    77  	return fmt.Sprintf("Vault/%s (%s)", v, strings.Join(c, "; "))
    78  }