From 40445b7d7335f3597749e5d03ee2d4d4bcbbd1f7 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Thu, 15 Feb 2018 18:30:31 -0500 Subject: [PATCH] Add useragent helper (#3991) * Add useragent package This helper provides a consistent user-agent header for Vault, taking into account different versions. * Add user-agent headers to spanner and gcs --- helper/useragent/useragent.go | 29 +++++++++++++++++++++++++++++ helper/useragent/useragent_test.go | 18 ++++++++++++++++++ physical/gcs/gcs.go | 9 ++++++--- physical/spanner/spanner.go | 6 +++++- 4 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 helper/useragent/useragent.go create mode 100644 helper/useragent/useragent_test.go diff --git a/helper/useragent/useragent.go b/helper/useragent/useragent.go new file mode 100644 index 0000000000..e813254482 --- /dev/null +++ b/helper/useragent/useragent.go @@ -0,0 +1,29 @@ +package useragent + +import ( + "fmt" + "runtime" + + "github.com/hashicorp/vault/version" +) + +var ( + // projectURL is the project URL. + projectURL = "https://www.vaultproject.io/" + + // rt is the runtime - variable for tests. + rt = runtime.Version() + + // versionFunc is the func that returns the current version. This is a + // function to take into account the different build processes and distinguish + // between enterprise and oss builds. + versionFunc = func() string { + return version.GetVersion().VersionNumber() + } +) + +// String returns the consistent user-agent string for Vault. +func String() string { + return fmt.Sprintf("Vault/%s (+%s; %s)", + versionFunc(), projectURL, rt) +} diff --git a/helper/useragent/useragent_test.go b/helper/useragent/useragent_test.go new file mode 100644 index 0000000000..cb0cf32942 --- /dev/null +++ b/helper/useragent/useragent_test.go @@ -0,0 +1,18 @@ +package useragent + +import ( + "testing" +) + +func TestUserAgent(t *testing.T) { + projectURL = "https://vault-test.com" + rt = "go5.0" + versionFunc = func() string { return "1.2.3" } + + act := String() + + exp := "Vault/1.2.3 (+https://vault-test.com; go5.0)" + if exp != act { + t.Errorf("expected %q to be %q", act, exp) + } +} diff --git a/physical/gcs/gcs.go b/physical/gcs/gcs.go index e9748a5209..c81e048969 100644 --- a/physical/gcs/gcs.go +++ b/physical/gcs/gcs.go @@ -10,6 +10,7 @@ import ( "time" "github.com/hashicorp/errwrap" + "github.com/hashicorp/vault/helper/useragent" "github.com/hashicorp/vault/physical" log "github.com/mgutz/logxi/v1" @@ -84,8 +85,8 @@ func newGCSClient(ctx context.Context, conf map[string]string, logger log.Logger // else use application default credentials credentialsFile, ok := conf["credentials_file"] if ok { - client, err := storage.NewClient( - ctx, + client, err := storage.NewClient(ctx, + option.WithUserAgent(useragent.String()), option.WithServiceAccountFile(credentialsFile), ) @@ -95,7 +96,9 @@ func newGCSClient(ctx context.Context, conf map[string]string, logger log.Logger return client, nil } - client, err := storage.NewClient(ctx) + client, err := storage.NewClient(ctx, + option.WithUserAgent(useragent.String()), + ) if err != nil { return nil, errwrap.Wrapf("error with application default credentials: {{err}}", err) } diff --git a/physical/spanner/spanner.go b/physical/spanner/spanner.go index e6428e655b..48f654494d 100644 --- a/physical/spanner/spanner.go +++ b/physical/spanner/spanner.go @@ -11,9 +11,11 @@ import ( metrics "github.com/armon/go-metrics" "github.com/hashicorp/errwrap" "github.com/hashicorp/vault/helper/strutil" + "github.com/hashicorp/vault/helper/useragent" "github.com/hashicorp/vault/physical" log "github.com/mgutz/logxi/v1" "google.golang.org/api/iterator" + "google.golang.org/api/option" "google.golang.org/grpc/codes" "cloud.google.com/go/spanner" @@ -151,7 +153,9 @@ func NewBackend(c map[string]string, logger log.Logger) (physical.Backend, error logger.Debug("physical/spanner: creating client") ctx := context.Background() - client, err := spanner.NewClient(ctx, database) + client, err := spanner.NewClient(ctx, database, + option.WithUserAgent(useragent.String()), + ) if err != nil { return nil, errwrap.Wrapf("failed to create spanner client: {{err}}", err) }