forked from temporalio/temporal-worker-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclientpool.go
More file actions
194 lines (163 loc) · 4.99 KB
/
clientpool.go
File metadata and controls
194 lines (163 loc) · 4.99 KB
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
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2024 Datadog, Inc.
package clientpool
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"sync"
"time"
"github.com/temporalio/temporal-worker-controller/api/v1alpha1"
sdkclient "go.temporal.io/sdk/client"
"go.temporal.io/sdk/log"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
)
type ClientPoolKey struct {
HostPort string
Namespace string
MutualTLSSecret string // Include secret name in key to invalidate cache when the secret name changes
}
type ClientInfo struct {
client sdkclient.Client
tls *tls.Config // Storing the TLS config associated with the client to check certificate expiration. If the certificate is expired, a new client will be created.
expiryTime time.Time // Effective expiration time (cert.NotAfter - buffer) for efficient expiration checking
}
type ClientPool struct {
mux sync.RWMutex
logger log.Logger
clients map[ClientPoolKey]ClientInfo
k8sClient runtimeclient.Client
}
func New(l log.Logger, c runtimeclient.Client) *ClientPool {
return &ClientPool{
logger: l,
clients: make(map[ClientPoolKey]ClientInfo),
k8sClient: c,
}
}
func (cp *ClientPool) GetSDKClient(key ClientPoolKey, withMTLS bool) (sdkclient.Client, bool) {
cp.mux.RLock()
defer cp.mux.RUnlock()
info, ok := cp.clients[key]
if !ok {
return nil, false
}
if withMTLS {
// Check if any certificate is expired
expired, err := isCertificateExpired(info.expiryTime)
if err != nil {
cp.logger.Error("Error checking certificate expiration", "error", err)
return nil, false
}
if expired {
cp.logger.Warn("Certificate is expired or is going to expire soon")
return nil, false
}
}
return info.client, true
}
type NewClientOptions struct {
TemporalNamespace string
K8sNamespace string
Spec v1alpha1.TemporalConnectionSpec
}
func (cp *ClientPool) UpsertClient(ctx context.Context, opts NewClientOptions) (sdkclient.Client, error) {
clientOpts := sdkclient.Options{
Logger: cp.logger,
HostPort: opts.Spec.HostPort,
Namespace: opts.TemporalNamespace,
// TODO(jlegrone): Make API Keys work
}
var pemCert []byte
var expiryTime time.Time
// Get the connection secret if it exists
if opts.Spec.MutualTLSSecret != "" {
var secret corev1.Secret
if err := cp.k8sClient.Get(ctx, types.NamespacedName{
Name: opts.Spec.MutualTLSSecret,
Namespace: opts.K8sNamespace,
}, &secret); err != nil {
return nil, err
}
if secret.Type != corev1.SecretTypeTLS {
err := fmt.Errorf("secret %s must be of type kubernetes.io/tls", secret.Name)
return nil, err
}
// Extract the certificate to calculate the effective expiration time
pemCert = secret.Data["tls.crt"]
// Check if certificate is expired before creating the client
exp, err := calculateCertificateExpirationTime(pemCert, 5*time.Minute)
if err != nil {
return nil, fmt.Errorf("failed to check certificate expiration: %v", err)
}
expired, err := isCertificateExpired(exp)
if err != nil {
return nil, fmt.Errorf("failed to check certificate expiration: %v", err)
}
if expired {
return nil, fmt.Errorf("certificate is expired or is going to expire soon")
}
cert, err := tls.X509KeyPair(secret.Data["tls.crt"], secret.Data["tls.key"])
if err != nil {
return nil, err
}
clientOpts.ConnectionOptions.TLS = &tls.Config{
Certificates: []tls.Certificate{cert},
}
expiryTime = exp
}
c, err := sdkclient.Dial(clientOpts)
if err != nil {
return nil, err
}
if _, err := c.CheckHealth(context.Background(), &sdkclient.CheckHealthRequest{}); err != nil {
panic(err)
}
cp.mux.Lock()
defer cp.mux.Unlock()
key := ClientPoolKey{
HostPort: opts.Spec.HostPort,
Namespace: opts.TemporalNamespace,
MutualTLSSecret: opts.Spec.MutualTLSSecret,
}
cp.clients[key] = ClientInfo{
client: c,
tls: clientOpts.ConnectionOptions.TLS,
expiryTime: expiryTime,
}
return c, nil
}
func (cp *ClientPool) Close() {
cp.mux.Lock()
defer cp.mux.Unlock()
for _, c := range cp.clients {
c.client.Close()
}
cp.clients = make(map[ClientPoolKey]ClientInfo)
}
func calculateCertificateExpirationTime(certBytes []byte, bufferTime time.Duration) (time.Time, error) {
if len(certBytes) == 0 {
return time.Time{}, fmt.Errorf("no certificate bytes provided")
}
block, _ := pem.Decode(certBytes)
if block == nil {
return time.Time{}, fmt.Errorf("failed to decode PEM block")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return time.Time{}, fmt.Errorf("failed to parse certificate: %v", err)
}
expiryTime := cert.NotAfter.Add(-bufferTime)
return expiryTime, nil
}
func isCertificateExpired(expiryTime time.Time) (bool, error) {
if time.Now().After(expiryTime) {
return true, nil
}
return false, nil
}