/
client.go
205 lines (184 loc) · 6.09 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
package cmdoptions
import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"os"
"github.com/golang/protobuf/proto"
"github.com/spf13/pflag"
"go.temporal.io/api/common/v1"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
"go.uber.org/zap"
)
const AUTH_HEADER_ENV_VAR = "TEMPORAL_OMES_AUTH_HEADER"
type headersProvider map[string]string
func (hp headersProvider) GetHeaders(ctx context.Context) (map[string]string, error) {
return hp, nil
}
func newHeadersProvider(headers map[string]string) headersProvider {
return headers
}
// Options for creating a Temporal client.
type ClientOptions struct {
// Address of Temporal server to connect to
Address string
// Temporal namespace
Namespace string
// Enable TLS
EnableTLS bool
// TLS client cert
ClientCertPath string
// TLS client private key
ClientKeyPath string
// TLS server name
TLSServerName string
// Authorization header value
AuthHeader string
// Disable Host Verification
DisableHostVerification bool
}
// loadTLSConfig inits a TLS config from the provided cert and key files.
func (c *ClientOptions) loadTLSConfig() (*tls.Config, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: c.DisableHostVerification,
ServerName: c.TLSServerName,
MinVersion: tls.VersionTLS13,
}
if c.ClientCertPath != "" {
if c.ClientKeyPath == "" {
return nil, errors.New("got TLS cert with no key")
}
cert, err := tls.LoadX509KeyPair(c.ClientCertPath, c.ClientKeyPath)
if err != nil {
return nil, fmt.Errorf("failed to load certs: %s", err)
}
tlsConfig.Certificates = []tls.Certificate{cert}
return tlsConfig, nil
} else if c.ClientKeyPath != "" {
return nil, errors.New("got TLS key with no cert")
}
if c.EnableTLS {
return tlsConfig, nil
}
return nil, nil
}
// MustDial connects to a Temporal server, with logging, metrics and loaded TLS certs.
func (c *ClientOptions) MustDial(metrics *Metrics, logger *zap.SugaredLogger) client.Client {
client, err := c.Dial(metrics, logger)
if err != nil {
logger.Fatal(err)
}
return client
}
// Dial connects to a Temporal server, with logging, metrics, loaded TLS certs and set auth header.
func (c *ClientOptions) Dial(metrics *Metrics, logger *zap.SugaredLogger) (client.Client, error) {
tlsCfg, err := c.loadTLSConfig()
if err != nil {
return nil, fmt.Errorf("failed to load TLS config: %w", err)
}
var clientOptions client.Options
clientOptions.HostPort = c.Address
clientOptions.Namespace = c.Namespace
clientOptions.ConnectionOptions.TLS = tlsCfg
clientOptions.Logger = NewZapAdapter(logger.Desugar())
clientOptions.MetricsHandler = metrics.NewHandler()
var authHeader string
if c.AuthHeader == "" {
authHeader = os.Getenv(AUTH_HEADER_ENV_VAR)
} else {
authHeader = c.AuthHeader
}
if authHeader != "" {
clientOptions.HeadersProvider = newHeadersProvider(map[string]string{
"Authorization": authHeader,
})
}
dataConverter := converter.NewCompositeDataConverter(
converter.NewNilPayloadConverter(),
converter.NewByteSlicePayloadConverter(),
&PassThroughPayloadConverter{},
converter.NewProtoJSONPayloadConverter(),
converter.NewProtoPayloadConverter(),
converter.NewJSONPayloadConverter(),
)
clientOptions.DataConverter = dataConverter
client, err := client.Dial(clientOptions)
if err != nil {
return nil, fmt.Errorf("failed to dial: %w", err)
}
logger.Infof("Client connected to %s, namespace: %s", c.Address, c.Namespace)
return client, nil
}
// AddCLIFlags adds the relevant flags to populate the options struct.
func (c *ClientOptions) AddCLIFlags(fs *pflag.FlagSet) {
fs.StringVar(&c.Address, "server-address", client.DefaultHostPort, "Address of Temporal server")
fs.StringVar(&c.Namespace, "namespace", client.DefaultNamespace, "Namespace to connect to")
fs.BoolVar(&c.EnableTLS, "tls", false, "Enable TLS")
fs.StringVar(&c.ClientCertPath, "tls-cert-path", "", "Path to client TLS certificate")
fs.StringVar(&c.ClientKeyPath, "tls-key-path", "", "Path to client private key")
fs.BoolVar(&c.DisableHostVerification, "disable-tls-host-verification", false, "Disable TLS host verification")
fs.StringVar(&c.TLSServerName, "tls-server-name", "", "TLS target server name")
fs.StringVar(&c.AuthHeader, "auth-header", "",
fmt.Sprintf("Authorization header value (can also be set via %s env var)", AUTH_HEADER_ENV_VAR))
}
// ToFlags converts these options to string flags.
func (c *ClientOptions) ToFlags() (flags []string) {
if c.Address != "" {
flags = append(flags, "--server-address", c.Address)
}
if c.Namespace != "" {
flags = append(flags, "--namespace", c.Namespace)
}
if c.EnableTLS {
flags = append(flags, "--tls")
}
if c.ClientCertPath != "" {
flags = append(flags, "--tls-cert-path", c.ClientCertPath)
}
if c.ClientKeyPath != "" {
flags = append(flags, "--tls-key-path", c.ClientKeyPath)
}
if c.DisableHostVerification {
flags = append(flags, "--disable-tls-host-verification")
}
if c.TLSServerName != "" {
flags = append(flags, "--tls-server-name", c.TLSServerName)
}
if c.AuthHeader != "" {
flags = append(flags, "--auth-header", c.AuthHeader)
}
return
}
type PassThroughPayloadConverter struct{}
func (p *PassThroughPayloadConverter) ToPayload(value interface{}) (*common.Payload, error) {
if valuePayload, ok := value.(*common.Payload); ok {
asBytes, err := proto.Marshal(valuePayload)
if err != nil {
return nil, fmt.Errorf("unable to encode raw payload: %w", err)
}
return &common.Payload{
Metadata: map[string][]byte{
"encoding": []byte(p.Encoding()),
},
Data: asBytes,
}, nil
}
return nil, nil
}
func (p *PassThroughPayloadConverter) FromPayload(payload *common.Payload, valuePtr interface{}) error {
innerPayload := &common.Payload{}
err := proto.Unmarshal(payload.GetData(), innerPayload)
if err != nil {
return fmt.Errorf("unable to decode raw payload: %w", err)
}
return converter.GetDefaultDataConverter().FromPayload(innerPayload, valuePtr)
}
func (p *PassThroughPayloadConverter) ToString(payload *common.Payload) string {
return base64.RawStdEncoding.EncodeToString(payload.GetData())
}
func (p *PassThroughPayloadConverter) Encoding() string {
return "_passthrough"
}