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

Add flag --grpc-meta for attaching metadata #26

Merged
merged 3 commits into from
Nov 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"github.com/temporalio/temporal-cli/client"
"github.com/temporalio/temporal-cli/cluster"
"github.com/temporalio/temporal-cli/common"
"github.com/temporalio/temporal-cli/dataconverter"
"github.com/temporalio/temporal-cli/env"
"github.com/temporalio/temporal-cli/headers"
"github.com/temporalio/temporal-cli/namespace"
Expand Down Expand Up @@ -83,18 +82,7 @@ func SetFactory(factory client.ClientFactory) {

func configureCLI(ctx *cli.Context) error {
env.Build(ctx)
return configureSDK(ctx)
}

func configureSDK(ctx *cli.Context) error {
endpoint := ctx.String(common.FlagCodecEndpoint)
if endpoint != "" {
dataconverter.SetRemoteEndpoint(
endpoint,
ctx.String(common.FlagNamespace),
ctx.String(common.FlagCodecAuth),
)
}
client.Build(ctx)

return nil
}
Expand Down
43 changes: 43 additions & 0 deletions client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import (

"github.com/gogo/status"
"github.com/temporalio/temporal-cli/common"
"github.com/temporalio/temporal-cli/dataconverter"
"github.com/temporalio/temporal-cli/headersprovider"
"github.com/urfave/cli/v2"
"go.temporal.io/api/operatorservice/v1"
"go.temporal.io/api/serviceerror"
Expand All @@ -52,6 +54,7 @@ import (
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/metadata"
)

var (
Expand Down Expand Up @@ -83,6 +86,32 @@ type ClientFactory interface {
HealthClient(c *cli.Context) healthpb.HealthClient
}

func Build(c *cli.Context) {
for _, c := range c.App.Commands {
common.AddBeforeHandler(c, configureSDK)
}
}

func configureSDK(ctx *cli.Context) error {
endpoint := ctx.String(common.FlagCodecEndpoint)
if endpoint != "" {
dataconverter.SetRemoteEndpoint(
endpoint,
ctx.String(common.FlagNamespace),
ctx.String(common.FlagCodecAuth),
)
}

md, err := common.SplitKeyValuePairs(ctx.StringSlice(common.FlagMetadata))
if err != nil {
return err
}

headersprovider.SetGRPCHeadersProvider(md)

return nil
}

type clientFactory struct {
logger log.Logger
}
Expand Down Expand Up @@ -130,6 +159,7 @@ func (b *clientFactory) SDKClient(c *cli.Context, namespace string) sdkclient.Cl
ConnectionOptions: sdkclient.ConnectionOptions{
TLS: tlsConfig,
},
HeadersProvider: headersprovider.GetCurrent(),
})
if err != nil {
b.logger.Fatal("Failed to create SDK client", tag.Error(err))
Expand All @@ -145,6 +175,19 @@ func (b *clientFactory) HealthClient(c *cli.Context) healthpb.HealthClient {
return healthpb.NewHealthClient(connection)
}

func headersProviderInterceptor(headersProvider headersprovider.HeadersProvider) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
headers, err := headersProvider.GetHeaders(ctx)
if err != nil {
return err
}
for k, v := range headers {
ctx = metadata.AppendToOutgoingContext(ctx, k, v)
}
return invoker(ctx, method, req, reply, cc, opts...)
}
}

func errorInterceptor() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
err := invoker(ctx, method, req, reply, cc, opts...)
Expand Down
6 changes: 5 additions & 1 deletion common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var (
FlagMaxFieldLength = "max-field-length"
FlagMemo = "memo"
FlagMemoFile = "memo-file"
FlagMetadata = "grpc-meta"
FlagMetricsPort = "metrics-port"
FlagName = "name"
FlagNamespace = "namespace"
Expand Down Expand Up @@ -164,6 +165,10 @@ var SharedFlags = []cli.Flag{
Usage: "Temporal workflow namespace",
EnvVars: []string{"TEMPORAL_CLI_NAMESPACE"},
},
&cli.StringSliceFlag{
Name: FlagMetadata,
Usage: "gRPC metadata to send with requests. Format: key=value. Use valid JSON formats for value",
},
&cli.StringFlag{
Name: FlagTLSCertPath,
Value: "",
Expand Down Expand Up @@ -368,7 +373,6 @@ var FlagsForStackTraceQuery = append(FlagsForExecution, []cli.Flag{
}...)

func WithFlags(commands []*cli.Command, newFlags []cli.Flag) []*cli.Command {

for _, cmd := range commands {
if len(cmd.Subcommands) == 0 {
for _, newF := range newFlags {
Expand Down
55 changes: 55 additions & 0 deletions headersprovider/headers_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// The MIT License
//
// Copyright (c) 2021 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package headersprovider

import (
"context"
)

type HeadersProvider interface {
GetHeaders(context.Context) (map[string]string, error)
}

var (
headersProvider HeadersProvider = nil
)

type grpcHeaderProvider struct {
headers map[string]string
}

func (a grpcHeaderProvider) GetHeaders(ctx context.Context) (map[string]string, error) {
return a.headers, nil
}

func SetGRPCHeadersProvider(headers map[string]string) {
headersProvider = &grpcHeaderProvider{headers}
}

func SetCurrent(hp HeadersProvider) {
headersProvider = hp
}

func GetCurrent() HeadersProvider {
return headersProvider
}