Skip to content

Commit

Permalink
merge: #9468
Browse files Browse the repository at this point in the history
9468: [Backport stable/8.0] Set zbctl user agent r=Zelldon a=backport-action

# Description
Backport of #9434 to `stable/8.0`.

relates to 

Co-authored-by: Christopher Zell <zelldon91@googlemail.com>
  • Loading branch information
zeebe-bors-camunda[bot] and Zelldon committed May 31, 2022
2 parents b1cf5c2 + f4212d5 commit 06e2b77
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions clients/go/cmd/zbctl/internal/commands/root.go
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
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
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

0 comments on commit 06e2b77

Please sign in to comment.