Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions central/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ import (
"github.com/stackrox/rox/pkg/auth/authproviders/saml"
authProviderUserpki "github.com/stackrox/rox/pkg/auth/authproviders/userpki"
"github.com/stackrox/rox/pkg/auth/permissions"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/config"
"github.com/stackrox/rox/pkg/devbuild"
Expand Down Expand Up @@ -239,6 +240,8 @@ func main() {
return
}

clientconn.SetUserAgent("central")

ctx := context.Background()
proxy.WatchProxyConfig(ctx, proxyConfigPath, proxyConfigFile, true)

Expand Down
2 changes: 2 additions & 0 deletions compliance/collection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ func initializeStream(ctx context.Context, cli sensor.ComplianceServiceClient) (
func main() {
log.Infof("Running StackRox Version: %s", version.GetMainVersion())

clientconn.SetUserAgent("compliance")

conn, err := clientconn.AuthenticatedGRPCConnection(env.AdvertisedEndpoint.Setting(), mtls.SensorSubject)
if err != nil {
log.Fatal(err)
Expand Down
3 changes: 2 additions & 1 deletion pkg/clientconn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func AuthenticatedHTTPTransport(endpoint string, server mtls.Subject, baseTransp

// GRPCConnection establishes a gRPC connection to the given server, using the given connection options.
func GRPCConnection(dialCtx context.Context, server mtls.Subject, endpoint string, clientConnOpts Options, dialOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
allDialOpts := make([]grpc.DialOption, 0, len(dialOpts)+2)
allDialOpts := make([]grpc.DialOption, 0, len(dialOpts)+3)

clientConnOpts.TLS.GRPCOnly = true

Expand All @@ -377,6 +377,7 @@ func GRPCConnection(dialCtx context.Context, server mtls.Subject, endpoint strin
allDialOpts = append(allDialOpts, grpc.WithPerRPCCredentials(perRPCCreds))
}
allDialOpts = append(allDialOpts, dialOpts...)
allDialOpts = append(allDialOpts, grpc.WithUserAgent(GetUserAgent()))
return clientConnOpts.dialTLSFunc()(dialCtx, endpoint, tlsConf, allDialOpts...)
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/clientconn/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package clientconn

import (
"fmt"
"os"
"runtime"
"strconv"

"github.com/stackrox/rox/pkg/version"
)

var userAgent string

func init() {
SetUserAgent("stackrox")
}

// SetUserAgent formats and sets a value to be used in the User-Agent HTTP
// header for the requests, initiated by a process.
// Note: gorpc-go library will append the header value with its version,
// e.g. grpc-go/1.50.1.
func SetUserAgent(agent string) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test so we can see how user agent will look like?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of relying on the processes to call SetUserAgent, we could do something similar to our singleton's for datastore's, meaning we use once.Do() within GetUserAgent to set the user agent once and afterwards just return it.

This way we don't have the dependency on the consumer on making sure to have called SetUserAgent beforehand. Otherwise, I would expect a comment for GetUserAgent stating explicitly that SetUserAgent has to be called beforehand, otherwise it will return an empty string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetUserAgent() is called by packages which have no idea about the client binary, so the value has to be set on the process initialization. Default value is set to stackrox. I'll add a comment.

var ci string
if v, ok := os.LookupEnv("CI"); ok {
if v == "" {
ci = " CI"
} else if value, err := strconv.ParseBool(v); err == nil && value {
ci = " CI"
}
}
userAgent = fmt.Sprintf("%s/%s (%s; %s)%s", agent, version.GetMainVersion(), runtime.GOOS, runtime.GOARCH, ci)
}

// GetUserAgent returns the previously calculated value, which has to be set
// by the process main function via a call to SetUserAgent().
// Default value is the one produced by SetUserAgent("stackrox").
func GetUserAgent() string {
return userAgent
}
4 changes: 3 additions & 1 deletion pkg/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/stackrox/rox/pkg/audit"
"github.com/stackrox/rox/pkg/auth/authproviders"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/contextutil"
"github.com/stackrox/rox/pkg/env"
Expand Down Expand Up @@ -228,7 +229,8 @@ func (a *apiImpl) connectToLocalEndpoint(dialCtxFunc pipeconn.DialContextFunc) (
grpc.WithContextDialer(func(ctx context.Context, endpoint string) (net.Conn, error) {
return dialCtxFunc(ctx)
}),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxResponseMsgSize())))
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(maxResponseMsgSize())),
grpc.WithUserAgent(clientconn.GetUserAgent()))
}

func allowPrettyQueryParameter(h http.Handler) http.Handler {
Expand Down
3 changes: 2 additions & 1 deletion roxctl/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/errox"
"github.com/stackrox/rox/pkg/utils"
"github.com/stackrox/rox/roxctl/common/flags"
Expand Down Expand Up @@ -138,7 +139,7 @@ func (client *roxctlClientImpl) NewReq(method string, path string, body io.Reade
return nil, errors.Wrap(err, "could not inject authentication information")
}

req.Header.Set("User-Agent", GetUserAgent())
req.Header.Set("User-Agent", clientconn.GetUserAgent())

return req, nil
}
2 changes: 0 additions & 2 deletions roxctl/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func GetGRPCConnection(logger logger.Logger) (*grpc.ClientConn, error) {
}
}

grpcDialOpts = append(grpcDialOpts, grpc.WithUserAgent(GetUserAgent()))

connection, err := clientconn.GRPCConnection(common.Context(), mtls.CentralSubject, endpoint, opts, grpcDialOpts...)
return connection, errors.WithStack(err)
}
29 changes: 0 additions & 29 deletions roxctl/common/useragent.go

This file was deleted.

3 changes: 3 additions & 0 deletions roxctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/roxctl/maincommand"
"github.com/stackrox/rox/roxctl/properties"

Expand All @@ -30,6 +31,8 @@ func main() {

PatchPersistentPreRunHooks(c)

clientconn.SetUserAgent("roxctl")

if err := c.Execute(); err != nil {
os.Exit(1)
}
Expand Down
2 changes: 2 additions & 0 deletions sensor/admission-control/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func mainCmd() error {
log.Errorf("Failed to configure certificates: %v. Connection to sensor might fail.", err)
}

clientconn.SetUserAgent("admission-control")

// Note that the following call returns immediately (connecting happens in the background), hence this does not
// delay readiness of the admission-control service even if sensor is unavailable.
sensorConn, err := clientconn.AuthenticatedGRPCConnection(env.SensorEndpoint.Setting(), mtls.SensorSubject)
Expand Down
3 changes: 3 additions & 0 deletions sensor/common/centralclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/pkg/errors"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/pkg/centralsensor"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/cryptoutils"
"github.com/stackrox/rox/pkg/httputil"
"github.com/stackrox/rox/pkg/logging"
Expand Down Expand Up @@ -238,6 +239,8 @@ func (c *Client) doHTTPRequest(ctx context.Context, method, route string, params
return nil, nil, errors.Wrap(err, "creating tls-challenge request")
}

req.Header.Set("User-Agent", clientconn.GetUserAgent())

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, nil, errors.Wrapf(err, "calling %s", u.String())
Expand Down
2 changes: 2 additions & 0 deletions sensor/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os/signal"

"github.com/pkg/errors"
"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/devmode"
"github.com/stackrox/rox/pkg/env"
"github.com/stackrox/rox/pkg/logging"
Expand Down Expand Up @@ -46,6 +47,7 @@ func main() {
} else {
sharedClientInterface = client.MustCreateInterface()
}
clientconn.SetUserAgent("sensor")
centralConnFactory, err := centralclient.NewCentralConnectionFactory(env.CentralEndpoint.Setting())
if err != nil {
utils.CrashOnError(errors.Wrapf(err, "sensor failed to start while initializing gRPC client to endpoint %s", env.CentralEndpoint.Setting()))
Expand Down
3 changes: 3 additions & 0 deletions sensor/upgrader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"flag"

"github.com/stackrox/rox/pkg/clientconn"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/pkg/utils"
"github.com/stackrox/rox/pkg/version"
Expand Down Expand Up @@ -34,6 +35,8 @@ func mainCmd() error {
return err
}

clientconn.SetUserAgent("upgrader")

upgradeCtx, err := upgradectx.Create(context.Background(), upgraderCfg)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions sensor/upgrader/upgradectx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ func (c *UpgradeContext) DoCentralHTTPRequest(req *http.Request) (*http.Response
return nil, errors.New("no HTTP client configured")
}

req.Header.Set("User-Agent", clientconn.GetUserAgent())

return c.centralHTTPClient.Do(req)
}

Expand Down