-
Notifications
You must be signed in to change notification settings - Fork 32
/
client.go
220 lines (192 loc) · 6.46 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package temporalcli
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"os"
"os/user"
"strings"
"go.temporal.io/api/common/v1"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/log"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
func (c *ClientOptions) dialClient(cctx *CommandContext) (client.Client, error) {
clientOptions := client.Options{
HostPort: c.Address,
Namespace: c.Namespace,
Logger: log.NewStructuredLogger(cctx.Logger),
Identity: clientIdentity(),
// We do not put codec on data converter here, it is applied via
// interceptor. Same for failure conversion.
// XXX: If this is altered to be more dynamic, have to also update
// everywhere dataConverter is used.
DataConverter: dataConverter,
}
// API key
if c.ApiKey != "" {
clientOptions.Credentials = client.NewAPIKeyStaticCredentials(c.ApiKey)
}
// Headers
if len(c.GrpcMeta) > 0 {
headers := make(stringMapHeadersProvider, len(c.GrpcMeta))
for _, kv := range c.GrpcMeta {
pieces := strings.SplitN(kv, "=", 2)
if len(pieces) != 2 {
return nil, fmt.Errorf("gRPC meta of %q does not have '='", kv)
}
headers[pieces[0]] = pieces[1]
}
clientOptions.HeadersProvider = headers
}
// Remote codec
if c.CodecEndpoint != "" {
interceptor, err := payloadCodecInterceptor(c.Namespace, c.CodecEndpoint, c.CodecAuth)
if err != nil {
return nil, fmt.Errorf("failed creating payload codec interceptor: %w", err)
}
clientOptions.ConnectionOptions.DialOptions = append(
clientOptions.ConnectionOptions.DialOptions, grpc.WithChainUnaryInterceptor(interceptor))
}
// Fixed header overrides
clientOptions.ConnectionOptions.DialOptions = append(
clientOptions.ConnectionOptions.DialOptions, grpc.WithChainUnaryInterceptor(fixedHeaderOverrideInterceptor))
// Additional gRPC options
clientOptions.ConnectionOptions.DialOptions = append(
clientOptions.ConnectionOptions.DialOptions, cctx.Options.AdditionalClientGRPCDialOptions...)
// TLS
var err error
if clientOptions.ConnectionOptions.TLS, err = c.tlsConfig(); err != nil {
return nil, err
}
return client.Dial(clientOptions)
}
func (c *ClientOptions) tlsConfig() (*tls.Config, error) {
// We need TLS if any of these TLS options are set
if !c.Tls &&
c.TlsCaPath == "" && c.TlsCertPath == "" && c.TlsKeyPath == "" &&
c.TlsCaData == "" && c.TlsCertData == "" && c.TlsKeyData == "" {
return nil, nil
}
conf := &tls.Config{
ServerName: c.TlsServerName,
InsecureSkipVerify: c.TlsDisableHostVerification,
}
if c.TlsCertPath != "" {
if c.TlsCertData != "" {
return nil, fmt.Errorf("cannot specify both --tls-cert-path and --tls-cert-data")
}
clientCert, err := tls.LoadX509KeyPair(c.TlsCertPath, c.TlsKeyPath)
if err != nil {
return nil, fmt.Errorf("failed loading client cert key pair: %w", err)
}
conf.Certificates = append(conf.Certificates, clientCert)
} else if c.TlsCertData != "" {
clientCert, err := tls.X509KeyPair([]byte(c.TlsCertData), []byte(c.TlsKeyData))
if err != nil {
return nil, fmt.Errorf("failed loading client cert key pair: %w", err)
}
conf.Certificates = append(conf.Certificates, clientCert)
}
if c.TlsCaPath != "" {
if c.TlsCaData != "" {
return nil, fmt.Errorf("cannot specify both --tls-ca-path and --tls-ca-data")
}
conf.RootCAs = x509.NewCertPool()
if b, err := os.ReadFile(c.TlsCaPath); err != nil {
return nil, fmt.Errorf("failed reading CA cert from %v: %w", c.TlsCaPath, err)
} else if !conf.RootCAs.AppendCertsFromPEM(b) {
return nil, fmt.Errorf("invalid CA cert from %v", c.TlsCaPath)
}
} else if c.TlsCaData != "" {
conf.RootCAs = x509.NewCertPool()
if !conf.RootCAs.AppendCertsFromPEM([]byte(c.TlsCaData)) {
return nil, fmt.Errorf("invalid CA cert data")
}
}
return conf, nil
}
func fixedHeaderOverrideInterceptor(
ctx context.Context,
method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
// The SDK sets some values on the outgoing metadata that we can't override
// via normal headers, so we have to replace directly on the metadata
md, _ := metadata.FromOutgoingContext(ctx)
if md == nil {
md = metadata.MD{}
}
md.Set("client-name", "temporal-cli")
md.Set("client-version", Version)
md.Set("supported-server-versions", ">=1.0.0 <2.0.0")
md.Set("caller-type", "operator")
ctx = metadata.NewOutgoingContext(ctx, md)
return invoker(ctx, method, req, reply, cc, opts...)
}
func payloadCodecInterceptor(namespace, codecEndpoint, codecAuth string) (grpc.UnaryClientInterceptor, error) {
codecEndpoint = strings.ReplaceAll(codecEndpoint, "{namespace}", namespace)
payloadCodec := converter.NewRemotePayloadCodec(
converter.RemotePayloadCodecOptions{
Endpoint: codecEndpoint,
ModifyRequest: func(req *http.Request) error {
req.Header.Set("X-Namespace", namespace)
if codecAuth != "" {
req.Header.Set("Authorization", codecAuth)
}
return nil
},
},
)
return converter.NewPayloadCodecGRPCClientInterceptor(
converter.PayloadCodecGRPCClientInterceptorOptions{
Codecs: []converter.PayloadCodec{payloadCodec},
},
)
}
func clientIdentity() string {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown-host"
}
username := "unknown-user"
if u, err := user.Current(); err == nil {
username = u.Username
}
return "temporal-cli:" + username + "@" + hostname
}
type stringMapHeadersProvider map[string]string
func (s stringMapHeadersProvider) GetHeaders(context.Context) (map[string]string, error) {
return s, nil
}
var dataConverter = converter.NewCompositeDataConverter(
rawValuePayloadConverter{},
converter.NewNilPayloadConverter(),
converter.NewByteSlicePayloadConverter(),
converter.NewProtoJSONPayloadConverter(),
converter.NewProtoPayloadConverter(),
converter.NewJSONPayloadConverter(),
)
type rawValue struct{ payload *common.Payload }
type rawValuePayloadConverter struct{}
func (rawValuePayloadConverter) ToPayload(value any) (*common.Payload, error) {
// Only convert if value is a raw value
if r, ok := value.(rawValue); ok {
return r.payload, nil
}
return nil, nil
}
func (rawValuePayloadConverter) FromPayload(payload *common.Payload, valuePtr any) error {
return fmt.Errorf("raw value unsupported from payload")
}
func (rawValuePayloadConverter) ToString(p *common.Payload) string {
return fmt.Sprintf("<raw payload %v bytes>", len(p.Data))
}
func (rawValuePayloadConverter) Encoding() string {
// Should never be used
return "raw-value-encoding"
}