Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport stable/8.0] Set zbctl user agent #9468

Merged
merged 2 commits into from
May 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions clients/go/cmd/zbctl/internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ var initClient = func(cmd *cobra.Command, args []string) error {
client, err = zbc.NewClient(&zbc.ClientConfig{
GatewayAddress: fmt.Sprintf("%s:%s", host, port),
CredentialsProvider: credsProvider,
UserAgent: "zeebe-client-zbctl/" + Version,
})
return err
}
Expand Down
11 changes: 9 additions & 2 deletions clients/go/pkg/zbc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ type ClientConfig struct {
// of 45 seconds being used
KeepAlive time.Duration

DialOpts []grpc.DialOption
// UserAgent is an optional field, to specify the user-agent header which should be sent by each
// gRPC request. Defaults to zeebe-client-go/%version.
UserAgent string
DialOpts []grpc.DialOption
}

// ErrFileNotFound is returned whenever a file can't be found at the provided path. Use this value to do error comparison.
Expand Down Expand Up @@ -158,7 +161,11 @@ func NewClient(config *ClientConfig) (Client, error) {
return nil, err
}

config.DialOpts = append(config.DialOpts, grpc.WithUserAgent("zeebe-client-go/"+getVersion()))
if config.UserAgent == "" {
config.UserAgent = "zeebe-client-go/" + getVersion()
}

config.DialOpts = append(config.DialOpts, grpc.WithUserAgent(config.UserAgent))

conn, err := grpc.Dial(config.GatewayAddress, config.DialOpts...)
if err != nil {
Expand Down
57 changes: 57 additions & 0 deletions clients/go/pkg/zbc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"errors"
"fmt"
"github.com/camunda/zeebe/clients/go/v8/internal/utils"
"google.golang.org/grpc/metadata"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -164,6 +166,61 @@ func (s *clientTestSuite) TestGatewayAddressEnvVar() {
s.EqualValues(fmt.Sprintf("0.0.0.0:%s", parts[len(parts)-1]), config.GatewayAddress)
}

func (s *clientTestSuite) TestDefaultUserAgent() {
// given
var incomingContext = make(map[string][]string)
lis, server := createServerWithUnaryInterceptor(func(ctx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) {
incomingContext, _ = metadata.FromIncomingContext(ctx)
return nil, nil
})
go server.Serve(lis)
defer server.Stop()

// when
client, err := NewClient(&ClientConfig{
GatewayAddress: lis.Addr().String(),
UsePlaintextConnection: true,
})
s.Require().NoError(err)
ctx, cancel := context.WithTimeout(context.Background(), utils.DefaultTestTimeout)
defer cancel()

_, _ = client.NewTopologyCommand().Send(ctx)
userAgent := incomingContext["user-agent"]

// then
s.Require().Len(userAgent, 1)
s.Require().Contains(userAgent[0], "zeebe-client-go/"+getVersion())
}

func (s *clientTestSuite) TestSpecificUserAgent() {
// given
var incomingContext = make(map[string][]string)
lis, server := createServerWithUnaryInterceptor(func(ctx context.Context, _ interface{}, _ *grpc.UnaryServerInfo, _ grpc.UnaryHandler) (interface{}, error) {
incomingContext, _ = metadata.FromIncomingContext(ctx)
return nil, nil
})
go server.Serve(lis)
defer server.Stop()

// when
client, err := NewClient(&ClientConfig{
GatewayAddress: lis.Addr().String(),
UsePlaintextConnection: true,
UserAgent: "anotherUserAgentLikeZbctl",
})
s.Require().NoError(err)
ctx, cancel := context.WithTimeout(context.Background(), utils.DefaultTestTimeout)
defer cancel()

_, _ = client.NewTopologyCommand().Send(ctx)
userAgent := incomingContext["user-agent"]

// then
s.Require().Len(userAgent, 1)
s.Require().Contains(userAgent[0], "anotherUserAgentLikeZbctl")
}

func (s *clientTestSuite) TestCaCertificateEnvVar() {
// given
lis, grpcServer := createSecureServer(false)
Expand Down