Skip to content

Commit

Permalink
User agent (guacsec#781)
Browse files Browse the repository at this point in the history
* Do something with version from Makefile.

Signed-off-by: Jeff Mendoza <[email protected]>

* Add UserAgent to github, deps.dev, gcs, oci.

Signed-off-by: Jeff Mendoza <[email protected]>

---------

Signed-off-by: Jeff Mendoza <[email protected]>
  • Loading branch information
jeffmendoza authored and mlieberman85 committed Jul 4, 2023
1 parent 2ede0fa commit 009f845
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 15 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 4 additions & 2 deletions cmd/guaccollect/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions cmd/guaccsub/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
6 changes: 4 additions & 2 deletions cmd/guacgql/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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")
Expand Down
6 changes: 4 additions & 2 deletions cmd/guacingest/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
},
Expand Down
6 changes: 4 additions & 2 deletions cmd/guacone/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion internal/client/githubclient/githubclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion pkg/handler/collector/deps_dev/deps_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/handler/collector/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/handler/collector/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
42 changes: 42 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 009f845

Please sign in to comment.