From 009f845d5d449f61eb7c816ee1e8328719ee7c84 Mon Sep 17 00:00:00 2001 From: Jeff Mendoza Date: Fri, 28 Apr 2023 12:35:24 -0700 Subject: [PATCH] User agent (#781) * Do something with version from Makefile. Signed-off-by: Jeff Mendoza * Add UserAgent to github, deps.dev, gcs, oci. Signed-off-by: Jeff Mendoza --------- Signed-off-by: Jeff Mendoza --- Makefile | 4 +- cmd/guaccollect/cmd/root.go | 6 ++- cmd/guaccsub/cmd/root.go | 6 ++- cmd/guacgql/cmd/root.go | 6 ++- cmd/guacingest/cmd/root.go | 6 ++- cmd/guacone/cmd/root.go | 6 ++- internal/client/githubclient/githubclient.go | 8 +++- pkg/handler/collector/deps_dev/deps_dev.go | 5 ++- pkg/handler/collector/gcs/gcs.go | 5 ++- pkg/handler/collector/oci/oci.go | 2 + pkg/version/version.go | 42 ++++++++++++++++++++ 11 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 pkg/version/version.go diff --git a/Makefile b/Makefile index 3f37b90f65..8750b71904 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ VERSION=$(shell git describe --tags --always) COMMIT=$(shell git rev-parse HEAD) BUILD=$(shell date +%FT%T%z) -PKG=github.com/guacsec/guac +PKG=github.com/guacsec/guac/pkg/version -LDFLAGS="-X $(PKG).version=$(VERSION) -X $(PKG).commit=$(COMMIT) -X $(PKG).date=$(BUILD)" +LDFLAGS="-X $(PKG).Version=$(VERSION) -X $(PKG).Commit=$(COMMIT) -X $(PKG).Date=$(BUILD)" .DEFAULT_GOAL := build diff --git a/cmd/guaccollect/cmd/root.go b/cmd/guaccollect/cmd/root.go index 5aec62dfaa..0e0ba16b23 100644 --- a/cmd/guaccollect/cmd/root.go +++ b/cmd/guaccollect/cmd/root.go @@ -20,6 +20,7 @@ import ( "os" "github.com/guacsec/guac/pkg/cli" + "github.com/guacsec/guac/pkg/version" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -41,8 +42,9 @@ func init() { } var rootCmd = &cobra.Command{ - Use: "guaccollect", - Short: "guaccollect is an collector cmdline for GUAC", + Use: "guaccollect", + Short: "guaccollect is an collector cmdline for GUAC", + Version: version.Version, } func Execute() { diff --git a/cmd/guaccsub/cmd/root.go b/cmd/guaccsub/cmd/root.go index b2d17b2b07..05b8425c59 100644 --- a/cmd/guaccsub/cmd/root.go +++ b/cmd/guaccsub/cmd/root.go @@ -26,14 +26,16 @@ import ( "github.com/guacsec/guac/pkg/cli" "github.com/guacsec/guac/pkg/collectsub/server" "github.com/guacsec/guac/pkg/logging" + "github.com/guacsec/guac/pkg/version" "github.com/spf13/cobra" "github.com/spf13/viper" ) var rootCmd = &cobra.Command{ - Use: "guaccsub", - Short: "GUAC collect subscriber service for GUAC collectors", + Use: "guaccsub", + Short: "GUAC collect subscriber service for GUAC collectors", + Version: version.Version, Run: func(cmd *cobra.Command, args []string) { port := viper.GetInt("csub-listen-port") diff --git a/cmd/guacgql/cmd/root.go b/cmd/guacgql/cmd/root.go index ef5b0d301c..808a345570 100644 --- a/cmd/guacgql/cmd/root.go +++ b/cmd/guacgql/cmd/root.go @@ -20,6 +20,7 @@ import ( "os" "github.com/guacsec/guac/pkg/cli" + "github.com/guacsec/guac/pkg/version" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -39,8 +40,9 @@ var flags = struct { }{} var rootCmd = &cobra.Command{ - Use: "guacgql", - Short: "GUAC GraphQL server", + Use: "guacgql", + Short: "GUAC GraphQL server", + Version: version.Version, Run: func(cmd *cobra.Command, args []string) { flags.backend = viper.GetString("gql-backend") flags.port = viper.GetInt("gql-port") diff --git a/cmd/guacingest/cmd/root.go b/cmd/guacingest/cmd/root.go index 4a3ec3dfc9..ab3c846bf4 100644 --- a/cmd/guacingest/cmd/root.go +++ b/cmd/guacingest/cmd/root.go @@ -20,6 +20,7 @@ import ( "os" "github.com/guacsec/guac/pkg/cli" + "github.com/guacsec/guac/pkg/version" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -41,8 +42,9 @@ func init() { } var rootCmd = &cobra.Command{ - Use: "guacingest", - Short: "starts the GUAC processor, ingestor and assembler process", + Use: "guacingest", + Short: "starts the GUAC processor, ingestor and assembler process", + Version: version.Version, Run: func(cmd *cobra.Command, args []string) { ingest(cmd, args) }, diff --git a/cmd/guacone/cmd/root.go b/cmd/guacone/cmd/root.go index 4adac18a3d..1bcf717c6e 100644 --- a/cmd/guacone/cmd/root.go +++ b/cmd/guacone/cmd/root.go @@ -20,6 +20,7 @@ import ( "os" "github.com/guacsec/guac/pkg/cli" + "github.com/guacsec/guac/pkg/version" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -41,8 +42,9 @@ func init() { } var rootCmd = &cobra.Command{ - Use: "guacone", - Short: "guacone is an all in one flow cmdline for GUAC", + Use: "guacone", + Short: "guacone is an all in one flow cmdline for GUAC", + Version: version.Version, } func Execute() { diff --git a/internal/client/githubclient/githubclient.go b/internal/client/githubclient/githubclient.go index eb97a6970c..d3f5267eeb 100644 --- a/internal/client/githubclient/githubclient.go +++ b/internal/client/githubclient/githubclient.go @@ -23,6 +23,7 @@ import ( "github.com/google/go-github/v50/github" "github.com/guacsec/guac/internal/client" + "github.com/guacsec/guac/pkg/version" "golang.org/x/oauth2" ) @@ -56,7 +57,12 @@ var _ GithubClient = &githubClient{} func NewGithubClient(ctx context.Context, token string) (*githubClient, error) { ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) - tc := oauth2.NewClient(ctx, ts) + tc := &http.Client{ + Transport: &oauth2.Transport{ + Source: ts, + Base: version.UATransport, + }, + } gc := github.NewClient(tc) // Run a simple API call to verify authentication to Github API. diff --git a/pkg/handler/collector/deps_dev/deps_dev.go b/pkg/handler/collector/deps_dev/deps_dev.go index 65d99cbbba..37b14714e9 100644 --- a/pkg/handler/collector/deps_dev/deps_dev.go +++ b/pkg/handler/collector/deps_dev/deps_dev.go @@ -29,6 +29,7 @@ import ( pb "github.com/guacsec/guac/pkg/handler/collector/deps_dev/internal" "github.com/guacsec/guac/pkg/handler/processor" "github.com/guacsec/guac/pkg/logging" + "github.com/guacsec/guac/pkg/version" "google.golang.org/grpc" "google.golang.org/grpc/credentials" ) @@ -74,7 +75,9 @@ func NewDepsCollector(ctx context.Context, collectDataSource datasource.CollectS // Connect to the service using TLS. creds := credentials.NewClientTLSFromCert(sysPool, "") - conn, err := grpc.Dial("api.deps.dev:443", grpc.WithTransportCredentials(creds)) + conn, err := grpc.Dial("api.deps.dev:443", + grpc.WithTransportCredentials(creds), + grpc.WithUserAgent(version.UserAgent)) if err != nil { return nil, fmt.Errorf("failed to connect to api.deps.dev: %w", err) } diff --git a/pkg/handler/collector/gcs/gcs.go b/pkg/handler/collector/gcs/gcs.go index dd09ae6703..1ee238769c 100644 --- a/pkg/handler/collector/gcs/gcs.go +++ b/pkg/handler/collector/gcs/gcs.go @@ -29,6 +29,7 @@ import ( "github.com/guacsec/guac/pkg/handler/processor" "github.com/guacsec/guac/pkg/logging" + "github.com/guacsec/guac/pkg/version" ) type gcs struct { @@ -67,7 +68,9 @@ func NewGCSClient(ctx context.Context, poll bool, interval time.Duration) (*gcs, if getCredsPath() == "" { return nil, errors.New("gcs bucket not specified") } - client, err := storage.NewClient(ctx, option.WithCredentialsFile(os.Getenv(gcsCredsEnv))) + client, err := storage.NewClient(ctx, + option.WithCredentialsFile(os.Getenv(gcsCredsEnv)), + option.WithUserAgent(version.UserAgent)) if err != nil { return nil, err } diff --git a/pkg/handler/collector/oci/oci.go b/pkg/handler/collector/oci/oci.go index 18756413c0..2569f5dd24 100644 --- a/pkg/handler/collector/oci/oci.go +++ b/pkg/handler/collector/oci/oci.go @@ -24,6 +24,7 @@ import ( "github.com/guacsec/guac/pkg/collectsub/datasource" "github.com/guacsec/guac/pkg/handler/processor" "github.com/guacsec/guac/pkg/logging" + "github.com/guacsec/guac/pkg/version" "github.com/pkg/errors" "github.com/regclient/regclient" "github.com/regclient/regclient/types/manifest" @@ -128,6 +129,7 @@ func (o *ociCollector) getTagsAndFetch(ctx context.Context, repo string, tags [] rcOpts := []regclient.Opt{} rcOpts = append(rcOpts, regclient.WithDockerCreds()) rcOpts = append(rcOpts, regclient.WithDockerCerts()) + rcOpts = append(rcOpts, regclient.WithUserAgent(version.UserAgent)) if len(tags) > 0 { for _, tag := range tags { diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000000..7158bc3aac --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,42 @@ +// +// Copyright 2023 The GUAC Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package version + +import ( + "fmt" + "net/http" +) + +var ( + Version = "v0.0.1-custom" + Commit, Date string + UserAgent string + UATransport http.RoundTripper +) + +type uat struct { + tr http.RoundTripper +} + +func init() { + UserAgent = fmt.Sprintf("GUAC/%s", Version) + UATransport = uat{tr: http.DefaultTransport} +} + +func (u uat) RoundTrip(r *http.Request) (*http.Response, error) { + r.Header.Set("User-Agent", UserAgent) + return u.tr.RoundTrip(r) +}